format_test.go 633 B

123456789101112131415161718192021222324252627282930313233343536
  1. package format
  2. import (
  3. "testing"
  4. )
  5. func TestHumanNumber(t *testing.T) {
  6. type testCase struct {
  7. input uint64
  8. expected string
  9. }
  10. testCases := []testCase{
  11. {0, "0"},
  12. {999, "999"},
  13. {1000, "1K"},
  14. {1001, "1K"},
  15. {1000000, "1M"},
  16. {125000000, "125M"},
  17. {500500000, "500.50M"},
  18. {500550000, "500.55M"},
  19. {1000000000, "1B"},
  20. {2800000000, "2.8B"},
  21. {2850000000, "2.9B"},
  22. {1000000000000, "1000B"},
  23. }
  24. for _, tc := range testCases {
  25. t.Run(tc.expected, func(t *testing.T) {
  26. result := HumanNumber(tc.input)
  27. if result != tc.expected {
  28. t.Errorf("Expected %s, got %s", tc.expected, result)
  29. }
  30. })
  31. }
  32. }