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

Add unit tests to Max Product of 3 numbers problem #26

Merged
merged 2 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/_Problems_/max-product-of-3-numbers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ function maxProductof3NumbersII(arr) {

return p1 > p2 ? p1 : p2;
}

module.exports = { maxProductof3Numbers, maxProductof3NumbersII };
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { maxProductof3Numbers, maxProductof3NumbersII } = require(".");

describe("Maximum Product of three numbers", () => {
it("throws an error with no Array is passed", () => {
expect(() => {
maxProductof3Numbers("xunda");
}).toThrowError();
expect(() => {
maxProductof3NumbersII("xunda");
}).toThrowError();
});

it("returns the product of an array with 3 numbers", () => {
expect(maxProductof3Numbers([1, 2, 3])).toEqual(6);
expect(maxProductof3NumbersII([1, 2, 3])).toEqual(6);
});

it("returns the product of an array with positive and negative numbers", () => {
expect(maxProductof3Numbers([-10, -10, 2, 3])).toEqual(300);
expect(maxProductof3NumbersII([-10, -10, 2, 3])).toEqual(300);
});

it("returns the product of an array with negative numbers", () => {
expect(maxProductof3Numbers([-10, -1, -2, -10])).toEqual(-20);
expect(maxProductof3NumbersII([-10, -1, -2, -10])).toEqual(-20);
});

it("returns the proper calculation if the array is large", () => {
const largeArray = [100, 100, 100, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 45, 4, 3, 7, 8, 1, 3, 7, 8, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 1, 12, 3, 45, 4, 3, 7, 8, 1, 3, 7, 8, 1, 4, 3, 7, 8, 1, 3, 7, 8, 45, 4, 3, 7, 8, 1, 3, 7, 8];
expect(maxProductof3Numbers(largeArray)).toEqual(100 * 100 * 100);
expect(maxProductof3NumbersII(largeArray)).toEqual(100 * 100 * 100);
});

it("returns an error if there are less than 3 numbers", () => {
expect(() => {
maxProductof3Numbers([-10, -1]);
}).toThrowError();
});
});