Skip to content

Commit

Permalink
add a tests for the ? and | mini-notation operators
Browse files Browse the repository at this point in the history
The tests are probabilistic, so it is possible that if the
pseudo-random number generator changes in the future, we might
get results that fail. They work for the current PRNG, though,
and use boundaries for the number of values of different types
such that there should only be about a 1% probability that the
tests would fail by chance assuming that the PRNG returns
evenly distributed values.
  • Loading branch information
bpow committed Aug 5, 2022
1 parent c62c21c commit ddf6c91
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/mini/mini.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const applyOptions = (parent) => (pat, i) => {
case 'bjorklund':
return pat.euclid(operator.arguments_.pulse, operator.arguments_.step, operator.arguments_.rotation);
case 'degradeBy':
return reify(pat)._degradeByWith(rand.early(Math.PI * _nextSeed()).segment(1), operator.arguments_.amount);
return reify(pat)._degradeByWith(strudel.rand.early(Math.PI * _nextSeed()).segment(1), operator.arguments_.amount);
// TODO: case 'fixed-step': "%"
}
console.warn(`operator "${operator.type_}" not implemented`);
Expand Down Expand Up @@ -91,7 +91,7 @@ export function patternifyAST(ast) {
return stack(...children);
}
if (alignment === 'r') {
return strudel.chooseInWith(rand.early(Math.PI * _nextSeed()).segment(1), children);
return strudel.chooseInWith(strudel.rand.early(Math.PI * _nextSeed()).segment(1), children);
}
const weightedChildren = ast.source_.some((child) => !!child.options_?.weight);
if (!weightedChildren && alignment === 't') {
Expand Down
36 changes: 35 additions & 1 deletion packages/mini/test/mini.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ This program is free software: you can redistribute it and/or modify it under th

import { strict as assert } from 'assert';
import { mini } from '../mini.mjs';
import '@strudel.cycles/core/euclid.mjs';

describe('mini', () => {
const minV = (v) => mini(v)._firstCycleValues;
Expand Down Expand Up @@ -50,4 +49,39 @@ describe('mini', () => {
it('supports euclidean rhythms', () => {
assert.deepStrictEqual(minS('a(3, 8)'), ['a: 0 - 1/8', 'a: 3/8 - 1/2', 'a: 3/4 - 7/8']);
});
// testing things, so there's a chance we could fail by probability.
// these next few tests work with the current PRNG, and are intended to succeed with p > 0.99 even if the PRNG changes
// (as long as the PRNG has a relatively-uniform distribution of values)
it('supports degradeBy with default of 50%', () => {
const haps = mini('a?').queryArc(0, 1000);
assert(459 <= haps.length && haps.length <= 541, 'Number of elements did not fall in 99% confidence interval for binomial with p=0.5');
});
it('supports degradeBy with an argument', () => {
const haps = mini('a?0.8').queryArc(0, 1000);
assert(haps.length > 0, 'Should have had at least one element when degradeBy was set at 0.8');
assert(haps.length < 230, 'Had too many cycles remaining after degradeBy 0.8');
});
it('supports the random choice operator ("|") with nesting', () => {
const numCycles = 900;
const haps = mini('a | [b | c] | [d | e | f]').queryArc(0, numCycles);
// Should have about 1/3 a, 1/6 each of b | c, and 1/9 each of d | e | f.
// Evaluating this distribution with a chi-squared test.
// Note: this just evaluates the overall distribution, not things like correlation/runs of values
const observed = haps.reduce((acc, hap) => {
acc[hap.value] = (acc[hap.value] || 0) + 1;
return acc;
}, {});
const expected = {
a: numCycles / 3, b: numCycles / 6, c: numCycles / 6,
d: numCycles / 9, e: numCycles / 9, f: numCycles / 9
};
let chisq = -numCycles;
for (let k in expected) {
chisq += observed[k] * observed[k] / expected[k];
}
// 15.086 is the chisq for 5 degrees of freedom at 99%, so for 99% of uniformly-distributed
// PRNG, this test should succeed
assert(chisq <= 15.086,
chisq + ' was expected to be less than 15.086 under chi-squared test');
});
});

0 comments on commit ddf6c91

Please sign in to comment.