From 2fb59699db16952ec256c3fbf592ed29717d5289 Mon Sep 17 00:00:00 2001 From: Marvin Date: Mon, 11 Dec 2023 01:22:48 -0500 Subject: [PATCH] Added more test cases that checks for correct errors, base 2, octal, and hex --- tests/fast_int.cpp | 131 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 108 insertions(+), 23 deletions(-) diff --git a/tests/fast_int.cpp b/tests/fast_int.cpp index 78127db..8da57aa 100644 --- a/tests/fast_int.cpp +++ b/tests/fast_int.cpp @@ -5,41 +5,126 @@ int main() { +// all tests are assuming int and unsigned is size 32 bits - const std::vector expected_int_basic_test{0, 10, -40, 1001}; - const std::vector accept_int_basic_test {"0", "10", "-40", "1001 with text"}; - //const std::vector expected_int_basic_test{ 0, 1, -50, 9, 1001, std::numeric_limits::max(), std::numeric_limits::min()}; - //const std::vector accept_int_basic_test{ "0", "1", "-50", "9.999", "1001 with text", "2147483647", "-2147483648"}; - //const std::vector reject_int_basic_test{ "2147483648", "-2147483649", "+50"}; +// int basic tests + const std::vector int_basic_test_expected {0, 10, -40, 1001, 9, 2147483647, -2147483648}; + const std::vector int_basic_test {"0", "10", "-40", "1001 with text", "9.999", "2147483647 ", "-2147483648"}; - /* - const std::vector expected_int_base2_test{ 0, 1, 2, 2, 9}; - const std::vector accept_int_base2_test{ "0", "1", "10", "010", "101" }; - const std::vector reject_int_base2_test{ "2", "09" }; - - const std::vector expected_int_octal_test{ 0, 1, 7, 8}; - const std::vector accept_int_octal_test{ "00", "01", "07", "010"}; - const std::vector reject_int_octal_test{ "08", "1" }; - - const std::vector expected_int_hex_test{ 0, 1, 10, 16}; - const std::vector accept_int_hex_test{ "0", "1", "A", "0xF", "0X10"}; - const std::vector reject_int_hex_test{ "0x", "0X" }; - */ - - for (std::size_t i = 0; i < accept_int_basic_test.size(); ++i) + for (std::size_t i = 0; i < int_basic_test.size(); ++i) { - const auto& f = accept_int_basic_test[i]; - double result; + const auto& f = int_basic_test[i]; + int result; auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); if (answer.ec != std::errc()) { std::cerr << "could not convert to int for input:" << f << std::endl; return EXIT_FAILURE; } - else if (result != expected_int_basic_test[i]) { + else if (result != int_basic_test_expected[i]) { std::cerr << "result did not match with expected int for input:" << f << std::endl; return EXIT_FAILURE; } } +// invalid error test + const std::vector int_invalid_argument_test{ "text" , "text with 1002", "+50" " 50"}; + + for (std::size_t i = 0; i < int_invalid_argument_test.size(); ++i) + { + const auto& f = int_invalid_argument_test[i]; + int result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + if (answer.ec != std::errc::invalid_argument) { + std::cerr << "expected error should be 'invalid_argument'" << f << std::endl; + return EXIT_FAILURE; + } + } + + // out of range test + const std::vector int_out_of_range_test{ "2147483648", "-2147483649" }; + + for (std::size_t i = 0; i < int_out_of_range_test.size(); ++i) + { + const auto& f = int_out_of_range_test[i]; + int result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + if (answer.ec != std::errc::result_out_of_range) { + std::cerr << "expected error should be 'result_out_of_range'" << f << std::endl; + return EXIT_FAILURE; + } + } + + // base 2 test + const std::vector int_base_2_test_expected {0, 1, 4, 2, -1}; + const std::vector int_base_2_test {"0", "1", "100", "010", "-1"}; + + for (std::size_t i = 0; i < int_base_2_test.size(); ++i) + { + const auto& f = int_base_2_test[i]; + int result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 2); + if (answer.ec != std::errc()) { + std::cerr << "could not convert to int for input:" << f << std::endl; + return EXIT_FAILURE; + } + else if (result != int_base_2_test_expected[i]) { + std::cerr << "result did not match with expected int for input:" << f << std::endl; + return EXIT_FAILURE; + } + } + + // invalid error base 2 test + const std::vector int_invalid_argument_base_2_test{ "2", "A", "-2" }; + + for (std::size_t i = 0; i < int_invalid_argument_base_2_test.size(); ++i) + { + const auto& f = int_invalid_argument_base_2_test[i]; + int result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 2); + if (answer.ec != std::errc::invalid_argument) { + std::cerr << "expected error should be 'invalid_argument'" << f << std::endl; + return EXIT_FAILURE; + } + } + + // octal test + const std::vector int_base_octal_test_expected {0, 1, 7, 8, 9}; + const std::vector int_base_octal_test {"0", "1", "07", "010", "0011"}; + + for (std::size_t i = 0; i < int_base_octal_test.size(); ++i) + { + const auto& f = int_base_octal_test[i]; + int result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 8); + if (answer.ec != std::errc()) { + std::cerr << "could not convert to int for input:" << f << std::endl; + return EXIT_FAILURE; + } + else if (result != int_base_octal_test_expected[i]) { + std::cerr << "result did not match with expected int for input:" << f << std::endl; + return EXIT_FAILURE; + } + } + + // hex test + const std::vector int_base_hex_test_expected { 0, 1, 15, 16, 0, 16}; + const std::vector int_base_hex_test { "0", "1", "F", "010", "0x11", "10X11"}; + + for (std::size_t i = 0; i < int_base_hex_test.size(); ++i) + { + const auto& f = int_base_hex_test[i]; + int result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 16); + if (answer.ec != std::errc()) { + std::cerr << "could not convert to int for input:" << f << std::endl; + return EXIT_FAILURE; + } + else if (result != int_base_hex_test_expected[i]) { + std::cerr << "result did not match with expected int for input:" << f << std::endl; + return EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; } \ No newline at end of file