]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/platform-bionic.git/blob - tests/sstream_test.cpp
Merge "Sync with upstream for gethnamaddr.c."
[android-sdk/platform-bionic.git] / tests / sstream_test.cpp
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  */
17 #include <gtest/gtest.h>
19 #include <stdint.h>
20 #include <limits>
21 #include <sstream>
23 // TODO: move this test to libcxx.
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);
29   ss >> result;
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 }
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 }
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);
48   CheckOverflow<uint16_t>(std::numeric_limits<uint16_t>::max(), "65535", false);
49   CheckOverflow<uint16_t>(0, "65536", true);
50 }
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);
58   CheckOverflow<uint32_t>(std::numeric_limits<uint32_t>::max(), "4294967295", false);
59   CheckOverflow<uint32_t>(0, "4294967296", true);
60 }
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);
68   CheckOverflow<uint64_t>(std::numeric_limits<uint64_t>::max(), "18446744073709551615", false);
69   CheckOverflow<uint64_t>(0, "18446744073709551616", true);
70 }