spank-olm

WIP Do not look
git clone git://archive.git.mtrnord.blog/MTRNord/spank-olm.git
Log | Files | Refs | README | LICENSE

list_test.cpp (4354B)


      1 #include <iostream>
      2 #include <snitch/snitch.hpp>
      3 #include <list.hpp>
      4 
      5 TEST_CASE("FixedSizeArray basic operations")
      6 {
      7     using namespace spank_olm;
      8 
      9     FixedSizeArray<int, 5> array;
     10 
     11     // Test insertion
     12     REQUIRE(array.insert_at(0, 10) == FixedSizeArray<int, 5>::SUCCESS);
     13     REQUIRE(array.insert_at(1, 20) == FixedSizeArray<int, 5>::SUCCESS);
     14     REQUIRE(array.insert_at(1, 15) == FixedSizeArray<int, 5>::SUCCESS);
     15 
     16     // Test size
     17     REQUIRE(array.size() == 3);
     18 
     19     // Test element values
     20     // Test element values
     21     REQUIRE(array[0] == 10);
     22     REQUIRE(array[1] == 15);
     23     REQUIRE(array[2] == 20);
     24 
     25     // Test erasure
     26     REQUIRE(array.erase_at(1) == FixedSizeArray<int, 5>::SUCCESS);
     27     REQUIRE(array.size() == 2);
     28     REQUIRE(array[1] == 20);
     29 
     30     // Test boundary conditions
     31     REQUIRE(array.insert_at(5, 30) == FixedSizeArray<int, 5>::INDEX_OUT_OF_RANGE);
     32     REQUIRE(array.erase_at(5) == FixedSizeArray<int, 5>::INDEX_OUT_OF_RANGE);
     33 
     34     // Ensure iterator works
     35     int expected_values[] = {10, 20};
     36     int i = 0;
     37     for (const auto& value : array)
     38     {
     39         REQUIRE(*value == expected_values[i++]);
     40     }
     41 
     42     // Ensure that adding more elements than the maximum size is possible and the last element is dropped as expected
     43     // Loop to insert elements into the array, starting from 0 up to 9
     44     // This will cause the array to overflow, and only the last 5 elements will be kept
     45     for (int j = 0; j < 10; ++j)
     46     {
     47         array.insert_at(0, j);
     48     }
     49 
     50     // Check that the array size is 5 after the overflow
     51     REQUIRE(array.size() == 5);
     52 
     53     // Loop to verify that the elements in the array are as expected
     54     // The array should contain the last 5 inserted elements in reverse order
     55     for (int j = 0; j < 5; ++j)
     56     {
     57         REQUIRE(array[j] == 9 - j);
     58     }
     59 }
     60 
     61 TEST_CASE("FixedSizeArray erase element at pointer null")
     62 {
     63     using namespace spank_olm;
     64 
     65     FixedSizeArray<int, 5> array;
     66     array.insert(1);
     67     array.insert(2);
     68     array.insert(3);
     69 
     70     int* ptr = nullptr;
     71     REQUIRE(array.erase(ptr) == FixedSizeArray<int, 5>::INDEX_OUT_OF_RANGE);
     72     REQUIRE(array.size() == 3);
     73 }
     74 
     75 TEST_CASE("FixedSizeArray erase first element")
     76 {
     77     using namespace spank_olm;
     78 
     79     FixedSizeArray<int, 5> array;
     80     array.insert(1);
     81     array.insert(2);
     82     array.insert(3);
     83     REQUIRE(array.size() == 3);
     84 
     85     REQUIRE(array.erase_at(0) == FixedSizeArray<int, 5>::SUCCESS);
     86     REQUIRE(array.size() == 2);
     87 
     88     REQUIRE(array[1] == 2);
     89     REQUIRE(array[0] == 3);
     90 }
     91 
     92 TEST_CASE("FixedSizeArray erase last element")
     93 {
     94     using namespace spank_olm;
     95 
     96     FixedSizeArray<int, 5> array;
     97     array.insert(1);
     98     array.insert(2);
     99     array.insert(3);
    100 
    101     int last_index = array.size() - 1;
    102     REQUIRE(array.erase_at(last_index) == FixedSizeArray<int, 5>::SUCCESS);
    103     REQUIRE(array.size() == 2);
    104     REQUIRE(array[0] == 2);
    105     REQUIRE(array[1] == 1);
    106 }
    107 
    108 TEST_CASE("FixedSizeArray erase element in full array")
    109 {
    110     using namespace spank_olm;
    111 
    112     FixedSizeArray<int, 5> array;
    113     for (int i = 0; i < 5; ++i)
    114     {
    115         array.insert(i);
    116     }
    117 
    118     REQUIRE(array.erase_at(2) == FixedSizeArray<int, 5>::SUCCESS);
    119     REQUIRE(array.size() == 4);
    120     REQUIRE(array[0] == 4);
    121     REQUIRE(array[1] == 3);
    122     REQUIRE(array[2] == 1);
    123     REQUIRE(array[3] == 0);
    124 }
    125 
    126 TEST_CASE("FixedSizeArray erase element in empty array")
    127 {
    128     using namespace spank_olm;
    129 
    130     FixedSizeArray<int, 5> array;
    131 
    132     int value = 1;
    133     int* ptr = &value;
    134     REQUIRE(array.erase(ptr) == FixedSizeArray<int, 5>::INDEX_OUT_OF_RANGE);
    135     REQUIRE(array.size() == 0);
    136 }
    137 
    138 TEST_CASE("FixedSizeArray empty and size match")
    139 {
    140     using namespace spank_olm;
    141 
    142     FixedSizeArray<int, 5> array;
    143 
    144     // Initially, the array should be empty
    145     REQUIRE(array.empty() == true);
    146     REQUIRE(array.size() == 0);
    147 
    148     // Insert an element and check again
    149     array.insert(1);
    150     REQUIRE(array.empty() == false);
    151     REQUIRE(array.size() == 1);
    152 
    153     // Insert another element and check again
    154     array.insert(2);
    155     REQUIRE(array.empty() == false);
    156     REQUIRE(array.size() == 2);
    157 
    158     // Erase an element and check again
    159     array.erase_at(0);
    160     REQUIRE(array.empty() == false);
    161     REQUIRE(array.size() == 1);
    162 
    163     // Erase the last element and check again
    164     array.erase_at(0);
    165     REQUIRE(array.empty() == true);
    166     REQUIRE(array.size() == 0);
    167 }