Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 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 "base/parseint.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | TEST(parseint, signed_smoke) { |
| 22 | int i; |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 23 | ASSERT_FALSE(android::base::ParseInt("x", &i)); |
| 24 | ASSERT_FALSE(android::base::ParseInt("123x", &i)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 25 | |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 26 | ASSERT_TRUE(android::base::ParseInt("123", &i)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 27 | ASSERT_EQ(123, i); |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 28 | ASSERT_TRUE(android::base::ParseInt("-123", &i)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 29 | ASSERT_EQ(-123, i); |
| 30 | |
| 31 | short s; |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 32 | ASSERT_TRUE(android::base::ParseInt("1234", &s)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 33 | ASSERT_EQ(1234, s); |
| 34 | |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 35 | ASSERT_TRUE(android::base::ParseInt("12", &i, 0, 15)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 36 | ASSERT_EQ(12, i); |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 37 | ASSERT_FALSE(android::base::ParseInt("-12", &i, 0, 15)); |
| 38 | ASSERT_FALSE(android::base::ParseInt("16", &i, 0, 15)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | TEST(parseint, unsigned_smoke) { |
| 42 | unsigned int i; |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 43 | ASSERT_FALSE(android::base::ParseUint("x", &i)); |
| 44 | ASSERT_FALSE(android::base::ParseUint("123x", &i)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 45 | |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 46 | ASSERT_TRUE(android::base::ParseUint("123", &i)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 47 | ASSERT_EQ(123u, i); |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 48 | ASSERT_FALSE(android::base::ParseUint("-123", &i)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 49 | |
| 50 | unsigned short s; |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(android::base::ParseUint("1234", &s)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 52 | ASSERT_EQ(1234u, s); |
| 53 | |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 54 | ASSERT_TRUE(android::base::ParseUint("12", &i, 15u)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 55 | ASSERT_EQ(12u, i); |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 56 | ASSERT_FALSE(android::base::ParseUint("-12", &i, 15u)); |
| 57 | ASSERT_FALSE(android::base::ParseUint("16", &i, 15u)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST(parseint, no_implicit_octal) { |
| 61 | int i; |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 62 | ASSERT_TRUE(android::base::ParseInt("0123", &i)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 63 | ASSERT_EQ(123, i); |
| 64 | |
| 65 | unsigned int u; |
Spencer Low | b2d4949 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 66 | ASSERT_TRUE(android::base::ParseUint("0123", &u)); |
Elliott Hughes | afe151f | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 67 | ASSERT_EQ(123u, u); |
| 68 | } |