Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Side by Side Diff: core/src/fxcrt/fx_system_unittest.cpp

Issue 1274423003: Fix FXSYS_itoa() implementation. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Actually include test, fix comment. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "testing/gtest/include/gtest/gtest.h"
8 #include "../../../testing/fx_string_testhelpers.h"
9 #include "../../include/fxcrt/fx_system.h"
10
11 // Unit test covering cases where PDFium replaces well-known library
12 // functionality on any given platformn.
13
14 TEST(fxcrt, FXSYS_itoa) {
15 const size_t kBase2SentinelLocation = 34; // "-" + 32 digits + null == 34.
brucedawson 2015/08/07 20:55:49 I think that the preferred abbreviation for the nu
16 const size_t kBase10SentinelLocation = 12; // "-" + 10 digits + null == 12.
17 const size_t kBase16SentinelLocation = 10; // "-" + 8 digits + null == 10.
18 const size_t kBufLen = 35;
19 const FX_CHAR kSentinel = 0x7f;
20
21 FX_CHAR buf[kBufLen];
22
23 FXSYS_itoa(42, buf, 17);
24 EXPECT_EQ(std::string(""), buf);
25
26 FXSYS_itoa(42, buf, 1);
27 EXPECT_EQ(std::string(""), buf);
28
29 FXSYS_itoa(42, buf, 0);
30 EXPECT_EQ(std::string(""), buf);
31
32 FXSYS_itoa(42, buf, -1);
33 EXPECT_EQ(std::string(""), buf);
34
35 buf[kBase16SentinelLocation] = kSentinel;
Lei Zhang 2015/08/07 20:50:12 Maybe write CheckBase16ItoA(int32_t input, const c
Tom Sepez 2015/08/07 21:58:14 Done.
36 FXSYS_itoa(std::numeric_limits<int32_t>::min(), buf, 16);
37 EXPECT_EQ(std::string("-80000000"), buf);
38 EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
39
40 buf[kBase10SentinelLocation] = kSentinel;
41 FXSYS_itoa(std::numeric_limits<int32_t>::min(), buf, 10);
42 EXPECT_EQ(std::string("-2147483648"), buf);
43 EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
44
45 buf[kBase2SentinelLocation] = kSentinel;
46 FXSYS_itoa(std::numeric_limits<int32_t>::min(), buf, 2);
47 EXPECT_EQ(std::string("-10000000000000000000000000000000"), buf);
48 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
49 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
Lei Zhang 2015/08/07 20:50:12 Repeat of last line?
Tom Sepez 2015/08/07 21:58:14 Done.
50
51 buf[kBase16SentinelLocation] = kSentinel;
52 FXSYS_itoa(-1, buf, 16);
53 EXPECT_EQ(std::string("-1"), buf);
54 EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
55
56 buf[kBase10SentinelLocation] = kSentinel;
57 FXSYS_itoa(-1, buf, 10);
58 EXPECT_EQ(std::string("-1"), buf);
59 EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
60
61 buf[kBase2SentinelLocation] = kSentinel;
62 FXSYS_itoa(-1, buf, 2);
63 EXPECT_EQ(std::string("-1"), buf);
64 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
65
66 buf[kBase16SentinelLocation] = kSentinel;
67 FXSYS_itoa(0, buf, 16);
68 EXPECT_EQ(std::string("0"), buf);
69 EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
70
71 buf[kBase10SentinelLocation] = kSentinel;
72 FXSYS_itoa(0, buf, 10);
73 EXPECT_EQ(std::string("0"), buf);
74 EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
75
76 buf[kBase2SentinelLocation] = kSentinel;
77 FXSYS_itoa(0, buf, 2);
78 EXPECT_EQ(std::string("0"), buf);
79 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
80
81 buf[kBase16SentinelLocation] = kSentinel;
82 FXSYS_itoa(42, buf, 16);
83 EXPECT_EQ(std::string("2a"), buf);
84 EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
85
86 buf[kBase10SentinelLocation] = kSentinel;
87 FXSYS_itoa(42, buf, 10);
88 EXPECT_EQ(std::string("42"), buf);
89 EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
90
91 buf[kBase2SentinelLocation] = kSentinel;
92 FXSYS_itoa(42, buf, 2);
93 EXPECT_EQ(std::string("101010"), buf);
94 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
95
96 buf[kBase16SentinelLocation] = kSentinel;
97 FXSYS_itoa(std::numeric_limits<int32_t>::max(), buf, 16);
98 EXPECT_EQ(std::string("7fffffff"), buf);
99 EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
100
101 buf[kBase10SentinelLocation] = kSentinel;
102 FXSYS_itoa(std::numeric_limits<int32_t>::max(), buf, 10);
103 EXPECT_EQ(std::string("2147483647"), buf);
104 EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
105
106 buf[kBase2SentinelLocation] = kSentinel;
107 FXSYS_itoa(std::numeric_limits<int32_t>::max(), buf, 2);
108 EXPECT_EQ(std::string("1111111111111111111111111111111"), buf);
109 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
110 }
111
112 TEST(fxcrt, FXSYS_i64toa) {
113 const size_t kBufLen = 67;
114 const size_t kBase2SentinelLocation = 66; // "-" + 64 digits + null.
115 const size_t kBase10SentinelLocation = 21; // "-" + 19 digits + null.
116 const size_t kBase16SentinelLocation = 18; // "-" + 16 digits + null.
117 const FX_CHAR kSentinel = 0x7f;
118
119 FX_CHAR buf[kBufLen];
120
121 FXSYS_i64toa(42, buf, 17);
122 EXPECT_EQ(std::string(""), buf);
123
124 FXSYS_i64toa(42, buf, 1);
125 EXPECT_EQ(std::string(""), buf);
126
127 FXSYS_i64toa(42, buf, 0);
128 EXPECT_EQ(std::string(""), buf);
129
130 FXSYS_i64toa(42, buf, -1);
131 EXPECT_EQ(std::string(""), buf);
132
133 buf[kBase16SentinelLocation] = kSentinel;
134 FXSYS_i64toa(std::numeric_limits<int64_t>::min(), buf, 16);
135 EXPECT_EQ(std::string("-8000000000000000"), buf);
136 EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
137
138 buf[kBase10SentinelLocation] = kSentinel;
139 FXSYS_i64toa(std::numeric_limits<int64_t>::min(), buf, 10);
140 EXPECT_EQ(std::string("-9223372036854775808"), buf);
141 EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
142
143 buf[kBase2SentinelLocation] = kSentinel;
144 FXSYS_i64toa(std::numeric_limits<int64_t>::min(), buf, 2);
145 EXPECT_EQ(std::string(
146 "-1000000000000000000000000000000000000000000000000000000000000000"),
147 buf);
148 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
149 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
150
151 buf[kBase16SentinelLocation] = kSentinel;
152 FXSYS_i64toa(-1, buf, 16);
153 EXPECT_EQ(std::string("-1"), buf);
154 EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
155
156 buf[kBase10SentinelLocation] = kSentinel;
157 FXSYS_i64toa(-1, buf, 10);
158 EXPECT_EQ(std::string("-1"), buf);
159 EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
160
161 buf[kBase2SentinelLocation] = kSentinel;
162 FXSYS_i64toa(-1, buf, 2);
163 EXPECT_EQ(std::string("-1"), buf);
164 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
165
166 buf[kBase16SentinelLocation] = kSentinel;
167 FXSYS_i64toa(0, buf, 16);
168 EXPECT_EQ(std::string("0"), buf);
169 EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
170
171 buf[kBase10SentinelLocation] = kSentinel;
172 FXSYS_i64toa(0, buf, 10);
173 EXPECT_EQ(std::string("0"), buf);
174 EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
175
176 buf[kBase2SentinelLocation] = kSentinel;
177 FXSYS_i64toa(0, buf, 2);
178 EXPECT_EQ(std::string("0"), buf);
179 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
180
181 buf[kBase16SentinelLocation] = kSentinel;
182 FXSYS_i64toa(42, buf, 16);
183 EXPECT_EQ(std::string("2a"), buf);
184 EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
185
186 buf[kBase10SentinelLocation] = kSentinel;
187 FXSYS_i64toa(42, buf, 10);
188 EXPECT_EQ(std::string("42"), buf);
189 EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
190
191 buf[kBase2SentinelLocation] = kSentinel;
192 FXSYS_i64toa(42, buf, 2);
193 EXPECT_EQ(std::string("101010"), buf);
194 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
195
196 buf[kBase16SentinelLocation] = kSentinel;
197 FXSYS_i64toa(std::numeric_limits<int64_t>::max(), buf, 16);
198 EXPECT_EQ(std::string("7fffffffffffffff"), buf);
199 EXPECT_EQ(kSentinel, buf[kBase16SentinelLocation]);
200
201 buf[kBase10SentinelLocation] = kSentinel;
202 FXSYS_i64toa(std::numeric_limits<int64_t>::max(), buf, 10);
203 EXPECT_EQ(std::string("9223372036854775807"), buf);
204 EXPECT_EQ(kSentinel, buf[kBase10SentinelLocation]);
205
206 buf[kBase2SentinelLocation] = kSentinel;
207 FXSYS_i64toa(std::numeric_limits<int64_t>::max(), buf, 2);
208 EXPECT_EQ(std::string(
209 "111111111111111111111111111111111111111111111111111111111111111"),
210 buf);
211 EXPECT_EQ(kSentinel, buf[kBase2SentinelLocation]);
212 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698