1 // Copyright 2015 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <string>
6
7 #include "core/include/fxcrt/fx_system.h"
8 #include "testing/fx_string_testhelpers.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 // Unit test covering cases where PDFium replaces well-known library
12 // functionality on any given platformn.
13
14 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
15
16 namespace {
17
18 const FX_CHAR kSentinel = 0x7f;
19
Check32BitBase16Itoa(int32_t input,const char * expected_output)20 void Check32BitBase16Itoa(int32_t input, const char* expected_output) {
21 const size_t kBufLen = 11; // "-" + 8 digits + NUL + sentinel.
22 FX_CHAR buf[kBufLen];
23 buf[kBufLen - 1] = kSentinel;
24 FXSYS_itoa(input, buf, 16);
25 EXPECT_EQ(std::string(expected_output), buf);
26 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
27 }
28
Check32BitBase10Itoa(int32_t input,const char * expected_output)29 void Check32BitBase10Itoa(int32_t input, const char* expected_output) {
30 const size_t kBufLen = 13; // "-" + 10 digits + NUL + sentinel.
31 FX_CHAR buf[kBufLen];
32 buf[kBufLen - 1] = kSentinel;
33 FXSYS_itoa(input, buf, 10);
34 EXPECT_EQ(std::string(expected_output), buf);
35 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
36 }
37
Check32BitBase2Itoa(int32_t input,const char * expected_output)38 void Check32BitBase2Itoa(int32_t input, const char* expected_output) {
39 const size_t kBufLen = 35; // "-" + 32 digits + NUL + sentinel.
40 FX_CHAR buf[kBufLen];
41 buf[kBufLen - 1] = kSentinel;
42 FXSYS_itoa(input, buf, 2);
43 EXPECT_EQ(std::string(expected_output), buf);
44 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
45 }
46
Check64BitBase16Itoa(int64_t input,const char * expected_output)47 void Check64BitBase16Itoa(int64_t input, const char* expected_output) {
48 const size_t kBufLen = 19; // "-" + 16 digits + NUL + sentinel.
49 FX_CHAR buf[kBufLen];
50 buf[kBufLen - 1] = kSentinel;
51 FXSYS_i64toa(input, buf, 16);
52 EXPECT_EQ(std::string(expected_output), buf);
53 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
54 }
55
Check64BitBase10Itoa(int64_t input,const char * expected_output)56 void Check64BitBase10Itoa(int64_t input, const char* expected_output) {
57 const size_t kBufLen = 22; // "-" + 19 digits + NUL + sentinel.
58 FX_CHAR buf[kBufLen];
59 buf[kBufLen - 1] = kSentinel;
60 FXSYS_i64toa(input, buf, 10);
61 EXPECT_EQ(std::string(expected_output), buf);
62 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
63 }
64
Check64BitBase2Itoa(int64_t input,const char * expected_output)65 void Check64BitBase2Itoa(int64_t input, const char* expected_output) {
66 const size_t kBufLen = 67; // "-" + 64 digits + NUL + sentinel.
67 FX_CHAR buf[kBufLen];
68 buf[kBufLen - 1] = kSentinel;
69 FXSYS_i64toa(input, buf, 2);
70 EXPECT_EQ(std::string(expected_output), buf);
71 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
72 }
73
74 } // namespace
75
TEST(fxcrt,FXSYS_itoa_InvalidRadix)76 TEST(fxcrt, FXSYS_itoa_InvalidRadix) {
77 FX_CHAR buf[32];
78
79 FXSYS_itoa(42, buf, 17); // Ours stops at 16.
80 EXPECT_EQ(std::string(""), buf);
81
82 FXSYS_itoa(42, buf, 1);
83 EXPECT_EQ(std::string(""), buf);
84
85 FXSYS_itoa(42, buf, 0);
86 EXPECT_EQ(std::string(""), buf);
87
88 FXSYS_itoa(42, buf, -1);
89 EXPECT_EQ(std::string(""), buf);
90 }
91
TEST(fxcrt,FXSYS_itoa)92 TEST(fxcrt, FXSYS_itoa) {
93 Check32BitBase16Itoa(std::numeric_limits<int32_t>::min(), "-80000000");
94 Check32BitBase10Itoa(std::numeric_limits<int32_t>::min(), "-2147483648");
95 Check32BitBase2Itoa(std::numeric_limits<int32_t>::min(),
96 "-10000000000000000000000000000000");
97
98 Check32BitBase16Itoa(-1, "-1");
99 Check32BitBase10Itoa(-1, "-1");
100 Check32BitBase2Itoa(-1, "-1");
101
102 Check32BitBase16Itoa(0, "0");
103 Check32BitBase10Itoa(0, "0");
104 Check32BitBase2Itoa(0, "0");
105
106 Check32BitBase16Itoa(42, "2a");
107 Check32BitBase10Itoa(42, "42");
108 Check32BitBase2Itoa(42, "101010");
109
110 Check32BitBase16Itoa(std::numeric_limits<int32_t>::max(), "7fffffff");
111 Check32BitBase10Itoa(std::numeric_limits<int32_t>::max(), "2147483647");
112 Check32BitBase2Itoa(std::numeric_limits<int32_t>::max(),
113 "1111111111111111111111111111111");
114 }
115
TEST(fxcrt,FXSYS_i64toa_InvalidRadix)116 TEST(fxcrt, FXSYS_i64toa_InvalidRadix) {
117 FX_CHAR buf[32];
118
119 FXSYS_i64toa(42, buf, 17); // Ours stops at 16.
120 EXPECT_EQ(std::string(""), buf);
121
122 FXSYS_i64toa(42, buf, 1);
123 EXPECT_EQ(std::string(""), buf);
124
125 FXSYS_i64toa(42, buf, 0);
126 EXPECT_EQ(std::string(""), buf);
127
128 FXSYS_i64toa(42, buf, -1);
129 EXPECT_EQ(std::string(""), buf);
130 };
131
TEST(fxcrt,FXSYS_i64toa)132 TEST(fxcrt, FXSYS_i64toa) {
133 Check64BitBase16Itoa(std::numeric_limits<int64_t>::min(),
134 "-8000000000000000");
135 Check64BitBase10Itoa(std::numeric_limits<int64_t>::min(),
136 "-9223372036854775808");
137 Check64BitBase2Itoa(
138 std::numeric_limits<int64_t>::min(),
139 "-1000000000000000000000000000000000000000000000000000000000000000");
140
141 Check64BitBase16Itoa(-1, "-1");
142 Check64BitBase10Itoa(-1, "-1");
143 Check64BitBase2Itoa(-1, "-1");
144
145 Check64BitBase16Itoa(0, "0");
146 Check64BitBase10Itoa(0, "0");
147 Check64BitBase2Itoa(0, "0");
148
149 Check64BitBase16Itoa(42, "2a");
150 Check64BitBase10Itoa(42, "42");
151 Check64BitBase2Itoa(42, "101010");
152
153 Check64BitBase16Itoa(std::numeric_limits<int64_t>::max(), "7fffffffffffffff");
154 Check64BitBase10Itoa(std::numeric_limits<int64_t>::max(),
155 "9223372036854775807");
156 Check64BitBase2Itoa(
157 std::numeric_limits<int64_t>::max(),
158 "111111111111111111111111111111111111111111111111111111111111111");
159 }
160
161 #endif // _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
162