refactor: finish rust parse and add testbench for it.

This commit is contained in:
2025-06-26 10:27:33 +08:00
parent a6382d6a22
commit e166dc41ac
11 changed files with 229 additions and 26 deletions

View File

@ -12,20 +12,22 @@ namespace yycctest::string::parse {
// These 2 test macros build string container via given string.
// Check `try_parse` first, and then check `parse`.
#define TEST_SUCCESS(type_t, value, string_value, ...) { \
u8string cache_string(YYCC_U8(string_value)); \
type_t cache; \
ASSERT_TRUE(PARSE::try_parse<type_t>(cache_string, cache, ##__VA_ARGS__)); \
EXPECT_EQ(cache, value); \
EXPECT_EQ(PARSE::parse<type_t>(cache_string, ##__VA_ARGS__), value); \
}
#define TEST_SUCCESS(type_t, value, string_value, ...) \
{ \
u8string cache_string(YYCC_U8(string_value)); \
type_t cache; \
ASSERT_TRUE(PARSE::try_parse<type_t>(cache_string, cache, ##__VA_ARGS__)); \
EXPECT_EQ(cache, value); \
EXPECT_EQ(PARSE::parse<type_t>(cache_string, ##__VA_ARGS__), value); \
}
#define TEST_FAIL(type_t, string_value, ...) { \
u8string cache_string(YYCC_U8(string_value)); \
type_t cache; \
EXPECT_FALSE(PARSE::try_parse<type_t>(cache_string, cache, ##__VA_ARGS__)); \
EXPECT_ANY_THROW(PARSE::parse<type_t>(cache_string, ##__VA_ARGS__)); \
}
#define TEST_FAIL(type_t, string_value, ...) \
{ \
u8string cache_string(YYCC_U8(string_value)); \
type_t cache; \
EXPECT_FALSE(PARSE::try_parse<type_t>(cache_string, cache, ##__VA_ARGS__)); \
EXPECT_ANY_THROW(PARSE::parse<type_t>(cache_string, ##__VA_ARGS__)); \
}
TEST(StringParse, Common) {
TEST_SUCCESS(i8, INT8_C(-61), "-61");
@ -79,4 +81,4 @@ namespace yycctest::string::parse {
TEST_FAIL(bool, "hello, world!");
}
}
} // namespace yycctest::string::parse

View File

@ -1,7 +1,7 @@
#include <gtest/gtest.h>
#include <yycc.hpp>
#include <yycc/string/stringify.hpp>
#include <yycc/string/reinterpret.hpp>
#include <yycc/string/stringify.hpp>
#include <yycc/prelude/rust.hpp>
@ -9,11 +9,12 @@
namespace yycctest::string::stringify {
#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)); \
}
#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)); \
}
TEST(StringStringify, Common) {
TEST_SUCCESS(i8, INT8_C(-61), "-61");
@ -38,4 +39,4 @@ namespace yycctest::string::stringify {
TEST_SUCCESS(u32, UINT32_C(0B1011), "1011", 2);
}
}
} // namespace yycctest::string::stringify