|
| 1 | +import { test } from "https://deno.land/std/testing/mod.ts"; |
| 2 | +import { |
| 3 | + assertEquals, |
| 4 | + assertStrictEq, |
| 5 | + assertThrows |
| 6 | +} from "https://deno.land/std/testing/asserts.ts"; |
| 7 | +import { createLinkedListNode, getNthNode } from "./linked_list.ts"; |
| 8 | + |
| 9 | +test("createLinkedListNode() returns a LinkedListNode by the given array", () => { |
| 10 | + assertEquals(createLinkedListNode([1, 2, 3, 4, 5, 6, 7]), { |
| 11 | + val: 1, |
| 12 | + next: { |
| 13 | + val: 2, |
| 14 | + next: { |
| 15 | + val: 3, |
| 16 | + next: { |
| 17 | + val: 4, |
| 18 | + next: { |
| 19 | + val: 5, |
| 20 | + next: { |
| 21 | + val: 6, |
| 22 | + next: { |
| 23 | + val: 7, |
| 24 | + next: null |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + }); |
| 32 | + assertEquals(createLinkedListNode([1]), { |
| 33 | + val: 1, |
| 34 | + next: null |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +test("createLinkedListNode() returns null if the given array is empty", () => { |
| 39 | + assertStrictEq(createLinkedListNode([]), null); |
| 40 | +}); |
| 41 | + |
| 42 | +test("getNthNode(node, n) returns the n-th node", () => { |
| 43 | + assertStrictEq( |
| 44 | + getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, 0).val, |
| 45 | + 0 |
| 46 | + ); |
| 47 | + assertStrictEq( |
| 48 | + getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, 2).val, |
| 49 | + 2 |
| 50 | + ); |
| 51 | + assertStrictEq( |
| 52 | + getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, -1).val, |
| 53 | + 5 |
| 54 | + ); |
| 55 | + assertStrictEq( |
| 56 | + getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, -3).val, |
| 57 | + 3 |
| 58 | + ); |
| 59 | +}); |
| 60 | + |
| 61 | +test("getNthNode(node, n) doesn't mutates the given linked list", () => { |
| 62 | + const head = createLinkedListNode([0, 1, 2, 3, 4, 5])!; |
| 63 | + |
| 64 | + getNthNode(head, 1); |
| 65 | + getNthNode(head, 2); |
| 66 | + getNthNode(head, 3); |
| 67 | + |
| 68 | + assertStrictEq(getNthNode(head, 2).val, 2); |
| 69 | +}); |
| 70 | + |
| 71 | +test("getNthNode(node, n) throws an Error if the given index is out of bounds", () => { |
| 72 | + assertThrows( |
| 73 | + () => getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, 7), |
| 74 | + Error |
| 75 | + ); |
| 76 | + assertThrows( |
| 77 | + () => getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, -7), |
| 78 | + Error |
| 79 | + ); |
| 80 | +}); |
| 81 | + |
| 82 | +test("getNthNode(node, n) throws a TypeError if the given index is not an integer", () => { |
| 83 | + assertThrows( |
| 84 | + () => getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, 1.1), |
| 85 | + TypeError |
| 86 | + ); |
| 87 | + assertThrows( |
| 88 | + () => getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, Infinity), |
| 89 | + TypeError |
| 90 | + ); |
| 91 | + assertThrows( |
| 92 | + () => |
| 93 | + getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, -Infinity), |
| 94 | + TypeError |
| 95 | + ); |
| 96 | + assertThrows( |
| 97 | + () => getNthNode(createLinkedListNode([0, 1, 2, 3, 4, 5])!, NaN), |
| 98 | + TypeError |
| 99 | + ); |
| 100 | +}); |
0 commit comments