Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use seed in randomised tests #6287

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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