Skip to content

Commit

Permalink
tests: added testcases for twos compliments
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Apr 5, 2023
1 parent 9cb2a9d commit 9060ded
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src.ts/_tests/test-utils-maths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from "assert";

import {
isError,
fromTwos, toTwos,
getBigInt, getNumber, toBeArray, toBeHex, toQuantity,
} from "../index.js";

Expand Down Expand Up @@ -178,3 +179,28 @@ describe("Tests Bad Math Values", function() {
});
});
});

describe("Tests Twos Compliemnts Functions", function() {
const tests = [
{ width: 8, value: 0, twos: 0 },
{ width: 8, value: 1, twos: 1 },
{ width: 8, value: -1, twos: 0xff },
{ width: 8, value: 127, twos: 127 },
{ width: 8, value: -128, twos: 0x80 },
];

for (const { twos, width, value } of tests) {
it(`computes twos compliment values: ${ value }[${ width } bits]`, function() {
const result = toTwos(value, width);
assert.equal(result, twos);
});
}

for (const { twos, width, value } of tests) {
it(`computes values from twos compliment: ${ value }[${ width } bits]`, function() {
const result = fromTwos(twos, width);
assert.equal(result, value);
});
}
});

0 comments on commit 9060ded

Please sign in to comment.