encoder_options_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package zstd
  2. import (
  3. "strconv"
  4. "testing"
  5. )
  6. func TestEncoderLevelFromString(t *testing.T) {
  7. type args struct {
  8. s string
  9. }
  10. tests := []struct {
  11. name string
  12. args args
  13. want bool
  14. want1 EncoderLevel
  15. }{
  16. {
  17. name: "fastest",
  18. args: args{s: "fastest"},
  19. want: true,
  20. want1: SpeedFastest,
  21. },
  22. {
  23. name: "fastest-upper",
  24. args: args{s: "FASTEST"},
  25. want: true,
  26. want1: SpeedFastest,
  27. },
  28. {
  29. name: "default",
  30. args: args{s: "default"},
  31. want: true,
  32. want1: SpeedDefault,
  33. },
  34. {
  35. name: "default-UPPER",
  36. args: args{s: "Default"},
  37. want: true,
  38. want1: SpeedDefault,
  39. },
  40. {
  41. name: "invalid",
  42. args: args{s: "invalid"},
  43. want: false,
  44. want1: SpeedDefault,
  45. },
  46. {
  47. name: "unknown",
  48. args: args{s: "unknown"},
  49. want: false,
  50. want1: SpeedDefault,
  51. },
  52. {
  53. name: "empty",
  54. args: args{s: ""},
  55. want: false,
  56. want1: SpeedDefault,
  57. },
  58. {
  59. name: "fastest-string",
  60. args: args{s: SpeedFastest.String()},
  61. want: true,
  62. want1: SpeedFastest,
  63. },
  64. {
  65. name: "default-string",
  66. args: args{s: SpeedDefault.String()},
  67. want: true,
  68. want1: SpeedDefault,
  69. },
  70. }
  71. for _, tt := range tests {
  72. t.Run(tt.name, func(t *testing.T) {
  73. got, got1 := EncoderLevelFromString(tt.args.s)
  74. if got != tt.want {
  75. t.Errorf("EncoderLevelFromString() got = %v, want %v", got, tt.want)
  76. }
  77. if got1 != tt.want1 {
  78. t.Errorf("EncoderLevelFromString() got1 = %v, want %v", got1, tt.want1)
  79. }
  80. })
  81. }
  82. }
  83. func TestEncoderLevelFromZstd(t *testing.T) {
  84. type args struct {
  85. level int
  86. }
  87. tests := []struct {
  88. name string
  89. args args
  90. want EncoderLevel
  91. }{
  92. {
  93. name: "level-1",
  94. args: args{level: 1},
  95. want: SpeedFastest,
  96. },
  97. {
  98. name: "level-minus1",
  99. args: args{level: -1},
  100. want: SpeedFastest,
  101. },
  102. {
  103. name: "level-3",
  104. args: args{level: 3},
  105. want: SpeedDefault,
  106. },
  107. {
  108. name: "level-4",
  109. args: args{level: 4},
  110. want: SpeedDefault,
  111. },
  112. }
  113. for _, tt := range tests {
  114. t.Run(tt.name, func(t *testing.T) {
  115. if got := EncoderLevelFromZstd(tt.args.level); got != tt.want {
  116. t.Errorf("EncoderLevelFromZstd() = %v, want %v", got, tt.want)
  117. }
  118. })
  119. }
  120. }
  121. func TestWindowSize(t *testing.T) {
  122. tests := []struct {
  123. windowSize int
  124. err bool
  125. }{
  126. {1 << 9, true},
  127. {1 << 10, false},
  128. {(1 << 10) + 1, true},
  129. {(1 << 10) * 3, true},
  130. {MaxWindowSize, false},
  131. }
  132. for _, tt := range tests {
  133. t.Run(strconv.Itoa(tt.windowSize), func(t *testing.T) {
  134. var options encoderOptions
  135. err := WithWindowSize(tt.windowSize)(&options)
  136. if tt.err {
  137. if err == nil {
  138. t.Error("did not get error for invalid window size")
  139. }
  140. } else {
  141. if err != nil {
  142. t.Error("got error for valid window size")
  143. }
  144. if options.windowSize != tt.windowSize {
  145. t.Error("failed to set window size")
  146. }
  147. }
  148. })
  149. }
  150. }