1 Star 1 Fork 5

aosp-riscv/platform_bionic

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
sstream_test.cpp 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
Elliott Hughes 提交于 2014-04-11 07:22 +08:00 . Improve the test.
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <stdint.h>
#include <limits>
#include <sstream>
// TODO: move this test to libcxx.
template <typename T>
static void CheckOverflow(T expected, const char* value, bool should_overflow) {
std::stringstream ss(value);
T result = T(0);
ss >> result;
EXPECT_FALSE(ss.bad()) << value << ' ' << int64_t(result);
EXPECT_EQ(should_overflow, ss.fail()) << value << ' ' << int64_t(result);
if (!should_overflow) { // glibc writes garbage on overflow.
ASSERT_EQ(expected, result) << value;
}
}
TEST(sstream, __get_integer_overflow_8) {
// The usual byte/char confusion means that operator>> on 8-bit types is used
// for chars, so there's no possibility of overflow.
}
TEST(sstream, __get_integer_overflow_16) {
CheckOverflow<int16_t>(std::numeric_limits<int16_t>::min(), "-32768", false);
CheckOverflow<int16_t>(0, "-32769", true);
CheckOverflow<int16_t>(std::numeric_limits<int16_t>::max(), "32767", false);
CheckOverflow<int16_t>(0, "32768", true);
CheckOverflow<uint16_t>(std::numeric_limits<uint16_t>::max(), "65535", false);
CheckOverflow<uint16_t>(0, "65536", true);
}
TEST(sstream, __get_integer_overflow_32) {
CheckOverflow<int32_t>(std::numeric_limits<int32_t>::min(), "-2147483648", false);
CheckOverflow<int32_t>(0, "-2147483649", true);
CheckOverflow<int32_t>(std::numeric_limits<int32_t>::max(), "2147483647", false);
CheckOverflow<int32_t>(0, "2147483648", true);
CheckOverflow<uint32_t>(std::numeric_limits<uint32_t>::max(), "4294967295", false);
CheckOverflow<uint32_t>(0, "4294967296", true);
}
TEST(sstream, __get_integer_overflow_64) {
CheckOverflow<int64_t>(std::numeric_limits<int64_t>::min(), "-9223372036854775808", false);
CheckOverflow<int64_t>(0, "-9223372036854775809", true);
CheckOverflow<int64_t>(std::numeric_limits<int64_t>::max(), "9223372036854775807", false);
CheckOverflow<int64_t>(0, "9223372036854775808", true);
CheckOverflow<uint64_t>(std::numeric_limits<uint64_t>::max(), "18446744073709551615", false);
CheckOverflow<uint64_t>(0, "18446744073709551616", true);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aosp-riscv/platform_bionic.git
git@gitee.com:aosp-riscv/platform_bionic.git
aosp-riscv
platform_bionic
platform_bionic
riscv64-android-12.0.0_dev

搜索帮助