Skip to content

Commit b5ea328

Browse files
committed
TestUtils: added more tests to strToInt()
1 parent eb00a42 commit b5ea328

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

test/testutils.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,43 @@ class TestUtils : public TestFixture {
223223

224224
void strToInt() {
225225
ASSERT_EQUALS(1, ::strToInt<int>("1"));
226+
ASSERT_EQUALS(1, ::strToInt<int>("+1"));
226227
ASSERT_EQUALS(-1, ::strToInt<int>("-1"));
227228
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"));
228235
ASSERT_THROW_EQUALS(::strToInt<int>(""), std::runtime_error, "converting '' to integer failed - not an integer");
229236
ASSERT_THROW_EQUALS(::strToInt<std::size_t>(""), std::runtime_error, "converting '' to integer failed - not an integer");
230237
ASSERT_THROW_EQUALS(::strToInt<int>(" "), std::runtime_error, "converting ' ' to integer failed - not an integer");
231238
ASSERT_THROW_EQUALS(::strToInt<std::size_t>(" "), std::runtime_error, "converting ' ' to integer failed - not an integer");
232239
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");
233243
ASSERT_THROW_EQUALS(::strToInt<int>("1ms"), std::runtime_error, "converting '1ms' to integer failed - not an integer");
234244
ASSERT_THROW_EQUALS(::strToInt<int>("1.0"), std::runtime_error, "converting '1.0' to integer failed - not an integer");
245+
ASSERT_THROW_EQUALS(::strToInt<int>("+1.0"), std::runtime_error, "converting '+1.0' to integer failed - not an integer");
246+
ASSERT_THROW_EQUALS(::strToInt<int>("-1.0"), std::runtime_error, "converting '-1.0' to integer failed - not an integer");
235247
ASSERT_THROW_EQUALS(::strToInt<int>("one"), std::runtime_error, "converting 'one' to integer failed - not an integer");
248+
//ASSERT_THROW_EQUALS(::strToInt<int>(" 1"), std::runtime_error, "converting ' 1' to integer failed - not an integer"); // TODO: should fail
249+
//ASSERT_THROW_EQUALS(::strToInt<int>("\t1"), std::runtime_error, "converting '\t1' to integer failed - not an integer"); // TODO should fail
250+
ASSERT_THROW_EQUALS(::strToInt<int>("1 "), std::runtime_error, "converting '1 ' to integer failed - not an integer");
251+
ASSERT_THROW_EQUALS(::strToInt<int>("1\t"), std::runtime_error, "converting '1\t' to integer failed - not an integer");
252+
ASSERT_THROW_EQUALS(::strToInt<int>("+ 1"), std::runtime_error, "converting '+ 1' to integer failed - not an integer");
236253
ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1ms"), std::runtime_error, "converting '1ms' to integer failed - not an integer");
237254
ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1.0"), std::runtime_error, "converting '1.0' to integer failed - not an integer");
255+
ASSERT_THROW_EQUALS(::strToInt<unsigned int>("+1.0"), std::runtime_error, "converting '+1.0' to integer failed - not an integer");
256+
ASSERT_THROW_EQUALS(::strToInt<unsigned int>("-1.0"), std::runtime_error, "converting '-1.0' to integer failed - not an integer");
238257
ASSERT_THROW_EQUALS(::strToInt<unsigned int>("one"), std::runtime_error, "converting 'one' to integer failed - not an integer");
258+
//ASSERT_THROW_EQUALS(::strToInt<unsigned int>(" 1"), std::runtime_error, "converting ' 1' to integer failed - not an integer"); // TODO: should fail
259+
//ASSERT_THROW_EQUALS(::strToInt<unsigned int>("\t1"), std::runtime_error, "converting '\t1' to integer failed - not an integer"); // TODO: should fail
260+
ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1 "), std::runtime_error, "converting '1 ' to integer failed - not an integer");
261+
ASSERT_THROW_EQUALS(::strToInt<unsigned int>("1\t"), std::runtime_error, "converting '1\t' to integer failed - not an integer");
262+
ASSERT_THROW_EQUALS(::strToInt<unsigned int>("- 1"), std::runtime_error, "converting '- 1' to integer failed - not an integer");
239263
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)");
240264
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)");
241265
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)");

0 commit comments

Comments
 (0)