| Elliott Hughes | 8c42606 | 2014-04-10 11:34:14 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2014 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 <gtest/gtest.h> | 
 | 18 |  | 
| Elliott Hughes | 6455ac7 | 2014-04-10 16:22:23 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
 | 20 | #include <limits> | 
| Elliott Hughes | 8c42606 | 2014-04-10 11:34:14 -0700 | [diff] [blame] | 21 | #include <sstream> | 
 | 22 |  | 
 | 23 | // TODO: move this test to libcxx. | 
 | 24 |  | 
| Elliott Hughes | 6455ac7 | 2014-04-10 16:22:23 -0700 | [diff] [blame] | 25 | template <typename T> | 
 | 26 | static void CheckOverflow(T expected, const char* value, bool should_overflow) { | 
 | 27 |   std::stringstream ss(value); | 
 | 28 |   T result = T(0); | 
| Elliott Hughes | 8c42606 | 2014-04-10 11:34:14 -0700 | [diff] [blame] | 29 |   ss >> result; | 
| Elliott Hughes | 6455ac7 | 2014-04-10 16:22:23 -0700 | [diff] [blame] | 30 |   EXPECT_FALSE(ss.bad()) << value << ' ' << int64_t(result); | 
 | 31 |   EXPECT_EQ(should_overflow, ss.fail()) << value << ' ' << int64_t(result); | 
 | 32 |   if (!should_overflow) { // glibc writes garbage on overflow. | 
 | 33 |     ASSERT_EQ(expected, result) << value; | 
 | 34 |   } | 
 | 35 | } | 
 | 36 |  | 
 | 37 | TEST(sstream, __get_integer_overflow_8) { | 
 | 38 |   // The usual byte/char confusion means that operator>> on 8-bit types is used | 
 | 39 |   // for chars, so there's no possibility of overflow. | 
 | 40 | } | 
 | 41 |  | 
 | 42 | TEST(sstream, __get_integer_overflow_16) { | 
 | 43 |   CheckOverflow<int16_t>(std::numeric_limits<int16_t>::min(), "-32768", false); | 
 | 44 |   CheckOverflow<int16_t>(0, "-32769", true); | 
 | 45 |   CheckOverflow<int16_t>(std::numeric_limits<int16_t>::max(), "32767", false); | 
 | 46 |   CheckOverflow<int16_t>(0, "32768", true); | 
 | 47 |  | 
 | 48 |   CheckOverflow<uint16_t>(std::numeric_limits<uint16_t>::max(), "65535", false); | 
 | 49 |   CheckOverflow<uint16_t>(0, "65536", true); | 
 | 50 | } | 
 | 51 |  | 
 | 52 | TEST(sstream, __get_integer_overflow_32) { | 
 | 53 |   CheckOverflow<int32_t>(std::numeric_limits<int32_t>::min(), "-2147483648", false); | 
 | 54 |   CheckOverflow<int32_t>(0, "-2147483649", true); | 
 | 55 |   CheckOverflow<int32_t>(std::numeric_limits<int32_t>::max(), "2147483647", false); | 
 | 56 |   CheckOverflow<int32_t>(0, "2147483648", true); | 
 | 57 |  | 
 | 58 |   CheckOverflow<uint32_t>(std::numeric_limits<uint32_t>::max(), "4294967295", false); | 
 | 59 |   CheckOverflow<uint32_t>(0, "4294967296", true); | 
 | 60 | } | 
 | 61 |  | 
 | 62 | TEST(sstream, __get_integer_overflow_64) { | 
 | 63 |   CheckOverflow<int64_t>(std::numeric_limits<int64_t>::min(), "-9223372036854775808", false); | 
 | 64 |   CheckOverflow<int64_t>(0, "-9223372036854775809", true); | 
 | 65 |   CheckOverflow<int64_t>(std::numeric_limits<int64_t>::max(), "9223372036854775807", false); | 
 | 66 |   CheckOverflow<int64_t>(0, "9223372036854775808", true); | 
 | 67 |  | 
 | 68 |   CheckOverflow<uint64_t>(std::numeric_limits<uint64_t>::max(), "18446744073709551615", false); | 
 | 69 |   CheckOverflow<uint64_t>(0, "18446744073709551616", true); | 
| Elliott Hughes | 8c42606 | 2014-04-10 11:34:14 -0700 | [diff] [blame] | 70 | } |