|
| 1 | +package counts_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/github/git-sizer/counts" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +type humanTest struct { |
| 12 | + n uint64 |
| 13 | + number, unit string |
| 14 | +} |
| 15 | + |
| 16 | +func TestMetric(t *testing.T) { |
| 17 | + assert := assert.New(t) |
| 18 | + |
| 19 | + for _, ht := range []humanTest{ |
| 20 | + {0, "0", "cd"}, |
| 21 | + {1, "1", "cd"}, |
| 22 | + {999, "999", "cd"}, |
| 23 | + {1000, "1.00", "kcd"}, |
| 24 | + {1094, "1.09", "kcd"}, |
| 25 | + {1096, "1.10", "kcd"}, |
| 26 | + {9990, "9.99", "kcd"}, |
| 27 | + {9999, "10.00", "kcd"}, // Not ideal, but ok |
| 28 | + {10000, "10.0", "kcd"}, |
| 29 | + {10060, "10.1", "kcd"}, |
| 30 | + {99999, "100.0", "kcd"}, // Not ideal, but ok |
| 31 | + {100000, "100", "kcd"}, |
| 32 | + {999999, "1000", "kcd"}, // Not ideal, but ok |
| 33 | + {1000000, "1.00", "Mcd"}, |
| 34 | + {9999999, "10.00", "Mcd"}, // Not ideal, but ok |
| 35 | + {10000000, "10.0", "Mcd"}, |
| 36 | + {99999999, "100.0", "Mcd"}, // Not ideal, but ok |
| 37 | + {100000000, "100", "Mcd"}, |
| 38 | + {999999999, "1000", "Mcd"}, // Not ideal, but ok |
| 39 | + {1000000000, "1.00", "Gcd"}, |
| 40 | + {9999999999, "10.00", "Gcd"}, // Not ideal, but ok |
| 41 | + {10000000000, "10.0", "Gcd"}, |
| 42 | + {99999999999, "100.0", "Gcd"}, // Not ideal, but ok |
| 43 | + {100000000000, "100", "Gcd"}, |
| 44 | + {999999999999, "1000", "Gcd"}, // Not ideal, but ok |
| 45 | + {1000000000000, "1.00", "Tcd"}, |
| 46 | + {999999999999999, "1000", "Tcd"}, // Not ideal, but ok |
| 47 | + {1000000000000000, "1.00", "Pcd"}, |
| 48 | + {999999999999999999, "1000", "Pcd"}, |
| 49 | + {1000000000000000000, "1000", "Pcd"}, |
| 50 | + {9999999999999999999, "10000", "Pcd"}, |
| 51 | + {10000000000000000000, "10000", "Pcd"}, |
| 52 | + {12345678900000000000, "12346", "Pcd"}, // Not ideal, but ok |
| 53 | + {0xffffffffffffffff, "18447", "Pcd"}, // Not ideal, but ok |
| 54 | + } { |
| 55 | + number, unit := counts.Human(ht.n, counts.MetricPrefixes, "cd") |
| 56 | + assert.Equalf(ht.number, number, "Number for %d in metric", ht.n) |
| 57 | + assert.Equalf(ht.unit, unit, "Unit for %d in metric", ht.n) |
| 58 | + if ht.n < 0xffffffff { |
| 59 | + c := counts.NewCount32(ht.n) |
| 60 | + number, unit := c.Human(counts.MetricPrefixes, "cd") |
| 61 | + assert.Equalf(ht.number, number, "Number for Count32(%d) in metric", ht.n) |
| 62 | + assert.Equalf(ht.unit, unit, "Unit for Count32(%d) in metric", ht.n) |
| 63 | + } |
| 64 | + if ht.n < 0xffffffffffffffff { |
| 65 | + c := counts.NewCount64(ht.n) |
| 66 | + number, unit := c.Human(counts.MetricPrefixes, "cd") |
| 67 | + assert.Equalf(ht.number, number, "Number for Count64(%d) in metric", ht.n) |
| 68 | + assert.Equalf(ht.unit, unit, "Unit for Count64(%d) in metric", ht.n) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestBinary(t *testing.T) { |
| 74 | + assert := assert.New(t) |
| 75 | + |
| 76 | + for _, ht := range []humanTest{ |
| 77 | + {0, "0", "B"}, |
| 78 | + {1, "1", "B"}, |
| 79 | + {1023, "1023", "B"}, |
| 80 | + {1024, "1.00", "KiB"}, |
| 81 | + {1234, "1.21", "KiB"}, |
| 82 | + {1048575, "1024", "KiB"}, // Not ideal, but ok |
| 83 | + {1048576, "1.00", "MiB"}, |
| 84 | + {1073741823, "1024", "MiB"}, // Not ideal, but ok |
| 85 | + {1073741824, "1.00", "GiB"}, |
| 86 | + {1099511627775, "1024", "GiB"}, // Not ideal, but ok |
| 87 | + {1099511627776, "1.00", "TiB"}, |
| 88 | + {1125899906842623, "1024", "TiB"}, // Not ideal, but ok |
| 89 | + {1125899906842624, "1.00", "PiB"}, |
| 90 | + {1152921504606846975, "1024", "PiB"}, |
| 91 | + {1152921504606846976, "1024", "PiB"}, |
| 92 | + {0xffffffffffffffff, "16384", "PiB"}, |
| 93 | + } { |
| 94 | + number, unit := counts.Human(ht.n, counts.BinaryPrefixes, "B") |
| 95 | + assert.Equalf(ht.number, number, "Number for %d in binary", ht.n) |
| 96 | + assert.Equalf(ht.unit, unit, "Unit for %d in binary", ht.n) |
| 97 | + if ht.n < 0xffffffff { |
| 98 | + c := counts.NewCount32(ht.n) |
| 99 | + number, unit := c.Human(counts.BinaryPrefixes, "B") |
| 100 | + assert.Equalf(ht.number, number, "Number for Count32(%d) in binary", ht.n) |
| 101 | + assert.Equalf(ht.unit, unit, "Unit for Count32(%d) in binary", ht.n) |
| 102 | + } |
| 103 | + if ht.n < 0xffffffffffffffff { |
| 104 | + c := counts.NewCount64(ht.n) |
| 105 | + number, unit := c.Human(counts.BinaryPrefixes, "B") |
| 106 | + assert.Equalf(ht.number, number, "Number for Count64(%d) in binary", ht.n) |
| 107 | + assert.Equalf(ht.unit, unit, "Unit for Count64(%d) in binary", ht.n) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestLimits32(t *testing.T) { |
| 113 | + assert := assert.New(t) |
| 114 | + |
| 115 | + c := counts.NewCount32(0xffffffff) |
| 116 | + number, unit := c.Human(counts.MetricPrefixes, "cd") |
| 117 | + assert.Equalf("∞", number, "Number for Count32(%d) in metric", c.ToUint64()) |
| 118 | + assert.Equalf("cd", unit, "Unit for Count32(%d) in metric", c.ToUint64()) |
| 119 | +} |
| 120 | + |
| 121 | +func TestLimits64(t *testing.T) { |
| 122 | + assert := assert.New(t) |
| 123 | + |
| 124 | + c := counts.NewCount64(0xffffffffffffffff) |
| 125 | + number, unit := c.Human(counts.MetricPrefixes, "B") |
| 126 | + assert.Equalf("∞", number, "Number for Count64(%d) in metric", c.ToUint64()) |
| 127 | + assert.Equalf("B", unit, "Unit for Count64(%d) in metric", c.ToUint64()) |
| 128 | +} |
0 commit comments