|
| 1 | +(ns data-structures.dynamic-array.core-test |
| 2 | + (:require [clojure.test :refer [deftest testing is]] |
| 3 | + [data-structures.dynamic-array.core :as da])) |
| 4 | + |
| 5 | +(deftest ->DynamicArray-test |
| 6 | + (testing "throws when invalid initial capacity is provided" |
| 7 | + (is (thrown? IllegalArgumentException (da/->DynamicArray 0))) |
| 8 | + (is (thrown? IllegalArgumentException (da/->DynamicArray -1))))) |
| 9 | +(deftest empty?-test |
| 10 | + (testing "return true if dynamic array is empty" |
| 11 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 12 | + (is (true? (da/empty? dynamic-array))))) |
| 13 | + |
| 14 | + (testing "return false if dynamic array is empty" |
| 15 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 16 | + (da/set dynamic-array (da/next-idx dynamic-array) 20) |
| 17 | + (da/set dynamic-array (da/next-idx dynamic-array) 30) |
| 18 | + (is (false? (da/empty? dynamic-array)))))) |
| 19 | + |
| 20 | +(deftest length-test |
| 21 | + (testing "returns initial capacity till dynamic array is full" |
| 22 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 23 | + (is (= 3 (da/length dynamic-array))))) |
| 24 | + |
| 25 | + (testing "returns expanded capacity once storage demands exceeds initial capacity" |
| 26 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 27 | + (da/set dynamic-array (da/next-idx dynamic-array) 10) |
| 28 | + (da/set dynamic-array (da/next-idx dynamic-array) 20) |
| 29 | + (da/set dynamic-array (da/next-idx dynamic-array) 30) |
| 30 | + (da/set dynamic-array (da/next-idx dynamic-array) 40) |
| 31 | + (is (= 6 (da/length dynamic-array)))))) |
| 32 | + |
| 33 | +(deftest next-idx-test |
| 34 | + (testing "returns initial index as the starting point when dynamic array is empty" |
| 35 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 36 | + (is (= 0 (da/next-idx dynamic-array))))) |
| 37 | + |
| 38 | + (testing "returns current filled index as the starting point when dynamic array is empty" |
| 39 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 40 | + (da/set dynamic-array (da/next-idx dynamic-array) 10) |
| 41 | + (da/set dynamic-array (da/next-idx dynamic-array) 20) |
| 42 | + (da/set dynamic-array (da/next-idx dynamic-array) 30) |
| 43 | + (is (= 3 (da/next-idx dynamic-array)))))) |
| 44 | + |
| 45 | +(deftest set-test |
| 46 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 47 | + (testing "throws Assertion error when index is less than 0" |
| 48 | + (is (thrown? AssertionError (da/set dynamic-array -1 10)))) |
| 49 | + |
| 50 | + (testing "stores the element within the dynamic array when a valid index is provided" |
| 51 | + (da/set dynamic-array (da/next-idx dynamic-array) 10) |
| 52 | + (is (false? (da/empty? dynamic-array))) |
| 53 | + (is (= 1 (da/next-idx dynamic-array)))) |
| 54 | + |
| 55 | + (testing "expands dynamic array to incorporate more elements" |
| 56 | + (da/set dynamic-array (da/next-idx dynamic-array) 20) |
| 57 | + (da/set dynamic-array (da/next-idx dynamic-array) 30) |
| 58 | + (da/set dynamic-array (da/next-idx dynamic-array) 40) |
| 59 | + (is (= 4 (da/next-idx dynamic-array))) |
| 60 | + (is (= 6 (da/length dynamic-array)))) |
| 61 | + |
| 62 | + (testing "expands dynamic array to incorporate for an arbitrary large index and sets next-idx accordingly" |
| 63 | + (da/set dynamic-array 60 40) |
| 64 | + (is (= 61 (da/next-idx dynamic-array)) "This behaviour causes fragmentation in the dynamic array") |
| 65 | + (is (= 120 (da/length dynamic-array)))))) |
| 66 | + |
| 67 | +(deftest get-test |
| 68 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 69 | + (testing "throws Assertion error when index is less than 0" |
| 70 | + (is (thrown? AssertionError (da/get dynamic-array -1)))) |
| 71 | + |
| 72 | + (testing "throws Assertion error when index is greater than next-index" |
| 73 | + (is (thrown? AssertionError (da/get dynamic-array (inc (da/next-idx dynamic-array)))))) |
| 74 | + |
| 75 | + (testing "fetches the content of the dynamic array stored at valid index" |
| 76 | + (da/set dynamic-array (da/next-idx dynamic-array) 40) |
| 77 | + (is (= 40 (da/get dynamic-array (dec (da/next-idx dynamic-array)))))))) |
| 78 | + |
| 79 | +(deftest remove-test |
| 80 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 81 | + (testing "throws Assertion error when index is less than 0" |
| 82 | + (is (thrown? AssertionError (da/remove dynamic-array -1)))) |
| 83 | + |
| 84 | + (testing "throws Assertion error when index is greater than next index" |
| 85 | + (is (thrown? AssertionError (da/remove dynamic-array (inc (da/next-idx dynamic-array)))))) |
| 86 | + |
| 87 | + (testing "removes the element from the dynamic array and returns it if the index is valid" |
| 88 | + (da/set dynamic-array (da/next-idx dynamic-array) 10) |
| 89 | + (da/set dynamic-array (da/next-idx dynamic-array) 20) |
| 90 | + (da/set dynamic-array (da/next-idx dynamic-array) 30) |
| 91 | + (da/set dynamic-array (da/next-idx dynamic-array) 40) |
| 92 | + (is (= 4 (da/next-idx dynamic-array)) "Sanity check to ensure that next-index is correctly calculated") |
| 93 | + (is (= 30 (da/remove dynamic-array 2))) |
| 94 | + (is (= 3 (da/next-idx dynamic-array))) |
| 95 | + (is (= "[ 10 20 40 ]" (.toString dynamic-array)))))) |
| 96 | + |
| 97 | +(deftest append-test |
| 98 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 99 | + (testing "appends values to dynamic array" |
| 100 | + (da/set dynamic-array (da/next-idx dynamic-array) 10) |
| 101 | + (da/set dynamic-array (da/next-idx dynamic-array) 20) |
| 102 | + (is (= 2 (da/next-idx dynamic-array))) |
| 103 | + (is (= 3 (da/length dynamic-array)))) |
| 104 | + |
| 105 | + (testing "expands dynamic array to allocate more elements" |
| 106 | + (da/set dynamic-array (da/next-idx dynamic-array) 30) |
| 107 | + (da/set dynamic-array (da/next-idx dynamic-array) 40) |
| 108 | + (is (= 4 (da/next-idx dynamic-array))) |
| 109 | + (is (= 6 (da/length dynamic-array)))))) |
| 110 | + |
| 111 | +(deftest pop-test |
| 112 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 113 | + (testing "throws when the dynamic array is empty" |
| 114 | + (is (thrown? AssertionError (da/pop dynamic-array)))) |
| 115 | + |
| 116 | + (testing "pops the element from dynamic array and returns it" |
| 117 | + (da/set dynamic-array (da/next-idx dynamic-array) 10) |
| 118 | + (da/set dynamic-array (da/next-idx dynamic-array) 20) |
| 119 | + (da/set dynamic-array (da/next-idx dynamic-array) 30) |
| 120 | + (is (= 30 (da/pop dynamic-array))) |
| 121 | + (is (= 2 (da/next-idx dynamic-array)))))) |
| 122 | + |
| 123 | +(deftest toString-test |
| 124 | + (testing "provides string representation of an empty array" |
| 125 | + (let [dynamic-array (da/->DynamicArray 3)] |
| 126 | + (is (= "[ ]" (.toString dynamic-array))))) |
| 127 | + |
| 128 | + (testing "provides string representation of a non empty array" |
| 129 | + (let [dynamic-array (da/->DynamicArray 10)] |
| 130 | + (doseq [i (range 10 15)] |
| 131 | + (da/append dynamic-array i)) |
| 132 | + (is (= "[ 10 11 12 13 14 ]" (.toString dynamic-array)))))) |
0 commit comments