1
0

refactor: rename testbench to test.

- rename testbench to test.
- add benchmark for future development.
This commit is contained in:
2025-09-29 13:34:02 +08:00
parent 82c3ed5b32
commit e7a05b3488
44 changed files with 55 additions and 19 deletions

View File

View File

@ -0,0 +1,121 @@
#include <gtest/gtest.h>
#include <yycc.hpp>
#include <yycc/carton/pycodec.hpp>
#include "../../shared/literals.hpp"
#define ENC ::yycc::carton::pycodec
namespace yycctest::carton::pycodec {
static auto UTF_LITERALS = ::yyccshared::literals::UtfLiterals();
static auto OTHER_LITERALS = ::yyccshared::literals::OtherLiterals();
TEST(CartonPycodec, ValidateName) {
EXPECT_TRUE(ENC::is_valid_encoding_name(u8"utf-8"));
EXPECT_TRUE(ENC::is_valid_encoding_name(u8"gb2312"));
EXPECT_TRUE(ENC::is_valid_encoding_name(u8"cp1252"));
EXPECT_FALSE(ENC::is_valid_encoding_name(u8"this must not be a valid encoding name"));
}
TEST(CartonPycodec, CharToUtf8) {
const auto& other_str_literals = OTHER_LITERALS.get_other_str_vec();
const auto& u8str_literals = OTHER_LITERALS.get_u8str_vec();
for (size_t i = 0; i < OTHER_LITERALS.get_size(); ++i) {
const auto& other_str_literal = other_str_literals[i];
ENC::CharToUtf8 cv(other_str_literal.get_pycodec_ident());
auto rv = cv.to_utf8(other_str_literal.get_other_str());
ASSERT_TRUE(rv.has_value());
EXPECT_EQ(rv.value(), u8str_literals[i]);
}
}
TEST(CartonPycodec, Utf8ToChar) {
const auto& other_str_literals = OTHER_LITERALS.get_other_str_vec();
const auto& u8str_literals = OTHER_LITERALS.get_u8str_vec();
for (size_t i = 0; i < OTHER_LITERALS.get_size(); ++i) {
const auto& other_str_literal = other_str_literals[i];
ENC::Utf8ToChar cv(other_str_literal.get_pycodec_ident());
auto rv = cv.to_char(u8str_literals[i]);
ASSERT_TRUE(rv.has_value());
EXPECT_EQ(rv.value(), other_str_literal.get_other_str());
}
}
TEST(CartonPycodec, Utf8ToWchar) {
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
const auto& wstr_literals = UTF_LITERALS.get_wstr_vec();
ENC::Utf8ToWchar cv;
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
auto rv = cv.to_wchar(u8str_literals[i]);
ASSERT_TRUE(rv.has_value());
EXPECT_EQ(rv.value(), wstr_literals[i]);
}
}
TEST(CartonPycodec, WcharToUtf8) {
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
const auto& wstr_literals = UTF_LITERALS.get_wstr_vec();
ENC::WcharToUtf8 cv;
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
auto rv = cv.to_utf8(wstr_literals[i]);
ASSERT_TRUE(rv.has_value());
EXPECT_EQ(rv.value(), u8str_literals[i]);
}
}
TEST(CartonPycodec, Utf8ToUtf16) {
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
const auto& u16str_literals = UTF_LITERALS.get_u16str_vec();
ENC::Utf8ToUtf16 cv;
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
auto rv = cv.to_utf16(u8str_literals[i]);
ASSERT_TRUE(rv.has_value());
EXPECT_EQ(rv.value(), u16str_literals[i]);
}
}
TEST(CartonPycodec, Utf16ToUtf8) {
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
const auto& u16str_literals = UTF_LITERALS.get_u16str_vec();
ENC::Utf16ToUtf8 cv;
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
auto rv = cv.to_utf8(u16str_literals[i]);
ASSERT_TRUE(rv.has_value());
EXPECT_EQ(rv.value(), u8str_literals[i]);
}
}
TEST(CartonPycodec, Utf8ToUtf32) {
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
const auto& u32str_literals = UTF_LITERALS.get_u32str_vec();
ENC::Utf8ToUtf32 cv;
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
auto rv = cv.to_utf32(u8str_literals[i]);
ASSERT_TRUE(rv.has_value());
EXPECT_EQ(rv.value(), u32str_literals[i]);
}
}
TEST(CartonPycodec, Utf32ToUtf8) {
const auto& u8str_literals = UTF_LITERALS.get_u8str_vec();
const auto& u32str_literals = UTF_LITERALS.get_u32str_vec();
ENC::Utf32ToUtf8 cv;
for (size_t i = 0; i < UTF_LITERALS.get_size(); ++i) {
auto rv = cv.to_utf8(u32str_literals[i]);
ASSERT_TRUE(rv.has_value());
EXPECT_EQ(rv.value(), u8str_literals[i]);
}
}
}

View File

