github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/unit/utils/units-test.js (about) 1 import { module, test } from 'qunit'; 2 import * as units from 'nomad-ui/utils/units'; 3 4 function table(fn, cases) { 5 cases.forEach((testCase) => { 6 test(testCase.name || testCase.out, function (assert) { 7 assert.deepEqual(fn.apply(null, testCase.in), testCase.out); 8 }); 9 }); 10 } 11 12 module('Unit | Util | units#formatBytes', function () { 13 table.call(this, units.formatBytes, [ 14 { in: [null], out: '0 Bytes', name: 'formats null as 0 bytes' }, 15 { in: [undefined], out: '0 Bytes', name: 'formats undefined as 0 bytes' }, 16 { in: [0], out: '0 Bytes', name: 'formats x < 1024 as bytes' }, 17 { in: [100], out: '100 Bytes' }, 18 { in: [1023], out: '1,023 Bytes' }, 19 { in: [1024], out: '1 KiB', name: 'formats 1024 <= x < 1024^2 as KiB' }, 20 { in: [1024 ** 2 - 1024 * 0.01], out: '1,023.99 KiB' }, 21 { 22 in: [1024 ** 2], 23 out: '1 MiB', 24 name: 'formats 1024^2 <= x < 1024^3 as MiB', 25 }, 26 { in: [1024 ** 2 * 1.016], out: '1.02 MiB' }, 27 { 28 in: [1024 ** 3], 29 out: '1 GiB', 30 name: 'formats 1024^3 <= x < 1024^4 as GiB', 31 }, 32 { in: [1024 ** 3 * 512.5], out: '512.5 GiB' }, 33 { 34 in: [1024 ** 4], 35 out: '1 TiB', 36 name: 'formats 1024^4 <= x < 1024^5 as TiB', 37 }, 38 { in: [1024 ** 4 * 2.1234], out: '2.12 TiB' }, 39 { in: [1024 ** 5], out: '1 PiB', name: 'formats x > 1024^5 as PiB' }, 40 { in: [1024 ** 5 * 4000], out: '4,000 PiB' }, 41 { 42 in: [1024 ** 2, 'MiB'], 43 out: '1 TiB', 44 name: 'accepts a starting unit size as an optional argument', 45 }, 46 { 47 in: [1024 ** 2 * -1], 48 out: '-1 MiB', 49 name: 'negative values are still reduced', 50 }, 51 ]); 52 }); 53 54 module('Unit | Util | units#formatScheduledBytes', function () { 55 table.call(this, units.formatScheduledBytes, [ 56 { in: [null], out: '0 Bytes', name: 'formats null as 0 bytes' }, 57 { in: [undefined], out: '0 Bytes', name: 'formats undefined as 0 bytes' }, 58 { in: [0], out: '0 Bytes', name: 'formats x < 1024 as bytes' }, 59 { 60 in: [1024 ** 3], 61 out: '1,024 MiB', 62 name: 'max unit is MiB, just like Nomad expects in job specs', 63 }, 64 { 65 in: [1024 ** 3 + 1024 ** 2 * 0.6], 66 out: '1,025 MiB', 67 name: 'MiBs are rounded to whole numbers', 68 }, 69 { 70 in: [2000, 'MiB'], 71 out: '2,000 MiB', 72 name: 'accepts a starting unit size as an optional argument', 73 }, 74 { 75 in: [1024 ** 3 * -1], 76 out: '-1,024 MiB', 77 name: 'negative values are still reduced', 78 }, 79 ]); 80 }); 81 82 module('Unit | Util | units#formatHertz', function () { 83 table.call(this, units.formatHertz, [ 84 { in: [null], out: '0 Hz', name: 'formats null as 0 Hz' }, 85 { in: [undefined], out: '0 Hz', name: 'formats undefined as 0 Hz' }, 86 { in: [0], out: '0 Hz', name: 'formats x < 1000 as Hz' }, 87 { in: [999], out: '999 Hz' }, 88 { in: [1000], out: '1 KHz', name: 'formats 1000 <= x < 1000^2 as KHz' }, 89 { in: [1000 ** 2 - 10], out: '999.99 KHz' }, 90 { 91 in: [1000 ** 2], 92 out: '1 MHz', 93 name: 'formats 1000^2 <= x < 1000^3 as MHz', 94 }, 95 { in: [1000 ** 2 * 5.234], out: '5.23 MHz' }, 96 { 97 in: [1000 ** 3], 98 out: '1 GHz', 99 name: 'formats 1000^3 <= x < 1000^4 as GHz', 100 }, 101 { in: [1000 ** 3 * 500.238], out: '500.24 GHz' }, 102 { 103 in: [1000 ** 4], 104 out: '1 THz', 105 name: 'formats 1000^4 <= x < 1000^5 as THz', 106 }, 107 { in: [1000 ** 4 * 12], out: '12 THz' }, 108 { in: [1000 ** 5], out: '1 PHz', name: 'formats x > 1000^5 as PHz' }, 109 { in: [1000 ** 5 * 34567.89], out: '34,567.89 PHz' }, 110 { 111 in: [2000, 'KHz'], 112 out: '2 MHz', 113 name: 'accepts a starting unit size as an optional argument', 114 }, 115 { 116 in: [1000 ** 3 * -1], 117 out: '-1 GHz', 118 name: 'negative values are still reduced', 119 }, 120 ]); 121 }); 122 123 module('Unit | Util | units#formatScheduledHertz', function () { 124 table.call(this, units.formatScheduledHertz, [ 125 { in: [null], out: '0 Hz', name: 'formats null as 0 Hz' }, 126 { in: [undefined], out: '0 Hz', name: 'formats undefined as 0 Hz' }, 127 { in: [0], out: '0 Hz', name: 'formats x < 1024 as Hz' }, 128 { 129 in: [1000 ** 3], 130 out: '1,000 MHz', 131 name: 'max unit is MHz, just like Nomad expects in job specs', 132 }, 133 { 134 in: [1000 ** 3 + 1000 ** 2 * 0.6], 135 out: '1,001 MHz', 136 name: 'MHz are rounded to whole numbers', 137 }, 138 { 139 in: [2000, 'MHz'], 140 out: '2,000 MHz', 141 name: 'accepts a starting unit size as an optional argument', 142 }, 143 { 144 in: [1000 ** 3 * -1], 145 out: '-1,000 MHz', 146 name: 'negative values are still reduced', 147 }, 148 ]); 149 }); 150 151 module('Unit | Util | units#reduceBytes', function () { 152 table.call(this, units.reduceBytes, [ 153 { 154 in: [], 155 out: [0, 'Bytes'], 156 name: 'No args behavior results in valid output', 157 }, 158 { in: [1024 ** 6], out: [1024, 'PiB'], name: 'Max default unit is PiB' }, 159 { 160 in: [1024 ** 6 * 1.12345], 161 out: [1024 * 1.12345, 'PiB'], 162 name: 'Returned numbers are not formatted', 163 }, 164 { 165 in: [1024 ** 2 * 2, 'KiB'], 166 out: [2048, 'KiB'], 167 name: 'Reduction ends when the specified max unit is reached', 168 }, 169 { 170 in: [1024 ** 2, 'MiB', 'KiB'], 171 out: [1024, 'MiB'], 172 name: 'accepts a starting unit size as an optional argument', 173 }, 174 { 175 in: [1024 ** 3 * -1], 176 out: [-1, 'GiB'], 177 name: 'negative values are still reduced', 178 }, 179 ]); 180 }); 181 182 module('Unit | Util | units#reduceHertz', function () { 183 table.call(this, units.reduceHertz, [ 184 { 185 in: [], 186 out: [0, 'Hz'], 187 name: 'No args behavior results in valid output', 188 }, 189 { in: [1000 ** 6], out: [1000, 'PHz'], name: 'Max default unit is PHz' }, 190 { 191 in: [1000 ** 6 * 1.12345], 192 out: [1000 * 1.12345, 'PHz'], 193 name: 'Returned numbers are not formatted', 194 }, 195 { 196 in: [1000 ** 4 * 2, 'GHz'], 197 out: [2000, 'GHz'], 198 name: 'Reduction ends when the specified max unit is reached', 199 }, 200 { 201 in: [1000 * 2, 'THz', 'MHz'], 202 out: [2, 'GHz'], 203 name: 'accepts a starting unit size as an optional argument', 204 }, 205 { 206 in: [1000 ** 3 * -1], 207 out: [-1, 'GHz'], 208 name: 'negative values are still reduced', 209 }, 210 ]); 211 });