refactor: finish rust parse and add testbench for it.
This commit is contained in:
@ -10,6 +10,8 @@ PRIVATE
|
||||
yycc/string/reinterpret.cpp
|
||||
yycc/string/parse.cpp
|
||||
yycc/string/stringify.cpp
|
||||
yycc/rust/parse.cpp
|
||||
yycc/rust/stringify.cpp
|
||||
)
|
||||
# Setup headers
|
||||
target_include_directories(YYCCTestbench
|
||||
|
87
testbench/yycc/rust/parse.cpp
Normal file
87
testbench/yycc/rust/parse.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <yycc.hpp>
|
||||
#include <yycc/rust/parse.hpp>
|
||||
|
||||
#include <yycc/prelude/rust.hpp>
|
||||
|
||||
#define PARSE ::yycc::rust::parse
|
||||
|
||||
namespace yycctest::rust::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_SUCCESS(type_t, expected_value, string_value, ...) \
|
||||
{ \
|
||||
u8string cache_string(YYCC_U8(string_value)); \
|
||||
auto rv = PARSE::parse<type_t>(cache_string, ##__VA_ARGS__); \
|
||||
ASSERT_TRUE(rv.has_value()); \
|
||||
EXPECT_EQ(rv.value(), expected_value); \
|
||||
}
|
||||
|
||||
#define TEST_FAIL(type_t, string_value, ...) \
|
||||
{ \
|
||||
u8string cache_string(YYCC_U8(string_value)); \
|
||||
auto rv = PARSE::parse<type_t>(cache_string, ##__VA_ARGS__); \
|
||||
EXPECT_FALSE(rv.has_value()); \
|
||||
}
|
||||
|
||||
TEST(RustParse, 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(RustParse, 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(RustParse, CaseInsensitive) {
|
||||
TEST_SUCCESS(bool, true, "tRUE");
|
||||
}
|
||||
|
||||
TEST(RustParse, 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(RustParse, BadRadix) {
|
||||
TEST_FAIL(u32, "fghj", 16);
|
||||
TEST_FAIL(u32, "099", 8);
|
||||
TEST_FAIL(u32, "12345", 2);
|
||||
}
|
||||
|
||||
TEST(RustParse, InvalidWords) {
|
||||
TEST_FAIL(u32, "hello, world!");
|
||||
TEST_FAIL(bool, "hello, world!");
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace yycctest::rust::parse
|
14
testbench/yycc/rust/stringify.cpp
Normal file
14
testbench/yycc/rust/stringify.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <yycc.hpp>
|
||||
#include <yycc/rust/stringify.hpp>
|
||||
|
||||
#include <yycc/prelude/rust.hpp>
|
||||
|
||||
#define STRINGIFY ::yycc::string::stringify
|
||||
|
||||
namespace yycctest::rust::stringify {
|
||||
|
||||
// There is not testbench for this
|
||||
// because it just a map to original implementation without any modification.
|
||||
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user