Skip to content

Commit 68ab1b2

Browse files
Copilotstreamich
andcommitted
Add tests for integer format floating-point precision fix
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent d02754f commit 68ab1b2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

packages/json-type/src/random/__tests__/random.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,35 @@ describe('Random', () => {
5858
}
5959
});
6060

61+
test('num with integer format and gte/lte always produces clean integers', () => {
62+
// Test u16 format with gte/lte constraints (the configuration that was failing)
63+
const type = t.Number({format: 'u16', gte: 1, lte: 65535});
64+
for (let i = 0; i < 100; i++) {
65+
const value = Random.gen(type);
66+
expect(typeof value).toBe('number');
67+
expect(Number.isInteger(value)).toBe(true);
68+
expect(value).toBeGreaterThanOrEqual(1);
69+
expect(value).toBeLessThanOrEqual(65535);
70+
// Verify no floating-point artifacts
71+
expect(value).toBe(Math.floor(value));
72+
expect(value.toString()).not.toContain('.');
73+
validate(type, value);
74+
}
75+
});
76+
77+
test('num with all integer formats produces clean integers', () => {
78+
const integerFormats = ['i8', 'i16', 'i32', 'u8', 'u16', 'u32'] as const;
79+
for (const format of integerFormats) {
80+
const type = t.Number({format, gte: 1, lte: 100});
81+
for (let i = 0; i < 50; i++) {
82+
const value = Random.gen(type);
83+
expect(Number.isInteger(value)).toBe(true);
84+
expect(value).toBe(Math.floor(value));
85+
validate(type, value);
86+
}
87+
}
88+
});
89+
6190
test('bool generates valid booleans', () => {
6291
const type = t.Boolean();
6392
for (let i = 0; i < 10; i++) {

0 commit comments

Comments
 (0)