You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.5 KiB

  1. package humanize
  2. import (
  3. "testing"
  4. )
  5. func TestByteParsing(t *testing.T) {
  6. tests := []struct {
  7. in string
  8. exp uint64
  9. }{
  10. {"42", 42},
  11. {"42MB", 42000000},
  12. {"42MiB", 44040192},
  13. {"42mb", 42000000},
  14. {"42mib", 44040192},
  15. {"42MIB", 44040192},
  16. {"42 MB", 42000000},
  17. {"42 MiB", 44040192},
  18. {"42 mb", 42000000},
  19. {"42 mib", 44040192},
  20. {"42 MIB", 44040192},
  21. {"42.5MB", 42500000},
  22. {"42.5MiB", 44564480},
  23. {"42.5 MB", 42500000},
  24. {"42.5 MiB", 44564480},
  25. // No need to say B
  26. {"42M", 42000000},
  27. {"42Mi", 44040192},
  28. {"42m", 42000000},
  29. {"42mi", 44040192},
  30. {"42MI", 44040192},
  31. {"42 M", 42000000},
  32. {"42 Mi", 44040192},
  33. {"42 m", 42000000},
  34. {"42 mi", 44040192},
  35. {"42 MI", 44040192},
  36. {"42.5M", 42500000},
  37. {"42.5Mi", 44564480},
  38. {"42.5 M", 42500000},
  39. {"42.5 Mi", 44564480},
  40. // Bug #42
  41. {"1,005.03 MB", 1005030000},
  42. // Large testing, breaks when too much larger than
  43. // this.
  44. {"12.5 EB", uint64(12.5 * float64(EByte))},
  45. {"12.5 E", uint64(12.5 * float64(EByte))},
  46. {"12.5 EiB", uint64(12.5 * float64(EiByte))},
  47. }
  48. for _, p := range tests {
  49. got, err := ParseBytes(p.in)
  50. if err != nil {
  51. t.Errorf("Couldn't parse %v: %v", p.in, err)
  52. }
  53. if got != p.exp {
  54. t.Errorf("Expected %v for %v, got %v",
  55. p.exp, p.in, got)
  56. }
  57. }
  58. }
  59. func TestByteErrors(t *testing.T) {
  60. got, err := ParseBytes("84 JB")
  61. if err == nil {
  62. t.Errorf("Expected error, got %v", got)
  63. }
  64. got, err = ParseBytes("")
  65. if err == nil {
  66. t.Errorf("Expected error parsing nothing")
  67. }
  68. got, err = ParseBytes("16 EiB")
  69. if err == nil {
  70. t.Errorf("Expected error, got %v", got)
  71. }
  72. }
  73. func TestBytes(t *testing.T) {
  74. testList{
  75. {"bytes(0)", Bytes(0), "0 B"},
  76. {"bytes(1)", Bytes(1), "1 B"},
  77. {"bytes(803)", Bytes(803), "803 B"},
  78. {"bytes(999)", Bytes(999), "999 B"},
  79. {"bytes(1024)", Bytes(1024), "1.0 kB"},
  80. {"bytes(9999)", Bytes(9999), "10 kB"},
  81. {"bytes(1MB - 1)", Bytes(MByte - Byte), "1000 kB"},
  82. {"bytes(1MB)", Bytes(1024 * 1024), "1.0 MB"},
  83. {"bytes(1GB - 1K)", Bytes(GByte - KByte), "1000 MB"},
  84. {"bytes(1GB)", Bytes(GByte), "1.0 GB"},
  85. {"bytes(1TB - 1M)", Bytes(TByte - MByte), "1000 GB"},
  86. {"bytes(10MB)", Bytes(9999 * 1000), "10 MB"},
  87. {"bytes(1TB)", Bytes(TByte), "1.0 TB"},
  88. {"bytes(1PB - 1T)", Bytes(PByte - TByte), "999 TB"},
  89. {"bytes(1PB)", Bytes(PByte), "1.0 PB"},
  90. {"bytes(1PB - 1T)", Bytes(EByte - PByte), "999 PB"},
  91. {"bytes(1EB)", Bytes(EByte), "1.0 EB"},
  92. // Overflows.
  93. // {"bytes(1EB - 1P)", Bytes((KByte*EByte)-PByte), "1023EB"},
  94. {"bytes(0)", IBytes(0), "0 B"},
  95. {"bytes(1)", IBytes(1), "1 B"},
  96. {"bytes(803)", IBytes(803), "803 B"},
  97. {"bytes(1023)", IBytes(1023), "1023 B"},
  98. {"bytes(1024)", IBytes(1024), "1.0 KiB"},
  99. {"bytes(1MB - 1)", IBytes(MiByte - IByte), "1024 KiB"},
  100. {"bytes(1MB)", IBytes(1024 * 1024), "1.0 MiB"},
  101. {"bytes(1GB - 1K)", IBytes(GiByte - KiByte), "1024 MiB"},
  102. {"bytes(1GB)", IBytes(GiByte), "1.0 GiB"},
  103. {"bytes(1TB - 1M)", IBytes(TiByte - MiByte), "1024 GiB"},
  104. {"bytes(1TB)", IBytes(TiByte), "1.0 TiB"},
  105. {"bytes(1PB - 1T)", IBytes(PiByte - TiByte), "1023 TiB"},
  106. {"bytes(1PB)", IBytes(PiByte), "1.0 PiB"},
  107. {"bytes(1PB - 1T)", IBytes(EiByte - PiByte), "1023 PiB"},
  108. {"bytes(1EiB)", IBytes(EiByte), "1.0 EiB"},
  109. // Overflows.
  110. // {"bytes(1EB - 1P)", IBytes((KIByte*EIByte)-PiByte), "1023EB"},
  111. {"bytes(5.5GiB)", IBytes(5.5 * GiByte), "5.5 GiB"},
  112. {"bytes(5.5GB)", Bytes(5.5 * GByte), "5.5 GB"},
  113. }.validate(t)
  114. }
  115. func BenchmarkParseBytes(b *testing.B) {
  116. for i := 0; i < b.N; i++ {
  117. ParseBytes("16.5 GB")
  118. }
  119. }
  120. func BenchmarkBytes(b *testing.B) {
  121. for i := 0; i < b.N; i++ {
  122. Bytes(16.5 * GByte)
  123. }
  124. }