diff --git a/int10.js b/int10.js index f572d03..1859bdd 100644 --- a/int10.js +++ b/int10.js @@ -13,7 +13,8 @@ // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -let max = 10000000000000; // biggest 10^n integer that can still fit 2^53 when multiplied by 256 +/** Biggest 10^n integer that can still fit 2^53 when multiplied by 256. */ +const max = 10000000000000; export class Int10 { /** @@ -26,11 +27,13 @@ export class Int10 { /** * Multiply value by m and add c. - * @param {number} m - multiplier, must be < =256 - * @param {number} c - value to add + * @param {number} m - multiplier, must be 0=0 */ mulAdd(m, c) { + // assert(m > 0) // assert(m <= 256) + // assert(c >= 0) let b = this.buf, l = b.length, i, t; @@ -71,11 +74,12 @@ export class Int10 { /** * Convert to decimal string representation. - * @param {*} base - optional value, only value accepted is 10 + * @param {number} [base=10] - optional value, only value accepted is 10 + * @returns {string} The decimal string representation. */ - toString(base) { - if ((base || 10) != 10) - throw 'only base 10 is supported'; + toString(base = 10) { + if (base != 10) + throw new Error('only base 10 is supported'); let b = this.buf, s = b[b.length - 1].toString(); for (let i = b.length - 2; i >= 0; --i) @@ -86,6 +90,7 @@ export class Int10 { /** * Convert to Number value representation. * Will probably overflow 2^53 and thus become approximate. + * @returns {number} The numeric value. */ valueOf() { let b = this.buf, @@ -97,6 +102,7 @@ export class Int10 { /** * Return value as a simple Number (if it is <= 10000000000000), or return this. + * @returns {number | Int10} The simplified value. */ simplify() { let b = this.buf; diff --git a/test.js b/test.js index ec3c450..cd88878 100755 --- a/test.js +++ b/test.js @@ -3,6 +3,7 @@ import { ASN1, Stream } from './asn1.js'; import { Hex } from './hex.js'; import { Base64 } from './base64.js'; +import { Int10 } from './int10.js'; const all = (process.argv[2] == 'all'); @@ -190,6 +191,27 @@ tests.push(new Tests(function (t) { 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQR\nSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456w==', ])); +tests.push(new Tests(function (t) { + this.row = (0|this.row) + 1; + this.num = this.num || new Int10(); + this.num.mulAdd(t[0], t[1]); + this.checkResult(this.num.toString(), t[2], 'Int10 row ' + this.row); +}, [ + [0, 1000000000, '1000000000'], + [256, 23, '256000000023'], + [256, 23, '65536000005911'], + [256, 23, '16777216001513239'], + [256, 23, '4294967296387389207'], + [256, 23, '1099511627875171637015'], + [256, 23, '281474976736043939075863'], + [253, 1, '71213169114219116586193340'], + [253, 1, '18016931785897436496306915021'], + [253, 1, '4558283741832051433565649500314'], + [253, 1, '1153245786683509012692109323579443'], + [253, 1, '291771184030927780211103658865599080'], + [1, 0, '291771184030927780211103658865599080'], +])); + for (const t of tests) t.checkAll();