bigbytes_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package humanize
  2. import (
  3. "math/big"
  4. "testing"
  5. )
  6. func TestBigByteParsing(t *testing.T) {
  7. tests := []struct {
  8. in string
  9. exp uint64
  10. }{
  11. {"42", 42},
  12. {"42MB", 42000000},
  13. {"42MiB", 44040192},
  14. {"42mb", 42000000},
  15. {"42mib", 44040192},
  16. {"42MIB", 44040192},
  17. {"42 MB", 42000000},
  18. {"42 MiB", 44040192},
  19. {"42 mb", 42000000},
  20. {"42 mib", 44040192},
  21. {"42 MIB", 44040192},
  22. {"42.5MB", 42500000},
  23. {"42.5MiB", 44564480},
  24. {"42.5 MB", 42500000},
  25. {"42.5 MiB", 44564480},
  26. // No need to say B
  27. {"42M", 42000000},
  28. {"42Mi", 44040192},
  29. {"42m", 42000000},
  30. {"42mi", 44040192},
  31. {"42MI", 44040192},
  32. {"42 M", 42000000},
  33. {"42 Mi", 44040192},
  34. {"42 m", 42000000},
  35. {"42 mi", 44040192},
  36. {"42 MI", 44040192},
  37. {"42.5M", 42500000},
  38. {"42.5Mi", 44564480},
  39. {"42.5 M", 42500000},
  40. {"42.5 Mi", 44564480},
  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 := ParseBigBytes(p.in)
  50. if err != nil {
  51. t.Errorf("Couldn't parse %v: %v", p.in, err)
  52. } else {
  53. if got.Uint64() != p.exp {
  54. t.Errorf("Expected %v for %v, got %v",
  55. p.exp, p.in, got)
  56. }
  57. }
  58. }
  59. }
  60. func TestBigByteErrors(t *testing.T) {
  61. got, err := ParseBigBytes("84 JB")
  62. if err == nil {
  63. t.Errorf("Expected error, got %v", got)
  64. }
  65. got, err = ParseBigBytes("")
  66. if err == nil {
  67. t.Errorf("Expected error parsing nothing")
  68. }
  69. }
  70. func bbyte(in uint64) string {
  71. return BigBytes((&big.Int{}).SetUint64(in))
  72. }
  73. func bibyte(in uint64) string {
  74. return BigIBytes((&big.Int{}).SetUint64(in))
  75. }
  76. func TestBigBytes(t *testing.T) {
  77. testList{
  78. {"bytes(0)", bbyte(0), "0 B"},
  79. {"bytes(1)", bbyte(1), "1 B"},
  80. {"bytes(803)", bbyte(803), "803 B"},
  81. {"bytes(999)", bbyte(999), "999 B"},
  82. {"bytes(1024)", bbyte(1024), "1.0 kB"},
  83. {"bytes(1MB - 1)", bbyte(MByte - Byte), "1000 kB"},
  84. {"bytes(1MB)", bbyte(1024 * 1024), "1.0 MB"},
  85. {"bytes(1GB - 1K)", bbyte(GByte - KByte), "1000 MB"},
  86. {"bytes(1GB)", bbyte(GByte), "1.0 GB"},
  87. {"bytes(1TB - 1M)", bbyte(TByte - MByte), "1000 GB"},
  88. {"bytes(1TB)", bbyte(TByte), "1.0 TB"},
  89. {"bytes(1PB - 1T)", bbyte(PByte - TByte), "999 TB"},
  90. {"bytes(1PB)", bbyte(PByte), "1.0 PB"},
  91. {"bytes(1PB - 1T)", bbyte(EByte - PByte), "999 PB"},
  92. {"bytes(1EB)", bbyte(EByte), "1.0 EB"},
  93. // Overflows.
  94. // {"bytes(1EB - 1P)", Bytes((KByte*EByte)-PByte), "1023EB"},
  95. {"bytes(0)", bibyte(0), "0 B"},
  96. {"bytes(1)", bibyte(1), "1 B"},
  97. {"bytes(803)", bibyte(803), "803 B"},
  98. {"bytes(1023)", bibyte(1023), "1023 B"},
  99. {"bytes(1024)", bibyte(1024), "1.0 KiB"},
  100. {"bytes(1MB - 1)", bibyte(MiByte - IByte), "1024 KiB"},
  101. {"bytes(1MB)", bibyte(1024 * 1024), "1.0 MiB"},
  102. {"bytes(1GB - 1K)", bibyte(GiByte - KiByte), "1024 MiB"},
  103. {"bytes(1GB)", bibyte(GiByte), "1.0 GiB"},
  104. {"bytes(1TB - 1M)", bibyte(TiByte - MiByte), "1024 GiB"},
  105. {"bytes(1TB)", bibyte(TiByte), "1.0 TiB"},
  106. {"bytes(1PB - 1T)", bibyte(PiByte - TiByte), "1023 TiB"},
  107. {"bytes(1PB)", bibyte(PiByte), "1.0 PiB"},
  108. {"bytes(1PB - 1T)", bibyte(EiByte - PiByte), "1023 PiB"},
  109. {"bytes(1EiB)", bibyte(EiByte), "1.0 EiB"},
  110. // Overflows.
  111. // {"bytes(1EB - 1P)", bibyte((KIByte*EIByte)-PiByte), "1023EB"},
  112. {"bytes(5.5GiB)", bibyte(5.5 * GiByte), "5.5 GiB"},
  113. {"bytes(5.5GB)", bbyte(5.5 * GByte), "5.5 GB"},
  114. }.validate(t)
  115. }
  116. func TestVeryBigBytes(t *testing.T) {
  117. b, _ := (&big.Int{}).SetString("15347691069326346944512", 10)
  118. s := BigBytes(b)
  119. if s != "15 ZB" {
  120. t.Errorf("Expected 15 ZB, got %v", s)
  121. }
  122. s = BigIBytes(b)
  123. if s != "13 ZiB" {
  124. t.Errorf("Expected 13 ZiB, got %v", s)
  125. }
  126. b, _ = (&big.Int{}).SetString("15716035654990179271180288", 10)
  127. s = BigBytes(b)
  128. if s != "16 YB" {
  129. t.Errorf("Expected 16 YB, got %v", s)
  130. }
  131. s = BigIBytes(b)
  132. if s != "13 YiB" {
  133. t.Errorf("Expected 13 YiB, got %v", s)
  134. }
  135. }
  136. func TestVeryVeryBigBytes(t *testing.T) {
  137. b, _ := (&big.Int{}).SetString("16093220510709943573688614912", 10)
  138. s := BigBytes(b)
  139. if s != "16093 YB" {
  140. t.Errorf("Expected 16093 YB, got %v", s)
  141. }
  142. s = BigIBytes(b)
  143. if s != "13312 YiB" {
  144. t.Errorf("Expected 13312 YiB, got %v", s)
  145. }
  146. }
  147. func TestParseVeryBig(t *testing.T) {
  148. tests := []struct {
  149. in string
  150. out string
  151. }{
  152. {"16 ZB", "16000000000000000000000"},
  153. {"16 ZiB", "18889465931478580854784"},
  154. {"16.5 ZB", "16500000000000000000000"},
  155. {"16.5 ZiB", "19479761741837286506496"},
  156. {"16 Z", "16000000000000000000000"},
  157. {"16 Zi", "18889465931478580854784"},
  158. {"16.5 Z", "16500000000000000000000"},
  159. {"16.5 Zi", "19479761741837286506496"},
  160. {"16 YB", "16000000000000000000000000"},
  161. {"16 YiB", "19342813113834066795298816"},
  162. {"16.5 YB", "16500000000000000000000000"},
  163. {"16.5 YiB", "19947276023641381382651904"},
  164. {"16 Y", "16000000000000000000000000"},
  165. {"16 Yi", "19342813113834066795298816"},
  166. {"16.5 Y", "16500000000000000000000000"},
  167. {"16.5 Yi", "19947276023641381382651904"},
  168. }
  169. for _, test := range tests {
  170. x, err := ParseBigBytes(test.in)
  171. if err != nil {
  172. t.Errorf("Error parsing %q: %v", test.in, err)
  173. continue
  174. }
  175. if x.String() != test.out {
  176. t.Errorf("Expected %q for %q, got %v", test.out, test.in, x)
  177. }
  178. }
  179. }
  180. func BenchmarkParseBigBytes(b *testing.B) {
  181. for i := 0; i < b.N; i++ {
  182. ParseBigBytes("16.5 Z")
  183. }
  184. }
  185. func BenchmarkBigBytes(b *testing.B) {
  186. for i := 0; i < b.N; i++ {
  187. bibyte(16.5 * GByte)
  188. }
  189. }