Skip to content

Commit

Permalink
refactor(test): use constant generator
Browse files Browse the repository at this point in the history
  • Loading branch information
susisu committed Oct 24, 2020
1 parent b65203a commit 2619a0f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 84 deletions.
53 changes: 23 additions & 30 deletions src/gen.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { TFGen } from "./gen";
import { Int32x8 } from "./tf";

function initGen(): TFGen {
return TFGen.seed(
0x00000000,
0x00000000,
0x01234567,
0x89abcdef,
0x89abcdef,
0x01234567,
0xffffffff,
0xffffffff
);
}
const defaultGen = TFGen.seed(
0x00000000,
0x00000000,
0x01234567,
0x89abcdef,
0x89abcdef,
0x01234567,
0xffffffff,
0xffffffff
);

describe("TFGen", () => {
describe(".seed", () => {
Expand Down Expand Up @@ -47,48 +45,43 @@ describe("TFGen", () => {

describe("#next", () => {
it("should return a pair of random 32-bit integer and a new generator", () => {
const gen = initGen();
const [val, nextGen] = gen.next();
const [val, nextGen] = defaultGen.next();
expect(val).toMatchInlineSnapshot(`78121409`);
expect(val).toBeGreaterThanOrEqual(-0x80000000);
expect(val).toBeLessThanOrEqual(0x7fffffff);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});
});

describe("#split", () => {
it("should return a pair of new generators", () => {
const gen = initGen();
const [left, right] = gen.split();
expect(left).not.toBe(gen);
expect(right).not.toBe(gen);
const [left, right] = defaultGen.split();
expect(left).not.toBe(defaultGen);
expect(right).not.toBe(defaultGen);
expect(left).not.toBe(right);
});
});

describe("#level", () => {
it("should return a new generator if needed", () => {
const gen1 = initGen();
const newGen1 = gen1.level();
expect(newGen1).toBe(gen1);
const newGen1 = defaultGen.level();
expect(newGen1).toBe(defaultGen);

const gen2 = gen1.splitn(32, 0);
const newGen2 = gen2.level();
expect(newGen2).not.toBe(gen2);
const gen = defaultGen.splitn(32, 0);
const newGen2 = gen.level();
expect(newGen2).not.toBe(gen);
});
});

describe("#splitn", () => {
it("should return a new generator", () => {
const gen = initGen();
const newGen = gen.splitn(32, 0x7fffffff);
expect(newGen).not.toBe(gen);
const newGen = defaultGen.splitn(32, 0x7fffffff);
expect(newGen).not.toBe(defaultGen);
});

it("should throw error if `nbits` is out of [0, 32]", () => {
const gen = initGen();
expect(() => {
gen.splitn(48, 0);
defaultGen.splitn(48, 0);
}).toThrow(Error);
});
});
Expand Down
92 changes: 38 additions & 54 deletions src/random.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,148 +10,132 @@ import {
random,
} from "./random";

function initGen(): TFGen {
return TFGen.seed(
0x00000000,
0x00000000,
0x01234567,
0x89abcdef,
0x89abcdef,
0x01234567,
0xffffffff,
0xffffffff
);
}
const defaultGen = TFGen.seed(
0x00000000,
0x00000000,
0x01234567,
0x89abcdef,
0x89abcdef,
0x01234567,
0xffffffff,
0xffffffff
);

describe("randomInt32", () => {
it("should generate a random 32-bit signed integer and a next generator", () => {
const gen = initGen();
const [val, nextGen] = randomInt32(gen);
const [val, nextGen] = randomInt32(defaultGen);
expect(val).toMatchInlineSnapshot(`78121409`);
expect(val).toBeGreaterThanOrEqual(-0x80000000);
expect(val).toBeLessThanOrEqual(0x7fffffff);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});
});

describe("randomInt32R", () => {
it("should generate a random 32-bit signed integer within a bounds and a next generator", () => {
const gen = initGen();
const [val, nextGen] = randomInt32R(gen, [-0x8000, 0x7fff]);
const [val, nextGen] = randomInt32R(defaultGen, [-0x8000, 0x7fff]);
expect(val).toMatchInlineSnapshot(`-30271`);
expect(val).toBeGreaterThanOrEqual(-0x8000);
expect(val).toBeLessThanOrEqual(0x7fff);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});

it("should work if bounds are flipped", () => {
const gen = initGen();
const [val, nextGen] = randomInt32R(gen, [0x7fff, -0x8000]);
const [val, nextGen] = randomInt32R(defaultGen, [0x7fff, -0x8000]);
expect(val).toMatchInlineSnapshot(`-30271`);
expect(val).toBeGreaterThanOrEqual(-0x8000);
expect(val).toBeLessThanOrEqual(0x7fff);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});

it("should work if bounds are equal", () => {
const gen = initGen();
const [val, nextGen] = randomInt32R(gen, [0x7fff, 0x7fff]);
const [val, nextGen] = randomInt32R(defaultGen, [0x7fff, 0x7fff]);
expect(val).toBe(0x7fff);
expect(nextGen).toBe(gen);
expect(nextGen).toBe(defaultGen);
});
});

describe("randomUint32", () => {
it("should generate a random 32-bit unsigned integer and a next generator", () => {
const gen = initGen();
const [val, nextGen] = randomUint32(gen);
const [val, nextGen] = randomUint32(defaultGen);
expect(val).toMatchInlineSnapshot(`78121409`);
expect(val).toBeGreaterThanOrEqual(0x00000000);
expect(val).toBeLessThanOrEqual(0xffffffff);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});
});

describe("randomUint32R", () => {
it("should generate a random 32-bit unsigned integer within a bounds and a next generator", () => {
const gen = initGen();
const [val, nextGen] = randomUint32R(gen, [0xffff0000, 0xffffffff]);
const [val, nextGen] = randomUint32R(defaultGen, [0xffff0000, 0xffffffff]);
expect(val).toMatchInlineSnapshot(`4294904257`);
expect(val).toBeGreaterThanOrEqual(0xffff0000);
expect(val).toBeLessThanOrEqual(0xffffffff);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});

it("should work if bounds are flipped", () => {
const gen = initGen();
const [val, nextGen] = randomUint32R(gen, [0xffffffff, 0xffff0000]);
const [val, nextGen] = randomUint32R(defaultGen, [0xffffffff, 0xffff0000]);
expect(val).toMatchInlineSnapshot(`4294904257`);
expect(val).toBeGreaterThanOrEqual(0xffff0000);
expect(val).toBeLessThanOrEqual(0xffffffff);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});

it("should work if bounds are equal", () => {
const gen = initGen();
const [val, nextGen] = randomUint32R(gen, [0xffff0000, 0xffff0000]);
const [val, nextGen] = randomUint32R(defaultGen, [0xffff0000, 0xffff0000]);
expect(val).toBe(0xffff0000);
expect(nextGen).toBe(gen);
expect(nextGen).toBe(defaultGen);
});
});

describe("randomBoolean", () => {
it("should generate a random boolean value and a next generator", () => {
const gen = initGen();
const [val, nextGen] = randomBoolean(gen);
const [val, nextGen] = randomBoolean(defaultGen);
expect(val).toMatchInlineSnapshot(`false`);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});
});

describe("randomInt", () => {
it("should generate a random safe integer and a next generator", () => {
const gen = initGen();
const [val, nextGen] = randomInt(gen);
const [val, nextGen] = randomInt(defaultGen);
expect(val).toMatchInlineSnapshot(`-4543985126733374`);
expect(Number.isSafeInteger(val)).toBe(true);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});
});

describe("randomIntR", () => {
it("should generate a random safe integer within a bounds and a next generator", () => {
const gen = initGen();
const [val, nextGen] = randomIntR(gen, [-100, 100]);
const [val, nextGen] = randomIntR(defaultGen, [-100, 100]);
expect(val).toMatchInlineSnapshot(`93`);
expect(val).toBeGreaterThanOrEqual(-100);
expect(val).toBeLessThanOrEqual(100);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});

it("should work if bounds are flipped", () => {
const gen = initGen();
const [val, nextGen] = randomIntR(gen, [100, -100]);
const [val, nextGen] = randomIntR(defaultGen, [100, -100]);
expect(val).toMatchInlineSnapshot(`93`);
expect(val).toBeGreaterThanOrEqual(-100);
expect(val).toBeLessThanOrEqual(100);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});

it("should work if bounds are equal", () => {
const gen = initGen();
const [val, nextGen] = randomIntR(gen, [42, 42]);
const [val, nextGen] = randomIntR(defaultGen, [42, 42]);
expect(val).toBe(42);
expect(nextGen).toBe(gen);
expect(nextGen).toBe(defaultGen);
});
});

describe("random", () => {
it("should generate a random number within [0, 1) and a next generator", () => {
const gen = initGen();
const [val, nextGen] = random(gen);
const [val, nextGen] = random(defaultGen);
expect(val).toMatchInlineSnapshot(`0.49551630887463477`);
expect(val).toBeGreaterThanOrEqual(0);
expect(val).toBeLessThan(1);
expect(nextGen).not.toBe(gen);
expect(nextGen).not.toBe(defaultGen);
});
});

0 comments on commit 2619a0f

Please sign in to comment.