MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / strToInt

Method strToInt

test/testutils.cpp:224–408  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

222 }
223
224 void strToInt() {
225 ASSERT_EQUALS(1, ::strToInt<int>("1"));
226 ASSERT_EQUALS(1, ::strToInt<int>("+1"));
227 ASSERT_EQUALS(-1, ::strToInt<int>("-1"));
228 ASSERT_EQUALS(1, ::strToInt<std::size_t>("1"));
229 ASSERT_EQUALS(1, ::strToInt<std::size_t>("+1"));
230 ASSERT_EQUALS(0, ::strToInt<int>("0"));
231 ASSERT_EQUALS(0, ::strToInt<int>("+0"));
232 ASSERT_EQUALS(0, ::strToInt<int>("-0"));
233 ASSERT_EQUALS(0, ::strToInt<std::size_t>("0"));
234 ASSERT_EQUALS(0, ::strToInt<std::size_t>("+0"));
235 ASSERT_THROW_EQUALS(::strToInt<int>(""), std::runtime_error, "converting '' to integer failed - not an integer (invalid_argument)");
236 ASSERT_THROW_EQUALS(::strToInt<std::size_t>(""), std::runtime_error, "converting '' to integer failed - not an integer (invalid_argument)");
237 ASSERT_THROW_EQUALS(::strToInt<int>(" "), std::runtime_error, "converting ' ' to integer failed - not an integer (invalid_argument)");
238 ASSERT_THROW_EQUALS(::strToInt<std::size_t>(" "), std::runtime_error, "converting ' ' to integer failed - not an integer (invalid_argument)");
239 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("-1"), std::runtime_error, "converting '-1' to integer failed - needs to be positive");
240 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("-0"), std::runtime_error, "converting '-0' to integer failed - needs to be positive");
241 ASSERT_THROW_EQUALS(::strToInt<std::size_t>("-1"), std::runtime_error, "converting '-1' to integer failed - needs to be positive");
242 ASSERT_THROW_EQUALS(::strToInt<std::size_t>("-0"), std::runtime_error, "converting '-0' to integer failed - needs to be positive");
243 ASSERT_THROW_EQUALS(::strToInt<int>("1ms"), std::runtime_error, "converting '1ms' to integer failed - not an integer (pos)");
244 ASSERT_THROW_EQUALS(::strToInt<int>("1.0"), std::runtime_error, "converting '1.0' to integer failed - not an integer (pos)");
245 ASSERT_THROW_EQUALS(::strToInt<int>("+1.0"), std::runtime_error, "converting '+1.0' to integer failed - not an integer (pos)");
246 ASSERT_THROW_EQUALS(::strToInt<int>("-1.0"), std::runtime_error, "converting '-1.0' to integer failed - not an integer (pos)");
247 ASSERT_THROW_EQUALS(::strToInt<int>("1U"), std::runtime_error, "converting '1U' to integer failed - not an integer (pos)");
248 ASSERT_THROW_EQUALS(::strToInt<int>("1L"), std::runtime_error, "converting '1L' to integer failed - not an integer (pos)");
249 ASSERT_THROW_EQUALS(::strToInt<int>("1Z"), std::runtime_error, "converting '1Z' to integer failed - not an integer (pos)");
250 ASSERT_THROW_EQUALS(::strToInt<int>("01"), std::runtime_error, "converting '01' to integer failed - not an integer");
251 ASSERT_THROW_EQUALS(::strToInt<int>("0x1"), std::runtime_error, "converting '0x1' to integer failed - not an integer (pos)");
252 ASSERT_THROW_EQUALS(::strToInt<int>("0b1"), std::runtime_error, "converting '0b1' to integer failed - not an integer (pos)");
253 ASSERT_THROW_EQUALS(::strToInt<int>("one"), std::runtime_error, "converting 'one' to integer failed - not an integer (invalid_argument)");
254 ASSERT_THROW_EQUALS(::strToInt<int>(" 1"), std::runtime_error, "converting ' 1' to integer failed - not an integer");
255 ASSERT_THROW_EQUALS(::strToInt<int>("\t1"), std::runtime_error, "converting '\t1' to integer failed - not an integer");
256 ASSERT_THROW_EQUALS(::strToInt<int>("1 "), std::runtime_error, "converting '1 ' to integer failed - not an integer (pos)");
257 ASSERT_THROW_EQUALS(::strToInt<int>("1\t"), std::runtime_error, "converting '1\t' to integer failed - not an integer (pos)");
258 ASSERT_THROW_EQUALS(::strToInt<int>("+ 1"), std::runtime_error, "converting '+ 1' to integer failed - not an integer (invalid_argument)");
259 ASSERT_THROW_EQUALS(::strToInt<int>("O1"), std::runtime_error, "converting 'O1' to integer failed - not an integer (invalid_argument)");
260 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1ms"), std::runtime_error, "converting '1ms' to integer failed - not an integer (pos)");
261 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1.0"), std::runtime_error, "converting '1.0' to integer failed - not an integer (pos)");
262 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("+1.0"), std::runtime_error, "converting '+1.0' to integer failed - not an integer (pos)");
263 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("-1.0"), std::runtime_error, "converting '-1.0' to integer failed - not an integer (pos)");
264 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1U"), std::runtime_error, "converting '1U' to integer failed - not an integer (pos)");
265 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1L"), std::runtime_error, "converting '1L' to integer failed - not an integer (pos)");
266 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1Z"), std::runtime_error, "converting '1Z' to integer failed - not an integer (pos)");
267 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("01"), std::runtime_error, "converting '01' to integer failed - not an integer");
268 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("0x1"), std::runtime_error, "converting '0x1' to integer failed - not an integer (pos)");
269 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("0b1"), std::runtime_error, "converting '0b1' to integer failed - not an integer (pos)");
270 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("one"), std::runtime_error, "converting 'one' to integer failed - not an integer (invalid_argument)");
271 ASSERT_THROW_EQUALS(::strToInt<unsigned int>(" 1"), std::runtime_error, "converting ' 1' to integer failed - not an integer");
272 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("\t1"), std::runtime_error, "converting '\t1' to integer failed - not an integer");
273 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1 "), std::runtime_error, "converting '1 ' to integer failed - not an integer (pos)");
274 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1\t"), std::runtime_error, "converting '1\t' to integer failed - not an integer (pos)");
275 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("- 1"), std::runtime_error, "converting '- 1' to integer failed - not an integer (invalid_argument)");
276 ASSERT_THROW_EQUALS(::strToInt<unsigned int>("O1"), std::runtime_error, "converting 'O1' to integer failed - not an integer (invalid_argument)");
277 ASSERT_THROW_EQUALS(::strToInt<int>(std::to_string(static_cast<int64_t>(std::numeric_limits<int>::max()) + 1)), std::runtime_error, "converting '2147483648' to integer failed - out of range (limits)");
278 ASSERT_THROW_EQUALS(::strToInt<int>(std::to_string(static_cast<int64_t>(std::numeric_limits<int>::min()) - 1)), std::runtime_error, "converting '-2147483649' to integer failed - out of range (limits)");
279 ASSERT_THROW_EQUALS(::strToInt<int8_t>(std::to_string(static_cast<int64_t>(std::numeric_limits<int8_t>::max()) + 1)), std::runtime_error, "converting '128' to integer failed - out of range (limits)");
280 ASSERT_THROW_EQUALS(::strToInt<int8_t>(std::to_string(static_cast<int64_t>(std::numeric_limits<int8_t>::min()) - 1)), std::runtime_error, "converting '-129' to integer failed - out of range (limits)");
281 ASSERT_THROW_EQUALS(::strToInt<unsigned int>(std::to_string(static_cast<uint64_t>(std::numeric_limits<unsigned int>::max()) + 1)), std::runtime_error, "converting '4294967296' to integer failed - out of range (limits)");

Callers

nothing calls this directly

Calls 1

strToIntFunction · 0.85

Tested by

no test coverage detected