gitee.com/quant1x/num@v0.3.2/diff_test.go (about) 1 package num 2 3 import ( 4 "fmt" 5 "gitee.com/quant1x/num/labs" 6 "slices" 7 "testing" 8 ) 9 10 func TestDiff1(t *testing.T) { 11 d1 := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} 12 fmt.Println(d1) 13 fmt.Println("------------------------------------------------------------") 14 N := 1 15 fmt.Println("固定的参数, N =", N) 16 r1 := v1Diff(d1, N) 17 fmt.Println("序列化结果:", r1) 18 fmt.Println("------------------------------------------------------------") 19 s1 := []float64{1, 2, 3, 4, 3, 3, 2, 1, __nilToFloat64, __nilToFloat64, __nilToFloat64, __nilToFloat64} 20 fmt.Printf("序列化参数: %+v\n", s1) 21 r2 := v1Diff(d1, s1) 22 fmt.Println("序列化结果:", r2) 23 } 24 25 func TestDiff2(t *testing.T) { 26 d1 := []float64{1, 2, 3, 4, 5} 27 r1 := v2Diff(d1, 1) 28 fmt.Println(r1) 29 } 30 31 func BenchmarkDiff_init(b *testing.B) { 32 testalignOnce.Do(initTestData) 33 } 34 35 func BenchmarkDiff_release(b *testing.B) { 36 testalignOnce.Do(initTestData) 37 f64s := slices.Clone(testDataFloat64) 38 for i := 0; i < b.N; i++ { 39 Diff(f64s, 1) 40 } 41 } 42 43 func BenchmarkDiff_v1(b *testing.B) { 44 testalignOnce.Do(initTestData) 45 f64s := slices.Clone(testDataFloat64) 46 for i := 0; i < b.N; i++ { 47 v1Diff(f64s, 1) 48 } 49 } 50 51 func BenchmarkDiff_v2(b *testing.B) { 52 testalignOnce.Do(initTestData) 53 f64s := slices.Clone(testDataFloat64) 54 for i := 0; i < b.N; i++ { 55 v2Diff(f64s, 1) 56 } 57 } 58 59 func BenchmarkDiff_v3(b *testing.B) { 60 testalignOnce.Do(initTestData) 61 f64s := slices.Clone(testDataFloat64) 62 for i := 0; i < b.N; i++ { 63 v3Diff(f64s, 1) 64 } 65 } 66 67 func BenchmarkDiff_v4(b *testing.B) { 68 testalignOnce.Do(initTestData) 69 useAvx2 := GetAvx2Enabled() 70 SetAvx2Enabled(true) 71 defer SetAvx2Enabled(useAvx2) 72 f64s := slices.Clone(testDataFloat64) 73 for i := 0; i < b.N; i++ { 74 v4Diff(f64s, 1) 75 } 76 } 77 78 func BenchmarkDiff_v5(b *testing.B) { 79 testalignOnce.Do(initTestData) 80 f64s := slices.Clone(testDataFloat64) 81 for i := 0; i < b.N; i++ { 82 v5Diff(f64s, 1) 83 } 84 } 85 86 func BenchmarkDiff_v6(b *testing.B) { 87 testalignOnce.Do(initTestData) 88 f64s := slices.Clone(testDataFloat64) 89 for i := 0; i < b.N; i++ { 90 v6Diff(f64s, 1) 91 } 92 } 93 94 func BenchmarkDiff_v7(b *testing.B) { 95 testalignOnce.Do(initTestData) 96 f64s := slices.Clone(testDataFloat64) 97 for i := 0; i < b.N; i++ { 98 v7Diff(f64s, 1) 99 } 100 } 101 102 func BenchmarkDiff_v8(b *testing.B) { 103 testalignOnce.Do(initTestData) 104 f64s := slices.Clone(testDataFloat64) 105 for i := 0; i < b.N; i++ { 106 v8Diff(f64s, 1) 107 } 108 } 109 110 func Test_v1Diff(t *testing.T) { 111 type args[E Number] struct { 112 S []E 113 n int 114 } 115 type testCase[E Number] struct { 116 name string 117 args args[E] 118 want []E 119 } 120 tests := []testCase[float64]{ 121 { 122 name: "v3", 123 args: args[float64]{ 124 S: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 125 n: 1, 126 }, 127 want: []float64{Float64NaN(), 1, 1, 1, 1, 1, 1, 1, 1, 1}, 128 }, 129 } 130 for _, tt := range tests { 131 t.Run(tt.name, func(t *testing.T) { 132 if got := v1Diff(tt.args.S, tt.args.n); !labs.DeepEqual(got, tt.want) { 133 t.Errorf("v1Diff() = %v, want %v", got, tt.want) 134 } 135 }) 136 } 137 } 138 139 func Test_v2Diff(t *testing.T) { 140 type args[E Number] struct { 141 S []E 142 n int 143 } 144 type testCase[E Number] struct { 145 name string 146 args args[E] 147 want []E 148 } 149 tests := []testCase[float64]{ 150 { 151 name: "v3", 152 args: args[float64]{ 153 S: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 154 n: 1, 155 }, 156 want: []float64{Float64NaN(), 1, 1, 1, 1, 1, 1, 1, 1, 1}, 157 }, 158 } 159 for _, tt := range tests { 160 t.Run(tt.name, func(t *testing.T) { 161 if got := v2Diff(tt.args.S, tt.args.n); !labs.DeepEqual(got, tt.want) { 162 t.Errorf("v2Diff() = %v, want %v", got, tt.want) 163 } 164 }) 165 } 166 } 167 168 func Test_v3Diff(t *testing.T) { 169 type args[E Number] struct { 170 S []E 171 n int 172 } 173 type testCase[E Number] struct { 174 name string 175 args args[E] 176 want []E 177 } 178 tests := []testCase[float64]{ 179 { 180 name: "v3", 181 args: args[float64]{ 182 S: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 183 n: 1, 184 }, 185 want: []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, Float64NaN()}, 186 }, 187 } 188 for _, tt := range tests { 189 t.Run(tt.name, func(t *testing.T) { 190 if got := v3Diff(tt.args.S, tt.args.n); !labs.DeepEqual(got, tt.want) { 191 t.Errorf("v3Diff() = %v, want %v", got, tt.want) 192 } 193 }) 194 } 195 } 196 197 func Test_v4Diff(t *testing.T) { 198 type args[E Number] struct { 199 S []E 200 n int 201 } 202 type testCase[E Number] struct { 203 name string 204 args args[E] 205 want []E 206 } 207 tests := []testCase[float64]{ 208 { 209 name: "v4", 210 args: args[float64]{ 211 S: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 212 n: -1, 213 }, 214 want: []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, Float64NaN()}, 215 }, 216 } 217 for _, tt := range tests { 218 t.Run(tt.name, func(t *testing.T) { 219 if got := v4Diff(tt.args.S, tt.args.n); !labs.DeepEqual(got, tt.want) { 220 t.Errorf("v4Diff() = %v, want %v", got, tt.want) 221 } 222 }) 223 } 224 } 225 226 func Test_v5Diff(t *testing.T) { 227 type args[E Number] struct { 228 S []E 229 n int 230 } 231 type testCase[E Number] struct { 232 name string 233 args args[E] 234 want []E 235 } 236 tests := []testCase[float64]{ 237 { 238 name: "v5", 239 args: args[float64]{ 240 S: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 241 n: 1, 242 }, 243 want: []float64{Float64NaN(), -1, -1, -1, -1, -1, -1, -1, -1, -1}, 244 }, 245 } 246 for _, tt := range tests { 247 t.Run(tt.name, func(t *testing.T) { 248 if got := v5Diff(tt.args.S, tt.args.n); !labs.DeepEqual(got, tt.want) { 249 t.Errorf("v5Diff() = %v, want %v", got, tt.want) 250 } 251 }) 252 } 253 } 254 255 func Test_v6Diff(t *testing.T) { 256 type args[E Number] struct { 257 S []E 258 n int 259 } 260 type testCase[E Number] struct { 261 name string 262 args args[E] 263 want []E 264 } 265 tests := []testCase[float64]{ 266 { 267 name: "v6", 268 args: args[float64]{ 269 S: []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 270 n: 1, 271 }, 272 want: []float64{Float64NaN(), -1, -1, -1, -1, -1, -1, -1, -1, -1}, 273 }, 274 } 275 for _, tt := range tests { 276 t.Run(tt.name, func(t *testing.T) { 277 if got := v6Diff(tt.args.S, tt.args.n); !labs.DeepEqual(got, tt.want) { 278 t.Errorf("v6Diff() = %v, want %v", got, tt.want) 279 } 280 }) 281 } 282 }