Skip to content

Commit dfb8a36

Browse files
🎨 style: Assert some documentated assumptions.
⚠️ Some of the added assertions call the measure method which causes a cascade of measure caching behaviour. This may cause problems when removing the assertion for optimized runtime. In general, I should find an elegant way to test with and without the assertions.
1 parent 791c39e commit dfb8a36

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

src/1-digit/1-One.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {Digit, Two, Three} from '.';
22
import {node2, node3} from '../2-node';
33
import {Split} from '../0-core';
44

5+
import assert from 'assert';
6+
57
export function One(a) {
68
this.a = a;
79
this.v = null;
@@ -43,9 +45,10 @@ One.prototype.node = function (M) {
4345
};
4446

4547
/**
46-
* It is assumed that p(|this|) is true.
48+
* It is assumed that p(i+|this|) is true.
4749
*/
4850
One.prototype.splitDigit = function (p, i, M) {
51+
assert(p(M.plus(i, this.measure(M)))); // /!\ Potential Heisenbug generator.
4952
return new Split([], this.a, []);
5053
};
5154

@@ -54,6 +57,7 @@ One.prototype._nodes = function (M, other) {
5457
if (other instanceof Two) return [node3(M, this.a, other.a, other.b)];
5558
if (other instanceof Three)
5659
return [node2(M, this.a, other.a), node2(M, other.b, other.c)];
60+
assert(other instanceof Digit);
5761
return [node3(M, this.a, other.a, other.b), node2(M, other.c, other.d)];
5862
};
5963

src/1-digit/2-Two.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {Digit, One, Three} from '.';
22
import {node2, node3} from '../2-node';
33
import {Split} from '../0-core';
44

5+
import assert from 'assert';
6+
57
export function Two(a, b) {
68
this.a = a;
79
this.b = b;
@@ -46,9 +48,10 @@ Two.prototype.node = function (M) {
4648
};
4749

4850
/**
49-
* It is assumed that p(|this|) is true.
51+
* It is assumed that p(i+|this|) is true.
5052
*/
5153
Two.prototype.splitDigit = function (p, i, M) {
54+
assert(p(M.plus(i, this.measure(M)))); // /!\ Potential Heisenbug generator.
5255
i = M.plus(i, M.measure(this.a));
5356
if (p(i)) return new Split([], this.a, [this.b]);
5457
return new Split([this.a], this.b, []);
@@ -60,6 +63,7 @@ Two.prototype._nodes = function (M, other) {
6063
return [node2(M, this.a, this.b), node2(M, other.a, other.b)];
6164
if (other instanceof Three)
6265
return [node3(M, this.a, this.b, other.a), node2(M, other.b, other.c)];
66+
assert(other instanceof Digit);
6367
return [
6468
node3(M, this.a, this.b, other.a),
6569
node3(M, other.b, other.c, other.d)

src/1-digit/3-Three.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {Digit, One, Two, Four} from '.';
22
import {node2, node3} from '../2-node';
33
import {Split} from '../0-core';
44

5+
import assert from 'assert';
6+
57
export function Three(a, b, c) {
68
this.a = a;
79
this.b = b;
@@ -49,9 +51,10 @@ Three.prototype.node = function (M) {
4951
};
5052

5153
/**
52-
* It is assumed that p(|this|) is true.
54+
* It is assumed that p(i+|this|) is true.
5355
*/
5456
Three.prototype.splitDigit = function (p, i, M) {
57+
assert(p(M.plus(i, this.measure(M)))); // /!\ Potential Heisenbug generator.
5558
i = M.plus(i, M.measure(this.a));
5659
if (p(i)) return new Split([], this.a, [this.b, this.c]);
5760
i = M.plus(i, M.measure(this.b));
@@ -69,6 +72,7 @@ Three.prototype._nodes = function (M, other) {
6972
node3(M, this.a, this.b, this.c),
7073
node3(M, other.a, other.b, other.c)
7174
];
75+
assert(other instanceof Four);
7276
return [
7377
node3(M, this.a, this.b, this.c),
7478
node2(M, other.a, other.b),

src/1-digit/4-Four.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {Digit, One, Two, Three} from '.';
22
import {node2, node3} from '../2-node';
33
import {Split} from '../0-core';
44

5+
import assert from 'assert';
6+
57
export function Four(a, b, c, d) {
68
this.a = a;
79
this.b = b;
@@ -50,9 +52,10 @@ Four.prototype.node = function (M) {
5052
};
5153

5254
/**
53-
* It is assumed that p(|this|) is true.
55+
* It is assumed that p(i+|this|) is true.
5456
*/
5557
Four.prototype.splitDigit = function (p, i, M) {
58+
assert(p(M.plus(i, this.measure(M)))); // /!\ Potential Heisenbug generator.
5659
i = M.plus(i, M.measure(this.a));
5760
if (p(i)) return new Split([], this.a, [this.b, this.c, this.d]);
5861
i = M.plus(i, M.measure(this.b));
@@ -76,6 +79,7 @@ Four.prototype._nodes = function (M, other) {
7679
node2(M, this.d, other.a),
7780
node2(M, other.b, other.c)
7881
];
82+
assert(other instanceof Four);
7983
return [
8084
node3(M, this.a, this.b, this.c),
8185
node3(M, this.d, other.a, other.b),

src/3-tree/implementations/2-Deep.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ Deep.prototype[Symbol.iterator] = function* () {
121121
};
122122

123123
/**
124-
* It is assumed that p(|this|) is true.
124+
* It is assumed that p(i+|this|) is true.
125125
*/
126126
Deep.prototype.splitTree = function (p, i) {
127+
assert(p(this.M.plus(i, this.measure())));
128+
127129
const {left, middle, right, M} = this;
128130

129131
// See if the split point is inside the left tree

0 commit comments

Comments
 (0)