1package decor 2 3import ( 4 "fmt" 5 "testing" 6) 7 8func TestB1024(t *testing.T) { 9 cases := map[string]struct { 10 value int64 11 verb string 12 expected string 13 }{ 14 "verb %f": {12345678, "%f", "11.773756MiB"}, 15 "verb %.0f": {12345678, "%.0f", "12MiB"}, 16 "verb %.1f": {12345678, "%.1f", "11.8MiB"}, 17 "verb %.2f": {12345678, "%.2f", "11.77MiB"}, 18 "verb %.3f": {12345678, "%.3f", "11.774MiB"}, 19 20 "verb % f": {12345678, "% f", "11.773756 MiB"}, 21 "verb % .0f": {12345678, "% .0f", "12 MiB"}, 22 "verb % .1f": {12345678, "% .1f", "11.8 MiB"}, 23 "verb % .2f": {12345678, "% .2f", "11.77 MiB"}, 24 "verb % .3f": {12345678, "% .3f", "11.774 MiB"}, 25 26 "1000 %f": {1000, "%f", "1000.000000b"}, 27 "1000 %d": {1000, "%d", "1000b"}, 28 "1000 %s": {1000, "%s", "1000b"}, 29 "1024 %f": {1024, "%f", "1.000000KiB"}, 30 "1024 %d": {1024, "%d", "1KiB"}, 31 "1024 %.1f": {1024, "%.1f", "1.0KiB"}, 32 "1024 %s": {1024, "%s", "1KiB"}, 33 "3*MiB+140KiB %f": {3*int64(_iMiB) + 140*int64(_iKiB), "%f", "3.136719MiB"}, 34 "3*MiB+140KiB %d": {3*int64(_iMiB) + 140*int64(_iKiB), "%d", "3MiB"}, 35 "3*MiB+140KiB %.1f": {3*int64(_iMiB) + 140*int64(_iKiB), "%.1f", "3.1MiB"}, 36 "3*MiB+140KiB %s": {3*int64(_iMiB) + 140*int64(_iKiB), "%s", "3.13671875MiB"}, 37 "2*GiB %f": {2 * int64(_iGiB), "%f", "2.000000GiB"}, 38 "2*GiB %d": {2 * int64(_iGiB), "%d", "2GiB"}, 39 "2*GiB %.1f": {2 * int64(_iGiB), "%.1f", "2.0GiB"}, 40 "2*GiB %s": {2 * int64(_iGiB), "%s", "2GiB"}, 41 "4*TiB %f": {4 * int64(_iTiB), "%f", "4.000000TiB"}, 42 "4*TiB %d": {4 * int64(_iTiB), "%d", "4TiB"}, 43 "4*TiB %.1f": {4 * int64(_iTiB), "%.1f", "4.0TiB"}, 44 "4*TiB %s": {4 * int64(_iTiB), "%s", "4TiB"}, 45 } 46 for name, tc := range cases { 47 t.Run(name, func(t *testing.T) { 48 got := fmt.Sprintf(tc.verb, SizeB1024(tc.value)) 49 if got != tc.expected { 50 t.Fatalf("expected: %q, got: %q\n", tc.expected, got) 51 } 52 }) 53 } 54} 55 56func TestB1000(t *testing.T) { 57 cases := map[string]struct { 58 value int64 59 verb string 60 expected string 61 }{ 62 "verb %f": {12345678, "%f", "12.345678MB"}, 63 "verb %.0f": {12345678, "%.0f", "12MB"}, 64 "verb %.1f": {12345678, "%.1f", "12.3MB"}, 65 "verb %.2f": {12345678, "%.2f", "12.35MB"}, 66 "verb %.3f": {12345678, "%.3f", "12.346MB"}, 67 68 "verb % f": {12345678, "% f", "12.345678 MB"}, 69 "verb % .0f": {12345678, "% .0f", "12 MB"}, 70 "verb % .1f": {12345678, "% .1f", "12.3 MB"}, 71 "verb % .2f": {12345678, "% .2f", "12.35 MB"}, 72 "verb % .3f": {12345678, "% .3f", "12.346 MB"}, 73 74 "1000 %f": {1000, "%f", "1.000000KB"}, 75 "1000 %d": {1000, "%d", "1KB"}, 76 "1000 %s": {1000, "%s", "1KB"}, 77 "1024 %f": {1024, "%f", "1.024000KB"}, 78 "1024 %d": {1024, "%d", "1KB"}, 79 "1024 %.1f": {1024, "%.1f", "1.0KB"}, 80 "1024 %s": {1024, "%s", "1.024KB"}, 81 "3*MB+140*KB %f": {3*int64(_MB) + 140*int64(_KB), "%f", "3.140000MB"}, 82 "3*MB+140*KB %d": {3*int64(_MB) + 140*int64(_KB), "%d", "3MB"}, 83 "3*MB+140*KB %.1f": {3*int64(_MB) + 140*int64(_KB), "%.1f", "3.1MB"}, 84 "3*MB+140*KB %s": {3*int64(_MB) + 140*int64(_KB), "%s", "3.14MB"}, 85 "2*GB %f": {2 * int64(_GB), "%f", "2.000000GB"}, 86 "2*GB %d": {2 * int64(_GB), "%d", "2GB"}, 87 "2*GB %.1f": {2 * int64(_GB), "%.1f", "2.0GB"}, 88 "2*GB %s": {2 * int64(_GB), "%s", "2GB"}, 89 "4*TB %f": {4 * int64(_TB), "%f", "4.000000TB"}, 90 "4*TB %d": {4 * int64(_TB), "%d", "4TB"}, 91 "4*TB %.1f": {4 * int64(_TB), "%.1f", "4.0TB"}, 92 "4*TB %s": {4 * int64(_TB), "%s", "4TB"}, 93 } 94 for name, tc := range cases { 95 t.Run(name, func(t *testing.T) { 96 got := fmt.Sprintf(tc.verb, SizeB1000(tc.value)) 97 if got != tc.expected { 98 t.Fatalf("expected: %q, got: %q\n", tc.expected, got) 99 } 100 }) 101 } 102} 103