diff options
author | Jose Bollo <jose.bollo@iot.bzh> | 2019-11-19 13:43:06 +0100 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2019-11-29 12:48:17 +0100 |
commit | 0fd887b7ab896e47b2f7ffe78f41738f3824a962 (patch) | |
tree | c8e1e39c1969f65387543bec49d460a4787f2624 /src/tests | |
parent | 5ac7bb0d9d16260d2235820e97ab47943ffc307b (diff) |
u16id: Add maps for identifiers id of 16 bits
These maps will shortly be used by protocol to handle
tokens/sessions/events.
Bug-AGL: SPEC-2968
Change-Id: Iadef7d6e01c8ef021516749524b10ccc1abec340
Signed-off-by: Jose Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/tests/u16id/CMakeLists.txt | 23 | ||||
-rw-r--r-- | src/tests/u16id/test-u16id.c | 157 |
3 files changed, 181 insertions, 0 deletions
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index ead6790e..ab5c6edd 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -26,6 +26,7 @@ if(check_FOUND) add_subdirectory(apiv3) add_subdirectory(wrap-json) add_subdirectory(globset) + add_subdirectory(u16id) else(check_FOUND) MESSAGE(WARNING "check not found! no test!") endif(check_FOUND) diff --git a/src/tests/u16id/CMakeLists.txt b/src/tests/u16id/CMakeLists.txt new file mode 100644 index 00000000..4141611e --- /dev/null +++ b/src/tests/u16id/CMakeLists.txt @@ -0,0 +1,23 @@ +########################################################################### +# Copyright (C) 2017-2019 "IoT.bzh" +# +# author: José Bollo <jose.bollo@iot.bzh> +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +########################################################################### + +add_executable(test-u16id test-u16id.c) +target_include_directories(test-u16id PRIVATE ../..) +target_link_libraries(test-u16id afb-lib ${link_libraries}) +add_test(NAME u16id COMMAND test-u16id) + diff --git a/src/tests/u16id/test-u16id.c b/src/tests/u16id/test-u16id.c new file mode 100644 index 00000000..54257414 --- /dev/null +++ b/src/tests/u16id/test-u16id.c @@ -0,0 +1,157 @@ +#include <stdlib.h> +#include <stdio.h> +#include <stdarg.h> +#include <errno.h> +#include <string.h> +#include <stdint.h> + +#include <check.h> + +#include "u16id.h" + +/*********************************************************************/ + +#define S 29 + +void cbi2p(void*closure, uint16_t id, void *ptr) +{ + int *y = closure; + int ni = y[0]; + ck_assert((ni >> id) & 1); + ck_assert((uintptr_t)ptr == (uintptr_t)(ni + id)); + y[1]++; +} + +void test_i2ptr(struct u16id2ptr **pi2p) +{ + int i, ni, n, x, y[2]; + uint16_t j; + void *p; + + i = 0; + while (!(i >> S)) { + ni = i * 3 + 1; + n = 0; + for (j = 0 ; j < S ; j++) { + if ((i >> j) & 1) { + ck_assert_int_eq(1, u16id2ptr_has(*pi2p, j)); + ck_assert_int_eq(0, u16id2ptr_get(*pi2p, j, &p)); + ck_assert((uintptr_t)p == (uintptr_t)(i + j)); + ck_assert_int_eq(-1, u16id2ptr_add(pi2p, j, p)); + ck_assert_int_eq(0, u16id2ptr_put(*pi2p, j, p)); + } else { + ck_assert_int_eq(0, u16id2ptr_has(*pi2p, j)); + ck_assert_int_eq(-1, u16id2ptr_get(*pi2p, j, &p)); + ck_assert_int_eq(-1, u16id2ptr_put(*pi2p, j, p)); + } + if ((ni >> j) & 1) { + p = (void*)(uintptr_t)(ni + j); + ck_assert_int_eq(0, u16id2ptr_set(pi2p, j, p)); + n++; + } else if ((i >> j) & 1) { + ck_assert_int_eq(0, u16id2ptr_drop(pi2p, j, &p)); + ck_assert((uintptr_t)p == (uintptr_t)(i + j)); + } else { + ck_assert_int_eq(-1, u16id2ptr_drop(pi2p, j, NULL)); + } + } + ck_assert_int_eq(n, u16id2ptr_count(*pi2p)); + for (x = 0 ; x < n ; x++) { + ck_assert_int_eq(0, u16id2ptr_at(*pi2p, x, &j, &p)); + ck_assert((ni >> j) & 1); + ck_assert((uintptr_t)p == (uintptr_t)(ni + j)); + } + y[0] = ni; + y[1] = 0; + u16id2ptr_forall(*pi2p, cbi2p, y); + ck_assert_int_eq(n, y[1]); + i = ni; + } +} + +START_TEST (check_u16id2ptr) +{ + struct u16id2ptr *i2p; + + i2p = NULL; + test_i2ptr(&i2p); + ck_assert(i2p); + u16id2ptr_destroy(&i2p); + ck_assert(!i2p); + ck_assert_int_eq(0, u16id2ptr_create(&i2p)); + test_i2ptr(&i2p); + ck_assert(i2p); + u16id2ptr_destroy(&i2p); + ck_assert(!i2p); +} +END_TEST + +/*********************************************************************/ + +void test_i2bool(struct u16id2bool **pi2b) +{ + int i, j, ni, v; + uint16_t x; + + i = 0; + while (!(i >> S)) { + ni = i * 3 + 1; + for (j = 0 ; j < S ; j++) { + x = (uint16_t)(j * 5); + v = (i >> j) & 1; + ck_assert_int_eq(v, u16id2bool_get(*pi2b, x)); + ck_assert_int_eq(v, u16id2bool_set(pi2b, x, (ni >> j) & 1)); + } + i = ni; + } + for (j = 0 ; j < S ; j++) { + x = (uint16_t)(j * 5); + v = (i >> j) & 1; + ck_assert_int_eq(v, u16id2bool_get(*pi2b, x)); + ck_assert_int_eq(v, u16id2bool_set(pi2b, x, 0)); + } +} + +START_TEST (check_u16id2bool) +{ + struct u16id2bool *i2b; + + i2b = NULL; + test_i2bool(&i2b); + ck_assert(i2b); + u16id2bool_destroy(&i2b); + ck_assert(!i2b); + ck_assert_int_eq(0, u16id2bool_create(&i2b)); + test_i2bool(&i2b); + ck_assert(i2b); + u16id2bool_destroy(&i2b); + ck_assert(!i2b); +} +END_TEST + +/*********************************************************************/ + +static Suite *suite; +static TCase *tcase; + +void mksuite(const char *name) { suite = suite_create(name); } +void addtcase(const char *name) { tcase = tcase_create(name); suite_add_tcase(suite, tcase); tcase_set_timeout(tcase, 120); } +void addtest(TFun fun) { tcase_add_test(tcase, fun); } +int srun() +{ + int nerr; + SRunner *srunner = srunner_create(suite); + srunner_run_all(srunner, CK_NORMAL); + nerr = srunner_ntests_failed(srunner); + srunner_free(srunner); + return nerr; +} + +int main(int ac, char **av) +{ + mksuite("u16id"); + addtcase("u16id"); + addtest(check_u16id2ptr); + addtest(check_u16id2bool); + return !!srun(); +} |