Wed, 26 Aug 2026 16:03:54 +0200
fix that test.h is not standalone - fixes #927
| 970 | 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 | */ | |
| 28 | ||
| 29 | #include "cx/test.h" | |
|
1710
d53be93acc10
fix that test.h is not standalone - fixes #927
Mike Becker <universe@uap-core.de>
parents:
970
diff
changeset
|
30 | #include "cx/common.h" |
|
d53be93acc10
fix that test.h is not standalone - fixes #927
Mike Becker <universe@uap-core.de>
parents:
970
diff
changeset
|
31 | |
|
d53be93acc10
fix that test.h is not standalone - fixes #927
Mike Becker <universe@uap-core.de>
parents:
970
diff
changeset
|
32 | #include <stdint.h> |
| 970 | 33 | |
| 34 | CX_TEST(test_szmul) { | |
| 35 | size_t r; | |
| 36 | int e; | |
| 37 | ||
| 38 | CX_TEST_DO { | |
| 39 | e = cx_szmul(5, 7, &r); | |
| 40 | CX_TEST_ASSERT(e == 0); | |
| 41 | CX_TEST_ASSERT(r == 35); | |
| 42 | ||
| 43 | size_t s = SIZE_MAX & ~3; | |
| 44 | ||
| 45 | e = cx_szmul(s / 4, 2, &r); | |
| 46 | CX_TEST_ASSERT(e == 0); | |
| 47 | CX_TEST_ASSERT(r == s / 2); | |
| 48 | ||
| 49 | e = cx_szmul(2, s / 4, &r); | |
| 50 | CX_TEST_ASSERT(e == 0); | |
| 51 | CX_TEST_ASSERT(r == s / 2); | |
| 52 | ||
| 53 | e = cx_szmul(s / 4, 4, &r); | |
| 54 | CX_TEST_ASSERT(e == 0); | |
| 55 | CX_TEST_ASSERT(r == s); | |
| 56 | ||
| 57 | e = cx_szmul(4, s / 4, &r); | |
| 58 | CX_TEST_ASSERT(e == 0); | |
| 59 | CX_TEST_ASSERT(r == s); | |
| 60 | ||
| 61 | e = cx_szmul(s / 4, 5, &r); | |
| 62 | CX_TEST_ASSERT(e != 0); | |
| 63 | ||
| 64 | e = cx_szmul(5, s / 4, &r); | |
| 65 | CX_TEST_ASSERT(e != 0); | |
| 66 | ||
| 67 | e = cx_szmul(SIZE_MAX - 4, 0, &r); | |
| 68 | CX_TEST_ASSERT(e == 0); | |
| 69 | CX_TEST_ASSERT(r == 0); | |
| 70 | ||
| 71 | e = cx_szmul(0, SIZE_MAX - 1, &r); | |
| 72 | CX_TEST_ASSERT(e == 0); | |
| 73 | CX_TEST_ASSERT(r == 0); | |
| 74 | ||
| 75 | e = cx_szmul(SIZE_MAX, 0, &r); | |
| 76 | CX_TEST_ASSERT(e == 0); | |
| 77 | CX_TEST_ASSERT(r == 0); | |
| 78 | ||
| 79 | e = cx_szmul(0, SIZE_MAX, &r); | |
| 80 | CX_TEST_ASSERT(e == 0); | |
| 81 | CX_TEST_ASSERT(r == 0); | |
| 82 | ||
| 83 | e = cx_szmul(0, 0, &r); | |
| 84 | CX_TEST_ASSERT(e == 0); | |
| 85 | CX_TEST_ASSERT(r == 0); | |
| 86 | } | |
| 87 | } | |
| 88 | ||
| 89 | #ifdef CX_SZMUL_BUILTIN | |
| 90 | ||
| 91 | // also test the custom implementation | |
| 92 | #undef CX_SZMUL_BUILTIN | |
| 93 | ||
| 94 | #include "../src/szmul.c" | |
| 95 | ||
| 96 | #define CX_SZMUL_BUILTIN | |
| 97 | ||
| 98 | CX_TEST(test_szmul_impl) { | |
| 99 | size_t r; | |
| 100 | int e; | |
| 101 | ||
| 102 | CX_TEST_DO { | |
| 103 | e = cx_szmul_impl(5, 7, &r); | |
| 104 | CX_TEST_ASSERT(e == 0); | |
| 105 | CX_TEST_ASSERT(r == 35); | |
| 106 | ||
| 107 | size_t s = SIZE_MAX & ~3; | |
| 108 | ||
| 109 | e = cx_szmul_impl(s / 4, 2, &r); | |
| 110 | CX_TEST_ASSERT(e == 0); | |
| 111 | CX_TEST_ASSERT(r == s / 2); | |
| 112 | ||
| 113 | e = cx_szmul_impl(2, s / 4, &r); | |
| 114 | CX_TEST_ASSERT(e == 0); | |
| 115 | CX_TEST_ASSERT(r == s / 2); | |
| 116 | ||
| 117 | e = cx_szmul_impl(s / 4, 4, &r); | |
| 118 | CX_TEST_ASSERT(e == 0); | |
| 119 | CX_TEST_ASSERT(r == s); | |
| 120 | ||
| 121 | e = cx_szmul_impl(4, s / 4, &r); | |
| 122 | CX_TEST_ASSERT(e == 0); | |
| 123 | CX_TEST_ASSERT(r == s); | |
| 124 | ||
| 125 | e = cx_szmul_impl(s / 4, 5, &r); | |
| 126 | CX_TEST_ASSERT(e != 0); | |
| 127 | ||
| 128 | e = cx_szmul_impl(5, s / 4, &r); | |
| 129 | CX_TEST_ASSERT(e != 0); | |
| 130 | ||
| 131 | e = cx_szmul_impl(SIZE_MAX - 4, 0, &r); | |
| 132 | CX_TEST_ASSERT(e == 0); | |
| 133 | CX_TEST_ASSERT(r == 0); | |
| 134 | ||
| 135 | e = cx_szmul_impl(0, SIZE_MAX - 1, &r); | |
| 136 | CX_TEST_ASSERT(e == 0); | |
| 137 | CX_TEST_ASSERT(r == 0); | |
| 138 | ||
| 139 | e = cx_szmul_impl(SIZE_MAX, 0, &r); | |
| 140 | CX_TEST_ASSERT(e == 0); | |
| 141 | CX_TEST_ASSERT(r == 0); | |
| 142 | ||
| 143 | e = cx_szmul_impl(0, SIZE_MAX, &r); | |
| 144 | CX_TEST_ASSERT(e == 0); | |
| 145 | CX_TEST_ASSERT(r == 0); | |
| 146 | ||
| 147 | e = cx_szmul_impl(0, 0, &r); | |
| 148 | CX_TEST_ASSERT(e == 0); | |
| 149 | CX_TEST_ASSERT(r == 0); | |
| 150 | } | |
| 151 | } | |
| 152 | ||
| 153 | #endif // CX_SZMUL_BUILTIN | |
| 154 | ||
| 155 | ||
| 156 | CxTestSuite *cx_test_suite_szmul(void) { | |
| 157 | CxTestSuite *suite = cx_test_suite_new("szmul"); | |
| 158 | ||
| 159 | cx_test_register(suite, test_szmul); | |
| 160 | #ifdef CX_SZMUL_BUILTIN | |
| 161 | cx_test_register(suite, test_szmul_impl); | |
| 162 | #endif | |
| 163 | ||
| 164 | return suite; | |
| 165 | } |