test/klib/data_structures/ds_1.cpp
// Create a new list, add and delete items, check the list is still valid.

Create a new list, add and delete items, check the list is still valid.
Create a new list add and delete items check the list is still valid .
TEST(DataStructuresTest, Lists1)
{
  cout << "Lists test 1" << endl;
  for (int i = 0; i < num_demo_items; i++)
  {
    klib_list_item_initialize(&demo_items[i]);
    demo_items[i].item = const_cast<uint32_t *>(&num_demo_items);
  }
  klib_list<uint32_t *> list_root;
  klib_list_initialize(&list_root);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(klib_list_is_empty(&list_root));
  list_root.head = &demo_items[0];
  ASSERT(!klib_list_is_valid(&list_root));
  list_root.head = nullptr;
  list_root.tail = &demo_items[0];
  ASSERT(!klib_list_is_valid(&list_root));
  list_root.tail = nullptr;
  klib_list_add_head(&list_root, &demo_items[0]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(!klib_list_is_empty(&list_root));
  klib_list_remove(&demo_items[0]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(klib_list_is_empty(&list_root));
  klib_list_add_tail(&list_root, &demo_items[0]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(!klib_list_is_empty(&list_root));
  klib_list_remove(&demo_items[0]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(klib_list_is_empty(&list_root));
  klib_list_add_head(&list_root, &demo_items[0]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(!klib_list_is_empty(&list_root));
  klib_list_add_head(&list_root, &demo_items[1]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(!klib_list_is_empty(&list_root));
  klib_list_add_tail(&list_root, &demo_items[2]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(!klib_list_is_empty(&list_root));
  klib_list_add_after(&demo_items[2], &demo_items[3]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(!klib_list_is_empty(&list_root));
  klib_list_add_before(&demo_items[1], &demo_items[4]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(!klib_list_is_empty(&list_root));
  klib_list_remove(&demo_items[3]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(!klib_list_is_empty(&list_root));
  klib_list_add_after(&demo_items[2], &demo_items[3]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(!klib_list_is_empty(&list_root));
  ASSERT(list_root.head == &demo_items[4]);
  ASSERT(demo_items[4].next == &demo_items[1]);
  ASSERT(demo_items[1].next == &demo_items[0]);
  ASSERT(demo_items[0].next == &demo_items[2]);
  ASSERT(demo_items[2].next == &demo_items[3]);
  ASSERT(list_root.tail == &demo_items[3]);
  ASSERT(klib_list_get_length(&list_root) == 5);
  klib_list_remove(&demo_items[3]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(list_root.tail == &demo_items[2]);
  klib_list_remove(&demo_items[4]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(list_root.head == &demo_items[1]);
  klib_list_remove(&demo_items[0]);
  ASSERT(klib_list_is_valid(&list_root));
  klib_list_remove(&demo_items[2]);
  ASSERT(klib_list_is_valid(&list_root));
  klib_list_remove(&demo_items[1]);
  ASSERT(klib_list_is_valid(&list_root));
  ASSERT(klib_list_is_empty(&list_root));
}
TEST ( DataStructuresTest , Lists1 ) { cout << " " << endl ; for ( int i = 0 ; i < num_demo_items ; i ++ ) { klib_list_item_initialize ( & demo_items [ i ] ) ; demo_items [ i ] . item = const_cast < uint32_t * > ( & num_demo_items ) ; } klib_list < uint32_t * > list_root ; klib_list_initialize ( & list_root ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( klib_list_is_empty ( & list_root ) ) ; list_root . head = & demo_items [ 0 ] ; ASSERT ( ! klib_list_is_valid ( & list_root ) ) ; list_root . head = nullptr ; list_root . tail = & demo_items [ 0 ] ; ASSERT ( ! klib_list_is_valid ( & list_root ) ) ; list_root . tail = nullptr ; klib_list_add_head ( & list_root , & demo_items [ 0 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( ! klib_list_is_empty ( & list_root ) ) ; klib_list_remove ( & demo_items [ 0 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( klib_list_is_empty ( & list_root ) ) ; klib_list_add_tail ( & list_root , & demo_items [ 0 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( ! klib_list_is_empty ( & list_root ) ) ; klib_list_remove ( & demo_items [ 0 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( klib_list_is_empty ( & list_root ) ) ; klib_list_add_head ( & list_root , & demo_items [ 0 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( ! klib_list_is_empty ( & list_root ) ) ; klib_list_add_head ( & list_root , & demo_items [ 1 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( ! klib_list_is_empty ( & list_root ) ) ; klib_list_add_tail ( & list_root , & demo_items [ 2 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( ! klib_list_is_empty ( & list_root ) ) ; klib_list_add_after ( & demo_items [ 2 ] , & demo_items [ 3 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( ! klib_list_is_empty ( & list_root ) ) ; klib_list_add_before ( & demo_items [ 1 ] , & demo_items [ 4 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( ! klib_list_is_empty ( & list_root ) ) ; klib_list_remove ( & demo_items [ 3 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( ! klib_list_is_empty ( & list_root ) ) ; klib_list_add_after ( & demo_items [ 2 ] , & demo_items [ 3 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( ! klib_list_is_empty ( & list_root ) ) ; ASSERT ( list_root . head == & demo_items [ 4 ] ) ; ASSERT ( demo_items [ 4 ] . next == & demo_items [ 1 ] ) ; ASSERT ( demo_items [ 1 ] . next == & demo_items [ 0 ] ) ; ASSERT ( demo_items [ 0 ] . next == & demo_items [ 2 ] ) ; ASSERT ( demo_items [ 2 ] . next == & demo_items [ 3 ] ) ; ASSERT ( list_root . tail == & demo_items [ 3 ] ) ; ASSERT ( klib_list_get_length ( & list_root ) == 5 ) ; klib_list_remove ( & demo_items [ 3 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( list_root . tail == & demo_items [ 2 ] ) ; klib_list_remove ( & demo_items [ 4 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( list_root . head == & demo_items [ 1 ] ) ; klib_list_remove ( & demo_items [ 0 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; klib_list_remove ( & demo_items [ 2 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; klib_list_remove ( & demo_items [ 1 ] ) ; ASSERT ( klib_list_is_valid ( & list_root ) ) ; ASSERT ( klib_list_is_empty ( & list_root ) ) ; }
Create a new list, add and delete items, check the list is still valid.
Create a new list add and delete items check the list is still valid .
// Initialize the demo items list
 // Test the empty list.
 // Try mushing the list object's pointers.
 // Add an item at the head and remove it again.
 // Do the same at the tail.
 // Do a bit of chopping and changing.
 // Check the ordering of items in the list. No need to do it both ways, that
 // ought to have been done by klib_list_is_valid().
