diff --git a/Knot.files b/Knot.files index 91431d1fe585c5bb89884c47a81bc812bc35e60f..7e33291ff3696abd80ee332eeac3cbc01aeee036 100644 --- a/Knot.files +++ b/Knot.files @@ -553,6 +553,7 @@ tests-fuzz/wrap/tcp-handler.c tests-fuzz/wrap/udp-handler.c tests/contrib/test_base32hex.c tests/contrib/test_base64.c +tests/contrib/test_dynarray.c tests/contrib/test_endian.c tests/contrib/test_heap.c tests/contrib/test_hhash.c diff --git a/tests/.gitignore b/tests/.gitignore index e74e5bac1804e3fe02f0c48925fe253eca75ad20..272972ffe0fa36a43fce9bd6d6fc083d078e9935 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -4,6 +4,7 @@ /contrib/test_base32hex /contrib/test_base64 +/contrib/test_dynarray /contrib/test_endian /contrib/test_heap /contrib/test_hhash diff --git a/tests/Makefile.am b/tests/Makefile.am index 898063bbbdc248aa041d6707fcee298cc8c58296..e3998ce9d72a4e281175400c7b4c9a3565ecd1af 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -14,6 +14,7 @@ LDADD = \ check_PROGRAMS = \ contrib/test_base32hex \ contrib/test_base64 \ + contrib/test_dynarray \ contrib/test_endian \ contrib/test_heap \ contrib/test_hhash \ diff --git a/tests/contrib/test_dynarray.c b/tests/contrib/test_dynarray.c new file mode 100644 index 0000000000000000000000000000000000000000..ea439b7d8786f41a6ec6ff0378f07594b6db0049 --- /dev/null +++ b/tests/contrib/test_dynarray.c @@ -0,0 +1,59 @@ +/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include "contrib/dynarray.h" +#include <tap/basic.h> + +#define test_capacity 2 + +#define test_type(type, prefix) \ + dynarray_define(prefix, type, test_capacity) \ + static void prefix ## _test(type const first, type const second) { \ + struct prefix ## _dynarray array = { 0 }; \ + prefix ## _dynarray_fix(&array); \ + ok(array.capacity == test_capacity && array.size == 0, \ + "%s: Fix - initial capacity set", #prefix); \ + prefix ## _dynarray_add(&array, &first); \ + ok(array.capacity == test_capacity && array.size == 1 && array.arr[0] == first, \ + "%s: Add item", #prefix); \ + prefix ## _dynarray_add(&array, &second); \ + ok(array.capacity == test_capacity && array.size == 2 && array.arr[1] == second, \ + "%s: Array filled (size not changed yet)", #prefix); \ + prefix ## _dynarray_add(&array, &first); \ + ok(array.capacity == 2*test_capacity+1 && array.size == 3 && array.arr[2] == first, \ + "%s: Array extended", #prefix); \ + prefix ## _dynarray_free(&array); \ + prefix ## _dynarray_add(&array, &first); \ + ok(array.capacity == test_capacity && array.size == 1 && array.arr[0] == first, \ + "%s: Free & add first- initial capacity set", #prefix); \ + } + +test_type(int, int) +test_type(char*, string) + +int main (int argc, char *argv[]) +{ + plan_lazy(); + + int_test(4, 2); + char a = 'a', b = 'b'; + string_test(&a, &b); + + return 0; +}