refactor: condense the shared test data of parse and stringify.

This commit is contained in:
2025-07-14 09:43:23 +08:00
parent e42a3b6e58
commit fa52d7416f
6 changed files with 135 additions and 133 deletions

View File

@ -0,0 +1,74 @@
/**
* \file
* This file is a template for Parse function testing.
*
* As you seen that there is 2 style Parse function locate in main namespace and Rust namespace respectively.
* Both of them share the exactly same test data sheet.
* So it is good idea to extract these common part and put them into a place, and include it in respectively testbench file.
* That what this file does.
*
* Before including this template file, you must make sure that:
* \li Have include <gtest/gtest.h>
* \li Have include <yycc/prelude/rust.hpp>
* \li Have define a macro named \c TEST_NS which indicate the testbench namespace passed to gtest.
* \li Have define a macro with syntax <TT>TEST_SUCCESS(type_t, value, string_value, ...)</TT>.
* This macro will be called for those success case. \c type_t is the generic type of Parse function.
* \c value is the expected value after parse and \c string_value is the string value to be parsed.
* Other arguments should be redirect to corresponding Parse function.
* \li Have define a macro with syntax <TT>TEST_FAIL(type_t, string_value, ...)</TT>.
* Opposite with \c TEST_SUCCESS, this macro is for those bad case testing.
* All arguments have the same meaning with \c TEST_SUCCESS.
*
*/
TEST(TEST_NS, Common) {
TEST_SUCCESS(i8, INT8_C(-61), "-61");
TEST_SUCCESS(u8, UINT8_C(200), "200");
TEST_SUCCESS(i16, INT16_C(6161), "6161");
TEST_SUCCESS(u16, UINT16_C(32800), "32800");
TEST_SUCCESS(i32, INT32_C(61616161), "61616161");
TEST_SUCCESS(u32, UINT32_C(4294967293), "4294967293");
TEST_SUCCESS(i64, INT64_C(616161616161), "616161616161");
TEST_SUCCESS(u64, UINT64_C(9223372036854775807), "9223372036854775807");
TEST_SUCCESS(float, 1.0f, "1.0");
TEST_SUCCESS(double, 1.0, "1.0");
TEST_SUCCESS(bool, true, "true");
TEST_SUCCESS(bool, false, "false");
}
TEST(TEST_NS, Radix) {
TEST_SUCCESS(u32, UINT32_C(0xffff), "ffff", 16);
TEST_SUCCESS(u32, UINT32_C(032), "032", 8);
TEST_SUCCESS(u32, UINT32_C(0B1011), "1011", 2);
}
TEST(TEST_NS, CaseInsensitive) {
TEST_SUCCESS(bool, true, "tRUE");
}
TEST(TEST_NS, Overflow) {
TEST_FAIL(i8, "6161");
TEST_FAIL(u8, "32800");
TEST_FAIL(i16, "61616161");
TEST_FAIL(u16, "4294967293");
TEST_FAIL(i32, "616161616161");
TEST_FAIL(u32, "9223372036854775807");
TEST_FAIL(i64, "616161616161616161616161");
TEST_FAIL(u64, "92233720368547758079223372036854775807");
TEST_FAIL(float, "1e40");
TEST_FAIL(double, "1e114514");
}
TEST(TEST_NS, BadRadix) {
TEST_FAIL(u32, "fghj", 16);
TEST_FAIL(u32, "099", 8);
TEST_FAIL(u32, "12345", 2);
}
TEST(TEST_NS, InvalidWords) {
TEST_FAIL(u32, "hello, world!");
TEST_FAIL(bool, "hello, world!");
}

View File

@ -0,0 +1,39 @@
/**
* \file
* This file is a template for Stringify function testing.
*
* Same as parse_template.hpp .
*
* Before including this template file, you must make sure that:
* \li Have include <gtest/gtest.h>
* \li Have include <yycc/prelude/rust.hpp>
* \li Have define a macro named \c TEST_NS which indicate the testbench namespace passed to gtest.
* \li Have define a macro with syntax <TT>TEST_SUCCESS(type_t, value, string_value, ...)</TT>.
* This macro will be called for those success case. \c type_t is the generic type of Stringify function.
* \c value is the value will be stringified and \c string_value is the expected string.
* Other arguments should be redirect to corresponding Stringify function.
*
*/
TEST(TEST_NS, Common) {
TEST_SUCCESS(i8, INT8_C(-61), "-61");
TEST_SUCCESS(u8, UINT8_C(200), "200");
TEST_SUCCESS(i16, INT16_C(6161), "6161");
TEST_SUCCESS(u16, UINT16_C(32800), "32800");
TEST_SUCCESS(i32, INT32_C(61616161), "61616161");
TEST_SUCCESS(u32, UINT32_C(4294967293), "4294967293");
TEST_SUCCESS(i64, INT64_C(616161616161), "616161616161");
TEST_SUCCESS(u64, UINT64_C(9223372036854775807), "9223372036854775807");
TEST_SUCCESS(float, 1.0f, "1.0", std::chars_format::fixed, 1);
TEST_SUCCESS(double, 1.0, "1.0", std::chars_format::fixed, 1);
TEST_SUCCESS(bool, true, "true");
TEST_SUCCESS(bool, false, "false");
}
TEST(TEST_NS, Radix) {
TEST_SUCCESS(u32, UINT32_C(0xffff), "ffff", 16);
TEST_SUCCESS(u32, UINT32_C(032), "32", 8);
TEST_SUCCESS(u32, UINT32_C(0B1011), "1011", 2);
}

