test/main.c

Wed, 27 Feb 2013 09:41:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 09:41:17 +0100
changeset 88
18823857ce79
parent 76
655020a30e77
child 89
47f7fdbddb62
permissions
-rw-r--r--

variadic test subroutines

olaf@0 1 /*
olaf@0 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@0 3 *
olaf@0 4 * Copyright 2011 Olaf Wintermann. All rights reserved.
olaf@0 5 *
olaf@0 6 * Redistribution and use in source and binary forms, with or without
olaf@0 7 * modification, are permitted provided that the following conditions are met:
olaf@0 8 *
olaf@0 9 * 1. Redistributions of source code must retain the above copyright
olaf@0 10 * notice, this list of conditions and the following disclaimer.
olaf@0 11 *
olaf@0 12 * 2. Redistributions in binary form must reproduce the above copyright
olaf@0 13 * notice, this list of conditions and the following disclaimer in the
olaf@0 14 * documentation and/or other materials provided with the distribution.
olaf@0 15 *
olaf@0 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
olaf@0 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
olaf@0 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
olaf@0 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
olaf@0 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
olaf@0 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
olaf@0 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
olaf@0 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
olaf@0 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
olaf@0 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
olaf@0 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@0 27 */
olaf@0 28
olaf@0 29 #include <stdio.h>
olaf@0 30 #include <stdlib.h>
olaf@0 31
universe@26 32 #include "ucx/test.h"
universe@26 33
universe@27 34 #include "main.h"
universe@27 35
universe@54 36 #include "logging_tests.h"
olaf@9 37 #include "list_tests.h"
universe@27 38 #include "dlist_tests.h"
universe@39 39 #include "string_tests.h"
olaf@13 40 #include "mpool_tests.h"
olaf@20 41 #include "map_tests.h"
universe@60 42 #include "buffer_tests.h"
olaf@9 43
universe@27 44 int cmp_string(void* o1, void* o2, void* data) {
universe@27 45 return strcmp((char*)o1, (char*)o2);
universe@27 46 }
universe@27 47
universe@27 48 void* copy_string(void* e, void* data) {
universe@27 49 char *str = (char*) e;
universe@27 50 size_t n = 1+strlen(str);
universe@27 51 char *cpy = (char*) malloc(n);
universe@27 52 memcpy(cpy, str, n);
universe@27 53 return cpy;
universe@27 54 }
universe@27 55
universe@33 56 UCX_TEST_IMPLEMENT(testTestSuitePositive) {
universe@33 57 UCX_TEST_BEGIN
universe@40 58 UCX_TEST_ASSERT(2*2 == 4, "the test framework fails");
universe@26 59 UCX_TEST_END
universe@26 60 }
universe@26 61
universe@33 62 UCX_TEST_IMPLEMENT(testTestSuiteNegative) {
universe@33 63 UCX_TEST_BEGIN
universe@40 64 UCX_TEST_ASSERT(2*(-2) == 4, "the test framework works");
universe@26 65 UCX_TEST_END
universe@26 66 }
universe@26 67
universe@88 68 UCX_TEST_SUBROUTINE(testTestSuiteRoutineRoutine, float f) {
universe@88 69 UCX_TEST_ASSERT(f == 3.14f, "calling routine in a routine fails");
universe@88 70 }
universe@88 71
universe@88 72 UCX_TEST_SUBROUTINE(testTestSuiteRoutine2Param, int i, float f) {
universe@88 73 UCX_TEST_ASSERT(i == 42, "two parameter routine fails");
universe@88 74 UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineRoutine, f);
universe@88 75 }
universe@88 76
universe@88 77 UCX_TEST_SUBROUTINE(testTestSuiteRoutineSuccess, int* i) {
universe@33 78 *i += 2;
universe@33 79 UCX_TEST_ASSERT(*i==4, "the test framework fails");
universe@33 80 }
universe@33 81
universe@88 82 UCX_TEST_SUBROUTINE(testTestSuiteRoutineFailure, int* i) {
universe@33 83 *i += 2;
universe@88 84 // Next test shall fail!
universe@33 85 UCX_TEST_ASSERT(*i==4, "the test framework works");
universe@33 86 }
universe@33 87
universe@33 88 UCX_TEST_IMPLEMENT(testTestSuiteRoutinePositive) {
universe@33 89 int i = 2;
universe@33 90 UCX_TEST_BEGIN
universe@33 91 UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineSuccess, &i);
universe@33 92 UCX_TEST_ASSERT(i==4, "the test framework fails");
universe@33 93 UCX_TEST_END
universe@33 94 }
universe@33 95
universe@33 96 UCX_TEST_IMPLEMENT(testTestSuiteRoutineNegative) {
universe@33 97 int i = 0;
universe@33 98 UCX_TEST_BEGIN
universe@33 99 UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineFailure, &i);
universe@33 100 UCX_TEST_ASSERT(1, "the test framework fails");
universe@33 101 UCX_TEST_END
universe@33 102 }
universe@33 103
universe@88 104 UCX_TEST_IMPLEMENT(testTestSuiteRoutineMultiparam) {
universe@88 105 UCX_TEST_BEGIN
universe@88 106 UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutine2Param, 42, 3.14f);
universe@88 107 UCX_TEST_END
universe@88 108 }
universe@88 109
olaf@0 110 int main(int argc, char **argv) {
olaf@13 111 printf("UCX Tests\n---------\n");
olaf@9 112
universe@33 113 printf("\nUcxTestSuite tests (2 failures are intended!)\n");
universe@26 114 UcxTestSuite* suite = ucx_test_suite_new();
universe@26 115 ucx_test_register(suite, testTestSuitePositive);
universe@26 116 ucx_test_register(suite, testTestSuiteNegative);
universe@33 117 ucx_test_register(suite, testTestSuiteRoutinePositive);
universe@33 118 ucx_test_register(suite, testTestSuiteRoutineNegative);
universe@88 119 ucx_test_register(suite, testTestSuiteRoutineMultiparam);
universe@26 120 ucx_test_run(suite, stdout);
universe@88 121 if (suite->failure == 2 && suite->success == 3) {
universe@26 122 ucx_test_suite_free(suite);
universe@27 123
universe@28 124 printf("\nLibrary function tests\n");
universe@27 125 suite = ucx_test_suite_new();
universe@54 126 /* UcxLogger Tests */
universe@54 127 ucx_test_register(suite, test_ucx_logger_log);
universe@54 128
universe@27 129 /* UcxList Tests */
universe@27 130 ucx_test_register(suite, test_ucx_list_append);
universe@27 131 ucx_test_register(suite, test_ucx_list_prepend);
universe@27 132 ucx_test_register(suite, test_ucx_list_equals);
universe@27 133 ucx_test_register(suite, test_ucx_list_concat);
universe@27 134 ucx_test_register(suite, test_ucx_list_size);
universe@27 135 ucx_test_register(suite, test_ucx_list_last);
universe@27 136 ucx_test_register(suite, test_ucx_list_get);
universe@27 137 ucx_test_register(suite, test_ucx_list_remove);
universe@27 138 ucx_test_register(suite, test_ucx_list_clone);
universe@36 139 ucx_test_register(suite, test_ucx_list_sort);
universe@27 140
universe@27 141 /* UcxDlist Tests */
universe@27 142 ucx_test_register(suite, test_ucx_dlist_append);
universe@27 143 ucx_test_register(suite, test_ucx_dlist_prepend);
universe@27 144 ucx_test_register(suite, test_ucx_dlist_equals);
universe@27 145 ucx_test_register(suite, test_ucx_dlist_concat);
universe@27 146 ucx_test_register(suite, test_ucx_dlist_size);
universe@27 147 ucx_test_register(suite, test_ucx_dlist_first);
universe@27 148 ucx_test_register(suite, test_ucx_dlist_last);
universe@27 149 ucx_test_register(suite, test_ucx_dlist_get);
universe@27 150 ucx_test_register(suite, test_ucx_dlist_remove);
universe@27 151 ucx_test_register(suite, test_ucx_dlist_clone);
universe@36 152 ucx_test_register(suite, test_ucx_dlist_sort);
universe@27 153
universe@28 154 /* UcxMemPool Tests */
universe@28 155 ucx_test_register(suite, test_ucx_mempool_new);
universe@28 156 ucx_test_register(suite, test_ucx_mempool_malloc);
universe@28 157 ucx_test_register(suite, test_ucx_mempool_malloc_with_chcap);
universe@28 158 ucx_test_register(suite, test_ucx_mempool_calloc);
universe@28 159 ucx_test_register(suite, test_ucx_mempool_set_destr);
universe@28 160 ucx_test_register(suite, test_ucx_mempool_reg_destr);
universe@28 161 ucx_test_register(suite, test_ucx_mempool_realloc);
universe@26 162
universe@29 163 /* UcxMap Tests */
universe@29 164 ucx_test_register(suite, test_ucx_map_new);
universe@29 165 ucx_test_register(suite, test_ucx_key);
universe@29 166 ucx_test_register(suite, test_ucx_map_put);
universe@29 167 ucx_test_register(suite, test_ucx_map_get);
universe@53 168 ucx_test_register(suite, test_ucx_map_remove);
olaf@31 169 ucx_test_register(suite, test_ucx_map_iterator);
universe@33 170 ucx_test_register(suite, test_ucx_map_iterator_chain);
universe@42 171 ucx_test_register(suite, test_ucx_map_store_load);
universe@48 172 ucx_test_register(suite, test_ucx_map_store_load_with_mempool);
olaf@44 173 ucx_test_register(suite, test_ucx_map_clone);
universe@51 174 ucx_test_register(suite, test_ucx_map_rehash);
universe@26 175
universe@39 176 /* sstring Tests */
olaf@47 177 ucx_test_register(suite, test_sstr);
olaf@47 178 ucx_test_register(suite, test_sstr_len_cat);
universe@39 179 ucx_test_register(suite, test_sstrsplit);
universe@39 180
universe@56 181 /* UcxMemstream Tests */
universe@60 182 ucx_test_register(suite, test_ucx_buffer_seektell);
universe@60 183 ucx_test_register(suite, test_ucx_buffer_putc);
olaf@76 184 ucx_test_register(suite, test_ucx_buffer_putc_ax);
universe@60 185 ucx_test_register(suite, test_ucx_buffer_getc);
universe@60 186 ucx_test_register(suite, test_ucx_buffer_write);
universe@64 187 ucx_test_register(suite, test_ucx_buffer_write_ax);
universe@60 188 ucx_test_register(suite, test_ucx_buffer_read);
universe@62 189 ucx_test_register(suite, test_ucx_buffer_extract);
olaf@76 190 ucx_test_register(suite, test_ucx_buffer_generic_copy);
universe@56 191
universe@27 192 ucx_test_run(suite, stdout);
universe@55 193 fflush(stdout);
universe@27 194 ucx_test_suite_free(suite);
universe@27 195
universe@26 196 return EXIT_SUCCESS;
universe@26 197 } else {
universe@26 198 ucx_test_suite_free(suite);
universe@26 199 return EXIT_FAILURE;
olaf@13 200 }
olaf@0 201 }
olaf@0 202

mercurial