tests/test_utils.c

Mon, 04 Mar 2024 08:57:26 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 04 Mar 2024 08:57:26 +0100
changeset 841
93851c0babe4
parent 768
0e1cf2cd500e
permissions
-rw-r--r--

fix build in case CX_SZMUL_BUILTIN is undefined

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2023 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 "cx/test.h"
    31 #include "cx/utils.h"
    32 #include "cx/buffer.h"
    34 CX_TEST(test_stream_bncopy) {
    35     CxBuffer source, target;
    36     char sbuf[32], tbuf[32];
    37     memset(tbuf, 0, 32);
    38     cxBufferInit(&source, sbuf, 32, NULL, 0);
    39     cxBufferInit(&target, tbuf, 32, NULL, 0);
    40     cxBufferPutString(&source, "This is a stream copy test.");
    41     cxBufferSeek(&source, 0, SEEK_SET);
    42     char tmp[4];
    44     CX_TEST_DO {
    45         size_t result = cx_stream_bncopy(&source, &target,
    46                                          (cx_read_func) cxBufferRead,
    47                                          (cx_write_func) cxBufferWrite,
    48                                          tmp, 4, 20);
    49         CX_TEST_ASSERT(result == 20);
    50         CX_TEST_ASSERT(target.size == 20);
    51         CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
    53         result = cx_stream_bcopy(&source, &target,
    54                                  (cx_read_func) cxBufferRead,
    55                                  (cx_write_func) cxBufferWrite,
    56                                  NULL, 16);
    58         CX_TEST_ASSERT(result == 7);
    59         CX_TEST_ASSERT(target.size == 27);
    60         CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0);
    61     }
    63     cxBufferDestroy(&source);
    64     cxBufferDestroy(&target);
    65 }
    67 CX_TEST(test_stream_ncopy) {
    68     CxBuffer source, target;
    69     char sbuf[32], tbuf[32];
    70     memset(tbuf, 0, 32);
    71     cxBufferInit(&source, sbuf, 32, NULL, 0);
    72     cxBufferInit(&target, tbuf, 32, NULL, 0);
    73     cxBufferPutString(&source, "This is a stream copy test.");
    74     cxBufferSeek(&source, 0, SEEK_SET);
    76     CX_TEST_DO {
    77         size_t result = cx_stream_ncopy(&source, &target,
    78                                         (cx_read_func) cxBufferRead,
    79                                         (cx_write_func) cxBufferWrite,
    80                                         20);
    81         CX_TEST_ASSERT(result == 20);
    82         CX_TEST_ASSERT(target.size == 20);
    83         CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0);
    85         result = cx_stream_copy(&source, &target,
    86                                 (cx_read_func) cxBufferRead,
    87                                 (cx_write_func) cxBufferWrite);
    89         CX_TEST_ASSERT(result == 7);
    90         CX_TEST_ASSERT(target.size == 27);
    91         CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0);
    92     }
    94     cxBufferDestroy(&source);
    95     cxBufferDestroy(&target);
    96 }
    98 CX_TEST(test_forn) {
    99     unsigned j;
   100     j = 0;
   101     CX_TEST_DO {
   102         cx_for_n(i, 50) {
   103             CX_TEST_ASSERT(i == j);
   104             j++;
   105         }
   106     }
   107 }
   109 CX_TEST(test_swap_ptr) {
   110     int i = 5;
   111     int j = 8;
   112     int *ip = &i;
   113     int *jp = &j;
   114     CX_TEST_DO {
   115         cx_swap_ptr(ip, jp);
   116         CX_TEST_ASSERT(ip == &j);
   117         CX_TEST_ASSERT(jp == &i);
   118     }
   119 }
   121 CX_TEST(test_szmul) {
   122     size_t r;
   123     int e;
   125     CX_TEST_DO {
   126         e = cx_szmul(5, 7, &r);
   127         CX_TEST_ASSERT(e == 0);
   128         CX_TEST_ASSERT(r == 35);
   130         size_t s = SIZE_MAX & ~3;
   132         e = cx_szmul(s / 4, 2, &r);
   133         CX_TEST_ASSERT(e == 0);
   134         CX_TEST_ASSERT(r == s / 2);
   136         e = cx_szmul(2, s / 4, &r);
   137         CX_TEST_ASSERT(e == 0);
   138         CX_TEST_ASSERT(r == s / 2);
   140         e = cx_szmul(s / 4, 4, &r);
   141         CX_TEST_ASSERT(e == 0);
   142         CX_TEST_ASSERT(r == s);
   144         e = cx_szmul(4, s / 4, &r);
   145         CX_TEST_ASSERT(e == 0);
   146         CX_TEST_ASSERT(r == s);
   148         e = cx_szmul(s / 4, 5, &r);
   149         CX_TEST_ASSERT(e != 0);
   151         e = cx_szmul(5, s / 4, &r);
   152         CX_TEST_ASSERT(e != 0);
   154         e = cx_szmul(SIZE_MAX - 4, 0, &r);
   155         CX_TEST_ASSERT(e == 0);
   156         CX_TEST_ASSERT(r == 0);
   158         e = cx_szmul(0, SIZE_MAX - 1, &r);
   159         CX_TEST_ASSERT(e == 0);
   160         CX_TEST_ASSERT(r == 0);
   162         e = cx_szmul(SIZE_MAX, 0, &r);
   163         CX_TEST_ASSERT(e == 0);
   164         CX_TEST_ASSERT(r == 0);
   166         e = cx_szmul(0, SIZE_MAX, &r);
   167         CX_TEST_ASSERT(e == 0);
   168         CX_TEST_ASSERT(r == 0);
   170         e = cx_szmul(0, 0, &r);
   171         CX_TEST_ASSERT(e == 0);
   172         CX_TEST_ASSERT(r == 0);
   173     }
   174 }
   176 #ifdef CX_SZMUL_BUILTIN
   178 // also test the custom implementation
   179 #undef CX_SZMUL_BUILTIN
   181 #include "../src/szmul.c"
   183 #define CX_SZMUL_BUILTIN
   185 CX_TEST(test_szmul_impl) {
   186     size_t r;
   187     int e;
   189     CX_TEST_DO {
   190         e = cx_szmul_impl(5, 7, &r);
   191         CX_TEST_ASSERT(e == 0);
   192         CX_TEST_ASSERT(r == 35);
   194         size_t s = SIZE_MAX & ~3;
   196         e = cx_szmul_impl(s / 4, 2, &r);
   197         CX_TEST_ASSERT(e == 0);
   198         CX_TEST_ASSERT(r == s / 2);
   200         e = cx_szmul_impl(2, s / 4, &r);
   201         CX_TEST_ASSERT(e == 0);
   202         CX_TEST_ASSERT(r == s / 2);
   204         e = cx_szmul_impl(s / 4, 4, &r);
   205         CX_TEST_ASSERT(e == 0);
   206         CX_TEST_ASSERT(r == s);
   208         e = cx_szmul_impl(4, s / 4, &r);
   209         CX_TEST_ASSERT(e == 0);
   210         CX_TEST_ASSERT(r == s);
   212         e = cx_szmul_impl(s / 4, 5, &r);
   213         CX_TEST_ASSERT(e != 0);
   215         e = cx_szmul_impl(5, s / 4, &r);
   216         CX_TEST_ASSERT(e != 0);
   218         e = cx_szmul_impl(SIZE_MAX - 4, 0, &r);
   219         CX_TEST_ASSERT(e == 0);
   220         CX_TEST_ASSERT(r == 0);
   222         e = cx_szmul_impl(0, SIZE_MAX - 1, &r);
   223         CX_TEST_ASSERT(e == 0);
   224         CX_TEST_ASSERT(r == 0);
   226         e = cx_szmul_impl(SIZE_MAX, 0, &r);
   227         CX_TEST_ASSERT(e == 0);
   228         CX_TEST_ASSERT(r == 0);
   230         e = cx_szmul_impl(0, SIZE_MAX, &r);
   231         CX_TEST_ASSERT(e == 0);
   232         CX_TEST_ASSERT(r == 0);
   234         e = cx_szmul_impl(0, 0, &r);
   235         CX_TEST_ASSERT(e == 0);
   236         CX_TEST_ASSERT(r == 0);
   237     }
   238 }
   240 #endif // CX_SZMUL_BUILTIN
   243 CxTestSuite *cx_test_suite_utils(void) {
   244     CxTestSuite *suite = cx_test_suite_new("utils");
   246     cx_test_register(suite, test_stream_bncopy);
   247     cx_test_register(suite, test_stream_ncopy);
   248     cx_test_register(suite, test_forn);
   249     cx_test_register(suite, test_swap_ptr);
   250     cx_test_register(suite, test_szmul);
   251 #ifdef CX_SZMUL_BUILTIN
   252     cx_test_register(suite, test_szmul_impl);
   253 #endif
   255     return suite;
   256 }

mercurial