@ -0,0 +1,61 @@
#include <gtest/gtest.h>
#include <yycc.hpp>
#include <yycc/carton/tabulate.hpp>
#include <yycc/string/reinterpret.hpp>
#include <sstream>
#define TABULATE ::yycc::carton::tabulate
#define REINTERPRET ::yycc::string::reinterpret
namespace yycctest::carton::tabulate {
class CartonTabulate : public ::testing::Test {
protected:
CartonTabulate() : table(3u), ss() {
// setup basic data
table.set_prefix(u8"# ");
table.set_header({u8"中文1", u8"中文2", u8"中文3"});
table.set_bar(u8"===");
table.add_row({u8"a", u8"b", u8"c"});
}
~CartonTabulate() override = default;
void expected_print(const std::u8string_view& exp) {
ss.str("");
table.print(ss);
EXPECT_EQ(REINTERPRET::as_utf8_view(ss.view()), exp);
}
TABULATE::Tabulate table;
std::stringstream ss;
};
TEST_F(CartonTabulate, Full) {
table.show_header(true);
table.show_bar(true);
expected_print(u8"# 中文1 中文2 中文3 \n"
u8"# === === === \n"
u8"# a b c \n");
}
TEST_F(CartonTabulate, NoHeader) {
table.show_header(false);
table.show_bar(true);
expected_print(u8"# === === === \n"
u8"# a b c \n");
}
TEST_F(CartonTabulate, NoBar) {
table.show_header(true);
table.show_bar(false);
expected_print(u8"# 中文1 中文2 中文3 \n"
u8"# a b c \n");
}
TEST_F(CartonTabulate, OnlyData) {
table.show_header(false);
table.show_bar(false);
expected_print(u8"# a b c \n");
}
} // namespace yycctest::carton::tabulate

View File

@ -0,0 +1,44 @@
#include <gtest/gtest.h>
#include <yycc.hpp>
#include <yycc/carton/termcolor.hpp>
#include <yycc/flag_enum.hpp>
#define TERMCOLOR ::yycc::carton::termcolor
#define FLAG_ENUM ::yycc::flag_enum
using namespace std::literals::string_view_literals;
using Color = TERMCOLOR::Color;
using Attribute = TERMCOLOR::Attribute;
namespace yycctest::carton::termcolor {
TEST(CartonTermcolor, Lowlevel) {
EXPECT_EQ(TERMCOLOR::foreground(Color::Default), u8"");
EXPECT_EQ(TERMCOLOR::foreground(Color::Red), u8"\033[31m");
EXPECT_EQ(TERMCOLOR::foreground(Color::LightRed), u8"\033[91m");
EXPECT_EQ(TERMCOLOR::background(Color::Default), u8"");
EXPECT_EQ(TERMCOLOR::background(Color::Red), u8"\033[41m");
EXPECT_EQ(TERMCOLOR::background(Color::LightRed), u8"\033[101m");
EXPECT_EQ(TERMCOLOR::style(Attribute::Default), u8"");
EXPECT_EQ(TERMCOLOR::style(Attribute::Italic), u8"\033[3m");
EXPECT_EQ(TERMCOLOR::styles(FLAG_ENUM::merge(Attribute::Italic, Attribute::Bold)),
u8"\033[1m"
"\033[3m");
EXPECT_EQ(TERMCOLOR::reset(), u8"\033[0m"sv);
}
TEST(CartonTermcolor, Highlevel) {
EXPECT_EQ(TERMCOLOR::colored(u8"hello"sv), u8"hello\033[0m");
EXPECT_EQ(TERMCOLOR::colored(u8"hello"sv, Color::LightWhite, Color::Red, FLAG_ENUM::merge(Attribute::Italic, Attribute::Bold)),
u8"\033[97m"
"\033[41m"
"\033[1m"
"\033[3m"
"hello"
"\033[0m");
}
} // namespace yycctest::carton::termcolor

View File

@ -0,0 +1,54 @@
#include <gtest/gtest.h>
#include <yycc.hpp>
#include <yycc/carton/wcwidth.hpp>
#include <yycc/carton/termcolor.hpp>
#define WCWDITH ::yycc::carton::wcwidth
#define TERMCOLOR ::yycc::carton::termcolor
namespace yycctest::carton::wcwidth {
#define TEST_SUCCESS(strl, len) \
{ \
auto rv = WCWDITH::wcswidth(strl); \
ASSERT_TRUE(rv.has_value()); \
EXPECT_EQ(rv.value(), len); \
}
#define TEST_FAIL(strl) \
{ \
auto rv = WCWDITH::wcswidth(strl); \
EXPECT_FALSE(rv.has_value()); \
}
TEST(CartonWcwdith, BadAnsi) {
TEST_FAIL(u8"\033?");
}
TEST(CartonWcwdith, BadCsi) {
TEST_FAIL(u8"\033[\t");
}
TEST(CartonWcwdith, English) {
TEST_SUCCESS(u8"abc", 3);
}
TEST(CartonWcwdith, Chinese) {
TEST_SUCCESS(u8"中文", 4);
TEST_SUCCESS(u8"中a文", 5);
}
TEST(CartonWcwdith, Japanese) {
TEST_SUCCESS(u8"ありがとう", 10);
TEST_SUCCESS(u8"アリガトウ", 10);
TEST_SUCCESS(u8"アリガトウ", 6);
}
TEST(CartonWcwdith, Termcolor) {
using Color = TERMCOLOR::Color;
TEST_SUCCESS(TERMCOLOR::colored(u8"abc", Color::Red), 3);
TEST_SUCCESS(TERMCOLOR::colored(u8"中文", Color::Red), 4);
TEST_SUCCESS(TERMCOLOR::colored(u8"ありがとう", Color::Red), 10);
}
} // namespace yycctest::carton::wcwidth