@@ -181,3 +181,72 @@ test(testAddingWithSortAndResizeBy5) {
181181 assertEqual (btreeList.getByKey (9 )->getItem (), 108 );
182182
183183}
184+
185+ class NumericStorageItem {
186+ private:
187+ uint32_t item;
188+
189+ public:
190+ NumericStorageItem () : item(0xffffffff ) {}
191+ explicit NumericStorageItem (uint32_t value) : item(value) {}
192+ NumericStorageItem (const NumericStorageItem& other) = default ;
193+ NumericStorageItem& operator = (const NumericStorageItem& other) = default ;
194+ uint32_t getKey () const { return item; }
195+ };
196+
197+ void printArray (const NumericStorageItem* list, int size) {
198+ char sz[160 ];
199+ strcpy (sz, " array: " );
200+ for (int i=0 ;i<size;i++) {
201+ char intBuf[10 ];
202+ itoa ((int )(list[i].getKey ()), intBuf, 10 );
203+ strcat (sz, intBuf);
204+ strcat (sz, " " );
205+ }
206+ serdebug (sz);
207+ }
208+
209+
210+ test (testAddingThenRemovingThenAddingItems) {
211+ BtreeList<uint32_t , NumericStorageItem> myList;
212+
213+ for (int i=0 ;i<20 ;i++) {
214+ myList.add (NumericStorageItem (i + 100 ));
215+ }
216+
217+ // should have 20 items now
218+ assertEqual (myList.count (), bsize_t (20 ));
219+
220+ // remove key 102
221+ assertTrue (myList.removeByKey (102 ));
222+ assertFalse (myList.removeByKey (10002 ));
223+
224+ printArray (myList.items (), myList.count ());
225+
226+ // should have 19 items
227+ assertEqual (myList.count (), bsize_t (19 ));
228+
229+ // ensure that only 102 was removed
230+ assertTrue (myList.getByKey (102 ) == nullptr );
231+ assertTrue (myList.getByKey (103 ) != nullptr );
232+ assertTrue (myList.getByKey (101 ) != nullptr );
233+
234+ // put 102 back
235+ myList.add (NumericStorageItem (102 ));
236+
237+ printArray (myList.items (), myList.count ());
238+
239+ for (int i=0 ;i<20 ;i++) {
240+ auto entry = myList.itemAtIndex (i);
241+ assertEqual (entry->getKey (), uint32_t (i + 100 ));
242+ }
243+
244+ // now test removing the edge cases, IE head and tail of list
245+ myList.removeIndex (0 );
246+ myList.removeIndex (19 );
247+ printArray (myList.items (), myList.count ());
248+
249+ assertEqual (myList.count (), bsize_t (18 ));
250+ assertTrue (myList.getByKey (100 ) == nullptr );
251+ assertTrue (myList.getByKey (119 ) == nullptr );
252+ }
0 commit comments