From 5a47a830d38388c6bd3721c12f253a3a90cf2cf4 Mon Sep 17 00:00:00 2001 From: "Daniel R. Carvalho" Date: Fri, 12 Feb 2021 11:55:46 -0300 Subject: [PATCH] base: Add enum to_number tests Add a test to convert a string containing a number into enums. One of the tests has been disabled to highlight an error-prone situation where a number that is not a valid enum manages to be converted to an enum. Change-Id: I7967c62feea335f3ffda40d8bf0334c20b53ee6c Signed-off-by: Daniel R. Carvalho Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/41334 Tested-by: kokoro Reviewed-by: Bobby R. Bruce Maintainer: Bobby R. Bruce --- src/base/str.test.cc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/base/str.test.cc b/src/base/str.test.cc index a064a87cb..f0cd483ab 100644 --- a/src/base/str.test.cc +++ b/src/base/str.test.cc @@ -301,6 +301,30 @@ TEST(StrTest, ToNumber64BitIntInvalidString) EXPECT_FALSE(to_number(input, output)); } +TEST(StrTest, ToNumberEnum) +{ + enum Number + { + TWO=2, + }; + Number output; + std::string input = "2"; + EXPECT_TRUE(to_number(input, output)); + EXPECT_EQ(TWO, output); +} + +/** Test that trying to convert a number to an enum that is not valid fails. */ +TEST(StrTest, DISABLED_ToNumberEnumInvalid) +{ + enum Number + { + TWO=2, + }; + Number output; + std::string input = "3"; + EXPECT_FALSE(to_number(input, output)); +} + TEST(StrTest, ToNumberFloat) { float output; -- 2.30.2