data_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package city_test
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "testing"
  6. "github.com/go-faster/city"
  7. )
  8. // Generated with:
  9. // go run ./internal/citygen > _testdata/data.json
  10. // Not worth it to use go:generate here.
  11. //go:embed _testdata/data.json
  12. var testData []byte
  13. func TestData(t *testing.T) {
  14. var data struct {
  15. Seed city.U128
  16. Entries []struct {
  17. Input string
  18. City32 uint32
  19. City64 uint64
  20. City128 city.U128
  21. City128Seed city.U128
  22. ClickHouse64 uint64
  23. ClickHouse128 city.U128
  24. ClickHouse128Seed city.U128
  25. }
  26. }
  27. if err := json.Unmarshal(testData, &data); err != nil {
  28. t.Fatal(err)
  29. }
  30. for _, e := range data.Entries {
  31. input := []byte(e.Input)
  32. if v := city.Hash32(input); v != e.City32 {
  33. t.Errorf("Hash32(%q) %d (got) != %d (expected)", input, v, e.City32)
  34. }
  35. if v := city.Hash64(input); v != e.City64 {
  36. t.Errorf("Hash64(%q) %d (got) != %d (expected)", input, v, e.City64)
  37. }
  38. if v := city.Hash128(input); v != e.City128 {
  39. t.Errorf("Hash128(%q) %v (got) != %v (expected)", input, v, e.City128)
  40. }
  41. if v := city.Hash128Seed(input, data.Seed); v != e.City128Seed {
  42. t.Errorf("Hash128Seed(%q, %v) %v (got) != %v (expected)", input, data.Seed, v, e.City128Seed)
  43. }
  44. if v := city.CH64(input); v != e.ClickHouse64 {
  45. t.Errorf("CH64(%q) %v (got) != %v (expected)", input, v, e.City64)
  46. }
  47. if v := city.CH128(input); v != e.ClickHouse128 {
  48. t.Errorf("CH128(%q) %v (got) != %v (expected)", input, v, e.ClickHouse128)
  49. }
  50. if v := city.CH128Seed(input, data.Seed); v != e.ClickHouse128Seed {
  51. t.Errorf("CH128Seed(%q, %v) %v (got) != %v (expected)", input, data.Seed, v, e.ClickHouse128Seed)
  52. }
  53. }
  54. }