2018-06-05 10:36:26 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
2023-08-01 07:47:09 +00:00
|
|
|
// string_util_test - Unit tests for src/string_util.cc
|
2018-06-05 10:36:26 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
2023-03-16 11:07:13 +00:00
|
|
|
#include <tuple>
|
|
|
|
|
2019-09-20 09:25:31 +00:00
|
|
|
#include "../src/internal_macros.h"
|
2021-11-10 16:22:31 +00:00
|
|
|
#include "../src/string_util.h"
|
2023-07-14 12:56:01 +00:00
|
|
|
#include "gmock/gmock.h"
|
2018-06-05 10:36:26 +00:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
TEST(StringUtilTest, stoul) {
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
2018-12-10 10:24:22 +00:00
|
|
|
EXPECT_EQ(0ul, benchmark::stoul("0", &pos));
|
|
|
|
EXPECT_EQ(1ul, pos);
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
2018-12-10 10:24:22 +00:00
|
|
|
EXPECT_EQ(7ul, benchmark::stoul("7", &pos));
|
|
|
|
EXPECT_EQ(1ul, pos);
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
2018-12-10 10:24:22 +00:00
|
|
|
EXPECT_EQ(135ul, benchmark::stoul("135", &pos));
|
|
|
|
EXPECT_EQ(3ul, pos);
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
#if ULONG_MAX == 0xFFFFFFFFul
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(0xFFFFFFFFul, benchmark::stoul("4294967295", &pos));
|
2018-12-10 10:24:22 +00:00
|
|
|
EXPECT_EQ(10ul, pos);
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
#elif ULONG_MAX == 0xFFFFFFFFFFFFFFFFul
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
2021-11-10 16:22:31 +00:00
|
|
|
EXPECT_EQ(0xFFFFFFFFFFFFFFFFul,
|
|
|
|
benchmark::stoul("18446744073709551615", &pos));
|
2018-12-10 10:24:22 +00:00
|
|
|
EXPECT_EQ(20ul, pos);
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
2018-12-10 10:24:22 +00:00
|
|
|
EXPECT_EQ(10ul, benchmark::stoul("1010", &pos, 2));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
2018-12-10 10:24:22 +00:00
|
|
|
EXPECT_EQ(520ul, benchmark::stoul("1010", &pos, 8));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
2018-12-10 10:24:22 +00:00
|
|
|
EXPECT_EQ(1010ul, benchmark::stoul("1010", &pos, 10));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
2018-12-10 10:24:22 +00:00
|
|
|
EXPECT_EQ(4112ul, benchmark::stoul("1010", &pos, 16));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
2018-12-10 10:24:22 +00:00
|
|
|
EXPECT_EQ(0xBEEFul, benchmark::stoul("BEEF", &pos, 16));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
2019-09-20 09:25:31 +00:00
|
|
|
#ifndef BENCHMARK_HAS_NO_EXCEPTIONS
|
2023-03-16 11:07:13 +00:00
|
|
|
{
|
|
|
|
ASSERT_THROW(std::ignore = benchmark::stoul("this is a test"),
|
|
|
|
std::invalid_argument);
|
|
|
|
}
|
2019-09-20 09:25:31 +00:00
|
|
|
#endif
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 16:22:31 +00:00
|
|
|
TEST(StringUtilTest, stoi){{size_t pos = 0;
|
|
|
|
EXPECT_EQ(0, benchmark::stoi("0", &pos));
|
|
|
|
EXPECT_EQ(1ul, pos);
|
|
|
|
} // namespace
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(-17, benchmark::stoi("-17", &pos));
|
|
|
|
EXPECT_EQ(3ul, pos);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(1357, benchmark::stoi("1357", &pos));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(10, benchmark::stoi("1010", &pos, 2));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(520, benchmark::stoi("1010", &pos, 8));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(1010, benchmark::stoi("1010", &pos, 10));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(4112, benchmark::stoi("1010", &pos, 16));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(0xBEEF, benchmark::stoi("BEEF", &pos, 16));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
|
|
|
}
|
2019-09-20 09:25:31 +00:00
|
|
|
#ifndef BENCHMARK_HAS_NO_EXCEPTIONS
|
2023-03-16 11:07:13 +00:00
|
|
|
{
|
|
|
|
ASSERT_THROW(std::ignore = benchmark::stoi("this is a test"),
|
|
|
|
std::invalid_argument);
|
|
|
|
}
|
2019-09-20 09:25:31 +00:00
|
|
|
#endif
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 16:22:31 +00:00
|
|
|
TEST(StringUtilTest, stod){{size_t pos = 0;
|
|
|
|
EXPECT_EQ(0.0, benchmark::stod("0", &pos));
|
|
|
|
EXPECT_EQ(1ul, pos);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(-84.0, benchmark::stod("-84", &pos));
|
|
|
|
EXPECT_EQ(3ul, pos);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(1234.0, benchmark::stod("1234", &pos));
|
|
|
|
EXPECT_EQ(4ul, pos);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
EXPECT_EQ(1.5, benchmark::stod("1.5", &pos));
|
|
|
|
EXPECT_EQ(3ul, pos);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
/* Note: exactly representable as double */
|
|
|
|
EXPECT_EQ(-1.25e+9, benchmark::stod("-1.25e+9", &pos));
|
|
|
|
EXPECT_EQ(8ul, pos);
|
|
|
|
}
|
2019-09-20 09:25:31 +00:00
|
|
|
#ifndef BENCHMARK_HAS_NO_EXCEPTIONS
|
2023-03-16 11:07:13 +00:00
|
|
|
{
|
|
|
|
ASSERT_THROW(std::ignore = benchmark::stod("this is a test"),
|
|
|
|
std::invalid_argument);
|
|
|
|
}
|
2019-09-20 09:25:31 +00:00
|
|
|
#endif
|
2018-06-05 10:36:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 08:25:29 +00:00
|
|
|
TEST(StringUtilTest, StrSplit) {
|
2021-05-06 18:12:36 +00:00
|
|
|
EXPECT_EQ(benchmark::StrSplit("", ','), std::vector<std::string>{});
|
2021-04-28 08:25:29 +00:00
|
|
|
EXPECT_EQ(benchmark::StrSplit("hello", ','),
|
|
|
|
std::vector<std::string>({"hello"}));
|
2021-06-01 14:50:42 +00:00
|
|
|
EXPECT_EQ(benchmark::StrSplit("hello,there,is,more", ','),
|
|
|
|
std::vector<std::string>({"hello", "there", "is", "more"}));
|
2021-04-28 08:25:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 12:56:01 +00:00
|
|
|
using HumanReadableFixture = ::testing::TestWithParam<
|
|
|
|
std::tuple<double, benchmark::Counter::OneK, std::string>>;
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(
|
|
|
|
HumanReadableTests, HumanReadableFixture,
|
|
|
|
::testing::Values(
|
|
|
|
std::make_tuple(0.0, benchmark::Counter::kIs1024, "0"),
|
|
|
|
std::make_tuple(999.0, benchmark::Counter::kIs1024, "999"),
|
|
|
|
std::make_tuple(1000.0, benchmark::Counter::kIs1024, "1000"),
|
|
|
|
std::make_tuple(1024.0, benchmark::Counter::kIs1024, "1Ki"),
|
|
|
|
std::make_tuple(1000 * 1000.0, benchmark::Counter::kIs1024,
|
|
|
|
"976\\.56.Ki"),
|
|
|
|
std::make_tuple(1024 * 1024.0, benchmark::Counter::kIs1024, "1Mi"),
|
|
|
|
std::make_tuple(1000 * 1000 * 1000.0, benchmark::Counter::kIs1024,
|
|
|
|
"953\\.674Mi"),
|
|
|
|
std::make_tuple(1024 * 1024 * 1024.0, benchmark::Counter::kIs1024,
|
|
|
|
"1Gi"),
|
|
|
|
std::make_tuple(0.0, benchmark::Counter::kIs1000, "0"),
|
|
|
|
std::make_tuple(999.0, benchmark::Counter::kIs1000, "999"),
|
|
|
|
std::make_tuple(1000.0, benchmark::Counter::kIs1000, "1k"),
|
|
|
|
std::make_tuple(1024.0, benchmark::Counter::kIs1000, "1.024k"),
|
|
|
|
std::make_tuple(1000 * 1000.0, benchmark::Counter::kIs1000, "1M"),
|
|
|
|
std::make_tuple(1024 * 1024.0, benchmark::Counter::kIs1000,
|
|
|
|
"1\\.04858M"),
|
|
|
|
std::make_tuple(1000 * 1000 * 1000.0, benchmark::Counter::kIs1000,
|
|
|
|
"1G"),
|
|
|
|
std::make_tuple(1024 * 1024 * 1024.0, benchmark::Counter::kIs1000,
|
|
|
|
"1\\.07374G")));
|
|
|
|
|
|
|
|
TEST_P(HumanReadableFixture, HumanReadableNumber) {
|
|
|
|
std::string str = benchmark::HumanReadableNumber(std::get<0>(GetParam()),
|
|
|
|
std::get<1>(GetParam()));
|
|
|
|
ASSERT_THAT(str, ::testing::MatchesRegex(std::get<2>(GetParam())));
|
|
|
|
}
|
|
|
|
|
2018-06-05 10:36:26 +00:00
|
|
|
} // end namespace
|