Debian Codegraph @

unittest/gunit/mysys_my_getopt-t.cc

raw symbols SCIP
1 /* Copyright (c) 2018, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include "my_config.h"
25 #include "my_getopt.h"
26
27 #include <gtest/gtest.h>
28
29 namespace mysys_my_getopt_unittest {
30
31 constexpr unsigned long long one_kilo = 1024ULL;
32 constexpr unsigned long long one_mega = 1024ULL * one_kilo;
33 constexpr unsigned long long one_giga = 1024ULL * one_mega;
34 constexpr unsigned long long one_tera = 1024ULL * one_giga;
35 constexpr unsigned long long one_peta = 1024ULL * one_tera;
36 constexpr unsigned long long one_exa = 1024ULL * one_peta;
37
38 void no_error_report(loglevel, uint, ...) {}
39 void no_message_hook(loglevel, uint, va_list) {}
40
41 class MysysMyGetopTest : public ::testing::Test {
42 void SetUp() override {
43 m_foo = my_getopt_error_reporter;
44 my_getopt_error_reporter = &no_error_report;
45 m_bar = local_message_hook;
46 local_message_hook = &no_message_hook;
47 }
48 void TearDown() override {
49 my_getopt_error_reporter = m_foo;
50 local_message_hook = m_bar;
51 }
52
53 protected:
54 int m_error;
55
56 private:
57 my_error_reporter m_foo;
58 void (*m_bar)(loglevel, uint, va_list);
59 };
60
61 TEST_F(MysysMyGetopTest, GarbageInput) {
62 eval_num_suffix<unsigned long long>("X", &m_error, "");
63 EXPECT_EQ(m_error, 1);
64
65 eval_num_suffix<unsigned long long>("+X", &m_error, "");
66 EXPECT_EQ(m_error, 1);
67
68 eval_num_suffix<unsigned long long>("-X", &m_error, "");
69 EXPECT_EQ(m_error, 1);
70
71 eval_num_suffix<unsigned long long>("0X", &m_error, "");
72 EXPECT_EQ(m_error, 1);
73
74 eval_num_suffix<unsigned long long>("0Xm", &m_error, "");
75 EXPECT_EQ(m_error, 1);
76
77 eval_num_suffix<unsigned long long>("99999999999999999999999", &m_error, "");
78 EXPECT_EQ(m_error, 1);
79
80 eval_num_suffix<unsigned long long>("99999999999999999999999k", &m_error, "");
81 EXPECT_EQ(m_error, 1);
82
83 eval_num_suffix<long long>("X", &m_error, "");
84 EXPECT_EQ(m_error, 1);
85
86 eval_num_suffix<long long>("+X", &m_error, "");
87 EXPECT_EQ(m_error, 1);
88
89 eval_num_suffix<long long>("-X", &m_error, "");
90 EXPECT_EQ(m_error, 1);
91
92 eval_num_suffix<long long>("0X", &m_error, "");
93 EXPECT_EQ(m_error, 1);
94
95 eval_num_suffix<long long>("0Xm", &m_error, "");
96 EXPECT_EQ(m_error, 1);
97
98 eval_num_suffix<long long>("-99999999999999999999999", &m_error, "");
99 EXPECT_EQ(m_error, 1);
100
101 eval_num_suffix<long long>("-99999999999999999999999k", &m_error, "");
102 EXPECT_EQ(m_error, 1);
103 }
104
105 TEST_F(MysysMyGetopTest, Unsigned) {
106 unsigned long long result;
107 result = eval_num_suffix<unsigned long long>("16E", &m_error, "");
108 EXPECT_EQ(m_error, 1);
109
110 result = eval_num_suffix<unsigned long long>("1t", &m_error, "");
111 EXPECT_EQ(result, one_tera);
112
113 result = eval_num_suffix<unsigned long long>("1000000E", &m_error, "");
114 EXPECT_EQ(result, 0);
115 EXPECT_EQ(m_error, 1);
116
117 result =
118 eval_num_suffix<unsigned long long>("18446744073709551615", &m_error, "");
119 EXPECT_EQ(m_error, 0);
120
121 result =
122 eval_num_suffix<unsigned long long>("18446744073709551616", &m_error, "");
123 EXPECT_EQ(m_error, 1);
124 }
125
126 TEST_F(MysysMyGetopTest, UnsignedLoop) {
127 unsigned long long result;
128 char buffer[30];
129
130 for (unsigned long long counter = 0; counter < 100ULL; ++counter) {
131 sprintf(buffer, "%lluE", counter);
132 result = eval_num_suffix<unsigned long long>(buffer, &m_error, "");
133 if (counter >= 16ULL) {
134 EXPECT_EQ(result, 0);
135 EXPECT_EQ(m_error, 1);
136 } else {
137 EXPECT_EQ(result, counter * one_exa) << counter;
138 EXPECT_EQ(m_error, 0);
139 }
140 }
141 }
142
143 // -9223372036854775808 .. 9223372036854775807
144 TEST_F(MysysMyGetopTest, Signed) {
145 long long result;
146 result = eval_num_suffix<long long>("-1t", &m_error, "");
147 EXPECT_EQ(result, -1024LL * 1024LL * 1024LL * 1024LL);
148 EXPECT_EQ(m_error, 0);
149
150 result = eval_num_suffix<long long>("-1E", &m_error, "");
151 EXPECT_EQ(result, -static_cast<long long>(one_exa));
152 EXPECT_EQ(m_error, 0);
153
154 result = eval_num_suffix<long long>("-1000000E", &m_error, "");
155 EXPECT_EQ(result, 0);
156 EXPECT_EQ(m_error, 1);
157
158 result = eval_num_suffix<long long>("-9223372036854775808", &m_error, "");
159 EXPECT_EQ(result, LLONG_MIN);
160 EXPECT_EQ(m_error, 0);
161
162 result = eval_num_suffix<long long>("-9223372036854775808k", &m_error, "");
163 EXPECT_EQ(result, 0);
164 EXPECT_EQ(m_error, 1);
165
166 result = eval_num_suffix<long long>("-9223372036854775807", &m_error, "");
167 EXPECT_EQ(result, LLONG_MIN + 1);
168 EXPECT_EQ(m_error, 0);
169 }
170
171 TEST_F(MysysMyGetopTest, SignedLoop) {
172 long long counter;
173 long long result;
174 char buffer[30];
175
176 for (counter = 0; counter < 100LL; ++counter) {
177 sprintf(buffer, "-%lldE", counter);
178 result = eval_num_suffix<long long>(buffer, &m_error, "");
179 if (counter >= 9) {
180 EXPECT_EQ(result, 0);
181 EXPECT_EQ(m_error, 1);
182 } else {
183 EXPECT_EQ(result, (longlong)(-counter * one_exa)) << counter;
184 EXPECT_EQ(m_error, 0);
185 }
186 }
187 for (counter = 0; counter < 100LL; ++counter) {
188 sprintf(buffer, "%+lldE", counter);
189 result = eval_num_suffix<long long>(buffer, &m_error, "");
190 if (counter >= 8) {
191 EXPECT_EQ(result, 0);
192 EXPECT_EQ(m_error, 1);
193 } else {
194 EXPECT_EQ(result, counter * one_exa) << counter;
195 EXPECT_EQ(m_error, 0);
196 }
197 }
198 }
199
200 } // namespace mysys_my_getopt_unittest