Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / third-party / benchmark / test / string_util_gtest.cc
blob698f2d43eb88fee059d70b47031df7958a136495
1 //===---------------------------------------------------------------------===//
2 // statistics_test - Unit tests for src/statistics.cc
3 //===---------------------------------------------------------------------===//
5 #include "../src/internal_macros.h"
6 #include "../src/string_util.h"
7 #include "gtest/gtest.h"
9 namespace {
10 TEST(StringUtilTest, stoul) {
12 size_t pos = 0;
13 EXPECT_EQ(0ul, benchmark::stoul("0", &pos));
14 EXPECT_EQ(1ul, pos);
17 size_t pos = 0;
18 EXPECT_EQ(7ul, benchmark::stoul("7", &pos));
19 EXPECT_EQ(1ul, pos);
22 size_t pos = 0;
23 EXPECT_EQ(135ul, benchmark::stoul("135", &pos));
24 EXPECT_EQ(3ul, pos);
26 #if ULONG_MAX == 0xFFFFFFFFul
28 size_t pos = 0;
29 EXPECT_EQ(0xFFFFFFFFul, benchmark::stoul("4294967295", &pos));
30 EXPECT_EQ(10ul, pos);
32 #elif ULONG_MAX == 0xFFFFFFFFFFFFFFFFul
34 size_t pos = 0;
35 EXPECT_EQ(0xFFFFFFFFFFFFFFFFul,
36 benchmark::stoul("18446744073709551615", &pos));
37 EXPECT_EQ(20ul, pos);
39 #endif
41 size_t pos = 0;
42 EXPECT_EQ(10ul, benchmark::stoul("1010", &pos, 2));
43 EXPECT_EQ(4ul, pos);
46 size_t pos = 0;
47 EXPECT_EQ(520ul, benchmark::stoul("1010", &pos, 8));
48 EXPECT_EQ(4ul, pos);
51 size_t pos = 0;
52 EXPECT_EQ(1010ul, benchmark::stoul("1010", &pos, 10));
53 EXPECT_EQ(4ul, pos);
56 size_t pos = 0;
57 EXPECT_EQ(4112ul, benchmark::stoul("1010", &pos, 16));
58 EXPECT_EQ(4ul, pos);
61 size_t pos = 0;
62 EXPECT_EQ(0xBEEFul, benchmark::stoul("BEEF", &pos, 16));
63 EXPECT_EQ(4ul, pos);
65 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
66 { ASSERT_THROW(benchmark::stoul("this is a test"), std::invalid_argument); }
67 #endif
70 TEST(StringUtilTest, stoi){{size_t pos = 0;
71 EXPECT_EQ(0, benchmark::stoi("0", &pos));
72 EXPECT_EQ(1ul, pos);
73 } // namespace
75 size_t pos = 0;
76 EXPECT_EQ(-17, benchmark::stoi("-17", &pos));
77 EXPECT_EQ(3ul, pos);
80 size_t pos = 0;
81 EXPECT_EQ(1357, benchmark::stoi("1357", &pos));
82 EXPECT_EQ(4ul, pos);
85 size_t pos = 0;
86 EXPECT_EQ(10, benchmark::stoi("1010", &pos, 2));
87 EXPECT_EQ(4ul, pos);
90 size_t pos = 0;
91 EXPECT_EQ(520, benchmark::stoi("1010", &pos, 8));
92 EXPECT_EQ(4ul, pos);
95 size_t pos = 0;
96 EXPECT_EQ(1010, benchmark::stoi("1010", &pos, 10));
97 EXPECT_EQ(4ul, pos);
100 size_t pos = 0;
101 EXPECT_EQ(4112, benchmark::stoi("1010", &pos, 16));
102 EXPECT_EQ(4ul, pos);
105 size_t pos = 0;
106 EXPECT_EQ(0xBEEF, benchmark::stoi("BEEF", &pos, 16));
107 EXPECT_EQ(4ul, pos);
109 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
110 { ASSERT_THROW(benchmark::stoi("this is a test"), std::invalid_argument); }
111 #endif
114 TEST(StringUtilTest, stod){{size_t pos = 0;
115 EXPECT_EQ(0.0, benchmark::stod("0", &pos));
116 EXPECT_EQ(1ul, pos);
119 size_t pos = 0;
120 EXPECT_EQ(-84.0, benchmark::stod("-84", &pos));
121 EXPECT_EQ(3ul, pos);
124 size_t pos = 0;
125 EXPECT_EQ(1234.0, benchmark::stod("1234", &pos));
126 EXPECT_EQ(4ul, pos);
129 size_t pos = 0;
130 EXPECT_EQ(1.5, benchmark::stod("1.5", &pos));
131 EXPECT_EQ(3ul, pos);
134 size_t pos = 0;
135 /* Note: exactly representable as double */
136 EXPECT_EQ(-1.25e+9, benchmark::stod("-1.25e+9", &pos));
137 EXPECT_EQ(8ul, pos);
139 #ifndef BENCHMARK_HAS_NO_EXCEPTIONS
140 { ASSERT_THROW(benchmark::stod("this is a test"), std::invalid_argument); }
141 #endif
144 TEST(StringUtilTest, StrSplit) {
145 EXPECT_EQ(benchmark::StrSplit("", ','), std::vector<std::string>{});
146 EXPECT_EQ(benchmark::StrSplit("hello", ','),
147 std::vector<std::string>({"hello"}));
148 EXPECT_EQ(benchmark::StrSplit("hello,there,is,more", ','),
149 std::vector<std::string>({"hello", "there", "is", "more"}));
152 } // end namespace