| 283 | } |
| 284 | |
| 285 | func TestHashCallInvariants(t *testing.T) { |
| 286 | // Must hash equal (the load-bearing invariant: matches() == true => hashCall agrees): |
| 287 | t.Run("cross-type numeric equal", func(t *testing.T) { |
| 288 | hInt := hashCall(1, overloads.ContainsString, []ref.Val{types.Int(1)}) |
| 289 | hUint := hashCall(1, overloads.ContainsString, []ref.Val{types.Uint(1)}) |
| 290 | hDouble := hashCall(1, overloads.ContainsString, []ref.Val{types.Double(1.0)}) |
| 291 | if hInt != hUint || hInt != hDouble { |
| 292 | t.Errorf("expected Int(1), Uint(1), Double(1.0) to hash equal, got int=%d, uint=%d, double=%d", hInt, hUint, hDouble) |
| 293 | } |
| 294 | }) |
| 295 | |
| 296 | t.Run("signed zero equal", func(t *testing.T) { |
| 297 | hZero := hashCall(1, overloads.ContainsString, []ref.Val{types.Double(0.0)}) |
| 298 | hNegZero := hashCall(1, overloads.ContainsString, []ref.Val{types.Double(math.Copysign(0.0, -1.0))}) |
| 299 | hIntZero := hashCall(1, overloads.ContainsString, []ref.Val{types.Int(0)}) |
| 300 | hUintZero := hashCall(1, overloads.ContainsString, []ref.Val{types.Uint(0)}) |
| 301 | if hZero != hNegZero || hZero != hIntZero || hZero != hUintZero { |
| 302 | t.Errorf("expected 0.0, -0.0, Int(0), Uint(0) to hash equal, got 0.0=%d, -0.0=%d, int0=%d, uint0=%d", hZero, hNegZero, hIntZero, hUintZero) |
| 303 | } |
| 304 | }) |
| 305 | |
| 306 | t.Run("NaN payloads equal", func(t *testing.T) { |
| 307 | nan1 := types.Double(math.NaN()) |
| 308 | nan2 := types.Double(math.Float64frombits(0x7ff8000000000001)) |
| 309 | if hashCall(1, overloads.ContainsString, []ref.Val{nan1}) != hashCall(1, overloads.ContainsString, []ref.Val{nan2}) { |
| 310 | t.Errorf("expected different NaN payloads to hash equal") |
| 311 | } |
| 312 | }) |
| 313 | |
| 314 | t.Run("complex types equal", func(t *testing.T) { |
| 315 | map1 := types.DefaultTypeAdapter.NativeToValue(map[string]any{"a": 1}) |
| 316 | map2 := types.DefaultTypeAdapter.NativeToValue(map[string]any{"b": 2}) |
| 317 | if hashCall(1, overloads.ContainsString, []ref.Val{map1}) != hashCall(1, overloads.ContainsString, []ref.Val{map2}) { |
| 318 | t.Errorf("expected different complex types to share the same default bucket") |
| 319 | } |
| 320 | }) |
| 321 | |
| 322 | // Table test asserting the load-bearing invariant over a corpus of ref.Val pairs: |
| 323 | t.Run("equality implies hash equality corpus", func(t *testing.T) { |
| 324 | corpus := []ref.Val{ |
| 325 | types.Int(0), |
| 326 | types.Int(1), |
| 327 | types.Int(-1), |
| 328 | types.Int(42), |
| 329 | types.Uint(0), |
| 330 | types.Uint(1), |
| 331 | types.Uint(42), |
| 332 | types.Double(0.0), |
| 333 | types.Double(math.Copysign(0.0, -1.0)), |
| 334 | types.Double(1.0), |
| 335 | types.Double(-1.0), |
| 336 | types.Double(42.0), |
| 337 | types.Bool(true), |
| 338 | types.Bool(false), |
| 339 | types.String(""), |
| 340 | types.String("0"), |
| 341 | types.String("1"), |
| 342 | types.String("42"), |