Skip to content

Commit

Permalink
Use seed in randomised tests (software-mansion#6287)
Browse files Browse the repository at this point in the history
## Summary
Unfortunately it is impossible to seed Math.random function, so this
code includes custom implementation of pseudorandom number generator.


https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript/47593316#47593316

## Test plan
  • Loading branch information
Latropos authored Jul 19, 2024
1 parent 1339278 commit 0d551fb
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions packages/react-native-reanimated/__tests__/bezier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ import { Bezier } from '../src/Bezier';
*/
// spell-checker:enable

function splitMix32(seed: number) {
return function () {
seed |= 0;
seed = (seed + 0x9e3779b9) | 0;
let t = seed ^ (seed >>> 16);
t = Math.imul(t, 0x21f0aaad);
t = t ^ (t >>> 15);
t = Math.imul(t, 0x735a2d97);
return ((t = t ^ (t >>> 15)) >>> 0) / 4294967296;
};
}

function getSeededRandom(seed: number) {
return splitMix32(seed);
}

const seededRandom = getSeededRandom(2);
function repeat(n: number) {
return (f: () => void) => {
for (let i = 0; i < n; ++i) {
Expand Down Expand Up @@ -89,8 +106,8 @@ describe('Test `Bezier` function', () => {
const MONKEY_TRIES = 1000;
let allTestPass = true;
repeat(MONKEY_TRIES)(() => {
const a = Math.random();
const b = Math.random();
const a = seededRandom();
const b = seededRandom();

const easing = Bezier(a, a, b, b);

Expand Down Expand Up @@ -123,10 +140,10 @@ describe('Test `Bezier` function', () => {
const MONKEY_TRIES = 1000;
let allTestPass = true;
repeat(MONKEY_TRIES)(() => {
const a = Math.random();
const b = 3 * Math.random() - 1;
const c = Math.random();
const d = 3 * Math.random() - 1;
const a = seededRandom();
const b = 3 * seededRandom() - 1;
const c = seededRandom();
const d = 3 * seededRandom() - 1;
const easing = Bezier(a, b, c, d);

const satisfiesCondition = easing(0) === 0 && easing(1) === 1;
Expand Down Expand Up @@ -191,10 +208,10 @@ describe('Test `Bezier` function', () => {
const PRECISION_0 = 0.02;
let allTestPass0 = true;
repeat(MONKEY_TRIES)(() => {
const a = Math.random();
const b = Math.random();
const c = Math.random();
const d = Math.random();
const a = seededRandom();
const b = seededRandom();
const c = seededRandom();
const d = seededRandom();

const easing1 = Bezier(a, b, c, d);
const easing2 = Bezier(b, a, d, c);
Expand All @@ -214,10 +231,10 @@ describe('Test `Bezier` function', () => {
const PRECISION_1 = 1e-12;
let allTestPass1 = true;
repeat(MONKEY_TRIES)(() => {
const a = 0.9 * Math.random() + 0.05;
const b = 0.9 * Math.random() + 0.05;
const c = 0.9 * Math.random() + 0.05;
const d = 0.9 * Math.random() + 0.05;
const a = 0.9 * seededRandom() + 0.05;
const b = 0.9 * seededRandom() + 0.05;
const c = 0.9 * seededRandom() + 0.05;
const d = 0.9 * seededRandom() + 0.05;

const easing1 = Bezier(a, b, c, d);
const easing2 = Bezier(b, a, d, c);
Expand Down

0 comments on commit 0d551fb

Please sign in to comment.