10#include "build/build_config.h"
11#include "core/fxcrt/fx_string.h"
12#include "core/fxcrt/fx_system.h"
13#include "testing/gtest/include/gtest/gtest.h"
22const char kSentinel = 0x7f;
24void Check32BitBase16Itoa(int32_t input,
const char* expected_output) {
25 const size_t kBufLen = 11;
27 buf[kBufLen - 1] = kSentinel;
28 FXSYS_itoa(input, buf, 16);
29 EXPECT_STREQ(expected_output, buf);
30 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
33void Check32BitBase10Itoa(int32_t input,
const char* expected_output) {
34 const size_t kBufLen = 13;
36 buf[kBufLen - 1] = kSentinel;
37 FXSYS_itoa(input, buf, 10);
38 EXPECT_STREQ(expected_output, buf);
39 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
42void Check32BitBase2Itoa(int32_t input,
const char* expected_output) {
43 const size_t kBufLen = 35;
45 buf[kBufLen - 1] = kSentinel;
46 FXSYS_itoa(input, buf, 2);
47 EXPECT_STREQ(expected_output, buf);
48 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
51void Check64BitBase16Itoa(int64_t input,
const char* expected_output) {
52 const size_t kBufLen = 19;
54 buf[kBufLen - 1] = kSentinel;
55 FXSYS_i64toa(input, buf, 16);
56 EXPECT_STREQ(expected_output, buf);
57 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
60void Check64BitBase10Itoa(int64_t input,
const char* expected_output) {
61 const size_t kBufLen = 22;
63 buf[kBufLen - 1] = kSentinel;
64 FXSYS_i64toa(input, buf, 10);
65 EXPECT_STREQ(expected_output, buf);
66 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
69void Check64BitBase2Itoa(int64_t input,
const char* expected_output) {
70 const size_t kBufLen = 67;
72 buf[kBufLen - 1] = kSentinel;
73 FXSYS_i64toa(input, buf, 2);
74 EXPECT_STREQ(expected_output, buf);
75 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
80TEST(fxcrt, FXSYS_roundf) {
81 EXPECT_EQ(0, FXSYS_roundf(0.0f));
82 EXPECT_EQ(0, FXSYS_roundf(-0.0f));
83 EXPECT_EQ(0, FXSYS_roundf(0.00001f));
84 EXPECT_EQ(0, FXSYS_roundf(-0.00001f));
85 EXPECT_EQ(3, FXSYS_roundf(3.14159f));
86 EXPECT_EQ(4, FXSYS_roundf(3.5f));
89 EXPECT_EQ(0, FXSYS_roundf(std::numeric_limits<
float>::min()));
90 EXPECT_EQ(0, FXSYS_roundf(-std::numeric_limits<
float>::min()));
95 EXPECT_EQ(-3, FXSYS_roundf(-3.14159f));
96 EXPECT_EQ(-4, FXSYS_roundf(-3.5f));
105 EXPECT_EQ(2147483520, FXSYS_roundf(2.14748352e+9f));
108 EXPECT_EQ(2147483647, FXSYS_roundf(2.14748365e+9f));
110 EXPECT_EQ(2147483647, FXSYS_roundf(2.14748365e+10f));
111 EXPECT_EQ(2147483647, FXSYS_roundf(std::numeric_limits<
float>::max()));
121 EXPECT_EQ(-2147483648, FXSYS_roundf(-2.147483648e+10f));
122 EXPECT_EQ(-2147483648, FXSYS_roundf(-2.147483648e+9f));
123 EXPECT_EQ(-2147483520, FXSYS_roundf(-2.14748352e+9f));
124 EXPECT_EQ(-2147483648, FXSYS_roundf(-std::numeric_limits<
float>::max()));
127 EXPECT_EQ(0, FXSYS_roundf(NAN));
130TEST(fxcrt, FXSYS_round) {
131 EXPECT_EQ(0, FXSYS_round(0.0));
132 EXPECT_EQ(0, FXSYS_round(-0.0));
133 EXPECT_EQ(0, FXSYS_round(0.00001));
134 EXPECT_EQ(0, FXSYS_round(-0.00001));
135 EXPECT_EQ(3, FXSYS_round(3.14159));
136 EXPECT_EQ(4, FXSYS_round(3.5));
139 EXPECT_EQ(0, FXSYS_round(std::numeric_limits<
double>::min()));
140 EXPECT_EQ(0, FXSYS_round(-std::numeric_limits<
double>::min()));
145 EXPECT_EQ(-3, FXSYS_round(-3.14159));
146 EXPECT_EQ(-4, FXSYS_round(-3.5));
156 EXPECT_EQ(2147483647, FXSYS_round(2.147483647e+9));
159 EXPECT_EQ(2147483647, FXSYS_round(2.14748365e+9));
161 EXPECT_EQ(2147483647, FXSYS_round(2.14748365e+10));
162 EXPECT_EQ(2147483647, FXSYS_round(std::numeric_limits<
double>::max()));
172 EXPECT_EQ(-2147483648, FXSYS_round(-2.1474836479999998e+9));
173 EXPECT_EQ(-2147483648, FXSYS_round(-2.147483648e+9));
174 EXPECT_EQ(-2147483648, FXSYS_round(-2.147483648e+10));
175 EXPECT_EQ(-2147483648, FXSYS_round(-std::numeric_limits<
double>::max()));
178 EXPECT_EQ(0, FXSYS_round(NAN));
181TEST(fxcrt, FXSYS_itoa_InvalidRadix) {
184 FXSYS_itoa(42, buf, 17);
185 EXPECT_STREQ(
"", buf);
187 FXSYS_itoa(42, buf, 1);
188 EXPECT_STREQ(
"", buf);
190 FXSYS_itoa(42, buf, 0);
191 EXPECT_STREQ(
"", buf);
193 FXSYS_itoa(42, buf, -1);
194 EXPECT_STREQ(
"", buf);
197TEST(fxcrt, FXSYS_itoa) {
198 Check32BitBase16Itoa(std::numeric_limits<int32_t>::min(),
"-80000000");
199 Check32BitBase10Itoa(std::numeric_limits<int32_t>::min(),
"-2147483648");
200 Check32BitBase2Itoa(std::numeric_limits<int32_t>::min(),
201 "-10000000000000000000000000000000");
203 Check32BitBase16Itoa(-1,
"-1");
204 Check32BitBase10Itoa(-1,
"-1");
205 Check32BitBase2Itoa(-1,
"-1");
207 Check32BitBase16Itoa(0,
"0");
208 Check32BitBase10Itoa(0,
"0");
209 Check32BitBase2Itoa(0,
"0");
211 Check32BitBase16Itoa(42,
"2a");
212 Check32BitBase10Itoa(42,
"42");
213 Check32BitBase2Itoa(42,
"101010");
215 Check32BitBase16Itoa(std::numeric_limits<int32_t>::max(),
"7fffffff");
216 Check32BitBase10Itoa(std::numeric_limits<int32_t>::max(),
"2147483647");
217 Check32BitBase2Itoa(std::numeric_limits<int32_t>::max(),
218 "1111111111111111111111111111111");
221TEST(fxcrt, FXSYS_i64toa_InvalidRadix) {
224 FXSYS_i64toa(42, buf, 17);
225 EXPECT_STREQ(
"", buf);
227 FXSYS_i64toa(42, buf, 1);
228 EXPECT_STREQ(
"", buf);
230 FXSYS_i64toa(42, buf, 0);
231 EXPECT_STREQ(
"", buf);
233 FXSYS_i64toa(42, buf, -1);
234 EXPECT_STREQ(
"", buf);
237TEST(fxcrt, FXSYS_i64toa) {
238 Check64BitBase16Itoa(std::numeric_limits<int64_t>::min(),
239 "-8000000000000000");
240 Check64BitBase10Itoa(std::numeric_limits<int64_t>::min(),
241 "-9223372036854775808");
243 std::numeric_limits<int64_t>::min(),
244 "-1000000000000000000000000000000000000000000000000000000000000000");
246 Check64BitBase16Itoa(-1,
"-1");
247 Check64BitBase10Itoa(-1,
"-1");
248 Check64BitBase2Itoa(-1,
"-1");
250 Check64BitBase16Itoa(0,
"0");
251 Check64BitBase10Itoa(0,
"0");
252 Check64BitBase2Itoa(0,
"0");
254 Check64BitBase16Itoa(42,
"2a");
255 Check64BitBase10Itoa(42,
"42");
256 Check64BitBase2Itoa(42,
"101010");
258 Check64BitBase16Itoa(std::numeric_limits<int64_t>::max(),
"7fffffffffffffff");
259 Check64BitBase10Itoa(std::numeric_limits<int64_t>::max(),
260 "9223372036854775807");
262 std::numeric_limits<int64_t>::max(),
263 "111111111111111111111111111111111111111111111111111111111111111");
269 struct tm good_time = {};
270 good_time.tm_year = 74;
271 good_time.tm_mon = 7;
272 good_time.tm_mday = 9;
273 good_time.tm_hour = 11;
274 good_time.tm_min = 59;
275 good_time.tm_sec = 59;
277 wchar_t buf[100] = {};
278 EXPECT_EQ(19u,
FXSYS_wcsftime(buf, std::size(buf), L"%Y-%m-%dT%H:%M:%S",
280 EXPECT_STREQ(L"1974-08-09T11:59:59", buf);
283 struct tm year_time = {};
284 year_time.tm_mon = 7;
285 year_time.tm_mday = 9;
286 year_time.tm_hour = 11;
287 year_time.tm_min = 59;
288 year_time.tm_sec = 59;
290 for (
int year = -2500; year <= 8500; ++year) {
291 year_time.tm_year = year;
292 wchar_t year_buf[100] = {};
293 FXSYS_wcsftime(year_buf, std::size(year_buf), L"%Y-%m-%dT%H:%M:%S",
298 struct tm bad_time = {};
299 bad_time.tm_year = -1;
300 bad_time.tm_mon = -1;
301 bad_time.tm_mday = -1;
302 bad_time.tm_hour = -1;
303 bad_time.tm_min = -1;
304 bad_time.tm_sec = -1;
306 FXSYS_wcsftime(buf, std::size(buf), L"%y-%m-%dT%H:%M:%S", &bad_time);
309 struct tm feb_time = {};
310 feb_time.tm_year = 115;
312 feb_time.tm_mday = 30;
313 feb_time.tm_hour = 12;
314 feb_time.tm_min = 00;
315 feb_time.tm_sec = 00;
317 FXSYS_wcsftime(buf, std::size(buf), L"%y-%m-%dT%H:%M:%S", &feb_time);
321 EXPECT_EQ(0, FXSYS_atoi(
""));
322 EXPECT_EQ(0, FXSYS_atoi(
"0"));
323 EXPECT_EQ(-1, FXSYS_atoi(
"-1"));
324 EXPECT_EQ(2345, FXSYS_atoi(
"2345"));
325 EXPECT_EQ(-2147483647, FXSYS_atoi(
"-2147483647"));
327 EXPECT_EQ(-2345, FXSYS_atoi(
"-2345"));
328 EXPECT_EQ(2345, FXSYS_atoi(
"+2345"));
330 EXPECT_EQ(2147483647, FXSYS_atoi(
"2147483647"));
333 EXPECT_EQ(-2147483647 - 1, FXSYS_atoi(
"-2147483648"));
335 EXPECT_EQ(9, FXSYS_atoi(
"9x9"));
338 EXPECT_EQ(2147483647, FXSYS_atoi(
"2147483623423412348"));
339 EXPECT_EQ(2147483647, FXSYS_atoi(
"2147483648"));
340 EXPECT_EQ(-2147483647 - 1, FXSYS_atoi(
"-2147483650"));
344 EXPECT_EQ(0, FXSYS_atoi64(
""));
345 EXPECT_EQ(0, FXSYS_atoi64(
"0"));
346 EXPECT_EQ(-1, FXSYS_atoi64(
"-1"));
347 EXPECT_EQ(2345, FXSYS_atoi64(
"2345"));
348 EXPECT_EQ(-9223372036854775807LL, FXSYS_atoi64(
"-9223372036854775807"));
350 EXPECT_EQ(-2345, FXSYS_atoi64(
"-2345"));
351 EXPECT_EQ(2345, FXSYS_atoi64(
"+2345"));
353 EXPECT_EQ(9223372036854775807LL, FXSYS_atoi64(
"9223372036854775807"));
355 EXPECT_EQ(-9223372036854775807LL - 1LL, FXSYS_atoi64(
"-9223372036854775808"));
357 EXPECT_EQ(9, FXSYS_atoi64(
"9x9"));
360 EXPECT_EQ(9223372036854775807LL, FXSYS_atoi64(
"922337203685471234123475807"));
361 EXPECT_EQ(9223372036854775807LL, FXSYS_atoi64(
"9223372036854775808"));
362 EXPECT_EQ(-9223372036854775807LL - 1LL, FXSYS_atoi64(
"-9223372036854775810"));
366 EXPECT_EQ(0, FXSYS_wtoi(L""));
367 EXPECT_EQ(0, FXSYS_wtoi(L"0"));
368 EXPECT_EQ(-1, FXSYS_wtoi(L"-1"));
369 EXPECT_EQ(2345, FXSYS_wtoi(L"2345"));
370 EXPECT_EQ(-2147483647, FXSYS_wtoi(L"-2147483647"));
372 EXPECT_EQ(2147483647, FXSYS_wtoi(L"2147483647"));
374 EXPECT_EQ(-2147483647 - 1, FXSYS_wtoi(L"-2147483648"));
377 EXPECT_EQ(2147483647, FXSYS_wtoi(L"2147483623423412348"));
378 EXPECT_EQ(2147483647, FXSYS_wtoi(L"2147483648"));
379 EXPECT_EQ(-2147483647 - 1, FXSYS_wtoi(L"-2147483652"));
383 EXPECT_EQ(0u, FXSYS_atoui(
""));
384 EXPECT_EQ(0u, FXSYS_atoui(
"0"));
385 EXPECT_EQ(4294967295, FXSYS_atoui(
"-1"));
386 EXPECT_EQ(2345u, FXSYS_atoui(
"2345"));
388 EXPECT_EQ(4294964951, FXSYS_atoui(
"-2345"));
389 EXPECT_EQ(2345u, FXSYS_atoui(
"+2345"));
391 EXPECT_EQ(4294967295, FXSYS_atoui(
"4294967295"));
392 EXPECT_EQ(9u, FXSYS_atoui(
"9x9"));
395 EXPECT_EQ(4294967295, FXSYS_atoui(
"2147483623423412348"));
396 EXPECT_EQ(4294967295, FXSYS_atoui(
"4294967296"));
397 EXPECT_EQ(4294967295, FXSYS_atoui(
"-4294967345"));
TEST(FXCRYPT, CryptToBase16)