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

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: Helper routines. 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
« no previous file with comments | « core/src/fxcrt/fx_basic_gcc.cpp ('k') | pdfium.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 namespace {
14
15 const FX_CHAR kSentinel = 0x7f;
16
17 void Check32BitBase16Itoa(int32_t input, const char* expected_output) {
18 const size_t kBufLen = 11; // "-" + 8 digits + NUL + sentinel.
19 FX_CHAR buf[kBufLen];
20 buf[kBufLen - 1] = kSentinel;
21 FXSYS_itoa(input, buf, 16);
22 EXPECT_EQ(std::string(expected_output), buf);
23 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
24 }
25
26 void Check32BitBase10Itoa(int32_t input, const char* expected_output) {
27 const size_t kBufLen = 13; // "-" + 10 digits + NUL + sentinel.
28 FX_CHAR buf[kBufLen];
29 buf[kBufLen - 1] = kSentinel;
30 FXSYS_itoa(input, buf, 10);
31 EXPECT_EQ(std::string(expected_output), buf);
32 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
33 }
34
35 void Check32BitBase2Itoa(int32_t input, const char* expected_output) {
36 const size_t kBufLen = 35; // "-" + 32 digits + NUL + sentinel.
37 FX_CHAR buf[kBufLen];
38 buf[kBufLen - 1] = kSentinel;
39 FXSYS_itoa(input, buf, 2);
40 EXPECT_EQ(std::string(expected_output), buf);
41 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
42 }
43
44 void Check64BitBase16Itoa(int64_t input, const char* expected_output) {
45 const size_t kBufLen = 19; // "-" + 16 digits + NUL + sentinel.
46 FX_CHAR buf[kBufLen];
47 buf[kBufLen - 1] = kSentinel;
48 FXSYS_i64toa(input, buf, 16);
49 EXPECT_EQ(std::string(expected_output), buf);
50 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
51 }
52
53 void Check64BitBase10Itoa(int64_t input, const char* expected_output) {
54 const size_t kBufLen = 22; // "-" + 19 digits + NUL + sentinel.
55 FX_CHAR buf[kBufLen];
56 buf[kBufLen - 1] = kSentinel;
57 FXSYS_i64toa(input, buf, 10);
58 EXPECT_EQ(std::string(expected_output), buf);
59 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
60 }
61
62 void Check64BitBase2Itoa(int64_t input, const char* expected_output) {
63 const size_t kBufLen = 67; // "-" + 64 digits + NUL + sentinel.
64 FX_CHAR buf[kBufLen];
65 buf[kBufLen - 1] = kSentinel;
66 FXSYS_i64toa(input, buf, 2);
67 EXPECT_EQ(std::string(expected_output), buf);
68 EXPECT_EQ(kSentinel, buf[kBufLen - 1]);
69 }
70
71 } // namespace
72
73 TEST(fxcrt, FXSYS_itoa_InvalidRadix) {
74 FX_CHAR buf[32];
75
76 FXSYS_itoa(42, buf, 17);
77 EXPECT_EQ(std::string(""), buf);
78
79 FXSYS_itoa(42, buf, 1);
80 EXPECT_EQ(std::string(""), buf);
81
82 FXSYS_itoa(42, buf, 0);
83 EXPECT_EQ(std::string(""), buf);
84
85 FXSYS_itoa(42, buf, -1);
86 EXPECT_EQ(std::string(""), buf);
87 }
88
89 TEST(fxcrt, FXSYS_itoa) {
90 Check32BitBase16Itoa(std::numeric_limits<int32_t>::min(), "-80000000");
91 Check32BitBase10Itoa(std::numeric_limits<int32_t>::min(), "-2147483648");
92 Check32BitBase2Itoa(std::numeric_limits<int32_t>::min(),
93 "-10000000000000000000000000000000");
94
95 Check32BitBase16Itoa(-1, "-1");
96 Check32BitBase10Itoa(-1, "-1");
97 Check32BitBase2Itoa(-1, "-1");
98
99 Check32BitBase16Itoa(0, "0");
100 Check32BitBase10Itoa(0, "0");
101 Check32BitBase2Itoa(0, "0");
102
103 Check32BitBase16Itoa(42, "2a");
104 Check32BitBase10Itoa(42, "42");
105 Check32BitBase2Itoa(42, "101010");
106
107 Check32BitBase16Itoa(std::numeric_limits<int32_t>::max(), "7fffffff");
108 Check32BitBase10Itoa(std::numeric_limits<int32_t>::max(), "2147483647");
109 Check32BitBase2Itoa(std::numeric_limits<int32_t>::max(),
110 "1111111111111111111111111111111");
111 }
112
113
114 TEST(fxcrt, FXSYS_i64toa_InvalidRadix) {
115 FX_CHAR buf[32];
116
117 FXSYS_i64toa(42, buf, 17);
118 EXPECT_EQ(std::string(""), buf);
119
120 FXSYS_i64toa(42, buf, 1);
121 EXPECT_EQ(std::string(""), buf);
122
123 FXSYS_i64toa(42, buf, 0);
124 EXPECT_EQ(std::string(""), buf);
125
126 FXSYS_i64toa(42, buf, -1);
127 EXPECT_EQ(std::string(""), buf);
128 };
129
130 TEST(fxcrt, FXSYS_i64toa) {
131 Check64BitBase16Itoa(
132 std::numeric_limits<int64_t>::min(), "-8000000000000000");
133 Check64BitBase10Itoa(
134 std::numeric_limits<int64_t>::min(), "-9223372036854775808");
135 Check64BitBase2Itoa(
136 std::numeric_limits<int64_t>::min(),
137 "-1000000000000000000000000000000000000000000000000000000000000000");
138
139 Check64BitBase16Itoa(-1, "-1");
140 Check64BitBase10Itoa(-1, "-1");
141 Check64BitBase2Itoa(-1, "-1");
142
143 Check64BitBase16Itoa(0, "0");
144 Check64BitBase10Itoa(0, "0");
145 Check64BitBase2Itoa(0, "0");
146
147 Check64BitBase16Itoa(42, "2a");
148 Check64BitBase10Itoa(42, "42");
149 Check64BitBase2Itoa(42, "101010");
150
151 Check64BitBase16Itoa(
152 std::numeric_limits<int64_t>::max(), "7fffffffffffffff");
Nico 2015/08/12 16:52:21 This new code isn't clang-formatted. Did the presu
153 Check64BitBase10Itoa(
154 std::numeric_limits<int64_t>::max(), "9223372036854775807");
155 Check64BitBase2Itoa(
156 std::numeric_limits<int64_t>::max(),
157 "111111111111111111111111111111111111111111111111111111111111111");
158 }
OLDNEW
« no previous file with comments | « core/src/fxcrt/fx_basic_gcc.cpp ('k') | pdfium.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698