From be2bccadbcea2147d826fe665ef562ca961c178d Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 30 Oct 2023 22:10:42 +0800 Subject: [PATCH] [17_1] unit tests for as_int --- tests/Kernel/Types/string_test.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/Kernel/Types/string_test.cpp b/tests/Kernel/Types/string_test.cpp index 0fb305f39..561835dc2 100644 --- a/tests/Kernel/Types/string_test.cpp +++ b/tests/Kernel/Types/string_test.cpp @@ -69,6 +69,29 @@ TEST_CASE ("test as bool") { // implicit conversion from char* } +TEST_CASE ("as_int") { + SUBCASE ("normal conversion") { + CHECK_EQ (as_int ("1"), 1); + CHECK_EQ (as_int ("-1"), -1); + CHECK_EQ (as_int ("0"), 0); + } + + // assuming int is 32bit + SUBCASE ("min and max") { + CHECK_EQ (as_int ("-2147483648"), -2147483648); + CHECK_EQ (as_int ("2147483647"), 2147483647); + } + + SUBCASE ("int overflow") { + CHECK_EQ (as_int ("-2147483649"), 2147483647); + CHECK_EQ (as_int ("2147483648"), -2147483648); + } + + SUBCASE ("int overflow") { + CHECK_EQ (as_int ("not a int"), 0); + } +} + TEST_CASE ("test as strig bool") { CHECK_EQ (as_string_bool (true) == string ("true"), true); CHECK_EQ (as_string_bool (false) == string ("false"), true);