tests/test_iterator.c

changeset 850
b2bc48c2b251
child 851
adb4e0737c33
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tests/test_iterator.c	Thu May 23 15:05:24 2024 +0200
     1.3 @@ -0,0 +1,178 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + *   1. Redistributions of source code must retain the above copyright
    1.13 + *      notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *   2. Redistributions in binary form must reproduce the above copyright
    1.16 + *      notice, this list of conditions and the following disclaimer in the
    1.17 + *      documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 + * POSSIBILITY OF SUCH DAMAGE.
    1.30 + */
    1.31 +
    1.32 +#include "cx/test.h"
    1.33 +
    1.34 +#include "cx/iterator.h"
    1.35 +
    1.36 +CX_TEST(test_iterator_create) {
    1.37 +    size_t size = 20;
    1.38 +    unsigned array[size];
    1.39 +    for (unsigned i = 0 ; i < size ; i++) array[i] = i;
    1.40 +
    1.41 +    CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, false);
    1.42 +    CX_TEST_DO {
    1.43 +        CX_TEST_ASSERT(iter.index == 0);
    1.44 +        CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
    1.45 +        CX_TEST_ASSERT(iter.elem_count == size);
    1.46 +        CX_TEST_ASSERT(iter.src_handle == array);
    1.47 +        CX_TEST_ASSERT(iter.elem_handle == &array[0]);
    1.48 +        CX_TEST_ASSERT(cxIteratorValid(iter));
    1.49 +    }
    1.50 +}
    1.51 +
    1.52 +CX_TEST(test_iterator_create_null) {
    1.53 +    CxMutIterator iter = cxIterator(NULL, sizeof(unsigned), 47, false);
    1.54 +    CX_TEST_DO {
    1.55 +        CX_TEST_ASSERT(iter.index == 0);
    1.56 +        CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
    1.57 +        CX_TEST_ASSERT(iter.elem_count == 0);
    1.58 +        CX_TEST_ASSERT(iter.src_handle == NULL);
    1.59 +        CX_TEST_ASSERT(iter.elem_handle == NULL);
    1.60 +        CX_TEST_ASSERT(!cxIteratorValid(iter));
    1.61 +    }
    1.62 +}
    1.63 +
    1.64 +CX_TEST(test_iterator_iterate) {
    1.65 +    size_t size = 20;
    1.66 +    unsigned array[size];
    1.67 +    for (unsigned i = 0 ; i < size ; i++) array[i] = i;
    1.68 +
    1.69 +    CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, false);
    1.70 +    CX_TEST_DO {
    1.71 +        unsigned expected = 0;
    1.72 +        cx_foreach(unsigned *, e, iter) {
    1.73 +            CX_TEST_ASSERT(iter.index == expected);
    1.74 +            CX_TEST_ASSERT(*e == expected);
    1.75 +            CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
    1.76 +            CX_TEST_ASSERT(iter.elem_count == size);
    1.77 +            CX_TEST_ASSERT(iter.src_handle == array);
    1.78 +            CX_TEST_ASSERT(iter.elem_handle == &array[expected]);
    1.79 +            expected++;
    1.80 +        }
    1.81 +        CX_TEST_ASSERT(expected == size);
    1.82 +    }
    1.83 +}
    1.84 +
    1.85 +CX_TEST(test_iterator_with_slow_remove) {
    1.86 +    size_t size = 20;
    1.87 +    unsigned array[size];
    1.88 +    for (unsigned i = 0 ; i < size ; i++) array[i] = i;
    1.89 +
    1.90 +    size_t elem_counts[] = {
    1.91 +            20, 20, 19, 19, 18, 18, 17, 17, 16, 16,
    1.92 +            15, 15, 14, 14, 13, 13, 12, 12, 11, 11
    1.93 +    };
    1.94 +    size_t indices[] = {
    1.95 +            0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
    1.96 +            6, 6, 7, 7, 8, 8, 9, 9, 10
    1.97 +    };
    1.98 +    unsigned expected_result[] = {
    1.99 +            0, 2, 4, 6, 8, 10, 12, 14, 16, 18
   1.100 +    };
   1.101 +
   1.102 +    CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, true);
   1.103 +    CX_TEST_DO {
   1.104 +        unsigned expected = 0;
   1.105 +        cx_foreach(unsigned *, e, iter) {
   1.106 +            CX_TEST_ASSERT(*e == expected);
   1.107 +            CX_TEST_ASSERT(iter.index == indices[expected]);
   1.108 +            CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
   1.109 +            CX_TEST_ASSERT(iter.elem_count == elem_counts[expected]);
   1.110 +            CX_TEST_ASSERT(iter.src_handle == array);
   1.111 +            CX_TEST_ASSERT(iter.elem_handle == &array[indices[expected]]);
   1.112 +            expected++;
   1.113 +            if (expected % 2 == 0) {
   1.114 +                cxIteratorFlagRemoval(iter);
   1.115 +            }
   1.116 +        }
   1.117 +        CX_TEST_ASSERT(expected == 20);
   1.118 +        CX_TEST_ASSERT(iter.index == 10);
   1.119 +        CX_TEST_ASSERT(iter.elem_count == 10);
   1.120 +        for (unsigned i = 0 ; i < 9 ; i++) {
   1.121 +            CX_TEST_ASSERT(array[i] == expected_result[i]);
   1.122 +        }
   1.123 +    }
   1.124 +}
   1.125 +
   1.126 +CX_TEST(test_iterator_with_fast_remove) {
   1.127 +    size_t size = 20;
   1.128 +    unsigned array[size];
   1.129 +    for (unsigned i = 0 ; i < size ; i++) array[i] = i;
   1.130 +
   1.131 +    size_t elem_counts[] = {
   1.132 +            20, 20, 19, 19, 18, 18, 17, 17, 16, 16,
   1.133 +            15, 15, 14, 14, 13, 13, 12, 12, 11, 11
   1.134 +    };
   1.135 +    size_t indices[] = {
   1.136 +            0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
   1.137 +            6, 6, 7, 7, 8, 8, 9, 9, 10
   1.138 +    };
   1.139 +    unsigned expected_result[] = {
   1.140 +            0, 19, 18, 17, 16, 15, 14, 13, 12, 11
   1.141 +    };
   1.142 +    unsigned expected_visits[] = {
   1.143 +            0, 1, 19, 2, 18, 3, 17, 4, 16, 5,
   1.144 +            15, 6, 14, 7, 13, 8, 12, 9, 11, 10
   1.145 +    };
   1.146 +
   1.147 +    CxMutIterator iter = cxIterator(array, sizeof(unsigned), size, false);
   1.148 +    CX_TEST_DO {
   1.149 +        unsigned expected = 0;
   1.150 +        cx_foreach(unsigned *, e, iter) {
   1.151 +            CX_TEST_ASSERT(*e == expected_visits[expected]);
   1.152 +            CX_TEST_ASSERT(iter.index == indices[expected]);
   1.153 +            CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
   1.154 +            CX_TEST_ASSERT(iter.elem_count == elem_counts[expected]);
   1.155 +            CX_TEST_ASSERT(iter.src_handle == array);
   1.156 +            CX_TEST_ASSERT(iter.elem_handle == &array[indices[expected]]);
   1.157 +            expected++;
   1.158 +            if (expected % 2 == 0) {
   1.159 +                cxIteratorFlagRemoval(iter);
   1.160 +            }
   1.161 +        }
   1.162 +        CX_TEST_ASSERT(expected == 20);
   1.163 +        CX_TEST_ASSERT(iter.index == 10);
   1.164 +        CX_TEST_ASSERT(iter.elem_count == 10);
   1.165 +        for (unsigned i = 0 ; i < 9 ; i++) {
   1.166 +            CX_TEST_ASSERT(array[i] == expected_result[i]);
   1.167 +        }
   1.168 +    }
   1.169 +}
   1.170 +
   1.171 +CxTestSuite *cx_test_suite_iterator(void) {
   1.172 +    CxTestSuite *suite = cx_test_suite_new("iterator");
   1.173 +
   1.174 +    cx_test_register(suite, test_iterator_create);
   1.175 +    cx_test_register(suite, test_iterator_iterate);
   1.176 +    cx_test_register(suite, test_iterator_with_slow_remove);
   1.177 +    cx_test_register(suite, test_iterator_with_fast_remove);
   1.178 +
   1.179 +    return suite;
   1.180 +}
   1.181 +

mercurial