Skip to content

Commit 2619a0f

Browse files
committedOct 24, 2020
refactor(test): use constant generator
1 parent b65203a commit 2619a0f

File tree

2 files changed

+61
-84
lines changed

2 files changed

+61
-84
lines changed
 

‎src/gen.spec.ts

+23-30
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { TFGen } from "./gen";
22
import { Int32x8 } from "./tf";
33

4-
function initGen(): TFGen {
5-
return TFGen.seed(
6-
0x00000000,
7-
0x00000000,
8-
0x01234567,
9-
0x89abcdef,
10-
0x89abcdef,
11-
0x01234567,
12-
0xffffffff,
13-
0xffffffff
14-
);
15-
}
4+
const defaultGen = TFGen.seed(
5+
0x00000000,
6+
0x00000000,
7+
0x01234567,
8+
0x89abcdef,
9+
0x89abcdef,
10+
0x01234567,
11+
0xffffffff,
12+
0xffffffff
13+
);
1614

1715
describe("TFGen", () => {
1816
describe(".seed", () => {
@@ -47,48 +45,43 @@ describe("TFGen", () => {
4745

4846
describe("#next", () => {
4947
it("should return a pair of random 32-bit integer and a new generator", () => {
50-
const gen = initGen();
51-
const [val, nextGen] = gen.next();
48+
const [val, nextGen] = defaultGen.next();
5249
expect(val).toMatchInlineSnapshot(`78121409`);
5350
expect(val).toBeGreaterThanOrEqual(-0x80000000);
5451
expect(val).toBeLessThanOrEqual(0x7fffffff);
55-
expect(nextGen).not.toBe(gen);
52+
expect(nextGen).not.toBe(defaultGen);
5653
});
5754
});
5855

5956
describe("#split", () => {
6057
it("should return a pair of new generators", () => {
61-
const gen = initGen();
62-
const [left, right] = gen.split();
63-
expect(left).not.toBe(gen);
64-
expect(right).not.toBe(gen);
58+
const [left, right] = defaultGen.split();
59+
expect(left).not.toBe(defaultGen);
60+
expect(right).not.toBe(defaultGen);
6561
expect(left).not.toBe(right);
6662
});
6763
});
6864

6965
describe("#level", () => {
7066
it("should return a new generator if needed", () => {
71-
const gen1 = initGen();
72-
const newGen1 = gen1.level();
73-
expect(newGen1).toBe(gen1);
67+
const newGen1 = defaultGen.level();
68+
expect(newGen1).toBe(defaultGen);
7469

75-
const gen2 = gen1.splitn(32, 0);
76-
const newGen2 = gen2.level();
77-
expect(newGen2).not.toBe(gen2);
70+
const gen = defaultGen.splitn(32, 0);
71+
const newGen2 = gen.level();
72+
expect(newGen2).not.toBe(gen);
7873
});
7974
});
8075

8176
describe("#splitn", () => {
8277
it("should return a new generator", () => {
83-
const gen = initGen();
84-
const newGen = gen.splitn(32, 0x7fffffff);
85-
expect(newGen).not.toBe(gen);
78+
const newGen = defaultGen.splitn(32, 0x7fffffff);
79+
expect(newGen).not.toBe(defaultGen);
8680
});
8781

8882
it("should throw error if `nbits` is out of [0, 32]", () => {
89-
const gen = initGen();
9083
expect(() => {
91-
gen.splitn(48, 0);
84+
defaultGen.splitn(48, 0);
9285
}).toThrow(Error);
9386
});
9487
});

‎src/random.spec.ts

+38-54
Original file line numberDiff line numberDiff line change
@@ -10,148 +10,132 @@ import {
1010
random,
1111
} from "./random";
1212

13-
function initGen(): TFGen {
14-
return TFGen.seed(
15-
0x00000000,
16-
0x00000000,
17-
0x01234567,
18-
0x89abcdef,
19-
0x89abcdef,
20-
0x01234567,
21-
0xffffffff,
22-
0xffffffff
23-
);
24-
}
13+
const defaultGen = TFGen.seed(
14+
0x00000000,
15+
0x00000000,
16+
0x01234567,
17+
0x89abcdef,
18+
0x89abcdef,
19+
0x01234567,
20+
0xffffffff,
21+
0xffffffff
22+
);
2523

2624
describe("randomInt32", () => {
2725
it("should generate a random 32-bit signed integer and a next generator", () => {
28-
const gen = initGen();
29-
const [val, nextGen] = randomInt32(gen);
26+
const [val, nextGen] = randomInt32(defaultGen);
3027
expect(val).toMatchInlineSnapshot(`78121409`);
3128
expect(val).toBeGreaterThanOrEqual(-0x80000000);
3229
expect(val).toBeLessThanOrEqual(0x7fffffff);
33-
expect(nextGen).not.toBe(gen);
30+
expect(nextGen).not.toBe(defaultGen);
3431
});
3532
});
3633

3734
describe("randomInt32R", () => {
3835
it("should generate a random 32-bit signed integer within a bounds and a next generator", () => {
39-
const gen = initGen();
40-
const [val, nextGen] = randomInt32R(gen, [-0x8000, 0x7fff]);
36+
const [val, nextGen] = randomInt32R(defaultGen, [-0x8000, 0x7fff]);
4137
expect(val).toMatchInlineSnapshot(`-30271`);
4238
expect(val).toBeGreaterThanOrEqual(-0x8000);
4339
expect(val).toBeLessThanOrEqual(0x7fff);
44-
expect(nextGen).not.toBe(gen);
40+
expect(nextGen).not.toBe(defaultGen);
4541
});
4642

4743
it("should work if bounds are flipped", () => {
48-
const gen = initGen();
49-
const [val, nextGen] = randomInt32R(gen, [0x7fff, -0x8000]);
44+
const [val, nextGen] = randomInt32R(defaultGen, [0x7fff, -0x8000]);
5045
expect(val).toMatchInlineSnapshot(`-30271`);
5146
expect(val).toBeGreaterThanOrEqual(-0x8000);
5247
expect(val).toBeLessThanOrEqual(0x7fff);
53-
expect(nextGen).not.toBe(gen);
48+
expect(nextGen).not.toBe(defaultGen);
5449
});
5550

5651
it("should work if bounds are equal", () => {
57-
const gen = initGen();
58-
const [val, nextGen] = randomInt32R(gen, [0x7fff, 0x7fff]);
52+
const [val, nextGen] = randomInt32R(defaultGen, [0x7fff, 0x7fff]);
5953
expect(val).toBe(0x7fff);
60-
expect(nextGen).toBe(gen);
54+
expect(nextGen).toBe(defaultGen);
6155
});
6256
});
6357

6458
describe("randomUint32", () => {
6559
it("should generate a random 32-bit unsigned integer and a next generator", () => {
66-
const gen = initGen();
67-
const [val, nextGen] = randomUint32(gen);
60+
const [val, nextGen] = randomUint32(defaultGen);
6861
expect(val).toMatchInlineSnapshot(`78121409`);
6962
expect(val).toBeGreaterThanOrEqual(0x00000000);
7063
expect(val).toBeLessThanOrEqual(0xffffffff);
71-
expect(nextGen).not.toBe(gen);
64+
expect(nextGen).not.toBe(defaultGen);
7265
});
7366
});
7467

7568
describe("randomUint32R", () => {
7669
it("should generate a random 32-bit unsigned integer within a bounds and a next generator", () => {
77-
const gen = initGen();
78-
const [val, nextGen] = randomUint32R(gen, [0xffff0000, 0xffffffff]);
70+
const [val, nextGen] = randomUint32R(defaultGen, [0xffff0000, 0xffffffff]);
7971
expect(val).toMatchInlineSnapshot(`4294904257`);
8072
expect(val).toBeGreaterThanOrEqual(0xffff0000);
8173
expect(val).toBeLessThanOrEqual(0xffffffff);
82-
expect(nextGen).not.toBe(gen);
74+
expect(nextGen).not.toBe(defaultGen);
8375
});
8476

8577
it("should work if bounds are flipped", () => {
86-
const gen = initGen();
87-
const [val, nextGen] = randomUint32R(gen, [0xffffffff, 0xffff0000]);
78+
const [val, nextGen] = randomUint32R(defaultGen, [0xffffffff, 0xffff0000]);
8879
expect(val).toMatchInlineSnapshot(`4294904257`);
8980
expect(val).toBeGreaterThanOrEqual(0xffff0000);
9081
expect(val).toBeLessThanOrEqual(0xffffffff);
91-
expect(nextGen).not.toBe(gen);
82+
expect(nextGen).not.toBe(defaultGen);
9283
});
9384

9485
it("should work if bounds are equal", () => {
95-
const gen = initGen();
96-
const [val, nextGen] = randomUint32R(gen, [0xffff0000, 0xffff0000]);
86+
const [val, nextGen] = randomUint32R(defaultGen, [0xffff0000, 0xffff0000]);
9787
expect(val).toBe(0xffff0000);
98-
expect(nextGen).toBe(gen);
88+
expect(nextGen).toBe(defaultGen);
9989
});
10090
});
10191

10292
describe("randomBoolean", () => {
10393
it("should generate a random boolean value and a next generator", () => {
104-
const gen = initGen();
105-
const [val, nextGen] = randomBoolean(gen);
94+
const [val, nextGen] = randomBoolean(defaultGen);
10695
expect(val).toMatchInlineSnapshot(`false`);
107-
expect(nextGen).not.toBe(gen);
96+
expect(nextGen).not.toBe(defaultGen);
10897
});
10998
});
11099

111100
describe("randomInt", () => {
112101
it("should generate a random safe integer and a next generator", () => {
113-
const gen = initGen();
114-
const [val, nextGen] = randomInt(gen);
102+
const [val, nextGen] = randomInt(defaultGen);
115103
expect(val).toMatchInlineSnapshot(`-4543985126733374`);
116104
expect(Number.isSafeInteger(val)).toBe(true);
117-
expect(nextGen).not.toBe(gen);
105+
expect(nextGen).not.toBe(defaultGen);
118106
});
119107
});
120108

121109
describe("randomIntR", () => {
122110
it("should generate a random safe integer within a bounds and a next generator", () => {
123-
const gen = initGen();
124-
const [val, nextGen] = randomIntR(gen, [-100, 100]);
111+
const [val, nextGen] = randomIntR(defaultGen, [-100, 100]);
125112
expect(val).toMatchInlineSnapshot(`93`);
126113
expect(val).toBeGreaterThanOrEqual(-100);
127114
expect(val).toBeLessThanOrEqual(100);
128-
expect(nextGen).not.toBe(gen);
115+
expect(nextGen).not.toBe(defaultGen);
129116
});
130117

131118
it("should work if bounds are flipped", () => {
132-
const gen = initGen();
133-
const [val, nextGen] = randomIntR(gen, [100, -100]);
119+
const [val, nextGen] = randomIntR(defaultGen, [100, -100]);
134120
expect(val).toMatchInlineSnapshot(`93`);
135121
expect(val).toBeGreaterThanOrEqual(-100);
136122
expect(val).toBeLessThanOrEqual(100);
137-
expect(nextGen).not.toBe(gen);
123+
expect(nextGen).not.toBe(defaultGen);
138124
});
139125

140126
it("should work if bounds are equal", () => {
141-
const gen = initGen();
142-
const [val, nextGen] = randomIntR(gen, [42, 42]);
127+
const [val, nextGen] = randomIntR(defaultGen, [42, 42]);
143128
expect(val).toBe(42);
144-
expect(nextGen).toBe(gen);
129+
expect(nextGen).toBe(defaultGen);
145130
});
146131
});
147132

148133
describe("random", () => {
149134
it("should generate a random number within [0, 1) and a next generator", () => {
150-
const gen = initGen();
151-
const [val, nextGen] = random(gen);
135+
const [val, nextGen] = random(defaultGen);
152136
expect(val).toMatchInlineSnapshot(`0.49551630887463477`);
153137
expect(val).toBeGreaterThanOrEqual(0);
154138
expect(val).toBeLessThan(1);
155-
expect(nextGen).not.toBe(gen);
139+
expect(nextGen).not.toBe(defaultGen);
156140
});
157141
});

0 commit comments

Comments
 (0)
Please sign in to comment.