bytes_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package format
  2. import (
  3. "testing"
  4. )
  5. func TestHumanBytes(t *testing.T) {
  6. type testCase struct {
  7. input int64
  8. expected string
  9. }
  10. tests := []testCase{
  11. // Test bytes (B)
  12. {0, "0 B"},
  13. {1, "1 B"},
  14. {999, "999 B"},
  15. // Test kilobytes (KB)
  16. {1000, "1 KB"},
  17. {1500, "1.5 KB"},
  18. {999999, "999 KB"},
  19. // Test megabytes (MB)
  20. {1000000, "1 MB"},
  21. {1500000, "1.5 MB"},
  22. {999999999, "999 MB"},
  23. // Test gigabytes (GB)
  24. {1000000000, "1 GB"},
  25. {1500000000, "1.5 GB"},
  26. {999999999999, "999 GB"},
  27. // Test terabytes (TB)
  28. {1000000000000, "1 TB"},
  29. {1500000000000, "1.5 TB"},
  30. {1999999999999, "2.0 TB"},
  31. // Test fractional values
  32. {1234, "1.2 KB"},
  33. {1234567, "1.2 MB"},
  34. {1234567890, "1.2 GB"},
  35. }
  36. for _, tc := range tests {
  37. t.Run(tc.expected, func(t *testing.T) {
  38. result := HumanBytes(tc.input)
  39. if result != tc.expected {
  40. t.Errorf("Expected %s, got %s", tc.expected, result)
  41. }
  42. })
  43. }
  44. }
  45. func TestHumanBytes2(t *testing.T) {
  46. type testCase struct {
  47. input uint64
  48. expected string
  49. }
  50. tests := []testCase{
  51. // Test bytes (B)
  52. {0, "0 B"},
  53. {1, "1 B"},
  54. {1023, "1023 B"},
  55. // Test kibibytes (KiB)
  56. {1024, "1.0 KiB"},
  57. {1536, "1.5 KiB"},
  58. {1048575, "1024.0 KiB"},
  59. // Test mebibytes (MiB)
  60. {1048576, "1.0 MiB"},
  61. {1572864, "1.5 MiB"},
  62. {1073741823, "1024.0 MiB"},
  63. // Test gibibytes (GiB)
  64. {1073741824, "1.0 GiB"},
  65. {1610612736, "1.5 GiB"},
  66. {2147483648, "2.0 GiB"},
  67. }
  68. for _, tc := range tests {
  69. t.Run(tc.expected, func(t *testing.T) {
  70. result := HumanBytes2(tc.input)
  71. if result != tc.expected {
  72. t.Errorf("Expected %s, got %s", tc.expected, result)
  73. }
  74. })
  75. }
  76. }