bytes_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package util
  2. import "testing"
  3. func TestByteParsing(t *testing.T) {
  4. tests := []struct {
  5. in string
  6. exp uint64
  7. }{
  8. {"42", 42},
  9. {"42MB", 42000000},
  10. {"42MiB", 44040192},
  11. {"42mb", 42000000},
  12. {"42mib", 44040192},
  13. {"42MIB", 44040192},
  14. {"42 MB", 42000000},
  15. {"42 MiB", 44040192},
  16. {"42 mb", 42000000},
  17. {"42 mib", 44040192},
  18. {"42 MIB", 44040192},
  19. {"42.5MB", 42500000},
  20. {"42.5MiB", 44564480},
  21. {"42.5 MB", 42500000},
  22. {"42.5 MiB", 44564480},
  23. // No need to say B
  24. {"42M", 42000000},
  25. {"42Mi", 44040192},
  26. {"42m", 42000000},
  27. {"42mi", 44040192},
  28. {"42MI", 44040192},
  29. {"42 M", 42000000},
  30. {"42 Mi", 44040192},
  31. {"42 m", 42000000},
  32. {"42 mi", 44040192},
  33. {"42 MI", 44040192},
  34. {"42.5M", 42500000},
  35. {"42.5Mi", 44564480},
  36. {"42.5 M", 42500000},
  37. {"42.5 Mi", 44564480},
  38. // Bug #42
  39. {"1,005.03 MB", 1005030000},
  40. // Large testing, breaks when too much larger than
  41. // this.
  42. {"12.5 EB", uint64(12.5 * float64(EByte))},
  43. {"12.5 E", uint64(12.5 * float64(EByte))},
  44. {"12.5 EiB", uint64(12.5 * float64(EiByte))},
  45. }
  46. for _, p := range tests {
  47. got, err := ParseBytes(p.in)
  48. if err != nil {
  49. t.Errorf("Couldn't parse %v: %v", p.in, err)
  50. }
  51. if got != p.exp {
  52. t.Errorf("Expected %v for %v, got %v",
  53. p.exp, p.in, got)
  54. }
  55. }
  56. }