test/test_utils.cpp

Thu, 02 Feb 2023 20:25:34 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 02 Feb 2023 20:25:34 +0100
changeset 645
ec50abb285ad
parent 633
63a692642aa2
child 646
dfd0403ff8b6
permissions
-rw-r--r--

add strtok API - fixes #220

633
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/utils.h"
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include <gtest/gtest.h>
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 TEST(Utils, ForN) {
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 unsigned j;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 j = 0;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 cx_for_n(i, 50) {
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 EXPECT_EQ(i, j);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 j++;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 }
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 }
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 TEST(Utils, szmul) {
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 size_t r;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 int e;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 e = cx_szmul(5, 7, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 EXPECT_EQ(35, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 size_t s = SIZE_MAX & ~3;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 e = cx_szmul(s / 4, 2, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 EXPECT_EQ(s / 2, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 e = cx_szmul(2, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 EXPECT_EQ(s / 2, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 e = cx_szmul(s / 4, 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 EXPECT_EQ(s, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 e = cx_szmul(4, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 EXPECT_EQ(s, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 e = cx_szmul(s / 4, 5, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 EXPECT_NE(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 e = cx_szmul(5, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 EXPECT_NE(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 e = cx_szmul(SIZE_MAX - 4, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 e = cx_szmul(0, SIZE_MAX - 1, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 e = cx_szmul(SIZE_MAX, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 e = cx_szmul(0, SIZE_MAX, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 e = cx_szmul(0, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 }
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 #ifdef CX_SZMUL_BUILTIN
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 // also test the custom implementation
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 struct Utils_szmul_impl : ::testing::Test {
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 #undef CX_SZMUL_BUILTIN
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 #include "../src/utils.c"
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 #define CX_SZMUL_BUILTIN
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 };
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 TEST_F(Utils_szmul_impl, Test) {
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 size_t r;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 int e;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 e = cx_szmul_impl(5, 7, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 EXPECT_EQ(35, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 size_t s = SIZE_MAX & ~3;
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 e = cx_szmul_impl(s / 4, 2, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 EXPECT_EQ(s / 2, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 e = cx_szmul_impl(2, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 EXPECT_EQ(s / 2, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 e = cx_szmul_impl(s / 4, 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 EXPECT_EQ(s, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 e = cx_szmul_impl(4, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 EXPECT_EQ(s, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 e = cx_szmul_impl(s / 4, 5, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 EXPECT_NE(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 e = cx_szmul_impl(5, s / 4, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 EXPECT_NE(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 e = cx_szmul_impl(SIZE_MAX - 4, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 e = cx_szmul_impl(0, SIZE_MAX - 1, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 e = cx_szmul_impl(SIZE_MAX, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 e = cx_szmul_impl(0, SIZE_MAX, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 e = cx_szmul_impl(0, 0, &r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 EXPECT_EQ(0, e);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 EXPECT_EQ(0, r);
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 }
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154
63a692642aa2 add utils tests
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 #endif // CX_SZMUL_BUILTIN

mercurial