-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest.js
More file actions
82 lines (67 loc) · 3.05 KB
/
Copy pathtest.js
File metadata and controls
82 lines (67 loc) · 3.05 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
import nstr from './dist/index.mjs'
// Test cases demonstrating the functionality
const testCases = [
// Basic floating point artifacts
{ input: 0.1, expected: '0.1' },
{ input: 0.10000000000000001, expected: '0.1' },
{ input: 0.14499999582767487, expected: '0.145' },
// Clean numbers should be preserved
{ input: 3.1415926, expected: '3.1415926' },
{ input: 2.5, expected: '2.5' },
{ input: 42, expected: '42' },
// Edge cases
{ input: 0, expected: '0' },
{ input: 1.9999999999, expected: '2' }, // Should round up
{ input: 0.9999999999, expected: '1' }, // Should round up
// Very small numbers
{ input: 0.00000000001, expected: '0' },
{ input: 1e-10, expected: '0' },
// Large numbers with consecutive zeros in middle of fraction
{ input: 123.456000007890123, expected: '123.456' },
{ input: 987.123000000456789, expected: '987.123' },
{ input: 1234.567800000012345, expected: '1234.5678' },
// Large numbers with consecutive nines in middle of fraction
{ input: 456.78999999456789, expected: '456.79' },
{ input: 789.123999999876543, expected: '789.124' },
{ input: 2468.135999999024681, expected: '2468.136' },
// Mixed cases with longer sequences
{ input: 12345.678900000123456, expected: '12345.6789' },
{ input: 98765.432199999987654, expected: '98765.4322' },
// NaN handling
{ input: Number.NaN, expected: 'NaN' },
// Bug fix: trailing decimal point should be removed
{ input: 12.2 / 0.1, expected: '122' },
// Regression tests for rounding bug fixes
{ input: 0.149999, expected: '0.15' }, // Should round up from 0.14
{ input: 0.059999, expected: '0.06' }, // Should round up from 0.05
{ input: 0.249999, expected: '0.25' }, // Should round up from 0.24
{ input: 0.349999, expected: '0.35' }, // Should round up from 0.34
{ input: 1.149999, expected: '1.15' }, // Should round up from 1.14
{ input: 10.149999, expected: '10.15' }, // Should round up from 10.14
// Additional edge cases for rounding with different precisions
{ input: 0.0149999, expected: '0.015' }, // Three decimal places
{ input: 0.00149999, expected: '0.0015' }, // Four decimal places
{ input: 12.3499999, expected: '12.35' }, // Larger number with rounding
{ input: -0.149999, expected: '-0.15' }, // Negative number rounding
{ input: -0.059999, expected: '-0.06' }, // Negative number rounding
{ input: 123.456789999999, expected: '123.45679' }, // Long decimal with trailing 9s
]
console.log('Testing nstr function:')
console.log('======================')
testCases.forEach(({ input, expected }, index) => {
const result = nstr(input)
const passed = result === expected
console.log(
`Test ${index + 1}: ${input} → "${result}" ${passed ? '✅' : '❌'}`
)
if (!passed) {
console.log(` Expected: "${expected}"`)
}
})
console.log('\nTesting different thresholds:')
console.log('============================')
const testValue = 0.14499999582767487
;[2, 3, 4, 5, 6].forEach((threshold) => {
const result = nstr(testValue, { threshold })
console.log(`Threshold ${threshold}: ${testValue} → "${result}"`)
})