1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "android-base/parseint.h"
18
19 #include <errno.h>
20
21 #include <gtest/gtest.h>
22
TEST(parseint,signed_smoke)23 TEST(parseint, signed_smoke) {
24 errno = 0;
25 int i = 0;
26 ASSERT_FALSE(android::base::ParseInt("x", &i));
27 ASSERT_EQ(EINVAL, errno);
28 errno = 0;
29 ASSERT_FALSE(android::base::ParseInt("123x", &i));
30 ASSERT_EQ(EINVAL, errno);
31
32 ASSERT_TRUE(android::base::ParseInt("123", &i));
33 ASSERT_EQ(123, i);
34 ASSERT_EQ(0, errno);
35 i = 0;
36 EXPECT_TRUE(android::base::ParseInt(" 123", &i));
37 EXPECT_EQ(123, i);
38 ASSERT_TRUE(android::base::ParseInt("-123", &i));
39 ASSERT_EQ(-123, i);
40 i = 0;
41 EXPECT_TRUE(android::base::ParseInt(" -123", &i));
42 EXPECT_EQ(-123, i);
43
44 short s = 0;
45 ASSERT_TRUE(android::base::ParseInt("1234", &s));
46 ASSERT_EQ(1234, s);
47
48 ASSERT_TRUE(android::base::ParseInt("12", &i, 0, 15));
49 ASSERT_EQ(12, i);
50 errno = 0;
51 ASSERT_FALSE(android::base::ParseInt("-12", &i, 0, 15));
52 ASSERT_EQ(ERANGE, errno);
53 errno = 0;
54 ASSERT_FALSE(android::base::ParseInt("16", &i, 0, 15));
55 ASSERT_EQ(ERANGE, errno);
56
57 errno = 0;
58 ASSERT_FALSE(android::base::ParseInt<int>("x", nullptr));
59 ASSERT_EQ(EINVAL, errno);
60 errno = 0;
61 ASSERT_FALSE(android::base::ParseInt<int>("123x", nullptr));
62 ASSERT_EQ(EINVAL, errno);
63 ASSERT_TRUE(android::base::ParseInt<int>("1234", nullptr));
64 }
65
TEST(parseint,unsigned_smoke)66 TEST(parseint, unsigned_smoke) {
67 errno = 0;
68 unsigned int i = 0u;
69 ASSERT_FALSE(android::base::ParseUint("x", &i));
70 ASSERT_EQ(EINVAL, errno);
71 errno = 0;
72 ASSERT_FALSE(android::base::ParseUint("123x", &i));
73 ASSERT_EQ(EINVAL, errno);
74
75 ASSERT_TRUE(android::base::ParseUint("123", &i));
76 ASSERT_EQ(123u, i);
77 ASSERT_EQ(0, errno);
78 i = 0u;
79 EXPECT_TRUE(android::base::ParseUint(" 123", &i));
80 EXPECT_EQ(123u, i);
81 errno = 0;
82 ASSERT_FALSE(android::base::ParseUint("-123", &i));
83 EXPECT_EQ(EINVAL, errno);
84 errno = 0;
85 EXPECT_FALSE(android::base::ParseUint(" -123", &i));
86 EXPECT_EQ(EINVAL, errno);
87
88 unsigned short s = 0u;
89 ASSERT_TRUE(android::base::ParseUint("1234", &s));
90 ASSERT_EQ(1234u, s);
91
92 ASSERT_TRUE(android::base::ParseUint("12", &i, 15u));
93 ASSERT_EQ(12u, i);
94 errno = 0;
95 ASSERT_FALSE(android::base::ParseUint("-12", &i, 15u));
96 ASSERT_EQ(EINVAL, errno);
97 errno = 0;
98 ASSERT_FALSE(android::base::ParseUint("16", &i, 15u));
99 ASSERT_EQ(ERANGE, errno);
100
101 errno = 0;
102 ASSERT_FALSE(android::base::ParseUint<unsigned short>("x", nullptr));
103 ASSERT_EQ(EINVAL, errno);
104 errno = 0;
105 ASSERT_FALSE(android::base::ParseUint<unsigned short>("123x", nullptr));
106 ASSERT_EQ(EINVAL, errno);
107 ASSERT_TRUE(android::base::ParseUint<unsigned short>("1234", nullptr));
108
109 errno = 0;
110 unsigned long long int lli;
111 EXPECT_FALSE(android::base::ParseUint("-123", &lli));
112 EXPECT_EQ(EINVAL, errno);
113 errno = 0;
114 EXPECT_FALSE(android::base::ParseUint(" -123", &lli));
115 EXPECT_EQ(EINVAL, errno);
116 }
117
TEST(parseint,no_implicit_octal)118 TEST(parseint, no_implicit_octal) {
119 int i = 0;
120 ASSERT_TRUE(android::base::ParseInt("0123", &i));
121 ASSERT_EQ(123, i);
122
123 unsigned int u = 0u;
124 ASSERT_TRUE(android::base::ParseUint("0123", &u));
125 ASSERT_EQ(123u, u);
126 }
127
TEST(parseint,explicit_hex)128 TEST(parseint, explicit_hex) {
129 int i = 0;
130 ASSERT_TRUE(android::base::ParseInt("0x123", &i));
131 ASSERT_EQ(0x123, i);
132 i = 0;
133 EXPECT_TRUE(android::base::ParseInt(" 0x123", &i));
134 EXPECT_EQ(0x123, i);
135
136 unsigned int u = 0u;
137 ASSERT_TRUE(android::base::ParseUint("0x123", &u));
138 ASSERT_EQ(0x123u, u);
139 u = 0u;
140 EXPECT_TRUE(android::base::ParseUint(" 0x123", &u));
141 EXPECT_EQ(0x123u, u);
142 }
143
TEST(parseint,string)144 TEST(parseint, string) {
145 int i = 0;
146 ASSERT_TRUE(android::base::ParseInt(std::string("123"), &i));
147 ASSERT_EQ(123, i);
148
149 unsigned int u = 0u;
150 ASSERT_TRUE(android::base::ParseUint(std::string("123"), &u));
151 ASSERT_EQ(123u, u);
152 }
153
TEST(parseint,untouched_on_failure)154 TEST(parseint, untouched_on_failure) {
155 int i = 123;
156 ASSERT_FALSE(android::base::ParseInt("456x", &i));
157 ASSERT_EQ(123, i);
158
159 unsigned int u = 123u;
160 ASSERT_FALSE(android::base::ParseUint("456x", &u));
161 ASSERT_EQ(123u, u);
162 }
163
TEST(parseint,ParseByteCount)164 TEST(parseint, ParseByteCount) {
165 uint64_t i = 0;
166 ASSERT_TRUE(android::base::ParseByteCount("123b", &i));
167 ASSERT_EQ(123ULL, i);
168
169 ASSERT_TRUE(android::base::ParseByteCount("8k", &i));
170 ASSERT_EQ(8ULL * 1024, i);
171
172 ASSERT_TRUE(android::base::ParseByteCount("8M", &i));
173 ASSERT_EQ(8ULL * 1024 * 1024, i);
174
175 ASSERT_TRUE(android::base::ParseByteCount("6g", &i));
176 ASSERT_EQ(6ULL * 1024 * 1024 * 1024, i);
177
178 ASSERT_TRUE(android::base::ParseByteCount("1T", &i));
179 ASSERT_EQ(1ULL * 1024 * 1024 * 1024 * 1024, i);
180
181 ASSERT_TRUE(android::base::ParseByteCount("2p", &i));
182 ASSERT_EQ(2ULL * 1024 * 1024 * 1024 * 1024 * 1024, i);
183
184 ASSERT_TRUE(android::base::ParseByteCount("4e", &i));
185 ASSERT_EQ(4ULL * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, i);
186 }
187
TEST(parseint,ParseByteCount_invalid_suffix)188 TEST(parseint, ParseByteCount_invalid_suffix) {
189 unsigned u;
190 ASSERT_FALSE(android::base::ParseByteCount("1x", &u));
191 }
192
TEST(parseint,ParseByteCount_overflow)193 TEST(parseint, ParseByteCount_overflow) {
194 uint64_t u64;
195 ASSERT_FALSE(android::base::ParseByteCount("4294967295E", &u64));
196
197 uint16_t u16;
198 ASSERT_TRUE(android::base::ParseByteCount("63k", &u16));
199 ASSERT_EQ(63U * 1024, u16);
200 ASSERT_TRUE(android::base::ParseByteCount("65535b", &u16));
201 ASSERT_EQ(65535U, u16);
202 ASSERT_FALSE(android::base::ParseByteCount("65k", &u16));
203 }
204