Cleanup tests.

Moved service_node_test to assert to keep consistency.
pull/125/head
Mikunj 6 years ago
parent f9147663d5
commit 4eda13733a

@ -8,88 +8,98 @@ const {
greaterThan, greaterThan,
} = pow; } = pow;
describe('Proof of Work Worker', () => { describe('Proof of Work', () => {
it('should increment a Uint8Array nonce correctly', () => { describe('#incrementNonce', () => {
const arr1Before = new Uint8Array([0,0,0,0,0,0,0,0]); it('should increment a Uint8Array nonce correctly', () => {
const arr1After = incrementNonce(arr1Before); const arr1Before = new Uint8Array([0,0,0,0,0,0,0,0]);
assert.strictEqual(arr1After[0], 0); const arr1After = incrementNonce(arr1Before);
assert.strictEqual(arr1After[1], 0); assert.strictEqual(arr1After[0], 0);
assert.strictEqual(arr1After[2], 0); assert.strictEqual(arr1After[1], 0);
assert.strictEqual(arr1After[3], 0); assert.strictEqual(arr1After[2], 0);
assert.strictEqual(arr1After[4], 0); assert.strictEqual(arr1After[3], 0);
assert.strictEqual(arr1After[5], 0); assert.strictEqual(arr1After[4], 0);
assert.strictEqual(arr1After[6], 0); assert.strictEqual(arr1After[5], 0);
assert.strictEqual(arr1After[7], 1); assert.strictEqual(arr1After[6], 0);
}); assert.strictEqual(arr1After[7], 1);
});
it('should increment a Uint8Array nonce correctly', () => { it('should increment a Uint8Array nonce correctly in a loop', () => {
let arr = new Uint8Array([0,0,0,0,0,0,0,0]); let arr = new Uint8Array([0,0,0,0,0,0,0,0]);
assert.deepEqual(incrementNonce(arr), new Uint8Array([0,0,0,0,0,0,0,1])); assert.deepEqual(incrementNonce(arr), new Uint8Array([0,0,0,0,0,0,0,1]));
arr = new Uint8Array([0,0,0,0,0,0,0,0]); arr = new Uint8Array([0,0,0,0,0,0,0,0]);
for(let i = 0; i <= 255; i += 1) { for(let i = 0; i <= 255; i += 1) {
arr = incrementNonce(arr); arr = incrementNonce(arr);
} }
assert.deepEqual(arr, new Uint8Array([0,0,0,0,0,0,1,0])); assert.deepEqual(arr, new Uint8Array([0,0,0,0,0,0,1,0]));
arr = new Uint8Array([255,255,255,255,255,255,255,255]); arr = new Uint8Array([255,255,255,255,255,255,255,255]);
assert.deepEqual(incrementNonce(arr), new Uint8Array([0,0,0,0,0,0,0,0])); assert.deepEqual(incrementNonce(arr), new Uint8Array([0,0,0,0,0,0,0,0]));
});
}); });
it('should calculate a correct difficulty target', () => { describe('#calcTarget', () => {
// These values will need to be updated if we adjust the difficulty settings it('should calculate a correct difficulty target', () => {
let payloadLen = 625; // These values will need to be updated if we adjust the difficulty settings
const ttl = 86400; let payloadLen = 625;
let expectedTarget = new Uint8Array([0,4,119,164,35,224,222,64]); const ttl = 86400;
let expectedTarget = new Uint8Array([0,4,119,164,35,224,222,64]);
let actualTarget = calcTarget(ttl, payloadLen, 10); let actualTarget = calcTarget(ttl, payloadLen, 10);
assert.deepEqual(actualTarget, expectedTarget); assert.deepEqual(actualTarget, expectedTarget);
payloadLen = 6597; payloadLen = 6597;
expectedTarget = new Uint8Array([0,0,109,145,174,146,124,3]); expectedTarget = new Uint8Array([0,0,109,145,174,146,124,3]);
actualTarget = calcTarget(ttl, payloadLen, 10); actualTarget = calcTarget(ttl, payloadLen, 10);
assert.deepEqual(actualTarget, expectedTarget); assert.deepEqual(actualTarget, expectedTarget);
});
}); });
it('should correclty compare two Uint8Arrays', () => { describe('#greaterThan', () => {
let arr1 = new Uint8Array([0,0,0,0,0,0,0,0,0,1]); it('should correclty compare two Uint8Arrays', () => {
let arr2 = new Uint8Array([0,0,0,0,0,0,0,0,0,1]); let arr1 = new Uint8Array([0,0,0,0,0,0,0,0,0,1]);
assert.isFalse(greaterThan(arr1, arr2)) let arr2 = new Uint8Array([0,0,0,0,0,0,0,0,0,1]);
arr1 = new Uint8Array([0,0,0,0,0,0,0,0,0,2]); assert.isFalse(greaterThan(arr1, arr2))
arr2 = new Uint8Array([0,0,0,0,0,0,0,0,0,1]); arr1 = new Uint8Array([0,0,0,0,0,0,0,0,0,2]);
assert.isTrue(greaterThan(arr1, arr2)) arr2 = new Uint8Array([0,0,0,0,0,0,0,0,0,1]);
arr1 = new Uint8Array([255,255,255,255,255,255,255,255,255,255]); assert.isTrue(greaterThan(arr1, arr2))
arr2 = new Uint8Array([255,255,255,255,255,255,255,255,255,254]); arr1 = new Uint8Array([255,255,255,255,255,255,255,255,255,255]);
assert.isTrue(greaterThan(arr1, arr2)) arr2 = new Uint8Array([255,255,255,255,255,255,255,255,255,254]);
arr1 = new Uint8Array([254,255,255,255,255,255,255,255,255,255]); assert.isTrue(greaterThan(arr1, arr2))
arr2 = new Uint8Array([255,255,255,255,255,255,255,255,255,255]); arr1 = new Uint8Array([254,255,255,255,255,255,255,255,255,255]);
assert.isFalse(greaterThan(arr1, arr2)); arr2 = new Uint8Array([255,255,255,255,255,255,255,255,255,255]);
arr1 = new Uint8Array([0]); assert.isFalse(greaterThan(arr1, arr2));
arr2 = new Uint8Array([0,0]); arr1 = new Uint8Array([0]);
assert.isFalse(greaterThan(arr1, arr2)) arr2 = new Uint8Array([0,0]);
assert.isFalse(greaterThan(arr1, arr2))
});
}); });
it('should correclty convert a Uint8Array to a base64 string', () => { describe('#bufferToBase64', () => {
let arr = new Uint8Array([1,2,3]); it('should correclty convert a Uint8Array to a base64 string', () => {
let expected = 'AQID'; let arr = new Uint8Array([1,2,3]);
assert.strictEqual(bufferToBase64(arr), expected); let expected = 'AQID';
arr = new Uint8Array([123,25,3,121,45,87,24,111]); assert.strictEqual(bufferToBase64(arr), expected);
expected = 'exkDeS1XGG8='; arr = new Uint8Array([123,25,3,121,45,87,24,111]);
assert.strictEqual(bufferToBase64(arr), expected); expected = 'exkDeS1XGG8=';
arr = new Uint8Array([]); assert.strictEqual(bufferToBase64(arr), expected);
expected = ''; arr = new Uint8Array([]);
assert.strictEqual(bufferToBase64(arr), expected); expected = '';
assert.strictEqual(bufferToBase64(arr), expected);
});
}); });
it('should correclty convert a BigInteger to a Uint8Array', () => { describe('#bigIntToUint8Array', () => {
let bigInt = JSBI.BigInt(Number.MAX_SAFE_INTEGER); it('should correclty convert a BigInteger to a Uint8Array', () => {
let expected = new Uint8Array([0, 31, 255, 255, 255, 255, 255, 255]); let bigInt = JSBI.BigInt(Number.MAX_SAFE_INTEGER);
assert.deepEqual(bigIntToUint8Array(bigInt), expected); let expected = new Uint8Array([0, 31, 255, 255, 255, 255, 255, 255]);
bigInt = JSBI.BigInt('0'); assert.deepEqual(bigIntToUint8Array(bigInt), expected);
expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]); bigInt = JSBI.BigInt('0');
assert.deepEqual(bigIntToUint8Array(bigInt), expected); expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]);
bigInt = JSBI.BigInt('255'); assert.deepEqual(bigIntToUint8Array(bigInt), expected);
expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 255]); bigInt = JSBI.BigInt('255');
assert.deepEqual(bigIntToUint8Array(bigInt), expected); expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 255]);
bigInt = JSBI.BigInt('256'); assert.deepEqual(bigIntToUint8Array(bigInt), expected);
expected = new Uint8Array([0, 0, 0, 0, 0, 0, 1, 0]); bigInt = JSBI.BigInt('256');
assert.deepEqual(bigIntToUint8Array(bigInt), expected); expected = new Uint8Array([0, 0, 0, 0, 0, 0, 1, 0]);
assert.deepEqual(bigIntToUint8Array(bigInt), expected);
});
}); });
}); });

