testFifo.cpp (4425B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 */ 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #include "ds/Fifo.h" 9 10 #include "jsapi-tests/tests.h" 11 12 // Test with primitive type (int) 13 BEGIN_TEST(testFifoPrimitive) { 14 js::Fifo<int, 0, js::SystemAllocPolicy> fifo; 15 16 // Test initial state 17 CHECK(fifo.empty()); 18 CHECK(fifo.length() == 0); 19 20 // Test emplaceBack 21 CHECK(fifo.emplaceBack(1)); 22 CHECK(!fifo.empty()); 23 CHECK(fifo.length() == 1); 24 CHECK(fifo.front() == 1); 25 26 CHECK(fifo.emplaceBack(2)); 27 CHECK(fifo.emplaceBack(3)); 28 CHECK(fifo.length() == 3); 29 CHECK(fifo.front() == 1); 30 31 // Test popFront 32 fifo.popFront(); 33 CHECK(fifo.length() == 2); 34 CHECK(fifo.front() == 2); 35 36 // Test emplaceFront 37 CHECK(fifo.emplaceFront(10)); 38 CHECK(fifo.length() == 3); 39 CHECK(fifo.front() == 10); 40 41 // Fill to 5 elements 42 CHECK(fifo.emplaceBack(4)); 43 CHECK(fifo.emplaceBack(5)); 44 CHECK(fifo.length() == 5); 45 46 // Verify order by popping all 47 CHECK(fifo.front() == 10); 48 fifo.popFront(); 49 CHECK(fifo.front() == 2); 50 fifo.popFront(); 51 CHECK(fifo.front() == 3); 52 fifo.popFront(); 53 CHECK(fifo.front() == 4); 54 fifo.popFront(); 55 CHECK(fifo.front() == 5); 56 fifo.popFront(); 57 58 CHECK(fifo.empty()); 59 CHECK(fifo.length() == 0); 60 61 // Test clear 62 CHECK(fifo.emplaceBack(100)); 63 CHECK(fifo.emplaceBack(200)); 64 CHECK(fifo.length() == 2); 65 fifo.clear(); 66 CHECK(fifo.empty()); 67 CHECK(fifo.length() == 0); 68 69 return true; 70 } 71 END_TEST(testFifoPrimitive) 72 73 // Simple copyable class for testing 74 struct SimpleCopyable { 75 int value; 76 bool* destructorCalled; 77 78 explicit SimpleCopyable(int v, bool* dc = nullptr) 79 : value(v), destructorCalled(dc) {} 80 81 SimpleCopyable(const SimpleCopyable& other) = default; 82 SimpleCopyable& operator=(const SimpleCopyable& other) = default; 83 84 ~SimpleCopyable() { 85 if (destructorCalled) { 86 *destructorCalled = true; 87 } 88 } 89 }; 90 91 BEGIN_TEST(testFifoCopyable) { 92 js::Fifo<SimpleCopyable, 0, js::SystemAllocPolicy> fifo; 93 94 // Test initial state 95 CHECK(fifo.empty()); 96 CHECK(fifo.length() == 0); 97 98 // Test emplaceBack with constructor args 99 CHECK(fifo.emplaceBack(1, nullptr)); 100 CHECK(fifo.emplaceBack(2, nullptr)); 101 CHECK(fifo.length() == 2); 102 CHECK(fifo.front().value == 1); 103 104 // Test emplaceFront 105 CHECK(fifo.emplaceFront(0, nullptr)); 106 CHECK(fifo.length() == 3); 107 CHECK(fifo.front().value == 0); 108 109 // Pop and verify order 110 fifo.popFront(); 111 CHECK(fifo.front().value == 1); 112 fifo.popFront(); 113 CHECK(fifo.front().value == 2); 114 CHECK(fifo.length() == 1); 115 116 // Test destructor is called on clear 117 bool destructorCalled = false; 118 CHECK(fifo.emplaceBack(99, &destructorCalled)); 119 CHECK(fifo.length() == 2); 120 CHECK(!destructorCalled); 121 122 fifo.clear(); 123 CHECK(destructorCalled); 124 CHECK(fifo.empty()); 125 126 // Test with 5 elements 127 CHECK(fifo.emplaceBack(10, nullptr)); 128 CHECK(fifo.emplaceBack(20, nullptr)); 129 CHECK(fifo.emplaceBack(30, nullptr)); 130 CHECK(fifo.emplaceFront(5, nullptr)); 131 CHECK(fifo.emplaceBack(40, nullptr)); 132 CHECK(fifo.length() == 5); 133 134 // Verify FIFO order 135 CHECK(fifo.front().value == 5); 136 fifo.popFront(); 137 CHECK(fifo.front().value == 10); 138 fifo.popFront(); 139 CHECK(fifo.front().value == 20); 140 fifo.popFront(); 141 CHECK(fifo.front().value == 30); 142 fifo.popFront(); 143 CHECK(fifo.front().value == 40); 144 fifo.popFront(); 145 CHECK(fifo.empty()); 146 147 return true; 148 } 149 END_TEST(testFifoCopyable) 150 151 // Test mixing emplaceBack and emplaceFront operations 152 BEGIN_TEST(testFifoMixedOperations) { 153 js::Fifo<int, 0, js::SystemAllocPolicy> fifo; 154 155 // Interleave front and back operations 156 CHECK(fifo.emplaceBack(3)); // Queue: [3] 157 CHECK(fifo.emplaceFront(1)); // Queue: [1, 3] 158 CHECK(fifo.emplaceBack(5)); // Queue: [1, 3, 5] 159 CHECK(fifo.emplaceFront(0)); // Queue: [0, 1, 3, 5] 160 CHECK(fifo.emplaceBack(7)); // Queue: [0, 1, 3, 5, 7] 161 162 CHECK(fifo.length() == 5); 163 164 // Verify order 165 CHECK(fifo.front() == 0); 166 fifo.popFront(); 167 CHECK(fifo.front() == 1); 168 fifo.popFront(); 169 CHECK(fifo.front() == 3); 170 fifo.popFront(); 171 CHECK(fifo.front() == 5); 172 fifo.popFront(); 173 CHECK(fifo.front() == 7); 174 fifo.popFront(); 175 176 CHECK(fifo.empty()); 177 178 return true; 179 } 180 END_TEST(testFifoMixedOperations)