-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
113 lines (98 loc) · 4.77 KB
/
test.js
File metadata and controls
113 lines (98 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Test suite for file-size-humanizer
*/
const { humanize, format, toDecimal, toBinary } = require('./index.js');
let passed = 0;
let failed = 0;
function assert(condition, message) {
if (condition) {
console.log(`✓ ${message}`);
passed++;
} else {
console.error(`✗ ${message}`);
failed++;
}
}
function assertEquals(actual, expected, message) {
const isEqual = JSON.stringify(actual) === JSON.stringify(expected);
assert(isEqual, message + ` (expected: ${JSON.stringify(expected)}, got: ${JSON.stringify(actual)})`);
}
// Test toDecimal function
console.log('\n=== Testing toDecimal (1000-based) ===');
assertEquals(toDecimal(0), { value: 0, unit: 'B' }, 'Zero bytes');
assertEquals(toDecimal(500), { value: 500, unit: 'B' }, '500 bytes');
assertEquals(toDecimal(1000), { value: 1, unit: 'KB' }, '1000 bytes = 1 KB');
assertEquals(toDecimal(1500), { value: 1.5, unit: 'KB' }, '1500 bytes = 1.5 KB');
assertEquals(toDecimal(1000000), { value: 1, unit: 'MB' }, '1000000 bytes = 1 MB');
assertEquals(toDecimal(1500000), { value: 1.5, unit: 'MB' }, '1500000 bytes = 1.5 MB');
assertEquals(toDecimal(1000000000), { value: 1, unit: 'GB' }, '1000000000 bytes = 1 GB');
assertEquals(toDecimal(1500000000), { value: 1.5, unit: 'GB' }, '1500000000 bytes = 1.5 GB');
assertEquals(toDecimal(1000000000000), { value: 1, unit: 'TB' }, '1000000000000 bytes = 1 TB');
// Test toBinary function
console.log('\n=== Testing toBinary (1024-based) ===');
assertEquals(toBinary(0), { value: 0, unit: 'B' }, 'Zero bytes');
assertEquals(toBinary(512), { value: 512, unit: 'B' }, '512 bytes');
assertEquals(toBinary(1024), { value: 1, unit: 'KiB' }, '1024 bytes = 1 KiB');
assertEquals(toBinary(1536), { value: 1.5, unit: 'KiB' }, '1536 bytes = 1.5 KiB');
assertEquals(toBinary(1048576), { value: 1, unit: 'MiB' }, '1048576 bytes = 1 MiB');
assertEquals(toBinary(1572864), { value: 1.5, unit: 'MiB' }, '1572864 bytes = 1.5 MiB');
assertEquals(toBinary(1073741824), { value: 1, unit: 'GiB' }, '1073741824 bytes = 1 GiB');
assertEquals(toBinary(1610612736), { value: 1.5, unit: 'GiB' }, '1610612736 bytes = 1.5 GiB');
assertEquals(toBinary(1099511627776), { value: 1, unit: 'TiB' }, '1099511627776 bytes = 1 TiB');
// Test humanize function
console.log('\n=== Testing humanize (both decimal and binary) ===');
const result1 = humanize(1024);
assert(result1.bytes === 1024, 'Bytes value preserved');
assert(result1.decimal.value === 1.02 && result1.decimal.unit === 'KB', 'Decimal conversion correct');
assert(result1.binary.value === 1 && result1.binary.unit === 'KiB', 'Binary conversion correct');
assert(result1.decimal.string === '1.02 KB', 'Decimal string format correct');
assert(result1.binary.string === '1 KiB', 'Binary string format correct');
const result2 = humanize(1048576);
assert(result2.bytes === 1048576, 'Bytes value preserved');
assert(result2.decimal.value === 1.05 && result2.decimal.unit === 'MB', 'Decimal conversion correct');
assert(result2.binary.value === 1 && result2.binary.unit === 'MiB', 'Binary conversion correct');
// Test format function
console.log('\n=== Testing format function ===');
assert(format(1024) === '1.02 KB', 'Format with decimal (default)');
assert(format(1024, { binary: true }) === '1 KiB', 'Format with binary');
assert(format(1048576) === '1.05 MB', 'Format 1MB in decimal');
assert(format(1048576, { binary: true }) === '1 MiB', 'Format 1MiB in binary');
assert(format(1500000) === '1.5 MB', 'Format 1.5MB');
assert(format(1500000, { decimals: 1 }) === '1.5 MB', 'Format with custom decimals');
assert(format(0) === '0 B', 'Format zero bytes');
// Test error handling
console.log('\n=== Testing error handling ===');
try {
humanize(-100);
assert(false, 'Should throw error for negative bytes');
} catch (e) {
assert(e.message === 'Input must be a non-negative number', 'Correct error for negative bytes');
}
try {
humanize('not a number');
assert(false, 'Should throw error for non-number input');
} catch (e) {
assert(e.message === 'Input must be a non-negative number', 'Correct error for non-number input');
}
try {
format(-100);
assert(false, 'Format should throw error for negative bytes');
} catch (e) {
assert(e.message === 'Input must be a non-negative number', 'Correct error in format function');
}
// Test decimal precision
console.log('\n=== Testing decimal precision ===');
assertEquals(toDecimal(1234567, 0), { value: 1, unit: 'MB' }, 'Zero decimal places');
assertEquals(toDecimal(1234567, 3), { value: 1.235, unit: 'MB' }, 'Three decimal places');
// Summary
console.log('\n=== Test Summary ===');
console.log(`Total: ${passed + failed}`);
console.log(`Passed: ${passed}`);
console.log(`Failed: ${failed}`);
if (failed === 0) {
console.log('\n✓ All tests passed!');
process.exit(0);
} else {
console.log(`\n✗ ${failed} test(s) failed`);
process.exit(1);
}