"Fossies" - the Fresh Open Source Software Archive

Member "knot-2.8.3/tests/contrib/test_dynarray.c" (16 Jul 2019, 2115 Bytes) of package /linux/misc/dns/knot-2.8.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test_dynarray.c": 2.7.6_vs_2.8.0.

    1 /*  Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
    2 
    3     This program is free software: you can redistribute it and/or modify
    4     it under the terms of the GNU General Public License as published by
    5     the Free Software Foundation, either version 3 of the License, or
    6     (at your option) any later version.
    7 
    8     This program is distributed in the hope that it will be useful,
    9     but WITHOUT ANY WARRANTY; without even the implied warranty of
   10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11     GNU General Public License for more details.
   12 
   13     You should have received a copy of the GNU General Public License
   14     along with this program.  If not, see <https://www.gnu.org/licenses/>.
   15  */
   16 
   17 #include <string.h>
   18 #include <tap/basic.h>
   19 
   20 #include "contrib/dynarray.h"
   21 
   22 #define test_capacity 5
   23 // minimum 3
   24 
   25 typedef struct {
   26     int x;
   27     int x2;
   28 } quadrate_t;
   29 
   30 dynarray_declare(q, quadrate_t, DYNARRAY_VISIBILITY_STATIC, test_capacity);
   31 dynarray_define(q, quadrate_t, DYNARRAY_VISIBILITY_STATIC);
   32 
   33 static q_dynarray_t q_fill(size_t howmany)
   34 {
   35     quadrate_t q = { 0 };
   36     q_dynarray_t qd = { 0 };
   37 
   38     for (size_t i = 0; i < howmany; i++) {
   39         q.x2 = q.x * q.x;
   40         q_dynarray_add(&qd, &q);
   41         q.x++;
   42     }
   43     return qd;
   44 }
   45 
   46 static void check_arr(q_dynarray_t *q, size_t count, size_t index, const char *msg)
   47 {
   48     quadrate_t *arr = q->arr(q);
   49     ok(arr[index].x == index && arr[index].x2 == index * index,
   50        "%s check: index %zu", msg, index);
   51 
   52     size_t i = 0;
   53     dynarray_foreach(q, quadrate_t, p, *q) {
   54         ok(p->x == i && p->x2 == i * i, "%s foreach: index %zu", msg, i);
   55         i++;
   56     }
   57 
   58     ok(i == count, "%s foreach: whole array", msg);
   59 }
   60 
   61 int main(int argc, char *argv[])
   62 {
   63     plan_lazy();
   64 
   65     // first fill
   66     q_dynarray_t q = q_fill(test_capacity - 1);
   67     check_arr(&q, test_capacity - 1, test_capacity - 3, "initial");
   68     q_dynarray_free(&q);
   69 
   70     // second fill
   71     q = q_fill(test_capacity + 3);
   72     check_arr(&q, test_capacity + 3, test_capacity + 1, "second");
   73     q_dynarray_free(&q);
   74 
   75     // third fill
   76     q = q_fill(test_capacity * 5);
   77     check_arr(&q, test_capacity * 5, test_capacity * 4, "third");
   78     q_dynarray_free(&q);
   79 
   80     return 0;
   81 }