|
| 1 | +#include <datadog/error.h> |
| 2 | +#include <datadog/parse_util.h> |
| 3 | + |
| 4 | +#include <cassert> |
| 5 | +#include <cstdint> |
| 6 | +#include <limits> |
| 7 | +#include <string> |
| 8 | +#include <variant> |
| 9 | + |
| 10 | +#include "test.h" |
| 11 | + |
| 12 | +using namespace datadog::tracing; |
| 13 | + |
| 14 | +TEST_CASE("parse_int") { |
| 15 | + struct TestCase { |
| 16 | + int line; |
| 17 | + std::string name; |
| 18 | + std::string argument; |
| 19 | + int base; |
| 20 | + std::variant<int, Error::Code> expected; |
| 21 | + }; |
| 22 | + |
| 23 | + // clang-format off |
| 24 | + auto test_case = GENERATE(values<TestCase>({ |
| 25 | + {__LINE__, "zero (dec)", "0", 10, 0}, |
| 26 | + {__LINE__, "zeros (dec)", "000", 10, 0}, |
| 27 | + {__LINE__, "zero (hex)", "0", 16, 0}, |
| 28 | + {__LINE__, "zeros (hex)", "000", 16, 0}, |
| 29 | + {__LINE__, "leading whitespace (dec 1)", " 42", 10, Error::INVALID_INTEGER}, |
| 30 | + {__LINE__, "leading whitespace (dec 2)", "\t42", 10, Error::INVALID_INTEGER}, |
| 31 | + {__LINE__, "leading whitespace (dec 3)", "\n42", 10, Error::INVALID_INTEGER}, |
| 32 | + {__LINE__, "trailing whitespace (dec 1)", "42 ", 10, Error::INVALID_INTEGER}, |
| 33 | + {__LINE__, "trailing whitespace (dec 2)", "42\t", 10, Error::INVALID_INTEGER}, |
| 34 | + {__LINE__, "trailing whitespace (dec 3)", "42\n", 10, Error::INVALID_INTEGER}, |
| 35 | + {__LINE__, "leading whitespace (hex 1)", " 42", 16, Error::INVALID_INTEGER}, |
| 36 | + {__LINE__, "leading whitespace (hex 2)", "\t42", 16, Error::INVALID_INTEGER}, |
| 37 | + {__LINE__, "leading whitespace (hex 3)", "\n42", 16, Error::INVALID_INTEGER}, |
| 38 | + {__LINE__, "trailing whitespace (hex 1)", "42 ", 16, Error::INVALID_INTEGER}, |
| 39 | + {__LINE__, "trailing whitespace (hex 2)", "42\t", 16, Error::INVALID_INTEGER}, |
| 40 | + {__LINE__, "trailing whitespace (hex 3)", "42\n", 16, Error::INVALID_INTEGER}, |
| 41 | + {__LINE__, "no hex prefix", "0xbeef", 16, Error::INVALID_INTEGER}, |
| 42 | + {__LINE__, "dec rejects hex", "42beef", 10, Error::INVALID_INTEGER}, |
| 43 | + {__LINE__, "hex accepts hex", "42beef", 16, 0x42beef}, |
| 44 | + {__LINE__, "no trailing nonsense (dec)", "42xyz", 10, Error::INVALID_INTEGER}, |
| 45 | + {__LINE__, "no trailing nonsense (hex)", "42xyz", 16, Error::INVALID_INTEGER}, |
| 46 | + {__LINE__, "no leading nonsense (dec)", "xyz42", 10, Error::INVALID_INTEGER}, |
| 47 | + {__LINE__, "no leading nonsense (hex)", "xyz42", 16, Error::INVALID_INTEGER}, |
| 48 | + {__LINE__, "overflow", std::to_string(std::numeric_limits<int>::max()) + "0", 10, Error::OUT_OF_RANGE_INTEGER}, |
| 49 | + {__LINE__, "negative (dec)", "-10", 10, -10}, |
| 50 | + {__LINE__, "negative (hex)", "-a", 16, -10}, |
| 51 | + {__LINE__, "lower case", "a", 16, 10}, |
| 52 | + {__LINE__, "upper case", "A", 16, 10}, |
| 53 | + {__LINE__, "underflow", std::to_string(std::numeric_limits<int>::min()) + "0", 10, Error::OUT_OF_RANGE_INTEGER}, |
| 54 | + })); |
| 55 | + // clang-format on |
| 56 | + |
| 57 | + CAPTURE(test_case.line); |
| 58 | + CAPTURE(test_case.name); |
| 59 | + CAPTURE(test_case.argument); |
| 60 | + CAPTURE(test_case.base); |
| 61 | + |
| 62 | + const auto result = parse_int(test_case.argument, test_case.base); |
| 63 | + if (std::holds_alternative<int>(test_case.expected)) { |
| 64 | + const int& expected = std::get<int>(test_case.expected); |
| 65 | + REQUIRE(result); |
| 66 | + REQUIRE(*result == expected); |
| 67 | + } else { |
| 68 | + assert(std::holds_alternative<Error::Code>(test_case.expected)); |
| 69 | + const Error::Code& expected = std::get<Error::Code>(test_case.expected); |
| 70 | + REQUIRE(!result); |
| 71 | + REQUIRE(result.error().code == expected); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// This test case is similar to the one above, except that negative numbers are |
| 76 | +// not supported, and the underflow and overflow values are different. |
| 77 | +TEST_CASE("parse_uint64") { |
| 78 | + struct TestCase { |
| 79 | + int line; |
| 80 | + std::string name; |
| 81 | + std::string argument; |
| 82 | + int base; |
| 83 | + std::variant<std::uint64_t, Error::Code> expected; |
| 84 | + }; |
| 85 | + |
| 86 | + // clang-format off |
| 87 | + auto test_case = GENERATE(values<TestCase>({ |
| 88 | + {__LINE__, "zero (dec)", "0", 10, UINT64_C(0)}, |
| 89 | + {__LINE__, "zeros (dec)", "000", 10, UINT64_C(0)}, |
| 90 | + {__LINE__, "zero (hex)", "0", 16, UINT64_C(0)}, |
| 91 | + {__LINE__, "zeros (hex)", "000", 16, UINT64_C(0)}, |
| 92 | + {__LINE__, "leading whitespace (dec 1)", " 42", 10, Error::INVALID_INTEGER}, |
| 93 | + {__LINE__, "leading whitespace (dec 2)", "\t42", 10, Error::INVALID_INTEGER}, |
| 94 | + {__LINE__, "leading whitespace (dec 3)", "\n42", 10, Error::INVALID_INTEGER}, |
| 95 | + {__LINE__, "trailing whitespace (dec 1)", "42 ", 10, Error::INVALID_INTEGER}, |
| 96 | + {__LINE__, "trailing whitespace (dec 2)", "42\t", 10, Error::INVALID_INTEGER}, |
| 97 | + {__LINE__, "trailing whitespace (dec 3)", "42\n", 10, Error::INVALID_INTEGER}, |
| 98 | + {__LINE__, "leading whitespace (hex 1)", " 42", 16, Error::INVALID_INTEGER}, |
| 99 | + {__LINE__, "leading whitespace (hex 2)", "\t42", 16, Error::INVALID_INTEGER}, |
| 100 | + {__LINE__, "leading whitespace (hex 3)", "\n42", 16, Error::INVALID_INTEGER}, |
| 101 | + {__LINE__, "trailing whitespace (hex 1)", "42 ", 16, Error::INVALID_INTEGER}, |
| 102 | + {__LINE__, "trailing whitespace (hex 2)", "42\t", 16, Error::INVALID_INTEGER}, |
| 103 | + {__LINE__, "trailing whitespace (hex 3)", "42\n", 16, Error::INVALID_INTEGER}, |
| 104 | + {__LINE__, "no hex prefix", "0xbeef", 16, Error::INVALID_INTEGER}, |
| 105 | + {__LINE__, "dec rejects hex", "42beef", 10, Error::INVALID_INTEGER}, |
| 106 | + {__LINE__, "hex accepts hex", "42beef", 16, UINT64_C(0x42beef)}, |
| 107 | + {__LINE__, "no trailing nonsense (dec)", "42xyz", 10, Error::INVALID_INTEGER}, |
| 108 | + {__LINE__, "no trailing nonsense (hex)", "42xyz", 16, Error::INVALID_INTEGER}, |
| 109 | + {__LINE__, "no leading nonsense (dec)", "xyz42", 10, Error::INVALID_INTEGER}, |
| 110 | + {__LINE__, "no leading nonsense (hex)", "xyz42", 16, Error::INVALID_INTEGER}, |
| 111 | + {__LINE__, "overflow", std::to_string(std::numeric_limits<std::uint64_t>::max()) + "0", 10, Error::OUT_OF_RANGE_INTEGER}, |
| 112 | + {__LINE__, "negative (dec)", "-10", 10, Error::INVALID_INTEGER}, |
| 113 | + {__LINE__, "negative (hex)", "-a", 16, Error::INVALID_INTEGER}, |
| 114 | + {__LINE__, "lower case", "a", 16, UINT64_C(10)}, |
| 115 | + {__LINE__, "upper case", "A", 16, UINT64_C(10)}, |
| 116 | + })); |
| 117 | + // clang-format on |
| 118 | + |
| 119 | + CAPTURE(test_case.line); |
| 120 | + CAPTURE(test_case.name); |
| 121 | + CAPTURE(test_case.argument); |
| 122 | + CAPTURE(test_case.base); |
| 123 | + |
| 124 | + const auto result = parse_uint64(test_case.argument, test_case.base); |
| 125 | + if (std::holds_alternative<std::uint64_t>(test_case.expected)) { |
| 126 | + const std::uint64_t& expected = std::get<std::uint64_t>(test_case.expected); |
| 127 | + REQUIRE(result); |
| 128 | + REQUIRE(*result == expected); |
| 129 | + } else { |
| 130 | + assert(std::holds_alternative<Error::Code>(test_case.expected)); |
| 131 | + const Error::Code& expected = std::get<Error::Code>(test_case.expected); |
| 132 | + REQUIRE(!result); |
| 133 | + REQUIRE(result.error().code == expected); |
| 134 | + } |
| 135 | +} |
0 commit comments