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