stats_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import (
  3. "flag"
  4. "os"
  5. "testing"
  6. )
  7. func TestMain(m *testing.M) {
  8. flag.Parse()
  9. os.Exit(m.Run())
  10. }
  11. func TestPoolCC(t *testing.T) {
  12. tests := []struct {
  13. Input string
  14. Expected string
  15. Ok bool
  16. }{
  17. {"pool.ntp.org", "", false},
  18. {"2.pool.ntp.org", "", false},
  19. {"us.pool.ntp.org", "us", true},
  20. {"0.us.pool.ntp.org", "us", true},
  21. {"asia.pool.ntp.org", "asia", true},
  22. {"3.asia.pool.ntp.org", "asia", true},
  23. {"3.example.pool.ntp.org", "", false},
  24. }
  25. for _, x := range tests {
  26. got, ok := getPoolCC(x.Input)
  27. if got != x.Expected {
  28. t.Logf("Got '%s' but expected '%s' for '%s'", got, x.Expected, x.Input)
  29. t.Fail()
  30. }
  31. if ok != x.Ok {
  32. t.Logf("Got '%t' but expected '%t' for '%s'", ok, x.Ok, x.Input)
  33. t.Fail()
  34. }
  35. }
  36. }
  37. func TestVendorName(t *testing.T) {
  38. tests := []struct {
  39. Input string
  40. Expected string
  41. }{
  42. {"pool.ntp.org.", ""},
  43. {"2.pool.ntp.org.", ""},
  44. {"us.pool.ntp.org.", "_country"},
  45. {"2.us.pool.ntp.org.", "_country"},
  46. {"europe.pool.ntp.org.", "_continent"},
  47. {"2.europe.pool.ntp.org.", "_continent"},
  48. {"0.example.pool.ntp.org.", "example"},
  49. {"3.example.pool.ntp.org.", "example"},
  50. }
  51. for _, x := range tests {
  52. got := vendorName(x.Input)
  53. if got != x.Expected {
  54. t.Logf("Got '%s' but expected '%s' for '%s'", got, x.Expected, x.Input)
  55. t.Fail()
  56. }
  57. }
  58. }