-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement dynamic string size. (#110)
* Implement dynamic string size. * Add tests for string parameters.
- Loading branch information
1 parent
cbef86c
commit b44b981
Showing
3 changed files
with
101 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,77 @@ | ||
const crypto = require('crypto'); | ||
|
||
let addon =require('./dist'); | ||
const assert = require('assert'); | ||
|
||
assert.equal(addon.hello(2),"hello world 2"); | ||
|
||
assert.strictEqual(addon.hello(2),"hello world 2"); | ||
|
||
assert.throws( () => addon.hello("hello"),{ | ||
message: 'invalid type, expected: number, actual: string' | ||
}); | ||
|
||
|
||
|
||
assert.throws(() => addon.hello(),{ | ||
message: 'expected argument of type: i32' | ||
}); | ||
}); | ||
|
||
assert.equal(addon.sum(1,2),3); | ||
assert.strictEqual(addon.sum(1,2),3); | ||
|
||
assert.throws( () => addon.minMax(10,0),{ | ||
message: 'first arg is greater', | ||
message: 'first arg is greater', | ||
}); | ||
|
||
assert.equal(addon.minMax(1,2),3); | ||
assert.strictEqual(addon.minMax(1,2),3); | ||
|
||
assert.strictEqual(addon.multiply(2,5),10); | ||
|
||
assert.strictEqual(addon.sum2(10),10); | ||
assert.strictEqual(addon.sum2(5,100),105); | ||
|
||
const stringShort = _generateForCustomCharacters(5); | ||
const stringMedium = _generateForCustomCharacters(100); | ||
const stringLong = _generateForCustomCharacters(2000); | ||
const strings = new Set([stringShort, stringMedium, stringLong]); | ||
|
||
assert.strictEqual(addon.string(stringShort), stringShort); | ||
assert.strictEqual(addon.string(stringMedium), stringMedium); | ||
assert.strictEqual(addon.string(stringLong), stringLong); | ||
|
||
for(const string1 in strings) { | ||
for(const string2 in strings) { | ||
assert.strictEqual(addon.string(string1), string2); | ||
} | ||
} | ||
|
||
console.log("function tests succeed"); | ||
|
||
/* | ||
* attribution: https://github.com/sindresorhus/crypto-random-string | ||
* MIT License | ||
* Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com) | ||
*/ | ||
function _generateForCustomCharacters(length, characters) { | ||
characters = characters || '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'.split(''); | ||
// Generating entropy is faster than complex math operations, so we use the simplest way | ||
const characterCount = characters.length; | ||
const maxValidSelector = (Math.floor(0x10000 / characterCount) * characterCount) - 1; // Using values above this will ruin distribution when using modular division | ||
const entropyLength = 2 * Math.ceil(1.1 * length); // Generating a bit more than required so chances we need more than one pass will be really low | ||
let string = ''; | ||
let stringLength = 0; | ||
|
||
while (stringLength < length) { // In case we had many bad values, which may happen for character sets of size above 0x8000 but close to it | ||
const entropy = crypto.randomBytes(entropyLength); | ||
let entropyPosition = 0; | ||
|
||
assert.equal(addon.multiply(2,5),10); | ||
while (entropyPosition < entropyLength && stringLength < length) { | ||
const entropyValue = entropy.readUInt16LE(entropyPosition); | ||
entropyPosition += 2; | ||
if (entropyValue > maxValidSelector) { // Skip values which will ruin distribution when using modular division | ||
continue; | ||
} | ||
|
||
assert.equal(addon.sum2(10),10); | ||
assert.equal(addon.sum2(5,100),105); | ||
string += characters[entropyValue % characterCount]; | ||
stringLength++; | ||
} | ||
} | ||
|
||
console.log("function tests succeed"); | ||
return string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters