|
| 1 | +import { reverseArray, reverseArrayInPlace } from "./reversing_array.ts"; |
| 2 | +import { assert } from "./testing.ts"; |
| 3 | + |
| 4 | +Deno.test("[reverseArray]", () => { |
| 5 | + const exampleArray = [1, 2, 3, 4, 5]; |
| 6 | + const inversedArray = reverseArray(exampleArray); |
| 7 | + |
| 8 | + // It returns a new copy |
| 9 | + assert(exampleArray !== inversedArray); |
| 10 | + |
| 11 | + const stdReversed = [...exampleArray].reverse(); |
| 12 | + |
| 13 | + assert(inversedArray.length === stdReversed.length); |
| 14 | + |
| 15 | + for (let i = 0; i < inversedArray.length; i++) { |
| 16 | + assert(inversedArray[i] === stdReversed[i]); |
| 17 | + } |
| 18 | +}); |
| 19 | + |
| 20 | +Deno.test("[reverseArrayInPlace]", () => { |
| 21 | + const exampleArray = [1, 2, 3, 4, 5]; |
| 22 | + const expectedResult = [5, 4, 3, 2, 1]; |
| 23 | + const result = reverseArrayInPlace(exampleArray); |
| 24 | + |
| 25 | + // It's same because it mutates the same array and then returns it |
| 26 | + assert(exampleArray === result); |
| 27 | + |
| 28 | + assert(exampleArray.length === result.length); |
| 29 | + |
| 30 | + for (let i = 0; i < result.length; i++) { |
| 31 | + assert(result[i] === expectedResult[i]); |
| 32 | + } |
| 33 | +}); |
0 commit comments