Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update all-your-base.spec.js #137

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 30 additions & 112 deletions exercises/practice/all-your-base/all-your-base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,117 +58,35 @@ describe("Converter", () => {
}
});

test("single bit one to decimal", () => {
let [results, rc] = convert([1], 2, 10);
expect(rc).toEqual(0);
expect(results).toEqual([1]);
});

xtest("binary to single decimal", () => {
let [results, rc] = convert([1, 0, 1], 2, 10);
expect(rc).toEqual(0);
expect(results).toEqual([5]);
});

xtest("single decimal to binary", () => {
let [results, rc] = convert([5], 10, 2);
expect(rc).toEqual(0);
expect(results).toEqual([1, 0, 1]);
});

xtest("binary to multiple decimal", () => {
let [results, rc] = convert([1, 0, 1, 0, 1, 0], 2, 10);
expect(rc).toEqual(0);
expect(results).toEqual([4, 2]);
});

xtest("decimal to binary", () => {
let [results, rc] = convert([4, 2], 10, 2);
expect(rc).toEqual(0);
expect(results).toEqual([1, 0, 1, 0, 1, 0]);
});

xtest("trinary to hexadecimal", () => {
let [results, rc] = convert([1, 1, 2, 0], 3, 16);
expect(rc).toEqual(0);
expect(results).toEqual([2, 10]);
});

xtest("hexadecimal to trinary", () => {
let [results, rc] = convert([2, 10], 16, 3);
expect(rc).toEqual(0);
expect(results).toEqual([1, 1, 2, 0]);
});

xtest("15-bit integer", () => {
let [results, rc] = convert([3, 46, 60], 97, 73);
expect(rc).toEqual(0);
expect(results).toEqual([6, 10, 45]);
});

xtest("empty list", () => {
let [_, rc] = convert([], 2, 10);
expect(rc).toEqual(-1);
});

xtest("single zero", () => {
let [results, rc] = convert([0], 10, 2);
expect(rc).toEqual(0);
expect(results).toEqual([0]);
});

xtest("multiple zeros", () => {
let [_, rc] = convert([0, 0, 0], 10, 2);
expect(rc).toEqual(-1);
});

xtest("leading zeros", () => {
let [_, rc] = convert([0, 6, 0], 7, 10);
expect(rc).toEqual(-1);
});

xtest("negative digit", () => {
let [_, rc] = convert([1, -1, 1, 0, 1, 0], 2, 10);
expect(rc).toEqual(-1);
});

xtest("invalid positive digit", () => {
let [_, rc] = convert([1, 2, 1, 0, 1, 0], 2, 10);
expect(rc).toEqual(-1);
});

xtest("first base is one", () => {
let [_, rc] = convert([], 1, 10);
expect(rc).toEqual(-2);
});

xtest("second base is one", () => {
let [_, rc] = convert([1, 0, 1, 0, 1, 0], 2, 1);
expect(rc).toEqual(-3);
});

xtest("first base is zero", () => {
let [_, rc] = convert([], 0, 10);
expect(rc).toEqual(-2);
});

xtest("second base is zero", () => {
let [_, rc] = convert([7], 10, 0);
expect(rc).toEqual(-3);
});

xtest("first base is negative", () => {
let [_, rc] = convert([1], -2, 10);
expect(rc).toEqual(-2);
});

xtest("second base is negative", () => {
let [_, rc] = convert([1], 2, -7);
expect(rc).toEqual(-3);
});

xtest("both bases are negative", () => {
let [_, rc] = convert([1], -2, -7);
expect(rc).toEqual(-2);
const testCases = [
{ description: "single bit one to decimal", digits: [1], inputBase: 2, outputBase: 10, expected: [1] },
{ description: "binary to single decimal", digits: [1, 0, 1], inputBase: 2, outputBase: 10, expected: [5] },
{ description: "single decimal to binary", digits: [5], inputBase: 10, outputBase: 2, expected: [1, 0, 1] },
{ description: "binary to multiple decimal", digits: [1, 0, 1, 0, 1, 0], inputBase: 2, outputBase: 10, expected: [4, 2] },
{ description: "decimal to binary", digits: [4, 2], inputBase: 10, outputBase: 2, expected: [1, 0, 1, 0, 1, 0] },
{ description: "trinary to hexadecimal", digits: [1, 1, 2, 0], inputBase: 3, outputBase: 16, expected: [2, 10] },
{ description: "hexadecimal to trinary", digits: [2, 10], inputBase: 16, outputBase: 3, expected: [1, 1, 2, 0] },
{ description: "15-bit integer", digits: [3, 46, 60], inputBase: 97, outputBase: 73, expected: [6, 10, 45] },
{ description: "empty list", digits: [], inputBase: 2, outputBase: 10, expected: [], expectedErrorCode: -1 },
{ description: "single zero", digits: [0], inputBase: 10, outputBase: 2, expected: [0] },
{ description: "multiple zeros", digits: [0, 0, 0], inputBase: 10, outputBase: 2, expected: [], expectedErrorCode: -1 },
{ description: "leading zeros", digits: [0, 6, 0], inputBase: 7, outputBase: 10, expected: [], expectedErrorCode: -1 },
{ description: "negative digit", digits: [1, -1, 1, 0, 1, 0], inputBase: 2, outputBase: 10, expected: [], expectedErrorCode: -1 },
{ description: "invalid positive digit", digits: [1, 2, 1, 0, 1, 0], inputBase: 2, outputBase: 10, expected: [], expectedErrorCode: -1 },
{ description: "first base is one", digits: [], inputBase: 1, outputBase: 10, expected: [], expectedErrorCode: -2 },
{ description: "second base is one", digits: [1, 0, 1, 0, 1, 0], inputBase: 2, outputBase: 1, expected: [], expectedErrorCode: -3 },
{ description: "first base is zero", digits: [], inputBase: 0, outputBase: 10, expected: [], expectedErrorCode: -2 },
{ description: "second base is zero", digits: [7], inputBase: 10, outputBase: 0, expected: [], expectedErrorCode: -3 },
{ description: "first base is negative", digits: [1], inputBase: -2, outputBase: 10, expected: [], expectedErrorCode: -2 },
{ description: "second base is negative", digits: [1], inputBase: 2, outputBase: -7, expected: [], expectedErrorCode: -3 },
{ description: "both bases are negative", digits: [1], inputBase: -2, outputBase: -7, expected: [], expectedErrorCode: -2 }
];

testCases.forEach(({ description, digits, inputBase, outputBase, expected, expectedErrorCode }) => {
test(description, () => {
let [results, rc] = convert(digits, inputBase, outputBase);
expect(rc).toEqual(expectedErrorCode || 0);
expect(results).toEqual(expected);
});
});
});