feat replace gtest by doctest
All checks were successful
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 1m48s
linux-mips64-gcc / linux-gcc-mips64el (push) Successful in 1m11s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 2m3s

This commit is contained in:
tqcq 2024-03-30 23:40:17 +08:00
parent c7fe469c1b
commit 7c1582769e
28 changed files with 15471 additions and 445 deletions

View File

@ -17,6 +17,7 @@ set(BUILD_STATIC ON)
set(BUILD_RTTR_DYNAMIC OFF)
set(BUILD_UNIT_TESTS OFF)
set(BUILD_EXAMPLES OFF)
add_library(test_main STATIC src/sled/testing/test_main.cc)
add_library(sled STATIC "")
add_subdirectory(3party/gperftools EXCLUDE_FROM_ALL)
@ -42,6 +43,7 @@ endif()
# add_subdirectory(3party/eigen EXCLUDE_FROM_ALL)
target_include_directories(sled PUBLIC src/ 3party/eigen 3party/inja
3party/rxcpp)
target_include_directories(test_main PUBLIC src/)
target_sources(
sled
PRIVATE src/sled/async/async.cc
@ -68,6 +70,7 @@ target_sources(
src/sled/system/thread_pool.cc
src/sled/task_queue/pending_task_safety_flag.cc
src/sled/task_queue/task_queue_base.cc
src/sled/testing/test.cc
src/sled/timer/task_queue_timeout.cc
src/sled/timer/timer.cc
src/sled/units/time_delta.cc
@ -122,9 +125,6 @@ function(sled_add_test)
cmake_parse_arguments("${prefix}" "${options}" "${one_value_keywords}"
"${multi_value_keywords}" ${ARGN})
message(WARNING "SLED_TEST_NAME: ${SLED_TEST_NAME}")
message(WARNING "SLED_TEST_SRCS: ${SLED_TEST_SRCS}")
message(WARNING "SLED_TEST_LIBS: ${SLED_TEST_LIBS}")
add_executable(${SLED_TEST_NAME} ${SLED_TEST_SRCS})
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
@ -134,28 +134,30 @@ function(sled_add_test)
target_link_options(${SLED_TEST_NAME} PRIVATE ${EXTRA_FLAGS})
endif()
if(MSVC)
target_compile_options(${SLED_TEST_NAME} PRIVATE /FI"sled/testing/test.h")
else()
target_compile_options(${SLED_TEST_NAME} PRIVATE -include
sled/testing/test.h)
endif()
target_include_directories(${SLED_TEST_NAME} PRIVATE ${SLED_TEST_INC_DIRS})
target_link_libraries(${SLED_TEST_NAME} PRIVATE ${SLED_TEST_LIBS}
GTest::gtest)
target_link_libraries(${SLED_TEST_NAME} PRIVATE ${SLED_TEST_LIBS})
add_test(NAME ${SLED_TEST_NAME} COMMAND ${SLED_TEST_NAME})
endfunction()
if(SLED_BUILD_TESTS)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
FetchContent_MakeAvailable(googletest)
# include(FetchContent) FetchContent_Declare( googletest URL
# https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
# ) FetchContent_MakeAvailable(googletest)
sled_add_test(
NAME
sled_all_tests
SRCS
src/sled/debugging/demangle_test.cc
src/sled/async/async_test.cc
src/sled/any_test.cc
src/sled/filesystem/path_test.cc
src/sled/futures/detail/just_test.cc
src/sled/log/fmt_test.cc
src/sled/synchronization/sequence_checker_test.cc
src/sled/cleanup_test.cc
@ -169,7 +171,7 @@ if(SLED_BUILD_TESTS)
src/sled/uri_test.cc
LIBS
sled
GTest::gtest_main)
test_main)
sled_add_test(NAME sled_symbolize_test SRCS
src/sled/debugging/symbolize_test.cc LIBS sled)

View File

@ -1,48 +1,50 @@
#include <gtest/gtest.h>
#include <sled/any.h>
#include <sled/log/log.h>
TEST(Any, Assign)
TEST_SUITE("Any")
{
TEST_CASE("Assign")
{
sled::Any any1;
sled::Any any2(any1);
sled::Any any3(1);
sled::Any any4(any3);
sled::Any any5 = 1;
EXPECT_FALSE(any1.HasValue());
EXPECT_FALSE(any2.HasValue());
EXPECT_TRUE(any3.HasValue());
EXPECT_TRUE(any4.HasValue());
EXPECT_TRUE(any5.HasValue());
EXPECT_EQ(any3.Cast<int>(), 1);
EXPECT_EQ(any4.Cast<int>(), 1);
EXPECT_EQ(any5.Cast<int>(), 1);
EXPECT_EQ(any3.CastOr<std::string>("def"), "def");
EXPECT_EQ(any4.CastOr<std::string>("def"), "def");
EXPECT_EQ(any5.CastOr<std::string>("def"), "def");
EXPECT_EQ(any3.CastOr<int>(11), 1);
}
CHECK_FALSE(any1.HasValue());
CHECK_FALSE(any2.HasValue());
CHECK(any3.HasValue());
CHECK(any4.HasValue());
CHECK(any5.HasValue());
CHECK_EQ(any3.Cast<int>(), 1);
CHECK_EQ(any4.Cast<int>(), 1);
CHECK_EQ(any5.Cast<int>(), 1);
CHECK_EQ(any3.CastOr<std::string>("def"), "def");
CHECK_EQ(any4.CastOr<std::string>("def"), "def");
CHECK_EQ(any5.CastOr<std::string>("def"), "def");
CHECK_EQ(any3.CastOr<int>(11), 1);
}
TEST(Any, std_swap)
{
TEST_CASE("std_swap")
{
sled::Any a;
sled::Any b = 2;
EXPECT_FALSE(a.HasValue());
EXPECT_TRUE(b.HasValue());
CHECK_FALSE(a.HasValue());
CHECK(b.HasValue());
std::swap(a, b);
EXPECT_TRUE(a.HasValue());
EXPECT_FALSE(b.HasValue());
EXPECT_EQ(a.Cast<int>(), 2);
}
CHECK(a.HasValue());
CHECK_FALSE(b.HasValue());
CHECK_EQ(a.Cast<int>(), 2);
}
TEST(Any, custom_swap)
{
TEST_CASE("custom_swap")
{
sled::Any a;
sled::Any b = 2;
EXPECT_FALSE(a.HasValue());
EXPECT_TRUE(b.HasValue());
CHECK_FALSE(a.HasValue());
CHECK(b.HasValue());
a.swap(b);
EXPECT_TRUE(a.HasValue());
EXPECT_FALSE(b.HasValue());
EXPECT_EQ(a.Cast<int>(), 2);
CHECK(a.HasValue());
CHECK_FALSE(b.HasValue());
CHECK_EQ(a.Cast<int>(), 2);
}
}

View File

@ -1,31 +1,33 @@
#include <gtest/gtest.h>
#include <sled/async/async.h>
#include <sled/log/log.h>
#include <sled/system/fiber/wait_group.h>
#include <sled/system/thread.h>
TEST(Async, task)
TEST_SUITE("Async")
{
TEST_CASE("task")
{
auto task1 = async::spawn([] { return 42; }).then([](int value) { return value * 3; }).then([](int value) {
EXPECT_EQ(value, 126);
CHECK_EQ(value, 126);
return value;
});
task1.wait();
EXPECT_EQ(126, task1.get());
}
CHECK_EQ(126, task1.get());
}
TEST(Async, parallel_for)
{
TEST_CASE("parallel_for")
{
const int count = 1000;
std::vector<int> values(count);
async::parallel_for(async::irange(0, count), [&values](int x) {
EXPECT_FALSE(values[x]);
CHECK_FALSE(values[x]);
values[x] = true;
});
for (int i = 0; i < count; i++) { EXPECT_TRUE(values[i]) << i; }
}
for (int i = 0; i < count; i++) { CHECK(values[i]); }
}
TEST(Async, parallel_reduce)
{
TEST_CASE("parallel_reduce")
{
auto r = async::parallel_reduce(async::irange(1, 5), 0, [](int x, int y) { return x + y; });
}
}

View File

@ -1,9 +1,10 @@
#include <gtest/gtest.h>
#include <sled/cleanup.h>
#include <sled/random.h>
TEST(Cleanup, TestCleanup)
TEST_SUITE("Cleanup")
{
TEST_CASE("assign")
{
sled::Random rand(1314);
for (int i = 0; i < 100; ++i) {
int a = rand.Rand(10000);
@ -11,6 +12,7 @@ TEST(Cleanup, TestCleanup)
{
sled::Cleanup<> c([=, &a]() { a = b; });
}
ASSERT_EQ(a, b);
CHECK_EQ(a, b);
}
}
}

View File

@ -0,0 +1,58 @@
#include <sled/debugging/demangle.h>
#include <sled/testing/test.h>
TEST_SUITE("Demangle")
{
TEST_CASE("FunctionTemplate")
{
char tmp[100];
// template <typename T>
// int foo(T);
//
// foo<Wrapper<int>>({ .value = 5 });
CHECK(sled::Demangle("_Z3fooIiEiT_", tmp, sizeof(tmp)));
CHECK_EQ(doctest::toString(tmp), "foo<>()");
}
TEST_CASE("FunctionTemplateWithNesting")
{
char tmp[100];
// template <std::integral T>
// int foo(T);
//
// foo<int>(5);
CHECK(sled::Demangle("_Z3fooI7WrapperIiEEiT_", tmp, sizeof(tmp)));
CHECK_EQ(doctest::toString(tmp), "foo<>()");
}
TEST_CASE("FunctionTemplateWithNonTypeParamConstraint")
{
char tmp[100];
// template <typename T>
// int foo() requires std::integral<T>;
//
// foo<int>();
CHECK(sled::Demangle("_Z3fooITkSt8integraliEiT_", tmp, sizeof(tmp)));
CHECK_EQ(doctest::toString(tmp), "foo<>()");
}
TEST_CASE("AbiTags")
{
char tmp[100];
// Mangled name generated via:
// struct [[gnu::abi_tag("abc")]] A{};
// A a;
CHECK(sled::Demangle("_Z1aB3abc", tmp, sizeof(tmp)));
CHECK_EQ(doctest::toString(tmp), "a[abi:abc]");
CHECK(sled::Demangle("_ZN1BC2B3xyzEv", tmp, sizeof(tmp)));
CHECK_EQ(doctest::toString(tmp), "B::B[abi:xyz]()");
CHECK(sled::Demangle("_Z1CB3barB3foov", tmp, sizeof(tmp)));
CHECK_EQ(doctest::toString(tmp), "C[abi:bar][abi:foo]()");
}
}

View File

@ -1,15 +1,20 @@
#include <gtest/gtest.h>
#include <sled/debugging/symbolize.h>
#include <sled/make_unique.h>
void
TestFunc1()
TrivialFunc()
{}
static void
StaticFunc()
{}
class Class {
public:
Class() {}
static void StaticFunc() {}
void MemberFunc1() {}
int MemberFunc2() { return 0; }
@ -58,20 +63,29 @@ void_cast(TRet (TClass::*mem_func)(Args...))
return void_casted;
}
TEST(Symbolize, base)
TEST_CASE("Trivial Function")
{
char buf[1024];
EXPECT_STREQ("TestFunc1()", TrySymbolize(void_cast(TestFunc1)));
EXPECT_STREQ("Class::MemberFunc1()", TrySymbolize(void_cast(&Class::MemberFunc1)));
EXPECT_STREQ("Class::MemberFunc2()", TrySymbolize(void_cast(&Class::MemberFunc2)));
EXPECT_STREQ("Class::MemberFunc3()", TrySymbolize(void_cast(&Class::MemberFunc3)));
EXPECT_STREQ("TrySymbolizeWithLimit()", TrySymbolize(void_cast(&TrySymbolizeWithLimit)));
CHECK_EQ(doctest::String("TrivialFunc()"), TrySymbolize(void_cast(TrivialFunc)));
CHECK_EQ(doctest::String("TrySymbolizeWithLimit()"), TrySymbolize(void_cast(&TrySymbolizeWithLimit)));
}
TEST_CASE("Static Function") { CHECK_EQ(doctest::String("StaticFunc()"), TrySymbolize(void_cast(StaticFunc))); }
TEST_CASE("Member Function")
{
CHECK_EQ(doctest::String("Class::MemberFunc1()"), TrySymbolize(void_cast(&Class::MemberFunc1)));
CHECK_EQ(doctest::String("Class::MemberFunc2()"), TrySymbolize(void_cast(&Class::MemberFunc2)));
CHECK_EQ(doctest::String("Class::MemberFunc3()"), TrySymbolize(void_cast(&Class::MemberFunc3)));
}
TEST_CASE("Static Member Function")
{
CHECK_EQ(doctest::String("Class::StaticFunc()"), TrySymbolize(void_cast(&Class::StaticFunc)));
}
int
main(int argc, char *argv[])
{
sled::InitializeSymbolizer(argv[0]);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
return doctest::Context(argc, argv).run();
}

View File

@ -1,40 +0,0 @@
#include <gtest/gtest.h>
#include <sled/exec/exec.h>
struct cout_receiver {
template<typename T>
void SetValue(T &&val)
{
// 这个receiver什么都不干只对收集到的结果输出
std::cout << "Result: " << val << '\n';
}
void SetError(std::exception_ptr err) { std::terminate(); }
void SetStopped() { std::terminate(); }
};
TEST(Just, basic)
{
sled::Just(42).Connect(cout_receiver{}).Start();
sled::Just(11).Connect(cout_receiver{}).Start();
}
TEST(Then, basic)
{
auto s1 = sled::Just(42);
auto s2 = sled::Then(s1, [](int x) { return x + 1; });
auto s3 = sled::Then(s2, [](int x) { return x + 1; });
auto s4 = sled::Then(s3, [](int x) { return x + 1; });
s4.Connect(cout_receiver{}).Start();
}
TEST(SyncWait, basic)
{
auto s1 = sled::Just(42);
auto s2 = sled::Then(s1, [](int x) { return x + 1; });
auto s3 = sled::Then(s2, [](int x) { return x + 1; });
auto s4 = sled::Then(s3, [](int x) { return x + 1; });
auto s5 = sled::SyncWait(s4).value();
std::cout << "Result: " << s5 << '\n';
}

View File

@ -1,23 +1,22 @@
#include <gtest/gtest.h>
#include <sled/filesystem/path.h>
TEST(Path, TestCurrent)
TEST_SUITE("Path")
{
TEST_CASE("Current Directory")
{
sled::Path path = sled::Path::Current();
std::string str = path.ToString();
EXPECT_FALSE(str.empty());
}
CHECK_FALSE(path.ToString().empty());
}
TEST(Path, TestHome)
{
TEST_CASE("Home")
{
sled::Path path = sled::Path::Home();
std::string str = path.ToString();
EXPECT_FALSE(str.empty());
}
CHECK_FALSE(path.ToString().empty());
}
TEST(Path, TestTempDir)
{
TEST_CASE("Temparory Directory")
{
sled::Path path = sled::Path::TempDir();
std::string str = path.ToString();
EXPECT_FALSE(str.empty());
CHECK_FALSE(path.ToString().empty());
}
}

View File

@ -1,4 +1,3 @@
#include <gtest/gtest.h>
#include <sled/futures/detail/just.h>
TEST(Just, basic) { auto s1 = sled::detail::Just(42); }

View File

@ -1,8 +1,10 @@
#include <gtest/gtest.h>
#include <sled/log/log.h>
TEST(format, enum)
TEST_SUITE("fmt::format")
{
TEST_CASE("enum")
{
enum EnumType {
kOne = 1,
kTwo = 2,
@ -12,33 +14,30 @@ TEST(format, enum)
std::stringstream ss;
ss << kOne;
EXPECT_EQ(ss.str(), "1");
EXPECT_EQ(fmt::format("{}{}{}", kOne, kTwo, kThree), "123");
}
CHECK_EQ(ss.str(), "1");
CHECK_EQ(fmt::format("{}{}{}", kOne, kTwo, kThree), "123");
}
TEST(format, neg_enum)
{
TEST_CASE("neg_enum")
{
enum EnumType {
kOne = -1,
kTwo = -2,
kThree = -3,
};
EXPECT_EQ(fmt::format("{}{}{}", kOne, kTwo, kThree), "-1-2-3");
}
CHECK_EQ(fmt::format("{}{}{}", kOne, kTwo, kThree), "-1-2-3");
}
struct Streamable {
struct Streamable {
int value;
};
};
std::ostream &
operator<<(std::ostream &os, const Streamable &s)
{
return os << s.value;
}
std::ostream &operator<<(std::ostream &os, const Streamable &s) { return os << s.value; }
TEST(format, streamable)
{
TEST_CASE("streamable")
{
Streamable s{42};
EXPECT_EQ(fmt::format("{}", s), "42");
CHECK_EQ(fmt::format("{}", s), "42");
}
}

View File

@ -105,8 +105,8 @@ void Log(LogLevel level, const char *tag, const char *fmt, const char *file_name
#define LOGE_IF(cond, tag, fmt, ...) SLOG_IF(cond, sled::LogLevel::kError, tag, fmt, __VA_ARGS__)
#define LOGF_IF(cond, tag, fmt, ...) SLOG_IF(cond, sled::LogLevel::kFatal, tag, fmt, __VA_ARGS__)
#define CHECK(cond, fmt, ...) SLOG_ASSERT(cond, "DCHECK", fmt, ##__VA_ARGS__)
#define DCHECK(cond, fmt, ...) SLOG_ASSERT(cond, "DCHECK", fmt, ##__VA_ARGS__)
#define SLED_CHECK(cond, fmt, ...) SLOG_ASSERT(cond, "DCHECK", fmt, ##__VA_ARGS__)
#define SLED_DCHECK(cond, fmt, ...) SLOG_ASSERT(cond, "DCHECK", fmt, ##__VA_ARGS__)
#define LOGV(tag, fmt, ...) SLOG(sled::LogLevel::kTrace, tag, fmt, ##__VA_ARGS__)
#define LOGD(tag, fmt, ...) SLOG(sled::LogLevel::kDebug, tag, fmt, ##__VA_ARGS__)

View File

@ -1,15 +1,17 @@
#include <gtest/gtest.h>
#include <sled/rx.h>
TEST(RX, interval)
TEST_SUITE("RxCpp")
{
TEST_CASE("interval")
{
// 2 + 4 + 6 = 12
sled::observable<>::interval(std::chrono::milliseconds(300))
sled::observable<>::interval(std::chrono::milliseconds(100))
.subscribe_on(rxcpp::synchronize_new_thread())
.observe_on(rxcpp::observe_on_new_thread())
.map([](long value) { return value * 2; })
.take(3)
.reduce(0, [](int acc, int value) { return acc + value; })
.as_blocking()
.subscribe([=](long counter) { EXPECT_EQ(counter, 12); }, []() {});
.subscribe([=](long counter) { CHECK_EQ(counter, 12); }, []() {});
}
}

View File

@ -85,4 +85,7 @@ namespace async {}
#include "sled/system_time.h"
#include "sled/time_utils.h"
#include "sled/variant.h"
// testing
#include "sled/testing/test.h"
#endif// SLED_SLED_H

View File

@ -1,23 +1,25 @@
#include <gtest/gtest.h>
#include <sled/log/log.h>
#include <sled/status_or.h>
TEST(StatusOr, TestStatusOr)
TEST_SUITE("StatusOr")
{
TEST_CASE("StatusOr")
{
sled::StatusOr<int> so;
EXPECT_FALSE(so.ok());
CHECK_FALSE(so.ok());
so = sled::StatusOr<int>(1);
EXPECT_TRUE(so.ok());
EXPECT_EQ(so.value(), 1);
EXPECT_EQ(so.status().code(), sled::StatusCode::kOk);
}
CHECK(so.ok());
CHECK_EQ(so.value(), 1);
CHECK_EQ(so.status().code(), sled::StatusCode::kOk);
}
TEST(StatusOr, make_status_or)
{
TEST_CASE("MakeStatusOr")
{
auto from_raw_str = sled::MakeStatusOr("hello");
auto from_string = sled::MakeStatusOr(std::string("world"));
EXPECT_TRUE(from_raw_str.ok());
EXPECT_TRUE(from_string.ok());
EXPECT_EQ(from_raw_str.value(), "hello");
EXPECT_EQ(from_string.value(), "world");
CHECK(from_raw_str.ok());
CHECK(from_string.ok());
CHECK_EQ(from_raw_str.value(), "hello");
CHECK_EQ(from_string.value(), "world");
}
}

View File

@ -1,9 +1,11 @@
#include <gtest/gtest.h>
#include <sled/log/log.h>
#include <sled/status.h>
TEST(Status, format)
TEST_SUITE("Status")
{
TEST_CASE("format")
{
auto status = sled::Status(sled::StatusCode::kOk, "");
EXPECT_EQ(fmt::format("{}", status), "OK");
CHECK_EQ(fmt::format("{}", status), "OK");
}
}

View File

@ -1,47 +1,50 @@
#include <gtest/gtest.h>
#include <sled/strings/base64.h>
TEST(Base64, EncodedLength)
TEST_SUITE("Base64")
{
EXPECT_EQ(0, sled::Base64::EncodedLength(0));
EXPECT_EQ(4, sled::Base64::EncodedLength(1));
EXPECT_EQ(4, sled::Base64::EncodedLength(2));
EXPECT_EQ(4, sled::Base64::EncodedLength(3));
EXPECT_EQ(8, sled::Base64::EncodedLength(4));
EXPECT_EQ(8, sled::Base64::EncodedLength(5));
EXPECT_EQ(8, sled::Base64::EncodedLength(6));
EXPECT_EQ(12, sled::Base64::EncodedLength(7));
}
TEST(Base64, DecodedLength)
{
EXPECT_EQ(0, sled::Base64::DecodedLength(nullptr, 0));
EXPECT_EQ(1, sled::Base64::DecodedLength(nullptr, 1));
EXPECT_EQ(2, sled::Base64::DecodedLength(nullptr, 2));
EXPECT_EQ(2, sled::Base64::DecodedLength(nullptr, 3));
EXPECT_EQ(3, sled::Base64::DecodedLength(nullptr, 4));
TEST_CASE("EncodedLength")
{
CHECK_EQ(0, sled::Base64::EncodedLength(0));
CHECK_EQ(4, sled::Base64::EncodedLength(1));
CHECK_EQ(4, sled::Base64::EncodedLength(2));
CHECK_EQ(4, sled::Base64::EncodedLength(3));
CHECK_EQ(8, sled::Base64::EncodedLength(4));
CHECK_EQ(8, sled::Base64::EncodedLength(5));
CHECK_EQ(8, sled::Base64::EncodedLength(6));
CHECK_EQ(12, sled::Base64::EncodedLength(7));
}
EXPECT_EQ(0, sled::Base64::DecodedLength("", 0));
}
TEST_CASE("DecodedLength")
{
CHECK_EQ(0, sled::Base64::DecodedLength(nullptr, 0));
CHECK_EQ(1, sled::Base64::DecodedLength(nullptr, 1));
CHECK_EQ(2, sled::Base64::DecodedLength(nullptr, 2));
CHECK_EQ(2, sled::Base64::DecodedLength(nullptr, 3));
CHECK_EQ(3, sled::Base64::DecodedLength(nullptr, 4));
TEST(Base64, Encode)
{
EXPECT_EQ("aGVsbG8gd29ybGQK", sled::Base64::Encode("hello world\n"));
EXPECT_EQ("U2VuZCByZWluZm9yY2VtZW50cwo=", sled::Base64::Encode("Send reinforcements\n"));
EXPECT_EQ("", sled::Base64::Encode(""));
EXPECT_EQ("IA==", sled::Base64::Encode(" "));
EXPECT_EQ("AA==", sled::Base64::Encode(std::string("\0", 1)));
EXPECT_EQ("AAA=", sled::Base64::Encode(std::string("\0\0", 2)));
EXPECT_EQ("AAAA", sled::Base64::Encode(std::string("\0\0\0", 3)));
}
CHECK_EQ(0, sled::Base64::DecodedLength("", 0));
}
TEST(Base64, Decode)
{
EXPECT_EQ("hello world\n", sled::Base64::Decode("aGVsbG8gd29ybGQK").value());
EXPECT_EQ("Send reinforcements\n", sled::Base64::Decode("U2VuZCByZWluZm9yY2VtZW50cwo=").value());
EXPECT_EQ("", sled::Base64::Decode("").value());
EXPECT_EQ(" ", sled::Base64::Decode("IA==").value());
EXPECT_EQ(std::string("\0", 1), sled::Base64::Decode("AA==").value());
EXPECT_EQ(std::string("\0\0", 2), sled::Base64::Decode("AAA=").value());
EXPECT_EQ(std::string("\0\0\0", 3), sled::Base64::Decode("AAAA").value());
TEST_CASE("Encode")
{
CHECK_EQ("aGVsbG8gd29ybGQK", sled::Base64::Encode("hello world\n"));
CHECK_EQ("U2VuZCByZWluZm9yY2VtZW50cwo=", sled::Base64::Encode("Send reinforcements\n"));
CHECK_EQ("", sled::Base64::Encode(""));
CHECK_EQ("IA==", sled::Base64::Encode(" "));
CHECK_EQ("AA==", sled::Base64::Encode(std::string("\0", 1)));
CHECK_EQ("AAA=", sled::Base64::Encode(std::string("\0\0", 2)));
CHECK_EQ("AAAA", sled::Base64::Encode(std::string("\0\0\0", 3)));
}
TEST_CASE("Decode")
{
CHECK_EQ("hello world\n", sled::Base64::Decode("aGVsbG8gd29ybGQK").value());
CHECK_EQ("Send reinforcements\n", sled::Base64::Decode("U2VuZCByZWluZm9yY2VtZW50cwo=").value());
CHECK_EQ("", sled::Base64::Decode("").value());
CHECK_EQ(" ", sled::Base64::Decode("IA==").value());
CHECK_EQ(std::string("\0", 1), sled::Base64::Decode("AA==").value());
CHECK_EQ(std::string("\0\0", 2), sled::Base64::Decode("AAA=").value());
CHECK_EQ(std::string("\0\0\0", 3), sled::Base64::Decode("AAAA").value());
}
}

View File

@ -1,95 +1,98 @@
#include <gtest/gtest.h>
#include <sled/strings/utils.h>
TEST(ToLower, Char)
TEST_SUITE("String Utils")
{
for (char c = 'A'; c <= 'Z'; ++c) { EXPECT_EQ(sled::ToLower(c), c + 32) << c; }
for (char c = 'a'; c <= 'z'; ++c) { EXPECT_EQ(sled::ToLower(c), c) << c; }
EXPECT_EQ(sled::ToLower(' '), ' ');
EXPECT_EQ(sled::ToLower('\0'), '\0');
}
TEST(ToUpper, Char)
{
for (char c = 'A'; c <= 'Z'; ++c) { EXPECT_EQ(sled::ToUpper(c), c) << c; }
for (char c = 'a'; c <= 'z'; ++c) { EXPECT_EQ(sled::ToUpper(c), c - 32) << c; }
EXPECT_EQ(sled::ToUpper(' '), ' ');
EXPECT_EQ(sled::ToUpper('\0'), '\0');
}
TEST_CASE("Char")
{
for (char c = 'A'; c <= 'Z'; ++c) { CHECK_EQ(sled::ToLower(c), c + 32); }
for (char c = 'a'; c <= 'z'; ++c) { CHECK_EQ(sled::ToLower(c), c); }
CHECK_EQ(sled::ToLower(' '), ' ');
CHECK_EQ(sled::ToLower('\0'), '\0');
}
TEST(ToLower, String)
{
EXPECT_EQ(sled::ToLower("Hello World"), "hello world");
EXPECT_EQ(sled::ToLower("HELLO WORLD"), "hello world");
EXPECT_EQ(sled::ToLower("hello world"), "hello world");
EXPECT_EQ(sled::ToLower(" "), " ");
EXPECT_EQ(sled::ToLower(""), "");
}
TEST_CASE("Char")
{
for (char c = 'A'; c <= 'Z'; ++c) { CHECK_EQ(sled::ToUpper(c), c); }
for (char c = 'a'; c <= 'z'; ++c) { CHECK_EQ(sled::ToUpper(c), c - 32); }
CHECK_EQ(sled::ToUpper(' '), ' ');
CHECK_EQ(sled::ToUpper('\0'), '\0');
}
TEST(ToUpper, String)
{
EXPECT_EQ(sled::ToUpper("Hello World"), "HELLO WORLD");
EXPECT_EQ(sled::ToUpper("HELLO WORLD"), "HELLO WORLD");
EXPECT_EQ(sled::ToUpper("hello world"), "HELLO WORLD");
EXPECT_EQ(sled::ToUpper(" "), " ");
EXPECT_EQ(sled::ToUpper(""), "");
}
TEST_CASE("String")
{
CHECK_EQ(sled::ToLower("Hello World"), "hello world");
CHECK_EQ(sled::ToLower("HELLO WORLD"), "hello world");
CHECK_EQ(sled::ToLower("hello world"), "hello world");
CHECK_EQ(sled::ToLower(" "), " ");
CHECK_EQ(sled::ToLower(""), "");
}
TEST(StrJoin, Empty)
{
EXPECT_EQ(sled::StrJoin({}, ","), "");
EXPECT_EQ(sled::StrJoin({}, ",", true), "");
EXPECT_EQ(sled::StrJoin({}, ",", false), "");
}
TEST_CASE("String")
{
CHECK_EQ(sled::ToUpper("Hello World"), "HELLO WORLD");
CHECK_EQ(sled::ToUpper("HELLO WORLD"), "HELLO WORLD");
CHECK_EQ(sled::ToUpper("hello world"), "HELLO WORLD");
CHECK_EQ(sled::ToUpper(" "), " ");
CHECK_EQ(sled::ToUpper(""), "");
}
TEST(StrJoin, Delim)
{
EXPECT_EQ(sled::StrJoin({"a", "b", "c"}, ","), "a,b,c");
EXPECT_EQ(sled::StrJoin({"a", "b", "c"}, ",", true), "a,b,c");
EXPECT_EQ(sled::StrJoin({"a", "b", "c"}, ",", false), "a,b,c");
}
TEST_CASE("Empty")
{
CHECK_EQ(sled::StrJoin({}, ","), "");
CHECK_EQ(sled::StrJoin({}, ",", true), "");
CHECK_EQ(sled::StrJoin({}, ",", false), "");
}
TEST(StrJoin, DelimSkipEmpty)
{
EXPECT_EQ(sled::StrJoin({"a", "", "c"}, ","), "a,,c");
EXPECT_EQ(sled::StrJoin({"a", "", "c"}, ",", true), "a,c");
EXPECT_EQ(sled::StrJoin({"a", "", "c"}, ",", false), "a,,c");
}
TEST_CASE("Delim")
{
CHECK_EQ(sled::StrJoin({"a", "b", "c"}, ","), "a,b,c");
CHECK_EQ(sled::StrJoin({"a", "b", "c"}, ",", true), "a,b,c");
CHECK_EQ(sled::StrJoin({"a", "b", "c"}, ",", false), "a,b,c");
}
TEST(StrSplit, Empty)
{
EXPECT_EQ(sled::StrSplit("", ","), std::vector<std::string>());
EXPECT_EQ(sled::StrSplit("", ",", true), std::vector<std::string>());
EXPECT_EQ(sled::StrSplit("", ",", false), std::vector<std::string>());
}
TEST_CASE("DelimSkipEmpty")
{
CHECK_EQ(sled::StrJoin({"a", "", "c"}, ","), "a,,c");
CHECK_EQ(sled::StrJoin({"a", "", "c"}, ",", true), "a,c");
CHECK_EQ(sled::StrJoin({"a", "", "c"}, ",", false), "a,,c");
}
TEST(StrSplit, Delim)
{
TEST_CASE("Empty")
{
CHECK_EQ(sled::StrSplit("", ","), std::vector<std::string>());
CHECK_EQ(sled::StrSplit("", ",", true), std::vector<std::string>());
CHECK_EQ(sled::StrSplit("", ",", false), std::vector<std::string>());
}
TEST_CASE("Delim")
{
// single delim
EXPECT_EQ(sled::StrSplit("a,b,c", ","), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit("a,b,c", ",", true), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit("a,b,c", ",", false), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit("a,b,c,", ","), std::vector<std::string>({"a", "b", "c", ""}));
EXPECT_EQ(sled::StrSplit("a,b,c,", ",", true), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit("a,b,c,", ",", false), std::vector<std::string>({"a", "b", "c", ""}));
EXPECT_EQ(sled::StrSplit(",a,b,c", ","), std::vector<std::string>({"", "a", "b", "c"}));
EXPECT_EQ(sled::StrSplit(",a,b,c", ",", true), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit(",a,b,c", ",", false), std::vector<std::string>({"", "a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,b,c", ","), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,b,c", ",", true), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,b,c", ",", false), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,b,c,", ","), std::vector<std::string>({"a", "b", "c", ""}));
CHECK_EQ(sled::StrSplit("a,b,c,", ",", true), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,b,c,", ",", false), std::vector<std::string>({"a", "b", "c", ""}));
CHECK_EQ(sled::StrSplit(",a,b,c", ","), std::vector<std::string>({"", "a", "b", "c"}));
CHECK_EQ(sled::StrSplit(",a,b,c", ",", true), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit(",a,b,c", ",", false), std::vector<std::string>({"", "a", "b", "c"}));
// multi delim
EXPECT_EQ(sled::StrSplit(",,a,b,c", ",", true), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit(",,a,b,c", ",", false), std::vector<std::string>({"", "", "a", "b", "c"}));
EXPECT_EQ(sled::StrSplit("a,b,c,,", ",", true), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit("a,b,c,,", ",", false), std::vector<std::string>({"a", "b", "c", "", ""}));
EXPECT_EQ(sled::StrSplit("a,,b,c", ",", true), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit("a,,b,c", ",", false), std::vector<std::string>({"a", "", "b", "c"}));
}
CHECK_EQ(sled::StrSplit(",,a,b,c", ",", true), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit(",,a,b,c", ",", false), std::vector<std::string>({"", "", "a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,b,c,,", ",", true), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,b,c,,", ",", false), std::vector<std::string>({"a", "b", "c", "", ""}));
CHECK_EQ(sled::StrSplit("a,,b,c", ",", true), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,,b,c", ",", false), std::vector<std::string>({"a", "", "b", "c"}));
}
TEST(StrSplit, MultiDelim)
{
EXPECT_EQ(sled::StrSplit("a,b;c", ",;", true), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit("a,b;c", ",;", false), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit("a,b;c,", ",;", true), std::vector<std::string>({"a", "b", "c"}));
EXPECT_EQ(sled::StrSplit("a,b;c,", ",;", false), std::vector<std::string>({"a", "b", "c", ""}));
EXPECT_EQ(sled::StrSplit("a,b;c,", ";,", true), std::vector<std::string>({"a", "b", "c"}));
TEST_CASE("MultiDelim")
{
CHECK_EQ(sled::StrSplit("a,b;c", ",;", true), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,b;c", ",;", false), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,b;c,", ",;", true), std::vector<std::string>({"a", "b", "c"}));
CHECK_EQ(sled::StrSplit("a,b;c,", ",;", false), std::vector<std::string>({"a", "b", "c", ""}));
CHECK_EQ(sled::StrSplit("a,b;c,", ";,", true), std::vector<std::string>({"a", "b", "c"}));
}
}

View File

@ -45,7 +45,7 @@ public:
};
#define SLED_RUN_ON(x) THREAD_ANNOTATION_ATTRIBUTE__(exclusive_locks_required(x))
#define SLED_DCHECK_RUN_ON(x) DCHECK((x)->IsCurrent(), (x)->ExpectationToString())
#define SLED_DCHECK_RUN_ON(x) SLED_DCHECK((x)->IsCurrent(), (x)->ExpectationToString())
}// namespace sled

View File

@ -1,5 +1,5 @@
#include <gtest/gtest.h>
#include <sled/log/log.h>
#include <sled/make_unique.h>
#include <sled/synchronization/event.h>
#include <sled/synchronization/sequence_checker.h>
#include <sled/system/thread.h>
@ -13,56 +13,60 @@ RunOnDifferentThread(std::function<void()> func)
thread_has_run_event.Set();
});
thread.detach();
EXPECT_TRUE(thread_has_run_event.Wait(sled::TimeDelta::Seconds(1)));
CHECK(thread_has_run_event.Wait(sled::TimeDelta::Seconds(1)));
}
TEST(SequenceChecker, CallsAllowedOnSameThread)
TEST_SUITE("SequenceChecker")
{
TEST_CASE("CallsAllowedOnSameThread")
{
sled::SequenceChecker sequence_checker;
EXPECT_TRUE(sequence_checker.IsCurrent());
}
CHECK(sequence_checker.IsCurrent());
}
TEST(SequenceChecker, DestructorAllowedOnDifferentThread)
{
auto sequence_checker = std::make_unique<sled::SequenceChecker>();
TEST_CASE("DestructorAllowedOnDifferentThread")
{
auto sequence_checker = sled::MakeUnique<sled::SequenceChecker>();
RunOnDifferentThread([&] { sequence_checker.reset(); });
}
}
TEST(SequenceChecker, Detach)
{
TEST_CASE("Detach")
{
sled::SequenceChecker sequence_checker;
sequence_checker.Detach();
RunOnDifferentThread([&] { EXPECT_TRUE(sequence_checker.IsCurrent()); });
}
RunOnDifferentThread([&] { CHECK(sequence_checker.IsCurrent()); });
}
TEST(SequenceChecker, OnlyCurrentOnOneThread)
{
TEST_CASE("OnlyCurrentOnOneThread")
{
sled::SequenceChecker sequence_checker(sled::SequenceChecker::kDetached);
RunOnDifferentThread([&] {
EXPECT_TRUE(sequence_checker.IsCurrent());
RunOnDifferentThread([&] { EXPECT_FALSE(sequence_checker.IsCurrent()); });
EXPECT_TRUE(sequence_checker.IsCurrent());
CHECK(sequence_checker.IsCurrent());
RunOnDifferentThread([&] { CHECK_FALSE(sequence_checker.IsCurrent()); });
CHECK(sequence_checker.IsCurrent());
});
}
}
TEST(SequenceChecker, DeatchFromThreadAndUseOnTaskQueue)
{
TEST_CASE("DeatchFromThreadAndUseOnTaskQueue")
{
auto queue = sled::Thread::Create();
ASSERT_TRUE(queue->Start());
REQUIRE(queue->Start());
sled::SequenceChecker sequence_checker;
sequence_checker.Detach();
queue->BlockingCall([&] { EXPECT_TRUE(sequence_checker.IsCurrent()); });
}
queue->BlockingCall([&] { CHECK(sequence_checker.IsCurrent()); });
}
TEST(SequenceChecker, DetachFromTaskQueueAndUseOnThread)
{
TEST_CASE("DetachFromTaskQueueAndUseOnThread")
{
auto queue = sled::Thread::Create();
ASSERT_TRUE(queue->Start());
REQUIRE(queue->Start());
queue->BlockingCall([&] {
sled::SequenceChecker sequence_checker;
sequence_checker.Detach();
RunOnDifferentThread([&] { EXPECT_TRUE(sequence_checker.IsCurrent()); });
RunOnDifferentThread([&] { CHECK(sequence_checker.IsCurrent()); });
});
queue->Stop();
}
}

View File

@ -1,9 +1,11 @@
#include <gtest/gtest.h>
#include <sled/system/fiber/scheduler.h>
#include <sled/system/fiber/wait_group.h>
TEST(FiberScheduler, TestFiberScheduler)
TEST_SUITE("Fiber")
{
TEST_CASE("FiberScheduler")
{
sled::Scheduler scheduler(sled::Scheduler::Config::allCores());
scheduler.bind();
defer(scheduler.unbind());
@ -21,5 +23,6 @@ TEST(FiberScheduler, TestFiberScheduler)
sled::Schedule([=] { wg.Done(); });
wg2.Wait();
ASSERT_EQ(counter.load(), 1000);
CHECK_EQ(counter.load(), 1000);
}
}

View File

@ -1,4 +1,3 @@
#include <gtest/gtest.h>
#include <random>
#include <sled/system/thread_pool.h>
@ -38,28 +37,26 @@ multiply_return(const int a, const int b)
return res;
}
class ThreadPoolTest : public ::testing::Test {
public:
void SetUp() override { tp = new sled::ThreadPool(); }
void TearDown() override { delete tp; }
sled::ThreadPool *tp;
};
TEST_F(ThreadPoolTest, Output)
TEST_CASE("ThreadPool")
{
sled::ThreadPool *tp = new sled::ThreadPool();
REQUIRE_NE(tp, nullptr);
SUBCASE("Output")
{
for (int i = 0; i < 100; ++i) {
int out;
tp->submit(multiply_output, std::ref(out), i, i).get();
EXPECT_EQ(out, i * i);
CHECK_EQ(out, i * i);
}
}
TEST_F(ThreadPoolTest, Return)
{
}
SUBCASE("Return")
{
for (int i = 0; i < 100; ++i) {
auto f = tp->submit(multiply_return, i, i);
EXPECT_EQ(f.get(), i * i);
CHECK_EQ(f.get(), i * i);
}
}
delete tp;
}

7108
src/sled/testing/doctest.h Normal file

File diff suppressed because it is too large Load Diff

7845
src/sled/testing/fakeit.h Normal file

File diff suppressed because it is too large Load Diff

3
src/sled/testing/test.cc Normal file
View File

@ -0,0 +1,3 @@
#define DOCTEST_CONFIG_IMPLEMENT
#define SLED_TESTING_TEST_H
#include "sled/testing/doctest.h"

7
src/sled/testing/test.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef SLED_TESTING_TEST_H
#define SLED_TESTING_TEST_H
#include "sled/testing/doctest.h"
#include "sled/testing/fakeit.h"
#endif// SLED_TESTING_TEST_H

View File

@ -0,0 +1,8 @@
#define SLED_TESTING_TEST_H
#include "sled/testing/doctest.h"
int
main(int argc, char *argv[])
{
return doctest::Context(argc, argv).run();
}

View File

@ -43,7 +43,7 @@ TaskQueueTimeoutFactory::TaskQueueTimeout::Start(DurationMs duration_ms, Timeout
[timeout_id, this]() {
LOGV("timer", "Timeout expired: {}", timeout_id);
SLED_DCHECK_RUN_ON(&parent_.thread_checker_);
DCHECK(posted_task_expiration_ != std::numeric_limits<TimeMs>::max(), "");
SLED_DCHECK(posted_task_expiration_ != std::numeric_limits<TimeMs>::max(), "");
posted_task_expiration_ = std::numeric_limits<TimeMs>::max();
if (timeout_expiration_ == std::numeric_limits<TimeMs>::max()) {

View File

@ -1,14 +1,13 @@
#include <gtest/gtest.h>
#include <sled/uri.h>
TEST(URI, Absolute)
TEST_CASE("")
{
sled::URI uri("http://example.com:1234/dir1/dir2/file?a=1#anchor");
EXPECT_EQ(uri.scheme(), "http");
EXPECT_EQ(uri.host(), "example.com");
EXPECT_EQ(uri.port(), 1234);
EXPECT_EQ(uri.path(), "/dir1/dir2/file");
EXPECT_EQ(uri.query().size(), 1);
EXPECT_EQ(uri.query()["a"], "1");
EXPECT_EQ(uri.anchor(), "anchor");
CHECK_EQ(uri.scheme(), "http");
CHECK_EQ(uri.host(), "example.com");
CHECK_EQ(uri.port(), 1234);
CHECK_EQ(uri.path(), "/dir1/dir2/file");
CHECK_EQ(uri.query().size(), 1);
CHECK_EQ(uri.query()["a"], "1");
CHECK_EQ(uri.anchor(), "anchor");
}