Elliott Hughes | 7dd8e20 | 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 | |
Elliott Hughes | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/parseint.h" |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | TEST(parseint, signed_smoke) { |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 22 | int i = 0; |
Spencer Low | d646097 | 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 | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 25 | |
Spencer Low | d646097 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 26 | ASSERT_TRUE(android::base::ParseInt("123", &i)); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 27 | ASSERT_EQ(123, i); |
Spencer Low | d646097 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 28 | ASSERT_TRUE(android::base::ParseInt("-123", &i)); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 29 | ASSERT_EQ(-123, i); |
| 30 | |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 31 | short s = 0; |
Spencer Low | d646097 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 32 | ASSERT_TRUE(android::base::ParseInt("1234", &s)); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 33 | ASSERT_EQ(1234, s); |
| 34 | |
Spencer Low | d646097 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 35 | ASSERT_TRUE(android::base::ParseInt("12", &i, 0, 15)); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 36 | ASSERT_EQ(12, i); |
Spencer Low | d646097 | 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 | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | TEST(parseint, unsigned_smoke) { |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 42 | unsigned int i = 0u; |
Spencer Low | d646097 | 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 | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 45 | |
Spencer Low | d646097 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 46 | ASSERT_TRUE(android::base::ParseUint("123", &i)); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 47 | ASSERT_EQ(123u, i); |
Spencer Low | d646097 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 48 | ASSERT_FALSE(android::base::ParseUint("-123", &i)); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 49 | |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 50 | unsigned short s = 0u; |
Spencer Low | d646097 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 51 | ASSERT_TRUE(android::base::ParseUint("1234", &s)); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 52 | ASSERT_EQ(1234u, s); |
| 53 | |
Spencer Low | d646097 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 54 | ASSERT_TRUE(android::base::ParseUint("12", &i, 15u)); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 55 | ASSERT_EQ(12u, i); |
Spencer Low | d646097 | 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 | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST(parseint, no_implicit_octal) { |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 61 | int i = 0; |
Spencer Low | d646097 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 62 | ASSERT_TRUE(android::base::ParseInt("0123", &i)); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 63 | ASSERT_EQ(123, i); |
| 64 | |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 65 | unsigned int u = 0u; |
Spencer Low | d646097 | 2015-09-11 20:01:29 -0700 | [diff] [blame] | 66 | ASSERT_TRUE(android::base::ParseUint("0123", &u)); |
Elliott Hughes | 7dd8e20 | 2015-09-04 16:26:51 -0700 | [diff] [blame] | 67 | ASSERT_EQ(123u, u); |
| 68 | } |
Elliott Hughes | efa901e | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 69 | |
| 70 | TEST(parseint, explicit_hex) { |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 71 | int i = 0; |
Elliott Hughes | efa901e | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 72 | ASSERT_TRUE(android::base::ParseInt("0x123", &i)); |
| 73 | ASSERT_EQ(0x123, i); |
| 74 | |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 75 | unsigned int u = 0u; |
Elliott Hughes | efa901e | 2015-11-02 09:01:53 -0800 | [diff] [blame] | 76 | ASSERT_TRUE(android::base::ParseUint("0x123", &u)); |
| 77 | ASSERT_EQ(0x123u, u); |
| 78 | } |
Elliott Hughes | 31d04db | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 79 | |
| 80 | TEST(parseint, string) { |
| 81 | int i = 0; |
| 82 | ASSERT_TRUE(android::base::ParseInt(std::string("123"), &i)); |
| 83 | ASSERT_EQ(123, i); |
| 84 | |
| 85 | unsigned int u = 0u; |
| 86 | ASSERT_TRUE(android::base::ParseUint(std::string("123"), &u)); |
| 87 | ASSERT_EQ(123u, u); |
| 88 | } |
| 89 | |
| 90 | TEST(parseint, untouched_on_failure) { |
| 91 | int i = 123; |
| 92 | ASSERT_FALSE(android::base::ParseInt("456x", &i)); |
| 93 | ASSERT_EQ(123, i); |
| 94 | |
| 95 | unsigned int u = 123u; |
| 96 | ASSERT_FALSE(android::base::ParseInt("456x", &u)); |
| 97 | ASSERT_EQ(123u, u); |
| 98 | } |
Elliott Hughes | 0676899 | 2018-04-19 19:48:25 -0700 | [diff] [blame^] | 99 | |
| 100 | TEST(parseint, ParseByteCount) { |
| 101 | uint64_t i = 0; |
| 102 | ASSERT_TRUE(android::base::ParseByteCount("123b", &i)); |
| 103 | ASSERT_EQ(123ULL, i); |
| 104 | |
| 105 | ASSERT_TRUE(android::base::ParseByteCount("8k", &i)); |
| 106 | ASSERT_EQ(8ULL * 1024, i); |
| 107 | |
| 108 | ASSERT_TRUE(android::base::ParseByteCount("8M", &i)); |
| 109 | ASSERT_EQ(8ULL * 1024 * 1024, i); |
| 110 | |
| 111 | ASSERT_TRUE(android::base::ParseByteCount("6g", &i)); |
| 112 | ASSERT_EQ(6ULL * 1024 * 1024 * 1024, i); |
| 113 | |
| 114 | ASSERT_TRUE(android::base::ParseByteCount("1T", &i)); |
| 115 | ASSERT_EQ(1ULL * 1024 * 1024 * 1024 * 1024, i); |
| 116 | |
| 117 | ASSERT_TRUE(android::base::ParseByteCount("2p", &i)); |
| 118 | ASSERT_EQ(2ULL * 1024 * 1024 * 1024 * 1024 * 1024, i); |
| 119 | |
| 120 | ASSERT_TRUE(android::base::ParseByteCount("4e", &i)); |
| 121 | ASSERT_EQ(4ULL * 1024 * 1024 * 1024 * 1024 * 1024 * 1024, i); |
| 122 | } |
| 123 | |
| 124 | TEST(parseint, ParseByteCount_invalid_suffix) { |
| 125 | unsigned u; |
| 126 | ASSERT_FALSE(android::base::ParseByteCount("1x", &u)); |
| 127 | } |
| 128 | |
| 129 | TEST(parseint, ParseByteCount_overflow) { |
| 130 | uint64_t u64; |
| 131 | ASSERT_FALSE(android::base::ParseByteCount("4294967295E", &u64)); |
| 132 | |
| 133 | uint16_t u16; |
| 134 | ASSERT_TRUE(android::base::ParseByteCount("63k", &u16)); |
| 135 | ASSERT_EQ(63U * 1024, u16); |
| 136 | ASSERT_TRUE(android::base::ParseByteCount("65535b", &u16)); |
| 137 | ASSERT_EQ(65535U, u16); |
| 138 | ASSERT_FALSE(android::base::ParseByteCount("65k", &u16)); |
| 139 | } |