[838ea8aa] | 1 | /*
|
---|
| 2 | * Copyright (c) 2016 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <adt/odict.h>
|
---|
| 30 | #include <pcut/pcut.h>
|
---|
| 31 | #include <stdio.h>
|
---|
| 32 | #include <stdlib.h>
|
---|
| 33 |
|
---|
| 34 | /** Test entry */
|
---|
| 35 | typedef struct {
|
---|
| 36 | odlink_t odict;
|
---|
| 37 | int key;
|
---|
| 38 | } test_entry_t;
|
---|
| 39 |
|
---|
| 40 | enum {
|
---|
| 41 | /** Length of test number sequences */
|
---|
| 42 | test_seq_len = 100
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 | /** Test get key function.
|
---|
| 46 | *
|
---|
| 47 | * @param odlink Ordered dictionary link
|
---|
| 48 | * @return Pointer to key
|
---|
| 49 | */
|
---|
| 50 | static void *test_getkey(odlink_t *odlink)
|
---|
| 51 | {
|
---|
| 52 | return &odict_get_instance(odlink, test_entry_t, odict)->key;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /** Test compare function.
|
---|
| 56 | *
|
---|
| 57 | * @param a First key
|
---|
| 58 | * @param b Second key
|
---|
| 59 | * @return <0, 0, >0 if @a a is less than, equal or greater than @a b
|
---|
| 60 | */
|
---|
| 61 | static int test_cmp(void *a, void *b)
|
---|
| 62 | {
|
---|
| 63 | int *ia = (int *)a;
|
---|
| 64 | int *ib = (int *)b;
|
---|
| 65 |
|
---|
| 66 | return *ia - *ib;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | PCUT_INIT
|
---|
| 70 |
|
---|
| 71 | PCUT_TEST_SUITE(odict);
|
---|
| 72 |
|
---|
| 73 | /** Increasing sequence test.
|
---|
| 74 | *
|
---|
| 75 | * Test initialization, emptiness, insertion of increasing sequence and walking.
|
---|
| 76 | */
|
---|
| 77 | PCUT_TEST(incr_seq)
|
---|
| 78 | {
|
---|
| 79 | odict_t odict;
|
---|
| 80 | test_entry_t *e;
|
---|
| 81 | odlink_t *c;
|
---|
| 82 | int i;
|
---|
| 83 |
|
---|
| 84 | odict_initialize(&odict, test_getkey, test_cmp);
|
---|
| 85 |
|
---|
| 86 | PCUT_ASSERT_EQUALS(true, odict_empty(&odict));
|
---|
| 87 |
|
---|
| 88 | for (i = 0; i < test_seq_len; i++) {
|
---|
| 89 | e = calloc(1, sizeof(test_entry_t));
|
---|
| 90 | PCUT_ASSERT_NOT_NULL(e);
|
---|
| 91 |
|
---|
| 92 | e->key = i;
|
---|
| 93 | odict_insert(&e->odict, &odict, NULL);
|
---|
| 94 | PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | i = 0;
|
---|
| 98 | c = odict_first(&odict);
|
---|
| 99 | while (c != NULL) {
|
---|
| 100 | e = odict_get_instance(c, test_entry_t, odict);
|
---|
| 101 | PCUT_ASSERT_INT_EQUALS(i, e->key);
|
---|
| 102 | c = odict_next(c, &odict);
|
---|
| 103 | ++i;
|
---|
| 104 | }
|
---|
| 105 | PCUT_ASSERT_INT_EQUALS(test_seq_len, i);
|
---|
| 106 |
|
---|
| 107 | i = test_seq_len;
|
---|
| 108 | c = odict_last(&odict);
|
---|
| 109 | while (c != NULL) {
|
---|
| 110 | --i;
|
---|
| 111 | e = odict_get_instance(c, test_entry_t, odict);
|
---|
| 112 | PCUT_ASSERT_INT_EQUALS(i, e->key);
|
---|
| 113 | c = odict_prev(c, &odict);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | PCUT_ASSERT_INT_EQUALS(0, i);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /** Decreasing sequence test.
|
---|
| 120 | *
|
---|
| 121 | * Test initialization, emptiness, insertion of decreasing sequence and walking.
|
---|
| 122 | */
|
---|
| 123 | PCUT_TEST(decr_seq)
|
---|
| 124 | {
|
---|
| 125 | odict_t odict;
|
---|
| 126 | test_entry_t *e;
|
---|
| 127 | odlink_t *c;
|
---|
| 128 | int i;
|
---|
| 129 |
|
---|
| 130 | odict_initialize(&odict, test_getkey, test_cmp);
|
---|
| 131 |
|
---|
| 132 | PCUT_ASSERT_EQUALS(true, odict_empty(&odict));
|
---|
| 133 |
|
---|
| 134 | for (i = 0; i < test_seq_len; i++) {
|
---|
| 135 | e = calloc(1, sizeof(test_entry_t));
|
---|
| 136 | PCUT_ASSERT_NOT_NULL(e);
|
---|
| 137 |
|
---|
| 138 | e->key = test_seq_len - i - 1;
|
---|
| 139 | odict_insert(&e->odict, &odict, NULL);
|
---|
| 140 | PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | i = 0;
|
---|
| 144 | c = odict_first(&odict);
|
---|
| 145 | while (c != NULL) {
|
---|
| 146 | e = odict_get_instance(c, test_entry_t, odict);
|
---|
| 147 | PCUT_ASSERT_INT_EQUALS(i, e->key);
|
---|
| 148 | c = odict_next(c, &odict);
|
---|
| 149 | ++i;
|
---|
| 150 | }
|
---|
| 151 | PCUT_ASSERT_INT_EQUALS(test_seq_len, i);
|
---|
| 152 |
|
---|
| 153 | i = test_seq_len;
|
---|
| 154 | c = odict_last(&odict);
|
---|
| 155 | while (c != NULL) {
|
---|
| 156 | --i;
|
---|
| 157 | e = odict_get_instance(c, test_entry_t, odict);
|
---|
| 158 | PCUT_ASSERT_INT_EQUALS(i, e->key);
|
---|
| 159 | c = odict_prev(c, &odict);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | PCUT_ASSERT_INT_EQUALS(0, i);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /** Increasing sequence insertion and removal test.
|
---|
| 166 | *
|
---|
| 167 | * Test sequential insertion of increasing sequence and sequential removal.
|
---|
| 168 | */
|
---|
| 169 | PCUT_TEST(incr_seq_ins_rem)
|
---|
| 170 | {
|
---|
| 171 | odict_t odict;
|
---|
| 172 | test_entry_t *e;
|
---|
| 173 | odlink_t *c;
|
---|
| 174 | int i;
|
---|
| 175 |
|
---|
| 176 | odict_initialize(&odict, test_getkey, test_cmp);
|
---|
| 177 |
|
---|
| 178 | PCUT_ASSERT_EQUALS(true, odict_empty(&odict));
|
---|
| 179 |
|
---|
| 180 | for (i = 0; i < test_seq_len; i++) {
|
---|
| 181 | e = calloc(1, sizeof(test_entry_t));
|
---|
| 182 | PCUT_ASSERT_NOT_NULL(e);
|
---|
| 183 |
|
---|
| 184 | e->key = i;
|
---|
| 185 | odict_insert(&e->odict, &odict, NULL);
|
---|
| 186 | PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | i = 0;
|
---|
| 190 | c = odict_first(&odict);
|
---|
| 191 | while (c != NULL) {
|
---|
| 192 | e = odict_get_instance(c, test_entry_t, odict);
|
---|
| 193 | PCUT_ASSERT_INT_EQUALS(i, e->key);
|
---|
| 194 |
|
---|
| 195 | odict_remove(c);
|
---|
| 196 | PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
|
---|
| 197 |
|
---|
| 198 | c = odict_first(&odict);
|
---|
| 199 | ++i;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | PCUT_ASSERT_INT_EQUALS(test_seq_len, i);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /** Generate pseudorandom sequence. */
|
---|
| 206 | static int seq_next(int cur)
|
---|
| 207 | {
|
---|
| 208 | return (cur * 1951) % 1000000;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /** Test 4.
|
---|
| 212 | *
|
---|
| 213 | * Test inserting a pseudorandom sequence and then extracting it again.
|
---|
| 214 | */
|
---|
| 215 | PCUT_TEST(prseq_ins_extract)
|
---|
| 216 | {
|
---|
| 217 | odict_t odict;
|
---|
| 218 | test_entry_t *e, *ep;
|
---|
| 219 | odlink_t *c, *d;
|
---|
| 220 | int prev;
|
---|
| 221 | int i;
|
---|
| 222 | int v;
|
---|
| 223 |
|
---|
| 224 | odict_initialize(&odict, test_getkey, test_cmp);
|
---|
| 225 |
|
---|
| 226 | PCUT_ASSERT_EQUALS(true, odict_empty(&odict));
|
---|
| 227 |
|
---|
| 228 | v = 1; ep = NULL;
|
---|
| 229 | for (i = 0; i < test_seq_len; i++) {
|
---|
| 230 | e = calloc(1, sizeof(test_entry_t));
|
---|
| 231 | PCUT_ASSERT_NOT_NULL(e);
|
---|
| 232 |
|
---|
| 233 | e->key = v;
|
---|
| 234 | odict_insert(&e->odict, &odict, &ep->odict);
|
---|
| 235 | PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
|
---|
| 236 | v = seq_next(v);
|
---|
| 237 | ep = e;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | /* Verify that entries are in ascending order */
|
---|
| 241 | c = odict_first(&odict);
|
---|
| 242 | prev = -1;
|
---|
| 243 | i = 0;
|
---|
| 244 | while (c != NULL) {
|
---|
| 245 | e = odict_get_instance(c, test_entry_t, odict);
|
---|
| 246 | PCUT_ASSERT_EQUALS(true, e->key > prev);
|
---|
| 247 |
|
---|
| 248 | prev = e->key;
|
---|
| 249 | c = odict_next(c, &odict);
|
---|
| 250 | ++i;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | PCUT_ASSERT_INT_EQUALS(test_seq_len, i);
|
---|
| 254 |
|
---|
| 255 | /* Try extracting the sequence */
|
---|
| 256 | v = 1;
|
---|
| 257 | for (i = 0; i < test_seq_len; i++) {
|
---|
| 258 | c = odict_find_eq(&odict, (void *)&v, NULL);
|
---|
| 259 | PCUT_ASSERT_NOT_NULL(c);
|
---|
| 260 |
|
---|
| 261 | e = odict_get_instance(c, test_entry_t, odict);
|
---|
| 262 | PCUT_ASSERT_INT_EQUALS(v, e->key);
|
---|
| 263 |
|
---|
| 264 | d = odict_find_eq_last(&odict, (void *)&v, NULL);
|
---|
| 265 | PCUT_ASSERT_NOT_NULL(d);
|
---|
| 266 |
|
---|
| 267 | e = odict_get_instance(d, test_entry_t, odict);
|
---|
| 268 | PCUT_ASSERT_INT_EQUALS(v, e->key);
|
---|
| 269 |
|
---|
| 270 | odict_remove(c);
|
---|
| 271 | PCUT_ASSERT_ERRNO_VAL(EOK, odict_validate(&odict));
|
---|
| 272 |
|
---|
| 273 | v = seq_next(v);
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | PCUT_EXPORT(odict);
|
---|