@ -1,40 +1,50 @@
/* global libloki, chai */ /* global libloki, assert */
describe('ServiceNodes', () => { describe('ServiceNodes', () => {
describe('#consolidateLists', () => { describe('#consolidateLists', () => {
it('should throw when provided a non-iterable list', () => { it('should throw when provided a non-iterable list', () => {
chai.expect(() => libloki.serviceNodes.consolidateLists(null, 1)).to.throw(); assert.throws(() => libloki.serviceNodes.consolidateLists(null, 1), Error);
}); });
it('should throw when provided a non-iterable item in the list', () => { it('should throw when provided a non-iterable item in the list', () => {
chai.expect(() => libloki.serviceNodes.consolidateLists([1, 2, 3], 1)).to.throw(); assert.throws(() => libloki.serviceNodes.consolidateLists([1, 2, 3], 1), Error);
}); });
it('should throw when provided a non-number threshold', () => { it('should throw when provided a non-number threshold', () => {
chai.expect(() => libloki.serviceNodes.consolidateLists([], 'a')).to.throw(); assert.throws(
() => libloki.serviceNodes.consolidateLists([], 'a'),
'Provided threshold is not a number'
);
}); });
it('should return an empty array when the input is an empty array', () => { it('should return an empty array when the input is an empty array', () => {
const result = libloki.serviceNodes.consolidateLists([]); const result = libloki.serviceNodes.consolidateLists([]);
chai.expect(result).to.deep.equal([]); assert.deepEqual(result, []);
}); });
it('should return the input when only 1 list is provided', () => { it('should return the input when only 1 list is provided', () => {
const result = libloki.serviceNodes.consolidateLists([['a', 'b', 'c']]); const result = libloki.serviceNodes.consolidateLists([['a', 'b', 'c']]);
chai.expect(result).to.deep.equal(['a', 'b', 'c']); assert.deepEqual(result, ['a', 'b', 'c']);
}); });
it('should return the union of all lists when threshold is 0', () => { it('should return the union of all lists when threshold is 0', () => {
const result = libloki.serviceNodes.consolidateLists([ const result = libloki.serviceNodes.consolidateLists([
['a', 'b', 'c', 'h'], ['a', 'b', 'c', 'h'],
['d', 'e', 'f', 'g'], ['d', 'e', 'f', 'g'],
['g', 'h'], ['g', 'h'],
], 0); ], 0);
chai.expect(result.sort()).to.deep.equal(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']); assert.deepEqual(result.sort(), ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);
}); });
it('should return the intersection of all lists when threshold is 1', () => { it('should return the intersection of all lists when threshold is 1', () => {
const result = libloki.serviceNodes.consolidateLists([ const result = libloki.serviceNodes.consolidateLists([
['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'd'],
['a', 'e', 'f', 'g'], ['a', 'e', 'f', 'g'],
['a', 'h'], ['a', 'h'],
], 1); ], 1);
chai.expect(result).to.deep.equal(['a']); assert.deepEqual(result, ['a']);
}); });
it('should return the elements that have an occurence >= the provided threshold', () => { it('should return the elements that have an occurence >= the provided threshold', () => {
const result = libloki.serviceNodes.consolidateLists([ const result = libloki.serviceNodes.consolidateLists([
['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
@ -42,8 +52,9 @@ describe('ServiceNodes', () => {
['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['a', 'b', 'c', 'd', 'e', 'f', 'g'],
['a', 'b', 'c', 'd', 'e', 'g', 'h'], ['a', 'b', 'c', 'd', 'e', 'g', 'h'],
], 3/4); ], 3/4);
chai.expect(result).to.deep.equal(['a', 'b', 'c', 'd', 'e', 'f', 'g']); assert.deepEqual(result, ['a', 'b', 'c', 'd', 'e', 'f', 'g']);
}); });
it('should work with sets as well', () => { it('should work with sets as well', () => {
const result = libloki.serviceNodes.consolidateLists(new Set([ const result = libloki.serviceNodes.consolidateLists(new Set([
new Set(['a', 'b', 'c', 'd', 'e', 'f', 'g']), new Set(['a', 'b', 'c', 'd', 'e', 'f', 'g']),
@ -51,7 +62,7 @@ describe('ServiceNodes', () => {
new Set(['a', 'b', 'c', 'd', 'e', 'f', 'g']), new Set(['a', 'b', 'c', 'd', 'e', 'f', 'g']),
new Set(['a', 'b', 'c', 'd', 'e', 'g', 'h']), new Set(['a', 'b', 'c', 'd', 'e', 'g', 'h']),
]), 3/4); ]), 3/4);
chai.expect(result).to.deep.equal(['a', 'b', 'c', 'd', 'e', 'f', 'g']); assert.deepEqual(result, ['a', 'b', 'c', 'd', 'e', 'f', 'g']);
}); });
}); });
}); });

Loading…
Cancel
Save