Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | |
| 3 | #include <string> |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 7 | #include "aos/util/string_to_num.h" |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 8 | |
| 9 | namespace aos { |
| 10 | namespace util { |
| 11 | namespace testing { |
| 12 | |
| 13 | TEST(StringToNumTest, CorrectNumber) { |
| 14 | int result; |
Daniel Petti | 5aa2979 | 2014-12-27 17:48:07 -0500 | [diff] [blame] | 15 | ASSERT_TRUE(StringToNumber<int>(::std::string("42"), &result)); |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 16 | EXPECT_EQ(result, 42); |
| 17 | } |
| 18 | |
| 19 | TEST(StringToNumTest, NegativeTest) { |
| 20 | int result; |
Daniel Petti | 5aa2979 | 2014-12-27 17:48:07 -0500 | [diff] [blame] | 21 | ASSERT_TRUE(StringToNumber<int>(::std::string("-42"), &result)); |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 22 | EXPECT_EQ(result, -42); |
| 23 | } |
| 24 | |
| 25 | TEST(StringToNumTest, NonNumber) { |
| 26 | int result; |
Daniel Petti | 5aa2979 | 2014-12-27 17:48:07 -0500 | [diff] [blame] | 27 | ASSERT_FALSE(StringToNumber<int>(::std::string("Daniel"), &result)); |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | TEST(StringToNumTest, NumberWithText) { |
| 31 | int result; |
Daniel Petti | 5aa2979 | 2014-12-27 17:48:07 -0500 | [diff] [blame] | 32 | ASSERT_FALSE(StringToNumber<int>(::std::string("42Daniel"), &result)); |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | TEST(StringToNumTest, OverflowTest) { |
| 36 | uint32_t result; |
| 37 | // 2 << 32 should overflow. |
Daniel Petti | 5aa2979 | 2014-12-27 17:48:07 -0500 | [diff] [blame] | 38 | ASSERT_FALSE(StringToNumber<uint32_t>(::std::string("4294967296"), &result)); |
| 39 | } |
| 40 | |
| 41 | TEST(StringToNumTest, FloatingPointTest) { |
| 42 | double result; |
Brian Silverman | 84c0103 | 2015-09-20 16:58:01 -0400 | [diff] [blame] | 43 | ASSERT_TRUE(StringToNumber<double>(::std::string("3.1415927"), &result)); |
Daniel Petti | 5aa2979 | 2014-12-27 17:48:07 -0500 | [diff] [blame] | 44 | EXPECT_EQ(result, 3.1415927); |
Daniel Petti | b6c885b | 2014-09-12 10:04:28 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | } // testing |
| 48 | } // util |
| 49 | } // aos |