2019-09-06 03:54:26 +00:00
|
|
|
import { expect } from "chai";
|
2024-05-03 18:31:14 +00:00
|
|
|
import { PatternGenerator } from "./PatternGenerator.js";
|
2019-09-06 03:54:26 +00:00
|
|
|
|
|
|
|
describe("PatternGenerator", () => {
|
|
|
|
function getArrayValues(gen: Iterator<any>, length: number): any[] {
|
|
|
|
const ret: any[] = [];
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
ret.push(gen.next().value);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
context("API", () => {
|
2022-01-05 05:12:40 +00:00
|
|
|
it("can be constructed with an number and type", () => {
|
|
|
|
const pattern = PatternGenerator(4, "down");
|
2024-05-03 18:31:14 +00:00
|
|
|
expect(getArrayValues(pattern, 10)).to.deep.equal([
|
|
|
|
3, 2, 1, 0, 3, 2, 1, 0, 3, 2,
|
|
|
|
]);
|
2019-09-06 03:54:26 +00:00
|
|
|
});
|
|
|
|
|
2022-01-05 05:12:40 +00:00
|
|
|
it("throws an error with a number less than 1", () => {
|
2019-09-06 03:54:26 +00:00
|
|
|
expect(() => {
|
2022-01-05 05:12:40 +00:00
|
|
|
const pattern = PatternGenerator(0);
|
2019-09-06 03:54:26 +00:00
|
|
|
pattern.next();
|
|
|
|
}).to.throw(Error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
context("Patterns", () => {
|
|
|
|
it("does the up pattern", () => {
|
2022-01-05 05:12:40 +00:00
|
|
|
const pattern = PatternGenerator(4, "up");
|
2024-05-03 18:31:14 +00:00
|
|
|
expect(getArrayValues(pattern, 6)).to.deep.equal([
|
|
|
|
0, 1, 2, 3, 0, 1,
|
|
|
|
]);
|
2019-09-06 03:54:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does the down pattern", () => {
|
2022-01-05 05:12:40 +00:00
|
|
|
const pattern = PatternGenerator(4, "down");
|
2024-05-03 18:31:14 +00:00
|
|
|
expect(getArrayValues(pattern, 6)).to.deep.equal([
|
|
|
|
3, 2, 1, 0, 3, 2,
|
|
|
|
]);
|
2019-09-06 03:54:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does the upDown pattern", () => {
|
2022-01-05 05:12:40 +00:00
|
|
|
const pattern = PatternGenerator(4, "upDown");
|
2024-05-03 18:31:14 +00:00
|
|
|
expect(getArrayValues(pattern, 10)).to.deep.equal([
|
|
|
|
0, 1, 2, 3, 2, 1, 0, 1, 2, 3,
|
|
|
|
]);
|
2019-09-06 03:54:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does the downUp pattern", () => {
|
2022-01-05 05:12:40 +00:00
|
|
|
const pattern = PatternGenerator(4, "downUp");
|
2024-05-03 18:31:14 +00:00
|
|
|
expect(getArrayValues(pattern, 10)).to.deep.equal([
|
|
|
|
3, 2, 1, 0, 1, 2, 3, 2, 1, 0,
|
|
|
|
]);
|
2019-09-06 03:54:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does the alternateUp pattern", () => {
|
2022-01-05 05:12:40 +00:00
|
|
|
const pattern = PatternGenerator(5, "alternateUp");
|
2024-05-03 18:31:14 +00:00
|
|
|
expect(getArrayValues(pattern, 10)).to.deep.equal([
|
|
|
|
0, 2, 1, 3, 2, 4, 3, 0, 2, 1,
|
|
|
|
]);
|
2019-09-06 03:54:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("does the alternateDown pattern", () => {
|
2022-01-05 05:12:40 +00:00
|
|
|
const pattern = PatternGenerator(5, "alternateDown");
|
2024-05-03 18:31:14 +00:00
|
|
|
expect(getArrayValues(pattern, 10)).to.deep.equal([
|
|
|
|
4, 2, 3, 1, 2, 0, 1, 4, 2, 3,
|
|
|
|
]);
|
2019-09-06 03:54:26 +00:00
|
|
|
});
|
|
|
|
|
2020-03-02 05:53:57 +00:00
|
|
|
it("outputs random elements from the values", () => {
|
2022-01-05 05:12:40 +00:00
|
|
|
const numValues = 5;
|
|
|
|
const pattern = PatternGenerator(numValues, "random");
|
2019-09-06 03:54:26 +00:00
|
|
|
for (let i = 0; i < 10; i++) {
|
2024-05-03 18:31:14 +00:00
|
|
|
expect(pattern.next().value)
|
|
|
|
.to.be.at.least(0)
|
|
|
|
.and.at.most(numValues - 1);
|
2019-09-06 03:54:26 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does randomOnce pattern", () => {
|
2022-01-05 05:12:40 +00:00
|
|
|
const pattern = PatternGenerator(5, "randomOnce");
|
2024-05-03 18:31:14 +00:00
|
|
|
expect(getArrayValues(pattern, 10).sort()).to.deep.equal([
|
|
|
|
0, 0, 1, 1, 2, 2, 3, 3, 4, 4,
|
|
|
|
]);
|
2020-03-08 16:10:39 +00:00
|
|
|
});
|
2024-05-03 18:31:14 +00:00
|
|
|
|
2020-03-08 16:10:39 +00:00
|
|
|
it("randomly walks up or down 1 step without repeating", () => {
|
2020-03-02 05:53:57 +00:00
|
|
|
const values = [0, 1, 2, 3, 4];
|
2022-01-05 05:12:40 +00:00
|
|
|
const pattern = PatternGenerator(5, "randomWalk");
|
|
|
|
let currentIndex = pattern.next().value;
|
2020-03-02 05:53:57 +00:00
|
|
|
for (let i = 0; i < 10; i++) {
|
2022-01-05 05:12:40 +00:00
|
|
|
const nextIndex = pattern.next().value;
|
2020-03-08 16:10:39 +00:00
|
|
|
expect(currentIndex).to.not.equal(nextIndex);
|
|
|
|
// change always equals 1
|
|
|
|
expect(Math.abs(currentIndex - nextIndex)).to.equal(1);
|
|
|
|
currentIndex = nextIndex;
|
2020-03-02 05:53:57 +00:00
|
|
|
}
|
2019-09-06 03:54:26 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|