|
| 1 | +#include <iostream> |
| 2 | +#include <assert.h> |
| 3 | +#include "list.h" // Adjust the include path as needed |
| 4 | + |
| 5 | +void testPrepend() { |
| 6 | + LinkedList<int> list; |
| 7 | + list.prepend(1); |
| 8 | + assert(list.getHead()->data == 1); |
| 9 | + list.prepend(0); |
| 10 | + assert(list.getHead()->data == 0); |
| 11 | + assert(list.getHead()->next->data == 1); |
| 12 | + std::cout << "testPrepend passed!" << std::endl; |
| 13 | +} |
| 14 | + |
| 15 | +void testAppend() { |
| 16 | + LinkedList<int> list; |
| 17 | + list.append(1); |
| 18 | + assert(list.getTail()->data == 1); |
| 19 | + list.append(2); |
| 20 | + assert(list.getTail()->data == 2); |
| 21 | + std::cout << "testAppend passed!" << std::endl; |
| 22 | +} |
| 23 | + |
| 24 | +void testInsertBetween() { |
| 25 | + LinkedList<int> list; |
| 26 | + list.append(1); |
| 27 | + list.append(3); |
| 28 | + list.insertBetween(2, 3, 1); |
| 29 | + assert(list.getHead()->next->data == 2); |
| 30 | + assert(list.getHead()->next->next->data == 3); |
| 31 | + std::cout << "testInsertBetween passed!" << std::endl; |
| 32 | +} |
| 33 | + |
| 34 | +void testDeleteHead() { |
| 35 | + LinkedList<int> list; |
| 36 | + list.append(1); |
| 37 | + list.append(2); |
| 38 | + list.deleteHead(); |
| 39 | + assert(list.getHead()->data == 2); |
| 40 | + std::cout << "testDeleteHead passed!" << std::endl; |
| 41 | +} |
| 42 | + |
| 43 | +void testDeleteTail() { |
| 44 | + LinkedList<int> list; |
| 45 | + list.append(1); |
| 46 | + list.append(2); |
| 47 | + list.deleteTail(); |
| 48 | + assert(list.getTail()->data == 1); |
| 49 | + std::cout << "testDeleteTail passed!" << std::endl; |
| 50 | +} |
| 51 | + |
| 52 | +void testDeleteAny() { |
| 53 | + LinkedList<int> list; |
| 54 | + list.append(1); |
| 55 | + list.append(2); |
| 56 | + list.append(3); |
| 57 | + list.deleteAny(2); |
| 58 | + assert(list.getHead()->next->data == 3); |
| 59 | + std::cout << "testDeleteAny passed!" << std::endl; |
| 60 | +} |
| 61 | + |
| 62 | +void testSize() { |
| 63 | + LinkedList<int> list; |
| 64 | + assert(list.size() == 0); |
| 65 | + list.append(1); |
| 66 | + list.append(2); |
| 67 | + list.append(3); |
| 68 | + assert(list.size() == 3); |
| 69 | + std::cout << "testSize passed!" << std::endl; |
| 70 | +} |
| 71 | + |
| 72 | +void testReverseList() { |
| 73 | + LinkedList<int> list; |
| 74 | + list.append(1); |
| 75 | + list.append(2); |
| 76 | + list.append(3); |
| 77 | + list.reverseList(); |
| 78 | + assert(list.getHead()->data == 3); |
| 79 | + assert(list.getTail()->data == 1); |
| 80 | + std::cout << "testReverseList passed!" << std::endl; |
| 81 | +} |
| 82 | + |
| 83 | +void testFind() { |
| 84 | + LinkedList<int> list; |
| 85 | + list.append(1); |
| 86 | + list.append(2); |
| 87 | + assert(list.find(2)->data == 2); |
| 88 | + assert(list.find(3) == nullptr); |
| 89 | + std::cout << "testFind passed!" << std::endl; |
| 90 | +} |
| 91 | + |
| 92 | +void testClear() { |
| 93 | + LinkedList<int> list; |
| 94 | + list.append(1); |
| 95 | + list.append(2); |
| 96 | + list.clear(); |
| 97 | + assert(list.getHead() == nullptr); |
| 98 | + assert(list.size() == 0); |
| 99 | + std::cout << "testClear passed!" << std::endl; |
| 100 | +} |
| 101 | + |
| 102 | +int main() { |
| 103 | + testPrepend(); |
| 104 | + testAppend(); |
| 105 | + testInsertBetween(); |
| 106 | + testDeleteHead(); |
| 107 | + testDeleteTail(); |
| 108 | + testDeleteAny(); |
| 109 | + testSize(); |
| 110 | + testReverseList(); |
| 111 | + testFind(); |
| 112 | + testClear(); |
| 113 | + |
| 114 | + std::cout << "All tests passed!" << std::endl; |
| 115 | + return 0; |
| 116 | +} |
0 commit comments