Skip to content

Instantly share code, notes, and snippets.

@redaphid
Last active January 18, 2022 08:19
Show Gist options
  • Save redaphid/941da0bf57bff08346d7ff5961ebdbd6 to your computer and use it in GitHub Desktop.
Save redaphid/941da0bf57bff08346d7ff5961ebdbd6 to your computer and use it in GitHub Desktop.
atoi() leetcode problem Typescript tests!
import { myAtoi } from "./solution";
describe("myAtoi", () => {
test("Example 1", () => {
const input = "42";
const expected = 42;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
});
test("Example 2", () => {
const input = " -42";
const expected = -42;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
});
test("Example 3", () => {
const input = "4193 with words";
const expected = 4193;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
});
test("Example 4", () => {
const input = "words and 987";
const expected = 0;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
});
test("When the resulting # is too large", () => {
const input = "2147483699948";
const expected = 2147483647;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
})
test("When the resulting # is too small", () => {
const input = "-21474836948";
const expected = -2147483648;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
})
test("When the string has a decimal point", () => {
const input = "3.14159";
const expected = 3;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
})
test("When we get mixed signals re: the sign", () => {
const input = "-+42";
const expected = 0
const actual = myAtoi(input);
expect(actual).toEqual(expected);
})
test("when the string is a mess, with signs and letters", () => {
const input = "00000-42a1234"
const expected = 0;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
})
test("I guess we ignore letters now?", () => {
const input = "42a1378"
const expected = 42;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
})
test("When there's a sign on both ends of the string", () => {
const input = "-69-"
const expected = -69;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
})
test("I guess when there's a - in the middle, but the rest of the chars are ok", ()=>{
const input = "-13-8"
const expected = -13;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
})
test("I guess when there's a + in the middle, but the rest of the chars are ok", ()=>{
const input = "-13+8"
const expected = -13;
const actual = myAtoi(input);
expect(actual).toEqual(expected);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment