|
1 | | -// Copyright 2022 EMQ Technologies Co., Ltd. |
| 1 | +// Copyright 2022-2026 EMQ Technologies Co., Ltd. |
2 | 2 | // |
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | // you may not use this file except in compliance with the License. |
@@ -305,3 +305,53 @@ func TestExecIgnoreNull(t *testing.T) { |
305 | 305 | } |
306 | 306 | } |
307 | 307 | } |
| 308 | + |
| 309 | +func TestChangedValueEqual(t *testing.T) { |
| 310 | + tests := []struct { |
| 311 | + name string |
| 312 | + v1 any |
| 313 | + v2 any |
| 314 | + want bool |
| 315 | + }{ |
| 316 | + {name: "nil", want: true}, |
| 317 | + {name: "one nil", v1: int64(1), want: false}, |
| 318 | + {name: "string", v1: "value", v2: "value", want: true}, |
| 319 | + {name: "different string", v1: "value", v2: "other", want: false}, |
| 320 | + {name: "int64", v1: int64(1), v2: int64(1), want: true}, |
| 321 | + {name: "different numeric types", v1: int64(1), v2: int(1), want: false}, |
| 322 | + {name: "float64", v1: float64(1.5), v2: float64(1.5), want: true}, |
| 323 | + {name: "bool", v1: true, v2: true, want: true}, |
| 324 | + {name: "int", v1: int(1), v2: int(1), want: true}, |
| 325 | + {name: "slice fallback", v1: []int{1, 2}, v2: []int{1, 2}, want: true}, |
| 326 | + {name: "map fallback", v1: map[string]any{"a": 1}, v2: map[string]any{"a": 2}, want: false}, |
| 327 | + } |
| 328 | + for _, tt := range tests { |
| 329 | + t.Run(tt.name, func(t *testing.T) { |
| 330 | + if got := changedValueEqual(tt.v1, tt.v2); got != tt.want { |
| 331 | + t.Errorf("changedValueEqual(%v, %v) = %v, want %v", tt.v1, tt.v2, got, tt.want) |
| 332 | + } |
| 333 | + }) |
| 334 | + } |
| 335 | +} |
| 336 | + |
| 337 | +func BenchmarkChangedColsStable(b *testing.B) { |
| 338 | + contextLogger := conf.Log.WithField("rule", "BenchmarkChangedColsStable") |
| 339 | + ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger) |
| 340 | + tempStore, err := state.CreateStore("BenchmarkChangedColsStable", def.AtMostOnce) |
| 341 | + if err != nil { |
| 342 | + b.Fatal(err) |
| 343 | + } |
| 344 | + fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("BenchmarkChangedColsStable", "changed_cols", tempStore), 1) |
| 345 | + args := []any{"p_", true, "same", int64(42), true} |
| 346 | + keys := []string{"prefix", "ignore", "string", "integer", "boolean"} |
| 347 | + if _, err := changedFunc(fctx, args, keys); err != nil { |
| 348 | + b.Fatal(err) |
| 349 | + } |
| 350 | + b.ReportAllocs() |
| 351 | + b.ResetTimer() |
| 352 | + for i := 0; i < b.N; i++ { |
| 353 | + if _, err := changedFunc(fctx, args, keys); err != nil { |
| 354 | + b.Fatal(err) |
| 355 | + } |
| 356 | + } |
| 357 | +} |
0 commit comments