@fgv/ts-random 5.1.0-8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/jest.config.json +13 -0
- package/config/rig.json +4 -0
- package/config/typedoc.json +7 -0
- package/docs/Chars/README.md +152 -0
- package/docs/Chars/variables/all.md +9 -0
- package/docs/Chars/variables/alphanumeric.md +9 -0
- package/docs/Chars/variables/base64.md +9 -0
- package/docs/Chars/variables/base64Url.md +9 -0
- package/docs/Chars/variables/base64UrlNoPadding.md +9 -0
- package/docs/Chars/variables/digits.md +9 -0
- package/docs/Chars/variables/hex.md +9 -0
- package/docs/Chars/variables/hexDigits.md +9 -0
- package/docs/Chars/variables/hexDigitsUpper.md +9 -0
- package/docs/Chars/variables/letters.md +9 -0
- package/docs/Chars/variables/lowerCase.md +9 -0
- package/docs/Chars/variables/printable.md +9 -0
- package/docs/Chars/variables/symbols.md +9 -0
- package/docs/Chars/variables/upperCase.md +9 -0
- package/docs/Chars/variables/whitespace.md +9 -0
- package/docs/Generator/README.md +153 -0
- package/docs/Generator/classes/PseudoRandomGenerator.clone.md +17 -0
- package/docs/Generator/classes/PseudoRandomGenerator.create.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.createChild.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.ensureRng.md +25 -0
- package/docs/Generator/classes/PseudoRandomGenerator.getGlobalRng.md +17 -0
- package/docs/Generator/classes/PseudoRandomGenerator.md +258 -0
- package/docs/Generator/classes/PseudoRandomGenerator.nextBoolean.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.nextFloat.md +17 -0
- package/docs/Generator/classes/PseudoRandomGenerator.nextInRange.md +25 -0
- package/docs/Generator/classes/PseudoRandomGenerator.nextInt.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.nextString.md +25 -0
- package/docs/Generator/classes/PseudoRandomGenerator.pickNext.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.pickRandom.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.pickSequence.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.pickSequential.md +24 -0
- package/docs/Generator/classes/PseudoRandomGenerator.rng.md +11 -0
- package/docs/Generator/classes/PseudoRandomGenerator.setGlobalRng.md +24 -0
- package/docs/Generator/classes/SeededRandomSource.clone.md +17 -0
- package/docs/Generator/classes/SeededRandomSource.counter.md +11 -0
- package/docs/Generator/classes/SeededRandomSource.create.md +24 -0
- package/docs/Generator/classes/SeededRandomSource.createChild.md +24 -0
- package/docs/Generator/classes/SeededRandomSource.hashSeed.md +24 -0
- package/docs/Generator/classes/SeededRandomSource.hashStateAndLabel.md +25 -0
- package/docs/Generator/classes/SeededRandomSource.lineage.md +9 -0
- package/docs/Generator/classes/SeededRandomSource.md +187 -0
- package/docs/Generator/classes/SeededRandomSource.mulberryStep.md +24 -0
- package/docs/Generator/classes/SeededRandomSource.next.md +17 -0
- package/docs/Generator/classes/SeededRandomSource.seed.md +9 -0
- package/docs/Generator/interfaces/INextResult.md +61 -0
- package/docs/Generator/interfaces/INextResult.nextState.md +9 -0
- package/docs/Generator/interfaces/INextResult.value.md +9 -0
- package/docs/Generator/interfaces/IPseudoRandomGeneratorCreateParams.global.md +9 -0
- package/docs/Generator/interfaces/IPseudoRandomGeneratorCreateParams.md +78 -0
- package/docs/Generator/interfaces/IPseudoRandomGeneratorCreateParams.seed.md +9 -0
- package/docs/Generator/interfaces/IPseudoRandomGeneratorCreateParams.step.md +9 -0
- package/docs/Generator/interfaces/IRandomSequencePickParams.candidates.md +9 -0
- package/docs/Generator/interfaces/IRandomSequencePickParams.count.md +9 -0
- package/docs/Generator/interfaces/IRandomSequencePickParams.how.md +9 -0
- package/docs/Generator/interfaces/IRandomSequencePickParams.md +79 -0
- package/docs/Generator/interfaces/ISeedPair.md +61 -0
- package/docs/Generator/interfaces/ISeedPair.seed.md +9 -0
- package/docs/Generator/interfaces/ISeedPair.state.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.counter.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.lineage.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.md +112 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.seed.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.state.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceConstructorParams.step.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceCreateParams.md +61 -0
- package/docs/Generator/interfaces/ISeededRandomSourceCreateParams.seed.md +9 -0
- package/docs/Generator/interfaces/ISeededRandomSourceCreateParams.step.md +9 -0
- package/docs/Generator/interfaces/ISequentialPickParams.candidates.md +9 -0
- package/docs/Generator/interfaces/ISequentialPickParams.count.md +9 -0
- package/docs/Generator/interfaces/ISequentialPickParams.how.md +9 -0
- package/docs/Generator/interfaces/ISequentialPickParams.md +79 -0
- package/docs/Generator/type-aliases/GeneratorGlobalThis.md +11 -0
- package/docs/Generator/type-aliases/RandomStepFunction.md +11 -0
- package/docs/Generator/type-aliases/SequencePickParams.md +11 -0
- package/docs/GeneratorData/Chars/README.md +152 -0
- package/docs/GeneratorData/Chars/variables/all.md +9 -0
- package/docs/GeneratorData/Chars/variables/alphanumeric.md +9 -0
- package/docs/GeneratorData/Chars/variables/base64.md +9 -0
- package/docs/GeneratorData/Chars/variables/base64Url.md +9 -0
- package/docs/GeneratorData/Chars/variables/base64UrlNoPadding.md +9 -0
- package/docs/GeneratorData/Chars/variables/digits.md +9 -0
- package/docs/GeneratorData/Chars/variables/hex.md +9 -0
- package/docs/GeneratorData/Chars/variables/hexDigits.md +9 -0
- package/docs/GeneratorData/Chars/variables/hexDigitsUpper.md +9 -0
- package/docs/GeneratorData/Chars/variables/letters.md +9 -0
- package/docs/GeneratorData/Chars/variables/lowerCase.md +9 -0
- package/docs/GeneratorData/Chars/variables/printable.md +9 -0
- package/docs/GeneratorData/Chars/variables/symbols.md +9 -0
- package/docs/GeneratorData/Chars/variables/upperCase.md +9 -0
- package/docs/GeneratorData/Chars/variables/whitespace.md +9 -0
- package/docs/GeneratorData/README.md +35 -0
- package/docs/GeneratorData/Words/README.md +125 -0
- package/docs/GeneratorData/Words/variables/Adjectives.md +7 -0
- package/docs/GeneratorData/Words/variables/Animals.md +7 -0
- package/docs/GeneratorData/Words/variables/Cities.md +7 -0
- package/docs/GeneratorData/Words/variables/Colors.md +7 -0
- package/docs/GeneratorData/Words/variables/Countries.md +7 -0
- package/docs/GeneratorData/Words/variables/Domains.md +7 -0
- package/docs/GeneratorData/Words/variables/FamilyNames.md +9 -0
- package/docs/GeneratorData/Words/variables/FirstNames.md +9 -0
- package/docs/GeneratorData/Words/variables/FirstNamesFemale.md +9 -0
- package/docs/GeneratorData/Words/variables/FirstNamesMale.md +9 -0
- package/docs/GeneratorData/Words/variables/Gerunds.md +7 -0
- package/docs/GeneratorData/Words/variables/jobs.md +7 -0
- package/docs/GeneratorData/variables/Adjectives.md +7 -0
- package/docs/GeneratorData/variables/Animals.md +7 -0
- package/docs/GeneratorData/variables/Cities.md +7 -0
- package/docs/GeneratorData/variables/Colors.md +7 -0
- package/docs/GeneratorData/variables/Countries.md +7 -0
- package/docs/GeneratorData/variables/Domains.md +7 -0
- package/docs/GeneratorData/variables/FamilyNames.md +9 -0
- package/docs/GeneratorData/variables/FirstNames.md +9 -0
- package/docs/GeneratorData/variables/FirstNamesFemale.md +9 -0
- package/docs/GeneratorData/variables/FirstNamesMale.md +9 -0
- package/docs/GeneratorData/variables/Gerunds.md +7 -0
- package/docs/GeneratorData/variables/all.md +9 -0
- package/docs/GeneratorData/variables/alphanumeric.md +9 -0
- package/docs/GeneratorData/variables/base64.md +9 -0
- package/docs/GeneratorData/variables/base64Url.md +9 -0
- package/docs/GeneratorData/variables/base64UrlNoPadding.md +9 -0
- package/docs/GeneratorData/variables/digits.md +9 -0
- package/docs/GeneratorData/variables/hex.md +9 -0
- package/docs/GeneratorData/variables/hexDigits.md +9 -0
- package/docs/GeneratorData/variables/hexDigitsUpper.md +9 -0
- package/docs/GeneratorData/variables/jobs.md +7 -0
- package/docs/GeneratorData/variables/letters.md +9 -0
- package/docs/GeneratorData/variables/lowerCase.md +9 -0
- package/docs/GeneratorData/variables/printable.md +9 -0
- package/docs/GeneratorData/variables/symbols.md +9 -0
- package/docs/GeneratorData/variables/upperCase.md +9 -0
- package/docs/GeneratorData/variables/whitespace.md +9 -0
- package/docs/README.md +35 -0
- package/docs/Words/README.md +125 -0
- package/docs/Words/variables/Adjectives.md +7 -0
- package/docs/Words/variables/Animals.md +7 -0
- package/docs/Words/variables/Cities.md +7 -0
- package/docs/Words/variables/Colors.md +7 -0
- package/docs/Words/variables/Countries.md +7 -0
- package/docs/Words/variables/Domains.md +7 -0
- package/docs/Words/variables/FamilyNames.md +9 -0
- package/docs/Words/variables/FirstNames.md +9 -0
- package/docs/Words/variables/FirstNamesFemale.md +9 -0
- package/docs/Words/variables/FirstNamesMale.md +9 -0
- package/docs/Words/variables/Gerunds.md +7 -0
- package/docs/Words/variables/jobs.md +7 -0
- package/docs/classes/PseudoRandomGenerator.clone.md +17 -0
- package/docs/classes/PseudoRandomGenerator.create.md +24 -0
- package/docs/classes/PseudoRandomGenerator.createChild.md +24 -0
- package/docs/classes/PseudoRandomGenerator.ensureRng.md +25 -0
- package/docs/classes/PseudoRandomGenerator.getGlobalRng.md +17 -0
- package/docs/classes/PseudoRandomGenerator.md +258 -0
- package/docs/classes/PseudoRandomGenerator.nextBoolean.md +24 -0
- package/docs/classes/PseudoRandomGenerator.nextFloat.md +17 -0
- package/docs/classes/PseudoRandomGenerator.nextInRange.md +25 -0
- package/docs/classes/PseudoRandomGenerator.nextInt.md +24 -0
- package/docs/classes/PseudoRandomGenerator.nextString.md +25 -0
- package/docs/classes/PseudoRandomGenerator.pickNext.md +24 -0
- package/docs/classes/PseudoRandomGenerator.pickRandom.md +24 -0
- package/docs/classes/PseudoRandomGenerator.pickSequence.md +24 -0
- package/docs/classes/PseudoRandomGenerator.pickSequential.md +24 -0
- package/docs/classes/PseudoRandomGenerator.rng.md +11 -0
- package/docs/classes/PseudoRandomGenerator.setGlobalRng.md +24 -0
- package/docs/classes/SeededRandomSource.clone.md +17 -0
- package/docs/classes/SeededRandomSource.counter.md +11 -0
- package/docs/classes/SeededRandomSource.create.md +24 -0
- package/docs/classes/SeededRandomSource.createChild.md +24 -0
- package/docs/classes/SeededRandomSource.hashSeed.md +24 -0
- package/docs/classes/SeededRandomSource.hashStateAndLabel.md +25 -0
- package/docs/classes/SeededRandomSource.lineage.md +9 -0
- package/docs/classes/SeededRandomSource.md +187 -0
- package/docs/classes/SeededRandomSource.mulberryStep.md +24 -0
- package/docs/classes/SeededRandomSource.next.md +17 -0
- package/docs/classes/SeededRandomSource.seed.md +9 -0
- package/docs/interfaces/INextResult.md +61 -0
- package/docs/interfaces/INextResult.nextState.md +9 -0
- package/docs/interfaces/INextResult.value.md +9 -0
- package/docs/interfaces/IPseudoRandomGeneratorCreateParams.global.md +9 -0
- package/docs/interfaces/IPseudoRandomGeneratorCreateParams.md +78 -0
- package/docs/interfaces/IPseudoRandomGeneratorCreateParams.seed.md +9 -0
- package/docs/interfaces/IPseudoRandomGeneratorCreateParams.step.md +9 -0
- package/docs/interfaces/IRandomSequencePickParams.candidates.md +9 -0
- package/docs/interfaces/IRandomSequencePickParams.count.md +9 -0
- package/docs/interfaces/IRandomSequencePickParams.how.md +9 -0
- package/docs/interfaces/IRandomSequencePickParams.md +79 -0
- package/docs/interfaces/ISeedPair.md +61 -0
- package/docs/interfaces/ISeedPair.seed.md +9 -0
- package/docs/interfaces/ISeedPair.state.md +9 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.counter.md +9 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.lineage.md +9 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.md +112 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.seed.md +9 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.state.md +9 -0
- package/docs/interfaces/ISeededRandomSourceConstructorParams.step.md +9 -0
- package/docs/interfaces/ISeededRandomSourceCreateParams.md +61 -0
- package/docs/interfaces/ISeededRandomSourceCreateParams.seed.md +9 -0
- package/docs/interfaces/ISeededRandomSourceCreateParams.step.md +9 -0
- package/docs/interfaces/ISequentialPickParams.candidates.md +9 -0
- package/docs/interfaces/ISequentialPickParams.count.md +9 -0
- package/docs/interfaces/ISequentialPickParams.how.md +9 -0
- package/docs/interfaces/ISequentialPickParams.md +79 -0
- package/docs/type-aliases/GeneratorGlobalThis.md +11 -0
- package/docs/type-aliases/RandomStepFunction.md +11 -0
- package/docs/type-aliases/SequencePickParams.md +11 -0
- package/docs/variables/Adjectives.md +7 -0
- package/docs/variables/Animals.md +7 -0
- package/docs/variables/Cities.md +7 -0
- package/docs/variables/Colors.md +7 -0
- package/docs/variables/Countries.md +7 -0
- package/docs/variables/Domains.md +7 -0
- package/docs/variables/FamilyNames.md +9 -0
- package/docs/variables/FirstNames.md +9 -0
- package/docs/variables/FirstNamesFemale.md +9 -0
- package/docs/variables/FirstNamesMale.md +9 -0
- package/docs/variables/Gerunds.md +7 -0
- package/docs/variables/all.md +9 -0
- package/docs/variables/alphanumeric.md +9 -0
- package/docs/variables/base64.md +9 -0
- package/docs/variables/base64Url.md +9 -0
- package/docs/variables/base64UrlNoPadding.md +9 -0
- package/docs/variables/digits.md +9 -0
- package/docs/variables/hex.md +9 -0
- package/docs/variables/hexDigits.md +9 -0
- package/docs/variables/hexDigitsUpper.md +9 -0
- package/docs/variables/jobs.md +7 -0
- package/docs/variables/letters.md +9 -0
- package/docs/variables/lowerCase.md +9 -0
- package/docs/variables/printable.md +9 -0
- package/docs/variables/symbols.md +9 -0
- package/docs/variables/upperCase.md +9 -0
- package/docs/variables/whitespace.md +9 -0
- package/package.json +49 -0
- package/src/generator/generators.ts +273 -0
- package/src/generator/index.ts +22 -0
- package/src/generator/randomSource.ts +224 -0
- package/src/generator-data/charClasses.ts +95 -0
- package/src/generator-data/index.ts +22 -0
- package/src/generator-data/words/adjectives.ts +247 -0
- package/src/generator-data/words/animals.ts +75 -0
- package/src/generator-data/words/cities.ts +229 -0
- package/src/generator-data/words/colors.ts +56 -0
- package/src/generator-data/words/countries.ts +217 -0
- package/src/generator-data/words/domains.ts +47 -0
- package/src/generator-data/words/gerunds.ts +126 -0
- package/src/generator-data/words/index.ts +29 -0
- package/src/generator-data/words/jobs.ts +175 -0
- package/src/generator-data/words/names.ts +717 -0
- package/src/index.ts +7 -0
- package/src/test/unit/generators.test.ts +697 -0
- package/src/test/unit/randomSource.test.ts +526 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
import '@fgv/ts-utils-jest';
|
|
2
|
+
|
|
3
|
+
import { RandomStepFunction, SeededRandomSource } from '../../generator';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A passthrough step function useful for testing: returns the state as the value
|
|
7
|
+
* and increments the state by 1 each step, making sequences predictable.
|
|
8
|
+
*/
|
|
9
|
+
const passthroughStep: RandomStepFunction = (state: number) => {
|
|
10
|
+
return {
|
|
11
|
+
value: state,
|
|
12
|
+
nextState: state + 1
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function createSource(seed: number | string): SeededRandomSource {
|
|
17
|
+
return SeededRandomSource.create(seed).orThrow();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function readValues(source: SeededRandomSource, count: number): number[] {
|
|
21
|
+
const values: number[] = [];
|
|
22
|
+
for (let index = 0; index < count; index += 1) {
|
|
23
|
+
values.push(source.next());
|
|
24
|
+
}
|
|
25
|
+
return values;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('SeededRandomSource', () => {
|
|
29
|
+
describe('create', () => {
|
|
30
|
+
test('succeeds with a numeric seed', () => {
|
|
31
|
+
expect(SeededRandomSource.create(42)).toSucceedAndSatisfy((source) => {
|
|
32
|
+
expect(source.seed).toBe('42');
|
|
33
|
+
expect(source.counter).toBe(0);
|
|
34
|
+
expect(source.lineage).toEqual([]);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('succeeds with a string seed', () => {
|
|
39
|
+
expect(SeededRandomSource.create('hello')).toSucceedAndSatisfy((source) => {
|
|
40
|
+
expect(source.seed).toBe('hello');
|
|
41
|
+
expect(source.counter).toBe(0);
|
|
42
|
+
expect(source.lineage).toEqual([]);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('succeeds with no seed using Date.now as fallback', () => {
|
|
47
|
+
const nowSpy = jest.spyOn(Date, 'now').mockReturnValue(999);
|
|
48
|
+
try {
|
|
49
|
+
expect(SeededRandomSource.create()).toSucceedAndSatisfy((source) => {
|
|
50
|
+
expect(source.seed).toBe('999');
|
|
51
|
+
expect(source.counter).toBe(0);
|
|
52
|
+
});
|
|
53
|
+
} finally {
|
|
54
|
+
nowSpy.mockRestore();
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('unseeded sources created at different times produce different sequences', () => {
|
|
59
|
+
const nowSpy = jest.spyOn(Date, 'now');
|
|
60
|
+
nowSpy.mockReturnValueOnce(100);
|
|
61
|
+
nowSpy.mockReturnValueOnce(200);
|
|
62
|
+
try {
|
|
63
|
+
const first = SeededRandomSource.create().orThrow();
|
|
64
|
+
const second = SeededRandomSource.create().orThrow();
|
|
65
|
+
expect(readValues(first, 4)).not.toEqual(readValues(second, 4));
|
|
66
|
+
} finally {
|
|
67
|
+
nowSpy.mockRestore();
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('object-form initialization behaves like primitive initialization', () => {
|
|
72
|
+
const primitive = createSource(12345);
|
|
73
|
+
const object = SeededRandomSource.create({ seed: 12345 }).orThrow();
|
|
74
|
+
expect(readValues(primitive, 4)).toEqual(readValues(object, 4));
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('object-form with custom step function uses that step', () => {
|
|
78
|
+
expect(SeededRandomSource.create({ seed: 10, step: passthroughStep })).toSucceedAndSatisfy((source) => {
|
|
79
|
+
expect(source.next()).toBe(10);
|
|
80
|
+
expect(source.next()).toBe(11);
|
|
81
|
+
expect(source.next()).toBe(12);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('object-form without seed falls back to Date.now', () => {
|
|
86
|
+
const nowSpy = jest.spyOn(Date, 'now').mockReturnValue(555);
|
|
87
|
+
try {
|
|
88
|
+
expect(SeededRandomSource.create({ step: passthroughStep })).toSucceedAndSatisfy((source) => {
|
|
89
|
+
expect(source.seed).toBe('555');
|
|
90
|
+
expect(source.next()).toBe(555);
|
|
91
|
+
});
|
|
92
|
+
} finally {
|
|
93
|
+
nowSpy.mockRestore();
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('deterministic sequences', () => {
|
|
99
|
+
test('same seed always produces the same sequence', () => {
|
|
100
|
+
const first = createSource('deterministic');
|
|
101
|
+
const second = createSource('deterministic');
|
|
102
|
+
expect(readValues(first, 10)).toEqual(readValues(second, 10));
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test('numeric and string forms of the same seed produce identical sequences', () => {
|
|
106
|
+
const numeric = createSource(12345);
|
|
107
|
+
const stringForm = createSource('12345');
|
|
108
|
+
expect(readValues(numeric, 10)).toEqual(readValues(stringForm, 10));
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('different seeds produce different sequences', () => {
|
|
112
|
+
const first = createSource('alpha');
|
|
113
|
+
const second = createSource('beta');
|
|
114
|
+
expect(readValues(first, 4)).not.toEqual(readValues(second, 4));
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('next', () => {
|
|
119
|
+
test('counter increments with each call', () => {
|
|
120
|
+
const source = createSource(1);
|
|
121
|
+
expect(source.counter).toBe(0);
|
|
122
|
+
source.next();
|
|
123
|
+
expect(source.counter).toBe(1);
|
|
124
|
+
source.next();
|
|
125
|
+
source.next();
|
|
126
|
+
expect(source.counter).toBe(3);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('returns values in [0, 1) range over many iterations', () => {
|
|
130
|
+
const source = createSource(42);
|
|
131
|
+
for (let i = 0; i < 1000; i++) {
|
|
132
|
+
const value = source.next();
|
|
133
|
+
expect(value).toBeGreaterThanOrEqual(0);
|
|
134
|
+
expect(value).toBeLessThan(1);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('sequence does not degenerate to constant values', () => {
|
|
139
|
+
const source = createSource(42);
|
|
140
|
+
const values = readValues(source, 100);
|
|
141
|
+
const unique = new Set(values);
|
|
142
|
+
// With 100 values from a proper PRNG, we expect many unique values
|
|
143
|
+
expect(unique.size).toBeGreaterThan(90);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe('clone', () => {
|
|
148
|
+
test('clone produces identical sequence from clone point', () => {
|
|
149
|
+
const parent = createSource(7);
|
|
150
|
+
parent.next();
|
|
151
|
+
parent.next();
|
|
152
|
+
|
|
153
|
+
const clone = parent.clone();
|
|
154
|
+
expect(readValues(parent, 4)).toEqual(readValues(clone, 4));
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('clone preserves counter and lineage', () => {
|
|
158
|
+
const parent = createSource(7);
|
|
159
|
+
parent.next();
|
|
160
|
+
parent.next();
|
|
161
|
+
parent.next();
|
|
162
|
+
|
|
163
|
+
const clone = parent.clone();
|
|
164
|
+
expect(clone.counter).toBe(3);
|
|
165
|
+
expect(clone.seed).toBe(parent.seed);
|
|
166
|
+
expect(clone.lineage).toEqual(parent.lineage);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('advancing clone does not affect parent', () => {
|
|
170
|
+
const parent = createSource(7);
|
|
171
|
+
parent.next();
|
|
172
|
+
|
|
173
|
+
const clone = parent.clone();
|
|
174
|
+
clone.next();
|
|
175
|
+
clone.next();
|
|
176
|
+
clone.next();
|
|
177
|
+
|
|
178
|
+
// Parent should still be at counter 1 and produce its own next value
|
|
179
|
+
expect(parent.counter).toBe(1);
|
|
180
|
+
expect(clone.counter).toBe(4);
|
|
181
|
+
|
|
182
|
+
// Parent's next value should differ from clone's (different positions)
|
|
183
|
+
parent.next();
|
|
184
|
+
clone.next();
|
|
185
|
+
expect(parent.counter).toBe(2);
|
|
186
|
+
expect(clone.counter).toBe(5);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('clone of a child preserves child lineage', () => {
|
|
190
|
+
const parent = createSource(7);
|
|
191
|
+
parent.next();
|
|
192
|
+
const child = parent.createChild('branch');
|
|
193
|
+
child.next();
|
|
194
|
+
child.next();
|
|
195
|
+
|
|
196
|
+
const clone = child.clone();
|
|
197
|
+
expect(clone.lineage).toEqual(['1:branch']);
|
|
198
|
+
expect(clone.counter).toBe(2);
|
|
199
|
+
expect(readValues(child, 4)).toEqual(readValues(clone, 4));
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe('createChild', () => {
|
|
204
|
+
test('child starts with counter at 0', () => {
|
|
205
|
+
const parent = createSource(7);
|
|
206
|
+
parent.next();
|
|
207
|
+
parent.next();
|
|
208
|
+
|
|
209
|
+
const child = parent.createChild('test');
|
|
210
|
+
expect(child.counter).toBe(0);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
test('child records lineage as counter:label', () => {
|
|
214
|
+
const parent = createSource(7);
|
|
215
|
+
parent.next();
|
|
216
|
+
parent.next();
|
|
217
|
+
parent.next();
|
|
218
|
+
|
|
219
|
+
const child = parent.createChild('myLabel');
|
|
220
|
+
expect(child.lineage).toEqual(['3:myLabel']);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('grandchild accumulates lineage', () => {
|
|
224
|
+
const parent = createSource(7);
|
|
225
|
+
parent.next();
|
|
226
|
+
parent.next();
|
|
227
|
+
|
|
228
|
+
const child = parent.createChild('branch');
|
|
229
|
+
child.next();
|
|
230
|
+
|
|
231
|
+
const grandchild = child.createChild('leaf');
|
|
232
|
+
expect(grandchild.lineage).toEqual(['2:branch', '1:leaf']);
|
|
233
|
+
expect(grandchild.counter).toBe(0);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test('children with same label from same parent state are identical', () => {
|
|
237
|
+
const parent1 = createSource(7);
|
|
238
|
+
const parent2 = createSource(7);
|
|
239
|
+
parent1.next();
|
|
240
|
+
parent1.next();
|
|
241
|
+
parent2.next();
|
|
242
|
+
parent2.next();
|
|
243
|
+
|
|
244
|
+
const child1 = parent1.createChild('same');
|
|
245
|
+
const child2 = parent2.createChild('same');
|
|
246
|
+
expect(readValues(child1, 10)).toEqual(readValues(child2, 10));
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
test('children with different labels produce different sequences', () => {
|
|
250
|
+
const parent = createSource(7);
|
|
251
|
+
parent.next();
|
|
252
|
+
parent.next();
|
|
253
|
+
|
|
254
|
+
const childA = parent.createChild('alpha');
|
|
255
|
+
const childB = parent.createChild('beta');
|
|
256
|
+
expect(readValues(childA, 4)).not.toEqual(readValues(childB, 4));
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test('same label but different parent positions produce different children', () => {
|
|
260
|
+
const early = createSource(7);
|
|
261
|
+
const later = createSource(7);
|
|
262
|
+
early.next();
|
|
263
|
+
later.next();
|
|
264
|
+
later.next();
|
|
265
|
+
|
|
266
|
+
const earlyChild = early.createChild('shared');
|
|
267
|
+
const laterChild = later.createChild('shared');
|
|
268
|
+
expect(readValues(earlyChild, 4)).not.toEqual(readValues(laterChild, 4));
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
test('advancing child does not affect parent', () => {
|
|
272
|
+
const parent = createSource(7);
|
|
273
|
+
parent.next();
|
|
274
|
+
parent.next();
|
|
275
|
+
|
|
276
|
+
const child = parent.createChild('test');
|
|
277
|
+
child.next();
|
|
278
|
+
child.next();
|
|
279
|
+
child.next();
|
|
280
|
+
|
|
281
|
+
expect(parent.counter).toBe(2);
|
|
282
|
+
expect(child.counter).toBe(3);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test('child uses same step function as parent', () => {
|
|
286
|
+
const parent = SeededRandomSource.create({ seed: 10, step: passthroughStep }).orThrow();
|
|
287
|
+
parent.next(); // advances state to 11
|
|
288
|
+
|
|
289
|
+
const child = parent.createChild('x');
|
|
290
|
+
// Child should use passthroughStep too. Its initial state comes from hashStateAndLabel
|
|
291
|
+
// so value = hashed state, next call returns hashed state + 1
|
|
292
|
+
const first = child.next();
|
|
293
|
+
const second = child.next();
|
|
294
|
+
expect(second - first).toBe(1); // passthroughStep increments by 1
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test('child with empty label produces a valid source', () => {
|
|
298
|
+
const parent = createSource(7);
|
|
299
|
+
parent.next();
|
|
300
|
+
|
|
301
|
+
const child = parent.createChild('');
|
|
302
|
+
expect(child.lineage).toEqual(['1:']);
|
|
303
|
+
expect(child.counter).toBe(0);
|
|
304
|
+
// Should still produce valid numbers
|
|
305
|
+
const value = child.next();
|
|
306
|
+
expect(value).toBeGreaterThanOrEqual(0);
|
|
307
|
+
expect(value).toBeLessThan(1);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
describe('hashSeed', () => {
|
|
312
|
+
test('numeric input returns number as string seed and number as state', () => {
|
|
313
|
+
const result = SeededRandomSource.hashSeed(42);
|
|
314
|
+
expect(result).toEqual({ seed: '42', state: 42 });
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test('numeric zero is handled correctly', () => {
|
|
318
|
+
const result = SeededRandomSource.hashSeed(0);
|
|
319
|
+
expect(result).toEqual({ seed: '0', state: 0 });
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test('negative number is handled correctly', () => {
|
|
323
|
+
const result = SeededRandomSource.hashSeed(-5);
|
|
324
|
+
expect(result).toEqual({ seed: '-5', state: -5 });
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
test('string that parses as number uses numeric state', () => {
|
|
328
|
+
const result = SeededRandomSource.hashSeed('123');
|
|
329
|
+
expect(result).toEqual({ seed: '123', state: 123 });
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
test('string "0" parses as numeric zero', () => {
|
|
333
|
+
const result = SeededRandomSource.hashSeed('0');
|
|
334
|
+
expect(result).toEqual({ seed: '0', state: 0 });
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
test('non-numeric string is hashed to a state', () => {
|
|
338
|
+
const result = SeededRandomSource.hashSeed('hello');
|
|
339
|
+
expect(result.seed).toBe('hello');
|
|
340
|
+
expect(typeof result.state).toBe('number');
|
|
341
|
+
expect(result.state).toBeGreaterThanOrEqual(0);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test('same non-numeric string always produces same hash', () => {
|
|
345
|
+
const first = SeededRandomSource.hashSeed('test-seed');
|
|
346
|
+
const second = SeededRandomSource.hashSeed('test-seed');
|
|
347
|
+
expect(first).toEqual(second);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
test('different non-numeric strings produce different hashes', () => {
|
|
351
|
+
const a = SeededRandomSource.hashSeed('alpha');
|
|
352
|
+
const b = SeededRandomSource.hashSeed('beta');
|
|
353
|
+
expect(a.state).not.toBe(b.state);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
test('empty string is hashed (not treated as numeric)', () => {
|
|
357
|
+
const result = SeededRandomSource.hashSeed('');
|
|
358
|
+
expect(result.seed).toBe('');
|
|
359
|
+
// Empty string: Number('') is 0, isFinite(0) is true, so state should be 0
|
|
360
|
+
expect(result.state).toBe(0);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
test('string "Infinity" is hashed as non-numeric', () => {
|
|
364
|
+
const result = SeededRandomSource.hashSeed('Infinity');
|
|
365
|
+
expect(result.seed).toBe('Infinity');
|
|
366
|
+
// Number('Infinity') is Infinity, isFinite(Infinity) is false → hashed
|
|
367
|
+
expect(typeof result.state).toBe('number');
|
|
368
|
+
expect(result.state).not.toBe(Infinity);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
test('string "NaN" is hashed as non-numeric', () => {
|
|
372
|
+
const result = SeededRandomSource.hashSeed('NaN');
|
|
373
|
+
expect(result.seed).toBe('NaN');
|
|
374
|
+
// Number('NaN') is NaN, isNaN(NaN) is true → hashed
|
|
375
|
+
expect(typeof result.state).toBe('number');
|
|
376
|
+
expect(isNaN(result.state)).toBe(false);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
test('floating point string is treated as numeric', () => {
|
|
380
|
+
const result = SeededRandomSource.hashSeed('3.14');
|
|
381
|
+
expect(result).toEqual({ seed: '3.14', state: 3.14 });
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
test('negative numeric string is treated as numeric', () => {
|
|
385
|
+
const result = SeededRandomSource.hashSeed('-42');
|
|
386
|
+
expect(result).toEqual({ seed: '-42', state: -42 });
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
describe('hashStateAndLabel', () => {
|
|
391
|
+
test('produces a seed string in "state:label" format', () => {
|
|
392
|
+
const result = SeededRandomSource.hashStateAndLabel(100, 'test');
|
|
393
|
+
expect(result.seed).toBe('100:test');
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
test('state is treated as unsigned 32-bit integer in seed string', () => {
|
|
397
|
+
// -1 >>> 0 = 4294967295
|
|
398
|
+
const result = SeededRandomSource.hashStateAndLabel(-1, 'label');
|
|
399
|
+
expect(result.seed).toBe('4294967295:label');
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
test('same inputs produce same output', () => {
|
|
403
|
+
const first = SeededRandomSource.hashStateAndLabel(42, 'branch');
|
|
404
|
+
const second = SeededRandomSource.hashStateAndLabel(42, 'branch');
|
|
405
|
+
expect(first).toEqual(second);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
test('different states produce different results', () => {
|
|
409
|
+
const a = SeededRandomSource.hashStateAndLabel(1, 'same');
|
|
410
|
+
const b = SeededRandomSource.hashStateAndLabel(2, 'same');
|
|
411
|
+
expect(a.state).not.toBe(b.state);
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
test('different labels produce different results', () => {
|
|
415
|
+
const a = SeededRandomSource.hashStateAndLabel(42, 'alpha');
|
|
416
|
+
const b = SeededRandomSource.hashStateAndLabel(42, 'beta');
|
|
417
|
+
expect(a.state).not.toBe(b.state);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
test('state value is a non-negative number from hash', () => {
|
|
421
|
+
const result = SeededRandomSource.hashStateAndLabel(999, 'test');
|
|
422
|
+
expect(result.state).toBeGreaterThanOrEqual(0);
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
describe('mulberryStep', () => {
|
|
427
|
+
test('returns value in [0, 1) range', () => {
|
|
428
|
+
const result = SeededRandomSource.mulberryStep(42);
|
|
429
|
+
expect(result.value).toBeGreaterThanOrEqual(0);
|
|
430
|
+
expect(result.value).toBeLessThan(1);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
test('advances state deterministically', () => {
|
|
434
|
+
const first = SeededRandomSource.mulberryStep(42);
|
|
435
|
+
const second = SeededRandomSource.mulberryStep(42);
|
|
436
|
+
expect(first).toEqual(second);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
test('different states produce different results', () => {
|
|
440
|
+
const a = SeededRandomSource.mulberryStep(1);
|
|
441
|
+
const b = SeededRandomSource.mulberryStep(2);
|
|
442
|
+
expect(a.value).not.toBe(b.value);
|
|
443
|
+
expect(a.nextState).not.toBe(b.nextState);
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
test('nextState is an unsigned 32-bit integer', () => {
|
|
447
|
+
const result = SeededRandomSource.mulberryStep(0);
|
|
448
|
+
expect(result.nextState).toBeGreaterThanOrEqual(0);
|
|
449
|
+
expect(result.nextState).toBeLessThanOrEqual(0xffffffff);
|
|
450
|
+
expect(Number.isInteger(result.nextState)).toBe(true);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
test('state 0 produces valid output', () => {
|
|
454
|
+
const result = SeededRandomSource.mulberryStep(0);
|
|
455
|
+
expect(result.value).toBeGreaterThanOrEqual(0);
|
|
456
|
+
expect(result.value).toBeLessThan(1);
|
|
457
|
+
// nextState should be 0x6D2B79F5 (the constant added)
|
|
458
|
+
expect(result.nextState).toBe(0x6d2b79f5);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
test('produces [0,1) values across many states', () => {
|
|
462
|
+
let state = 12345;
|
|
463
|
+
for (let i = 0; i < 1000; i++) {
|
|
464
|
+
const result = SeededRandomSource.mulberryStep(state);
|
|
465
|
+
expect(result.value).toBeGreaterThanOrEqual(0);
|
|
466
|
+
expect(result.value).toBeLessThan(1);
|
|
467
|
+
state = result.nextState;
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
describe('seed edge cases', () => {
|
|
473
|
+
test('seed 0 produces a valid source', () => {
|
|
474
|
+
expect(SeededRandomSource.create(0)).toSucceedAndSatisfy((source) => {
|
|
475
|
+
expect(source.seed).toBe('0');
|
|
476
|
+
const value = source.next();
|
|
477
|
+
expect(value).toBeGreaterThanOrEqual(0);
|
|
478
|
+
expect(value).toBeLessThan(1);
|
|
479
|
+
});
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
test('negative seed produces a valid source', () => {
|
|
483
|
+
expect(SeededRandomSource.create(-1)).toSucceedAndSatisfy((source) => {
|
|
484
|
+
expect(source.seed).toBe('-1');
|
|
485
|
+
const value = source.next();
|
|
486
|
+
expect(value).toBeGreaterThanOrEqual(0);
|
|
487
|
+
expect(value).toBeLessThan(1);
|
|
488
|
+
});
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
test('very large numeric seed produces a valid source', () => {
|
|
492
|
+
expect(SeededRandomSource.create(Number.MAX_SAFE_INTEGER)).toSucceedAndSatisfy((source) => {
|
|
493
|
+
const value = source.next();
|
|
494
|
+
expect(value).toBeGreaterThanOrEqual(0);
|
|
495
|
+
expect(value).toBeLessThan(1);
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
test('empty string seed produces a valid source', () => {
|
|
500
|
+
expect(SeededRandomSource.create('')).toSucceedAndSatisfy((source) => {
|
|
501
|
+
expect(source.seed).toBe('');
|
|
502
|
+
const value = source.next();
|
|
503
|
+
expect(value).toBeGreaterThanOrEqual(0);
|
|
504
|
+
expect(value).toBeLessThan(1);
|
|
505
|
+
});
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
test('whitespace string seed produces a valid source', () => {
|
|
509
|
+
expect(SeededRandomSource.create(' ')).toSucceedAndSatisfy((source) => {
|
|
510
|
+
expect(source.seed).toBe(' ');
|
|
511
|
+
const value = source.next();
|
|
512
|
+
expect(value).toBeGreaterThanOrEqual(0);
|
|
513
|
+
expect(value).toBeLessThan(1);
|
|
514
|
+
});
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
test('unicode string seed produces a valid source', () => {
|
|
518
|
+
expect(SeededRandomSource.create('🎲🎰')).toSucceedAndSatisfy((source) => {
|
|
519
|
+
expect(source.seed).toBe('🎲🎰');
|
|
520
|
+
const value = source.next();
|
|
521
|
+
expect(value).toBeGreaterThanOrEqual(0);
|
|
522
|
+
expect(value).toBeLessThan(1);
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
});
|