View File

@ -11,6 +11,8 @@ namespace yycctest::num::parse {
// These 2 test macros build string container via given string.
// Check `try_parse` first, and then check `parse`.
#define TEST_NS NumParse
#define TEST_SUCCESS(type_t, value, string_value, ...) \
{ \
u8string cache_string(YYCC_U8(string_value)); \
@ -28,56 +30,6 @@ namespace yycctest::num::parse {
EXPECT_ANY_THROW(PARSE::parse<type_t>(cache_string, ##__VA_ARGS__)); \
}
TEST(NumParse, Common) {
TEST_SUCCESS(i8, INT8_C(-61), "-61");
TEST_SUCCESS(u8, UINT8_C(200), "200");
TEST_SUCCESS(i16, INT16_C(6161), "6161");
TEST_SUCCESS(u16, UINT16_C(32800), "32800");
TEST_SUCCESS(i32, INT32_C(61616161), "61616161");
TEST_SUCCESS(u32, UINT32_C(4294967293), "4294967293");
TEST_SUCCESS(i64, INT64_C(616161616161), "616161616161");
TEST_SUCCESS(u64, UINT64_C(9223372036854775807), "9223372036854775807");
#include "../../shared/parse_template.hpp"
TEST_SUCCESS(float, 1.0f, "1.0");
TEST_SUCCESS(double, 1.0, "1.0");
TEST_SUCCESS(bool, true, "true");
TEST_SUCCESS(bool, false, "false");
}
TEST(NumParse, Radix) {
TEST_SUCCESS(u32, UINT32_C(0xffff), "ffff", 16);
TEST_SUCCESS(u32, UINT32_C(032), "032", 8);
TEST_SUCCESS(u32, UINT32_C(0B1011), "1011", 2);
}
TEST(NumParse, CaseInsensitive) {
TEST_SUCCESS(bool, true, "tRUE");
}
TEST(NumParse, Overflow) {
TEST_FAIL(i8, "6161");
TEST_FAIL(u8, "32800");
TEST_FAIL(i16, "61616161");
TEST_FAIL(u16, "4294967293");
TEST_FAIL(i32, "616161616161");
TEST_FAIL(u32, "9223372036854775807");
TEST_FAIL(i64, "616161616161616161616161");
TEST_FAIL(u64, "92233720368547758079223372036854775807");
TEST_FAIL(float, "1e40");
TEST_FAIL(double, "1e114514");
}
TEST(NumParse, BadRadix) {
TEST_FAIL(u32, "fghj", 16);
TEST_FAIL(u32, "099", 8);
TEST_FAIL(u32, "12345", 2);
}
TEST(NumParse, InvalidWords) {
TEST_FAIL(u32, "hello, world!");
TEST_FAIL(bool, "hello, world!");
}
} // namespace yycctest::string::parse
} // namespace yycctest::num::parse

View File

@ -8,6 +8,8 @@
namespace yycctest::num::stringify {
#define TEST_NS NumStringify
#define TEST_SUCCESS(type_t, value, string_value, ...) \
{ \
type_t cache = value; \
@ -15,27 +17,6 @@ namespace yycctest::num::stringify {
EXPECT_EQ(ret, YYCC_U8(string_value)); \
}
TEST(NumStringify, Common) {
TEST_SUCCESS(i8, INT8_C(-61), "-61");
TEST_SUCCESS(u8, UINT8_C(200), "200");
TEST_SUCCESS(i16, INT16_C(6161), "6161");
TEST_SUCCESS(u16, UINT16_C(32800), "32800");
TEST_SUCCESS(i32, INT32_C(61616161), "61616161");
TEST_SUCCESS(u32, UINT32_C(4294967293), "4294967293");
TEST_SUCCESS(i64, INT64_C(616161616161), "616161616161");
TEST_SUCCESS(u64, UINT64_C(9223372036854775807), "9223372036854775807");
TEST_SUCCESS(float, 1.0f, "1.0", std::chars_format::fixed, 1);
TEST_SUCCESS(double, 1.0, "1.0", std::chars_format::fixed, 1);
TEST_SUCCESS(bool, true, "true");
TEST_SUCCESS(bool, false, "false");
}
TEST(NumStringify, Radix) {
TEST_SUCCESS(u32, UINT32_C(0xffff), "ffff", 16);
TEST_SUCCESS(u32, UINT32_C(032), "32", 8);
TEST_SUCCESS(u32, UINT32_C(0B1011), "1011", 2);
}
#include "../../shared/stringify_template.hpp"
} // namespace yycctest::string::stringify

View File

@ -6,14 +6,12 @@
#define PARSE ::yycc::rust::num::parse
namespace yycctest::rust::parse {
namespace yycctest::rust::num::parse {
// We only want to test it if C++ support it.
#if defined(YYCC_CPPFEAT_EXPECTED)
// This namespace is just a wrapper for legacy "parse" module.
// So the test is just a copy of original implementation.
// Please update this if original test was updated.
#define TEST_NS RustNumParse
#define TEST_SUCCESS(type_t, expected_value, string_value, ...) \
{ \
@ -30,57 +28,7 @@ namespace yycctest::rust::parse {
EXPECT_FALSE(rv.has_value()); \
}
TEST(RustNumParse, Common) {
TEST_SUCCESS(i8, INT8_C(-61), "-61");
TEST_SUCCESS(u8, UINT8_C(200), "200");
TEST_SUCCESS(i16, INT16_C(6161), "6161");
TEST_SUCCESS(u16, UINT16_C(32800), "32800");
TEST_SUCCESS(i32, INT32_C(61616161), "61616161");
TEST_SUCCESS(u32, UINT32_C(4294967293), "4294967293");
TEST_SUCCESS(i64, INT64_C(616161616161), "616161616161");
TEST_SUCCESS(u64, UINT64_C(9223372036854775807), "9223372036854775807");
TEST_SUCCESS(float, 1.0f, "1.0");
TEST_SUCCESS(double, 1.0, "1.0");
TEST_SUCCESS(bool, true, "true");
TEST_SUCCESS(bool, false, "false");
}
TEST(RustNumParse, Radix) {
TEST_SUCCESS(u32, UINT32_C(0xffff), "ffff", 16);
TEST_SUCCESS(u32, UINT32_C(032), "032", 8);
TEST_SUCCESS(u32, UINT32_C(0B1011), "1011", 2);
}
TEST(RustNumParse, CaseInsensitive) {
TEST_SUCCESS(bool, true, "tRUE");
}
TEST(RustNumParse, Overflow) {
TEST_FAIL(i8, "6161");
TEST_FAIL(u8, "32800");
TEST_FAIL(i16, "61616161");
TEST_FAIL(u16, "4294967293");
TEST_FAIL(i32, "616161616161");
TEST_FAIL(u32, "9223372036854775807");
TEST_FAIL(i64, "616161616161616161616161");
TEST_FAIL(u64, "92233720368547758079223372036854775807");
TEST_FAIL(float, "1e40");
TEST_FAIL(double, "1e114514");
}
TEST(RustNumParse, BadRadix) {
TEST_FAIL(u32, "fghj", 16);
TEST_FAIL(u32, "099", 8);
TEST_FAIL(u32, "12345", 2);
}
TEST(RustNumParse, InvalidWords) {
TEST_FAIL(u32, "hello, world!");
TEST_FAIL(bool, "hello, world!");
}
#include "../../../shared/parse_template.hpp"
#endif

View File

@ -4,11 +4,19 @@
#include <yycc/prelude/rust.hpp>
#define STRINGIFY ::yycc::string::stringify
#define STRINGIFY ::yycc::rust::num::stringify
namespace yycctest::rust::stringify {
namespace yycctest::rust::num::stringify {
// There is not testbench for this
// because it just a map to original implementation without any modification.
#define TEST_NS RustNumStringify
#define TEST_SUCCESS(type_t, value, string_value, ...) \
{ \
type_t cache = value; \
u8string ret = STRINGIFY::stringify<type_t>(cache, ##__VA_ARGS__); \
EXPECT_EQ(ret, YYCC_U8(string_value)); \
}
#include "../../../shared/stringify_template.hpp"
}