test/main.c

Tue, 24 Sep 2019 20:16:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 24 Sep 2019 20:16:00 +0200
branch
feature/array
changeset 355
d315a068235a
parent 354
7fd13b9f8f60
child 356
77efe51c6c9a
permissions
-rw-r--r--

adds array utility functions for user defined arrays

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include <stdio.h>
    30 #include <stdlib.h>
    32 #include <ucx/test.h>
    34 #include "main.h"
    36 #include "array_tests.h"
    37 #include "allocator_tests.h"
    38 #include "logging_tests.h"
    39 #include "list_tests.h"
    40 #include "string_tests.h"
    41 #include "mpool_tests.h"
    42 #include "stack_tests.h"
    43 #include "map_tests.h"
    44 #include "prop_tests.h"
    45 #include "buffer_tests.h"
    46 #include "utils_tests.h"
    47 #include "avl_tests.h"
    49 #ifdef __cplusplus
    50 extern "C" {
    51 #endif
    52 UCX_TEST(testTestSuitePositive) {
    53     UCX_TEST_BEGIN
    54     UCX_TEST_ASSERT(2*2 == 4, "the test framework fails");
    55     UCX_TEST_END
    56 }
    58 UCX_TEST(testTestSuiteNegative) {
    59     UCX_TEST_BEGIN
    60     UCX_TEST_ASSERT(2*(-2) == 4, "the test framework works");
    61     UCX_TEST_END
    62 }
    64 UCX_TEST_SUBROUTINE(testTestSuiteRoutineRoutine, float f) {
    65     UCX_TEST_ASSERT(f == 3.14f, "calling routine in a routine fails");
    66 }
    68 UCX_TEST_SUBROUTINE(testTestSuiteRoutine2Param, int i, float f) {
    69     UCX_TEST_ASSERT(i == 42, "two parameter routine fails");
    70     UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineRoutine, f);
    71 }
    73 UCX_TEST_SUBROUTINE(testTestSuiteRoutineSuccess, int* i) {
    74     *i += 2;
    75     UCX_TEST_ASSERT(*i==4, "the test framework fails");
    76 }
    78 UCX_TEST_SUBROUTINE(testTestSuiteRoutineFailure, int* i) {
    79     *i += 2;
    80     // Next test shall fail!
    81     UCX_TEST_ASSERT(*i==4, "the test framework works");
    82 }
    84 UCX_TEST(testTestSuiteRoutinePositive) {
    85     int i = 2;
    86     UCX_TEST_BEGIN
    87     UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineSuccess, &i);
    88     UCX_TEST_ASSERT(i==4, "the test framework fails");
    89     UCX_TEST_END
    90 }
    92 UCX_TEST(testTestSuiteRoutineNegative) {
    93     int i = 0;
    94     UCX_TEST_BEGIN
    95     UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineFailure, &i);
    96     UCX_TEST_ASSERT(1, "the test framework fails");
    97     UCX_TEST_END
    98 }
   100 UCX_TEST(testTestSuiteRoutineMultiparam) {
   101     UCX_TEST_BEGIN
   102     UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutine2Param, 42, 3.14f);
   103     UCX_TEST_END
   104 }
   105 #ifdef __cplusplus
   106 }
   107 #endif
   109 int main(int argc, char **argv) {
   110     printf("UCX Tests\n---------\n");
   112     printf("\nUcxTestSuite tests (2 failures are intended!)\n");
   113     UcxTestSuite* suite = ucx_test_suite_new();
   114     ucx_test_register(suite, testTestSuitePositive);
   115     ucx_test_register(suite, testTestSuiteNegative);
   116     ucx_test_register(suite, testTestSuiteRoutinePositive);
   117     ucx_test_register(suite, testTestSuiteRoutineNegative);
   118     ucx_test_register(suite, testTestSuiteRoutineMultiparam);
   119     ucx_test_run(suite, stdout);
   120     if (suite->failure == 2 && suite->success == 3) {
   121         ucx_test_suite_free(suite);
   123         printf("\nLibrary function tests\n");
   124         suite = ucx_test_suite_new();
   125         /* UcxAllocator Tests */
   126         ucx_test_register(suite, test_ucx_default_allocator);
   128         /* sstring Tests */
   129         ucx_test_register(suite, test_sstr_macros);
   130         ucx_test_register(suite, test_sstr);
   131         ucx_test_register(suite, test_sstr_len);
   132         ucx_test_register(suite, test_sstrcmp);
   133         ucx_test_register(suite, test_sstrcasecmp);
   134         ucx_test_register(suite, test_sstrcat);
   135         ucx_test_register(suite, test_sstrchr_sstrrchr);
   136         ucx_test_register(suite, test_sstrstr);
   137         ucx_test_register(suite, test_sstrsplit);
   138         ucx_test_register(suite, test_sstrtrim);
   139         ucx_test_register(suite, test_sstrprefixsuffix);
   141         /* UcxLogger Tests */
   142         ucx_test_register(suite, test_ucx_logger_new);
   143         ucx_test_register(suite, test_ucx_logger_log);
   145         /* UcxArray Tests */
   146         ucx_test_register(suite, test_ucx_array_free);
   147         ucx_test_register(suite, test_ucx_array_new);
   148         ucx_test_register(suite, test_ucx_array_at);
   149         ucx_test_register(suite, test_ucx_array_append_from);
   150         ucx_test_register(suite, test_ucx_array_prepend_from);
   151         ucx_test_register(suite, test_ucx_array_set_from);
   152         ucx_test_register(suite, test_ucx_array_append);
   153         ucx_test_register(suite, test_ucx_array_prepend);
   154         ucx_test_register(suite, test_ucx_array_set);
   155         ucx_test_register(suite, test_ucx_array_autogrow);
   156         ucx_test_register(suite, test_ucx_array_equals);
   157         ucx_test_register(suite, test_ucx_array_concat);
   158         ucx_test_register(suite, test_ucx_array_find);
   159         ucx_test_register(suite, test_ucx_array_contains);
   160         ucx_test_register(suite, test_ucx_array_remove);
   161         ucx_test_register(suite, test_ucx_array_clone);
   162         ucx_test_register(suite, test_ucx_array_sort);
   163         ucx_test_register(suite, test_ucx_array_shrink);
   164         ucx_test_register(suite, test_ucx_array_resize);
   165         ucx_test_register(suite, test_ucx_array_reserve);
   166         ucx_test_register(suite, test_ucx_array_util_set);
   168         /* UcxList Tests */
   169         ucx_test_register(suite, test_ucx_list_append);
   170         ucx_test_register(suite, test_ucx_list_prepend);
   171         ucx_test_register(suite, test_ucx_list_equals);
   172         ucx_test_register(suite, test_ucx_list_concat);
   173         ucx_test_register(suite, test_ucx_list_size);
   174         ucx_test_register(suite, test_ucx_list_first);
   175         ucx_test_register(suite, test_ucx_list_last);
   176         ucx_test_register(suite, test_ucx_list_get);
   177         ucx_test_register(suite, test_ucx_list_indexof);
   178         ucx_test_register(suite, test_ucx_list_find);
   179         ucx_test_register(suite, test_ucx_list_contains);
   180         ucx_test_register(suite, test_ucx_list_remove);
   181         ucx_test_register(suite, test_ucx_list_clone);
   182         ucx_test_register(suite, test_ucx_list_sort);
   184         /* UcxMemPool Tests */
   185         ucx_test_register(suite, test_ucx_mempool_new);
   186         ucx_test_register(suite, test_ucx_mempool_malloc);
   187         ucx_test_register(suite, test_ucx_mempool_malloc_with_chcap);
   188         ucx_test_register(suite, test_ucx_mempool_calloc);
   189         ucx_test_register(suite, test_ucx_mempool_free);
   190         ucx_test_register(suite, test_ucx_mempool_set_destr);
   191         ucx_test_register(suite, test_ucx_mempool_reg_destr);
   192         ucx_test_register(suite, test_ucx_mempool_realloc);
   194         /* UcxStack Tests */
   195         ucx_test_register(suite, test_ucx_stack_init);
   196         ucx_test_register(suite, test_ucx_stack_malloc);
   197         ucx_test_register(suite, test_ucx_stack_calloc);
   198         ucx_test_register(suite, test_ucx_stack_free);
   199         ucx_test_register(suite, test_ucx_stack_realloc);
   200         ucx_test_register(suite, test_ucx_stack_pop);
   202         /* UcxMap Tests */
   203         ucx_test_register(suite, test_ucx_map_new);
   204         ucx_test_register(suite, test_ucx_key);
   205         ucx_test_register(suite, test_ucx_map_put);
   206         ucx_test_register(suite, test_ucx_map_get);
   207         ucx_test_register(suite, test_ucx_map_remove);
   208         ucx_test_register(suite, test_ucx_map_clear);
   209         ucx_test_register(suite, test_ucx_map_iterator);
   210         ucx_test_register(suite, test_ucx_map_iterator_chain);
   211         ucx_test_register(suite, test_ucx_map_clone);
   212         ucx_test_register(suite, test_ucx_map_rehash);
   214         /* UcxPropertiesParser Tests */
   215         ucx_test_register(suite, test_ucx_properties_new);
   216         ucx_test_register(suite, test_ucx_properties_next);
   217         ucx_test_register(suite, test_ucx_properties_next_multi);
   218         ucx_test_register(suite, test_ucx_properties_next_part);
   219         ucx_test_register(suite, test_ucx_properties_next_long);
   220         ucx_test_register(suite, test_ucx_properties2map);
   221         ucx_test_register(suite, test_ucx_properties_load);
   222         ucx_test_register(suite, test_ucx_properties_store);
   224         /* UcxBuffer Tests */
   225         ucx_test_register(suite, test_ucx_buffer_new);
   226         ucx_test_register(suite, test_ucx_buffer_new_prealloc);
   227         ucx_test_register(suite, test_ucx_buffer_eof);
   228         ucx_test_register(suite, test_ucx_buffer_seek_set);
   229         ucx_test_register(suite, test_ucx_buffer_seek_cur);
   230         ucx_test_register(suite, test_ucx_buffer_seek_end);
   231         ucx_test_register(suite, test_ucx_buffer_seek_oob);
   232         ucx_test_register(suite, test_ucx_buffer_seek_invalid);
   233         ucx_test_register(suite, test_ucx_buffer_seek_overflow);
   234         ucx_test_register(suite, test_ucx_buffer_putc);
   235         ucx_test_register(suite, test_ucx_buffer_putc_ae);
   236         ucx_test_register(suite, test_ucx_buffer_putc_oob);
   237         ucx_test_register(suite, test_ucx_buffer_putc_oobae);
   238         ucx_test_register(suite, test_ucx_buffer_putc_size);
   239         ucx_test_register(suite, test_ucx_buffer_getc);
   240         ucx_test_register(suite, test_ucx_buffer_read);
   241         ucx_test_register(suite, test_ucx_buffer_read_oob);
   242         ucx_test_register(suite, test_ucx_buffer_extract);
   243         ucx_test_register(suite, test_ucx_buffer_extract_oob);
   244         ucx_test_register(suite, test_ucx_buffer_extract_overflow);
   245         ucx_test_register(suite, test_ucx_buffer_extend);
   246         ucx_test_register(suite, test_ucx_buffer_write);
   247         ucx_test_register(suite, test_ucx_buffer_write_oob);
   248         ucx_test_register(suite, test_ucx_buffer_write_ax);
   249         ucx_test_register(suite, test_ucx_buffer_shl);
   250         ucx_test_register(suite, test_ucx_buffer_shr);
   251         ucx_test_register(suite, test_ucx_buffer_shr_ax);
   253         /* Utils Tests*/
   254         ucx_test_register(suite, test_ucx_fprintf);
   255         ucx_test_register(suite, test_ucx_asprintf);
   256         ucx_test_register(suite, test_ucx_sprintf);
   257         ucx_test_register(suite, test_ucx_bprintf);
   258         ucx_test_register(suite, test_ucx_stream_copy);
   260         /* AVL Tests */
   261         ucx_test_register(suite, test_ucx_avl_put);
   262         ucx_test_register(suite, test_ucx_avl_remove);
   263         ucx_test_register(suite, test_ucx_avl_find);
   264         ucx_test_register(suite, test_ucx_avl_traverse);
   266         ucx_test_run(suite, stdout);
   267         fflush(stdout);
   269         int exit_code = suite->failure > 0 ? EXIT_FAILURE: EXIT_SUCCESS;
   271         ucx_test_suite_free(suite);
   273         return exit_code;
   274     } else {
   275         ucx_test_suite_free(suite);
   276         return EXIT_FAILURE;
   277     }
   278 }

mercurial