Skip to content

Commit 8ac6ce8

Browse files
Copilotstreamich
andcommitted
feat: implement int64 template for bigint generation
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent 3b60a67 commit 8ac6ce8

File tree

4 files changed

+109
-7
lines changed

4 files changed

+109
-7
lines changed

src/number.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ export const int = (min: number, max: number): number => {
33
int = Math.max(min, Math.min(max, int));
44
return int;
55
};
6+
7+
export const int64 = (min: bigint, max: bigint): bigint => {
8+
const range = max - min;
9+
const randomFloat = Math.random();
10+
const randomBigInt = BigInt(Math.floor(Number(range) * randomFloat));
11+
let result = min + randomBigInt;
12+
result = result < min ? min : result > max ? max : result;
13+
return result;
14+
};

src/structured/TemplateJson.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {int} from '../number';
1+
import {int, int64} from '../number';
22
import {randomString} from '../string';
33
import {clone} from '../util';
44
import * as templates from './templates';
@@ -8,6 +8,7 @@ import type {
88
BooleanTemplate,
99
FloatTemplate,
1010
IntegerTemplate,
11+
Int64Template,
1112
LiteralTemplate,
1213
MapTemplate,
1314
NumberTemplate,
@@ -67,6 +68,8 @@ export class TemplateJson {
6768
return this.generateNumber(template as NumberTemplate);
6869
case 'int':
6970
return this.generateInteger(template as IntegerTemplate);
71+
case 'int64':
72+
return this.generateInt64(template as Int64Template);
7073
case 'float':
7174
return this.generateFloat(template as FloatTemplate);
7275
case 'bool':
@@ -144,6 +147,11 @@ export class TemplateJson {
144147
return int(min, max);
145148
}
146149

150+
protected generateInt64(template: Int64Template): bigint {
151+
const [, min = BigInt('-9223372036854775808'), max = BigInt('9223372036854775807')] = template;
152+
return int64(min, max);
153+
}
154+
147155
protected generateFloat(template: FloatTemplate): number {
148156
const [, min = -Number.MAX_VALUE, max = Number.MAX_VALUE] = template;
149157
let float = Math.random() * (max - min) + min;

src/structured/__tests__/TemplateJson.spec.ts

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,70 @@ describe('TemplateJson', () => {
4848
});
4949
});
5050

51+
describe('int64', () => {
52+
test('uses default int64 schema, if not provided', () => {
53+
resetMathRandom();
54+
const result = TemplateJson.gen('int64') as bigint;
55+
expect(typeof result).toBe('bigint');
56+
expect(result >= BigInt('-9223372036854775808')).toBe(true);
57+
expect(result <= BigInt('9223372036854775807')).toBe(true);
58+
});
59+
60+
test('can specify int64 range', () => {
61+
resetMathRandom();
62+
expect(TemplateJson.gen(['int64', BigInt(-10), BigInt(10)])).toBe(BigInt(-9));
63+
expect(TemplateJson.gen(['int64', BigInt(0), BigInt(1)])).toBe(BigInt(0));
64+
expect(TemplateJson.gen(['int64', BigInt(1), BigInt(5)])).toBe(BigInt(4));
65+
});
66+
67+
test('handles edge cases', () => {
68+
resetMathRandom();
69+
expect(TemplateJson.gen(['int64', BigInt(0), BigInt(0)])).toBe(BigInt(0));
70+
expect(TemplateJson.gen(['int64', BigInt(-1), BigInt(-1)])).toBe(BigInt(-1));
71+
expect(TemplateJson.gen(['int64', BigInt('1000000000000'), BigInt('1000000000000')])).toBe(
72+
BigInt('1000000000000'),
73+
);
74+
});
75+
76+
test('handles very large ranges', () => {
77+
resetMathRandom();
78+
const result = TemplateJson.gen([
79+
'int64',
80+
BigInt('-9223372036854775808'),
81+
BigInt('9223372036854775807'),
82+
]) as bigint;
83+
expect(typeof result).toBe('bigint');
84+
expect(result >= BigInt('-9223372036854775808')).toBe(true);
85+
expect(result <= BigInt('9223372036854775807')).toBe(true);
86+
});
87+
88+
test('can be used in complex structures', () => {
89+
resetMathRandom();
90+
const template: any = [
91+
'obj',
92+
[
93+
['id', 'int64'],
94+
['timestamp', ['int64', BigInt('1000000000000'), BigInt('9999999999999')]],
95+
],
96+
];
97+
const result = TemplateJson.gen(template) as any;
98+
expect(typeof result).toBe('object');
99+
expect(typeof result.id).toBe('bigint');
100+
expect(typeof result.timestamp).toBe('bigint');
101+
expect(result.timestamp >= BigInt('1000000000000')).toBe(true);
102+
expect(result.timestamp <= BigInt('9999999999999')).toBe(true);
103+
});
104+
105+
test('works with or templates', () => {
106+
resetMathRandom();
107+
const result = TemplateJson.gen(['or', 'int', 'int64', 'str']);
108+
const isBigInt = typeof result === 'bigint';
109+
const isNumber = typeof result === 'number';
110+
const isString = typeof result === 'string';
111+
expect(isBigInt || isNumber || isString).toBe(true);
112+
});
113+
});
114+
51115
describe('num', () => {
52116
test('generates random number, without range', () => {
53117
resetMathRandom();
@@ -527,13 +591,16 @@ describe('TemplateJson', () => {
527591
[
528592
['name', 'str'],
529593
['data', ['bin', 3, 3]],
530-
['metadata', [
531-
'obj',
594+
[
595+
'metadata',
532596
[
533-
['hash', ['bin', 32, 32]],
534-
['signature', ['bin', 64, 64, 0, 127]],
597+
'obj',
598+
[
599+
['hash', ['bin', 32, 32]],
600+
['signature', ['bin', 64, 64, 0, 127]],
601+
],
535602
],
536-
]],
603+
],
537604
],
538605
];
539606
const result = TemplateJson.gen(template) as any;

src/structured/types.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type TemplateNode =
99
| LiteralTemplate
1010
| NumberTemplate
1111
| IntegerTemplate
12+
| Int64Template
1213
| FloatTemplate
1314
| StringTemplate
1415
| BooleanTemplate
@@ -19,7 +20,18 @@ export type TemplateNode =
1920
| MapTemplate
2021
| OrTemplate;
2122

22-
export type TemplateShorthand = 'num' | 'int' | 'float' | 'str' | 'bool' | 'bin' | 'nil' | 'arr' | 'obj' | 'map';
23+
export type TemplateShorthand =
24+
| 'num'
25+
| 'int'
26+
| 'int64'
27+
| 'float'
28+
| 'str'
29+
| 'bool'
30+
| 'bin'
31+
| 'nil'
32+
| 'arr'
33+
| 'obj'
34+
| 'map';
2335

2436
/**
2537
* Recursive reference allows for recursive template construction, for example:
@@ -60,6 +72,12 @@ export type NumberTemplate = [type: 'num', min?: number, max?: number];
6072
*/
6173
export type IntegerTemplate = [type: 'int', min?: number, max?: number];
6274

75+
/**
76+
* 64-bit integer template. Generates a random bigint within the specified range.
77+
* If no range is specified, it defaults to a reasonable range for 64-bit integers.
78+
*/
79+
export type Int64Template = [type: 'int64', min?: bigint, max?: bigint];
80+
6381
/**
6482
* Float template. Generates a random floating-point number within the specified
6583
* range. If no range is specified, it defaults to the full range of JavaScript

0 commit comments

Comments
 (0)