1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-25 13:55:47 -05:00

refactor(tests): Drop CppUnit

This commit is contained in:
Michael Carlberg 2016-10-25 01:47:00 +02:00
parent 9e22b3e693
commit 769a3debe7
21 changed files with 440 additions and 501 deletions

View file

@ -1,24 +1,19 @@
cmake_minimum_required(VERSION 3.1 FATAL_ERROR) #
project(testsuite CXX) # Based on https://github.com/modern-cpp-examples/match3/blob/master/test/CMakeLists.txt
#
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -include common/test.hpp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CPPUNIT_CFLAGS}") include_directories(${CMAKE_CURRENT_LIST_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wno-unused-parameter -Wno-unused-local-typedefs") include_directories(${APP_INCLUDE_DIRS})
find_package(CppUnit REQUIRED) function(unit_test file)
add_definitions(${CPPUNIT_CFLAGS_OTHER}) string(REPLACE "/" "_" testname ${file})
add_executable(unit_test.${testname} ${CMAKE_CURRENT_LIST_DIR}/unit_tests/${file}.cpp)
add_test(unit_test.${testname} unit_test.${testname})
endfunction()
file(GLOB_RECURSE source_files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") unit_test("utils/memory")
unit_test("utils/string")
make_executable(testrunner unit_test("components/command_line")
SOURCES unit_test("components/di")
${source_files} #unit_test("components/logger")
TARGET_DEPENDS
lemonlib_static
CMAKE_DEPENDS
cppunit
xpp)
#add_custom_command(TARGET testrunner POST_BUILD COMMAND testrunner)
add_custom_target(tests testrunner
DEPENDS testrunner
COMMENT "Running CppUnit tests...")

38
tests/common/test.hpp Normal file
View file

@ -0,0 +1,38 @@
//
// Copyright (c) 2016 Krzysztof Jusiak (krzysztof at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#pragma once
#include <cstdio>
#include <cstdlib>
#define expect(...) \
(void)((__VA_ARGS__) || (expect_fail__(#__VA_ARGS__, __FILE__, __LINE__), 0))
#define static_expect(...) static_assert((__VA_ARGS__), "fail")
void expect_fail__(const char* msg, const char* file, int line) {
std::printf("%s:%d:%s\n", file, line, msg);
std::exit(-1);
}
template <char...>
struct test {
template <class Test>
bool operator=(const Test& test) {
test();
return true;
}
};
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wgnu-string-literal-operator-template"
#endif
template <class T, T... Chars>
constexpr auto operator""_test() {
return test<Chars...>{};
}

View file

@ -1,125 +0,0 @@
#include <lemonbuddy/components/command_line.hpp>
#include <lemonbuddy/utils/string.hpp>
#include "../unit_test.hpp"
using namespace lemonbuddy;
using cli_parser = command_line::parser;
// clang-format off
const command_line::options g_opts{
command_line::option{"-f", "--flag", "Flag description"},
command_line::option{"-o", "--option", "Option description", "OPTION", {"foo", "bar", "baz"}},
};
// clang-format on
class test_command_line : public unit_test {
public:
CPPUNIT_TEST_SUITE(test_command_line);
CPPUNIT_TEST(test_has_short);
CPPUNIT_TEST(test_has_long);
CPPUNIT_TEST(test_compare);
CPPUNIT_TEST(test_get);
CPPUNIT_TEST(test_missing_value);
CPPUNIT_TEST(test_invalid_value);
CPPUNIT_TEST(test_unrecognized);
CPPUNIT_TEST_SUITE_END();
void test_has_short() {
auto cli = get_instance();
cli.process_input(string_util::split("-f", ' '));
CPPUNIT_ASSERT_EQUAL(true, cli.has("flag"));
CPPUNIT_ASSERT_EQUAL(false, cli.has("option"));
cli = get_instance();
cli.process_input(string_util::split("-f -o foo", ' '));
CPPUNIT_ASSERT_EQUAL(true, cli.has("flag"));
CPPUNIT_ASSERT_EQUAL(true, cli.has("option"));
cli = get_instance();
cli.process_input(string_util::split("-o baz", ' '));
CPPUNIT_ASSERT_EQUAL(false, cli.has("flag"));
CPPUNIT_ASSERT_EQUAL(true, cli.has("option"));
}
void test_has_long() {
auto cli = get_instance();
cli.process_input(string_util::split("--flag", ' '));
CPPUNIT_ASSERT_EQUAL(true, cli.has("flag"));
CPPUNIT_ASSERT_EQUAL(false, cli.has("option"));
cli = get_instance();
cli.process_input(string_util::split("--flag --option=foo", ' '));
CPPUNIT_ASSERT_EQUAL(true, cli.has("flag"));
CPPUNIT_ASSERT_EQUAL(true, cli.has("option"));
cli = get_instance();
cli.process_input(string_util::split("--option=foo --flag", ' '));
CPPUNIT_ASSERT_EQUAL(true, cli.has("flag"));
CPPUNIT_ASSERT_EQUAL(true, cli.has("option"));
cli = get_instance();
cli.process_input(string_util::split("--option=baz", ' '));
CPPUNIT_ASSERT_EQUAL(false, cli.has("flag"));
CPPUNIT_ASSERT_EQUAL(true, cli.has("option"));
}
void test_compare() {
auto cli = get_instance();
cli.process_input(string_util::split("-o baz", ' '));
CPPUNIT_ASSERT_EQUAL(true, cli.compare("option", "baz"));
cli = get_instance();
cli.process_input(string_util::split("--option=foo", ' '));
CPPUNIT_ASSERT_EQUAL(true, cli.compare("option", "foo"));
}
void test_get() {
auto cli = get_instance();
cli.process_input(string_util::split("--option=baz", ' '));
CPPUNIT_ASSERT_EQUAL(string{"baz"}, cli.get("option"));
cli = get_instance();
cli.process_input(string_util::split("--option=foo", ' '));
CPPUNIT_ASSERT_EQUAL(string{"foo"}, cli.get("option"));
}
void test_missing_value() {
auto input1 = string_util::split("--option", ' ');
auto input2 = string_util::split("-o", ' ');
auto input3 = string_util::split("--option baz", ' ');
using command_line::value_error;
CPPUNIT_ASSERT_THROW(get_instance().process_input(input1), value_error);
CPPUNIT_ASSERT_THROW(get_instance().process_input(input2), value_error);
CPPUNIT_ASSERT_THROW(get_instance().process_input(input3), value_error);
}
void test_invalid_value() {
auto input1 = string_util::split("--option=invalid", ' ');
auto input2 = string_util::split("-o invalid_value", ' ');
using command_line::value_error;
CPPUNIT_ASSERT_THROW(get_instance().process_input(input1), value_error);
CPPUNIT_ASSERT_THROW(get_instance().process_input(input2), value_error);
}
void test_unrecognized() {
auto input1 = string_util::split("-x", ' ');
auto input2 = string_util::split("--unrecognized", ' ');
using command_line::argument_error;
CPPUNIT_ASSERT_THROW(get_instance().process_input(input1), argument_error);
CPPUNIT_ASSERT_THROW(get_instance().process_input(input2), argument_error);
}
private:
cli_parser get_instance() {
return cli_parser::configure<cli_parser>("cmd", g_opts).create<cli_parser>();
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(test_command_line);

View file

@ -1,22 +0,0 @@
#include <lemonbuddy/components/logger.hpp>
#include "../unit_test.hpp"
using namespace lemonbuddy;
class test_logger : public unit_test {
public:
CPPUNIT_TEST_SUITE(test_logger);
CPPUNIT_TEST(test_output);
CPPUNIT_TEST_SUITE_END();
void test_output() {
auto l = logger::configure<logger>(loglevel::TRACE).create<logger>();
l.err("error");
l.warn("warning");
l.info("info");
l.trace("trace");
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(test_logger);

View file

@ -1,46 +0,0 @@
#include <csignal>
#include <lemonbuddy/components/x11/color.hpp>
#include "../../unit_test.hpp"
using namespace lemonbuddy;
class test_draw : public unit_test {
public:
CPPUNIT_TEST_SUITE(test_draw);
CPPUNIT_TEST(test_color);
CPPUNIT_TEST(test_cache);
CPPUNIT_TEST(test_predefined);
CPPUNIT_TEST(test_parse);
CPPUNIT_TEST_SUITE_END();
void test_color() {
color test{"#33990022"};
CPPUNIT_ASSERT_EQUAL(string{"#33990022"}, test.hex());
CPPUNIT_ASSERT_EQUAL(string{"#1E0006"}, test.rgb());
}
void test_cache() {
CPPUNIT_ASSERT_EQUAL(size_t{0}, g_colorstore.size());
auto c1 = color::parse("#100");
CPPUNIT_ASSERT_EQUAL(size_t{1}, g_colorstore.size());
auto c2 = color::parse("#200");
CPPUNIT_ASSERT_EQUAL(size_t{2}, g_colorstore.size());
auto c3 = color::parse("#200");
CPPUNIT_ASSERT_EQUAL(size_t{2}, g_colorstore.size());
CPPUNIT_ASSERT_EQUAL(c1.value(), g_colorstore.find("#100")->second.value());
}
void test_predefined() {
CPPUNIT_ASSERT_EQUAL(string{"#FF000000"}, g_colorblack.hex());
CPPUNIT_ASSERT_EQUAL(string{"#FFFFFFFF"}, g_colorwhite.hex());
}
void test_parse() {
CPPUNIT_ASSERT_EQUAL(string{"#FFFF9900"}, color::parse("#ff9900", g_colorblack).hex());
CPPUNIT_ASSERT_EQUAL(string{"#FFFFFFFF"}, color::parse("invalid", g_colorwhite).hex());
CPPUNIT_ASSERT_EQUAL(string{"#1E0006"}, color::parse("33990022", g_colorwhite).rgb());
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(test_draw);

View file

@ -1,20 +0,0 @@
#include <lemonbuddy/components/x11/connection.hpp>
#include "../../unit_test.hpp"
using namespace lemonbuddy;
class test_connection : public unit_test {
public:
CPPUNIT_TEST_SUITE(test_connection);
CPPUNIT_TEST(test_id);
CPPUNIT_TEST_SUITE_END();
void test_id() {
CPPUNIT_ASSERT_EQUAL(string{"0x12345678"}, m_connection.id(static_cast<xcb_window_t>(0x12345678)));
}
connection& m_connection = connection::configure().create<connection&>();
};
CPPUNIT_TEST_SUITE_REGISTRATION(test_connection);

View file

@ -1,33 +0,0 @@
#include <lemonbuddy/components/x11/window.hpp>
#include "../../unit_test.hpp"
using namespace lemonbuddy;
class test_window : public unit_test {
public:
CPPUNIT_TEST_SUITE(test_window);
CPPUNIT_TEST(test_cw_create);
CPPUNIT_TEST_SUITE_END();
void test_cw_create() {
// clang-format off
// auto win = winspec()
// << cw_size(100, 200)
// << cw_pos(10, -20)
// << cw_border(9)
// << cw_class(XCB_WINDOW_CLASS_INPUT_ONLY)
// << cw_parent(0x000110a)
// ;
// clang-format on
// CPPUNIT_ASSERT_EQUAL(win.width, uint16_t{100});
// CPPUNIT_ASSERT_EQUAL(win.height, uint16_t{200});
// CPPUNIT_ASSERT_EQUAL(win.x, int16_t{10});
// CPPUNIT_ASSERT_EQUAL(win.y, int16_t{-20});
// CPPUNIT_ASSERT_EQUAL(win.border_width, uint16_t{9});
// CPPUNIT_ASSERT_EQUAL(win.class_, uint16_t{XCB_WINDOW_CLASS_INPUT_ONLY});
// CPPUNIT_ASSERT_EQUAL(win.parent, xcb_window_t{0x000110a});
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(test_window);

View file

@ -1,13 +0,0 @@
#include <cppunit/CompilerOutputter.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
int main(int argc, char *argv[]) {
CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
CppUnit::TextUi::TestRunner runner;
runner.addTest(suite);
runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), std::cerr));
return !runner.run();
}

View file

@ -1,17 +0,0 @@
#pragma once
#include <cppunit/extensions/HelperMacros.h>
class unit_test : public CppUnit::TestFixture {
public:
virtual void prepare() {}
virtual void finish() {}
void set_up() {
this->prepare();
}
void tear_down() {
this->finish();
}
};

View file

@ -0,0 +1,159 @@
#include "components/command_line.hpp"
#include "utils/string.hpp"
int main() {
using namespace lemonbuddy;
using cli_parser = command_line::parser;
// clang-format off
const command_line::options opts{
command_line::option{"-f", "--flag", "Flag description"},
command_line::option{"-o", "--option", "Option description", "OPTION", {"foo", "bar", "baz"}},
};
// clang-format on
"has_short"_test = [&opts] {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("-f", ' '));
expect(cli.has("flag"));
expect(!cli.has("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("-f -o foo", ' '));
expect(cli.has("flag"));
expect(cli.has("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("-o baz", ' '));
expect(!cli.has("flag"));
expect(cli.has("option"));
};
"has_long"_test = [&opts] {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("--flag", ' '));
expect(cli.has("flag"));
expect(!cli.has("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("--flag --option=foo", ' '));
expect(cli.has("flag"));
expect(cli.has("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("--option=foo --flag", ' '));
expect(cli.has("flag"));
expect(cli.has("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("--option=baz", ' '));
expect(!cli.has("flag"));
expect(cli.has("option"));
};
"compare"_test = [&opts] {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("-o baz", ' '));
expect(cli.compare("option", "baz"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("--option=foo", ' '));
expect(cli.compare("option", "foo"));
};
"get"_test = [&opts] {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("--option=baz", ' '));
expect("baz" == cli.get("option"));
cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(string_util::split("--option=foo", ' '));
expect("foo" == cli.get("option"));
};
"missing_value"_test = [&opts] {
auto input1 = string_util::split("--option", ' ');
auto input2 = string_util::split("-o", ' ');
auto input3 = string_util::split("--option baz", ' ');
bool exception_thrown = false;
try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(input1);
} catch (const command_line::value_error&) {
exception_thrown = true;
} catch (...) {
}
expect(exception_thrown);
exception_thrown = false; // reset
try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(input2);
} catch (const command_line::value_error&) {
exception_thrown = true;
} catch (...) {
}
expect(exception_thrown);
exception_thrown = false; // reset
try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(input3);
} catch (const command_line::value_error&) {
exception_thrown = true;
} catch (...) {
}
expect(exception_thrown);
};
"invalid_value"_test = [&opts] {
auto input1 = string_util::split("--option=invalid", ' ');
auto input2 = string_util::split("-o invalid_value", ' ');
bool exception_thrown = false;
try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(input1);
} catch (const command_line::value_error&) {
exception_thrown = true;
} catch (...) {
}
expect(exception_thrown);
exception_thrown = false; // reset
try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(input2);
} catch (const command_line::value_error&) {
exception_thrown = true;
} catch (...) {
}
expect(exception_thrown);
};
"unrecognized"_test = [&opts] {
auto input1 = string_util::split("-x", ' ');
auto input2 = string_util::split("--unrecognized", ' ');
bool exception_thrown = false;
try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(input1);
} catch (const command_line::argument_error&) {
exception_thrown = true;
} catch (...) {
}
expect(exception_thrown);
exception_thrown = false; // reset
try {
auto cli = configure_cli_parser<cli_parser>("cmd", opts).create<cli_parser>();
cli.process_input(input2);
} catch (const command_line::argument_error&) {
exception_thrown = true;
} catch (...) {
}
expect(exception_thrown);
};
}

View file

@ -1,25 +1,16 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp> #include <boost/weak_ptr.hpp>
#include <lemonbuddy/components/logger.hpp> #include "components/logger.hpp"
#include <lemonbuddy/utils/inotify.hpp> #include "utils/inotify.hpp"
#include "../unit_test.hpp" #define CONFIGURE_ARGS(T, V, Args) configure_##T<decltype(V)>(Args).create<decltype(V)>()
#define CONFIGURE(T, V) configure_##T<decltype(V)>().create<decltype(V)>()
#define CONFIGURE_ARGS(T, V, Args) T::configure<decltype(V)>(Args).create<decltype(V)>() int main() {
#define CONFIGURE(T, V) T::configure<decltype(V)>().create<decltype(V)>() using namespace lemonbuddy;
using namespace lemonbuddy; "singleton"_test = [] {
class test_di : public unit_test {
public:
CPPUNIT_TEST_SUITE(test_di);
CPPUNIT_TEST(test_singleton);
CPPUNIT_TEST(test_unique);
CPPUNIT_TEST(test_instance);
CPPUNIT_TEST_SUITE_END();
void test_singleton() {
// clang-format off // clang-format off
logger& instance1{CONFIGURE(logger, instance1)}; logger& instance1{CONFIGURE(logger, instance1)};
const logger& instance2{CONFIGURE(logger, instance2)}; const logger& instance2{CONFIGURE(logger, instance2)};
@ -31,25 +22,23 @@ class test_di : public unit_test {
string mem_addr2{string_util::from_stream(std::stringstream() << &instance2)}; string mem_addr2{string_util::from_stream(std::stringstream() << &instance2)};
string mem_addr3{string_util::from_stream(std::stringstream() << instance3.get())}; string mem_addr3{string_util::from_stream(std::stringstream() << instance3.get())};
CPPUNIT_ASSERT_EQUAL(mem_addr1, mem_addr2); expect(mem_addr1 == mem_addr2);
CPPUNIT_ASSERT_EQUAL(mem_addr2, mem_addr3); expect(mem_addr2 == mem_addr3);
CPPUNIT_ASSERT_EQUAL(instance3.get(), instance4.get()); expect(instance3.get() == instance4.get());
} };
void test_unique() { "unique"_test = [] {
unique_ptr<inotify_watch> instance1{inotify_util::make_watch("A")}; unique_ptr<inotify_watch> instance1{inotify_util::make_watch("A")};
unique_ptr<inotify_watch> instance2{inotify_util::make_watch("B")}; unique_ptr<inotify_watch> instance2{inotify_util::make_watch("B")};
shared_ptr<inotify_watch> instance3{inotify_util::make_watch("B")}; shared_ptr<inotify_watch> instance3{inotify_util::make_watch("B")};
shared_ptr<inotify_watch> instance4{inotify_util::make_watch("B")}; shared_ptr<inotify_watch> instance4{inotify_util::make_watch("B")};
CPPUNIT_ASSERT(instance1.get() != instance2.get()); expect(instance1.get() != instance2.get());
CPPUNIT_ASSERT(instance2.get() != instance3.get()); expect(instance2.get() != instance3.get());
CPPUNIT_ASSERT(instance3.get() != instance4.get()); expect(instance3.get() != instance4.get());
} };
void test_instance() { "instance"_test = [] {
// TODO // TODO
} };
}; }
CPPUNIT_TEST_SUITE_REGISTRATION(test_di);

View file

@ -0,0 +1,13 @@
#include "components/logger.hpp"
int main() {
using namespace lemonbuddy;
"output"_test = [] {
auto l = logger::configure<logger>(loglevel::TRACE).create<logger>();
l.err("error");
l.warn("warning");
l.info("info");
l.trace("trace");
};
}

View file

@ -0,0 +1,35 @@
#include <csignal>
#include "components/x11/color.hpp"
int main() {
using namespace lemonbuddy;
"color"_test = [] {
color test{"#33990022"};
expect(test.hex() == "#33990022");
expect(test.rgb() == "#1E0006");
};
"cache"_test = [] {
expect(g_colorstore.size() == size_t{0});
auto c1 = color::parse("#100");
expect(g_colorstore.size() == size_t{1});
auto c2 = color::parse("#200");
expect(g_colorstore.size() == size_t{2});
auto c3 = color::parse("#200");
expect(g_colorstore.size() == size_t{2});
expect(g_colorstore.find("#100")->second.value() == c1.value());
};
"predefined"_test = [] {
expect(g_colorblack.hex() == "#FF000000");
expect(g_colorwhite.hex() == "#FFFFFFFF");
};
"parse"_test = [] {
expect(color::parse("#ff9900", g_colorblack).hex() == "#FFFF9900");
expect(color::parse("invalid", g_colorwhite).hex() == "#FFFFFFFF");
expect(color::parse("33990022", g_colorwhite).rgb() == "#1E0006");
};
}

View file

@ -0,0 +1,10 @@
#include "components/x11/connection.hpp"
int main() {
using namespace lemonbuddy;
"id"_test = [] {
connection& conn{connection::configure().create<connection&>()};
expect(conn.id(static_cast<xcb_window_t>(0x12345678)) == "0x12345678");
};
}

View file

@ -0,0 +1,25 @@
#include "components/x11/window.hpp"
int main() {
using namespace lemonbuddy;
"cw_create"_test = [] {
// clang-format off
auto win = winspec()
<< cw_size(100, 200)
<< cw_pos(10, -20)
<< cw_border(9)
<< cw_class(XCB_WINDOW_CLASS_INPUT_ONLY)
<< cw_parent(0x000110a)
;
// clang-format on
expect(win.width == 100);
expect(win.height == 200);
expect(win.x == 10);
expect(win.y == -20);
expect(win.border_width == 9);
expect(win.class_ == XCB_WINDOW_CLASS_INPUT_ONLY);
expect(win.parent == 0x000110a);
};
}

View file

@ -0,0 +1,24 @@
#include "utils/memory.hpp"
struct mytype {
int x, y, z;
};
int main() {
using namespace lemonbuddy;
"make_malloc_ptr"_test = [] {
auto ptr = memory_util::make_malloc_ptr<mytype>();
expect(sizeof(mytype*) == sizeof(ptr.get()));
ptr.reset();
expect(ptr.get() == nullptr);
};
"countof"_test = [] {
mytype A[3]{{}, {}, {}};
mytype B[8]{{}, {}, {}, {}, {}, {}, {}, {}};
expect(memory_util::countof(A) == size_t{3});
expect(memory_util::countof(B) == size_t{8});
};
}

View file

@ -0,0 +1,20 @@
#include "utils/scope.hpp"
int main() {
using namespace lemonbuddy;
"on_exit"_test = [] {
auto flag = false;
{
expect(!flag);
auto handler = scope_util::make_exit_handler<>([&] { flag = true; });
expect(!flag);
{
auto handler = scope_util::make_exit_handler<>([&] { flag = true; });
}
expect(flag);
flag = false;
}
expect(flag);
};
}

View file

@ -0,0 +1,81 @@
#include <iomanip>
#include "utils/string.hpp"
int main() {
using namespace lemonbuddy;
"upper"_test = [] { expect(string_util::upper("FOO") == "FOO"); };
"lower"_test = [] { expect(string_util::lower("BAR") == "bar"); };
"compare"_test = [] {
expect(string_util::compare("foo", "foo"));
expect(!string_util::compare("foo", "bar"));
};
"replace"_test = [] { expect(string_util::replace("Foo bar baz", "a", "x") == "Foo bxr baz"); };
"replace_all"_test = [] {
expect(string_util::replace_all("Foo bar baz", "a", "x") == "Foo bxr bxz");
expect(string_util::replace_all("hehehe", "he", "hoo") == "hoohoohoo");
expect(string_util::replace_all("131313", "3", "13") == "113113113");
};
"squeeze"_test = [] {
expect(string_util::squeeze("Squeeeeeze", 'e') == "Squeze");
expect(string_util::squeeze("bar baz foobar", ' ') == "bar baz foobar");
};
"strip"_test = [] {
expect(string_util::strip("Striip", 'i') == "Strp");
expect(string_util::strip_trailing_newline("test\n\n") == "test\n");
};
"trim"_test = [] {
expect(string_util::ltrim("xxtestxx", 'x') == "testxx");
expect(string_util::rtrim("xxtestxx", 'x') == "xxtest");
expect(string_util::trim("xxtestxx", 'x') == "test");
};
"join"_test = [] { expect(string_util::join({"A", "B", "C"}, ", ") == "A, B, C"); };
"split_into"_test = [] {
vector<string> strings;
string_util::split_into("A,B,C", ',', strings);
expect(strings.size() == size_t(3));
expect(strings[0] == "A");
expect(strings[2] == "C");
};
"split"_test = [] {
vector<string> strings{"foo", "bar"};
vector<string> result{string_util::split("foo,bar", ',')};
expect(result.size() == strings.size());
expect(result[0] == strings[0]);
expect(result[1] == "bar");
};
"find_nth"_test = [] {
expect(string_util::find_nth("foobarfoobar", 0, "f", 1) == size_t{0});
expect(string_util::find_nth("foobarfoobar", 0, "f", 2) == size_t{6});
expect(string_util::find_nth("foobarfoobar", 0, "o", 3) == size_t{7});
};
"from_stream"_test = [] {
auto result =
string_util::from_stream(std::stringstream() << std::setw(6) << std::setfill('z') << "foo"
<< "bar");
expect(result == "zzzfoobar");
};
"hash"_test = [] {
unsigned long hashA1{string_util::hash("foo")};
unsigned long hashA2{string_util::hash("foo")};
unsigned long hashB1{string_util::hash("Foo")};
unsigned long hashB2{string_util::hash("Bar")};
expect(hashA1 == hashA2);
expect(hashA1 != hashB1 != hashB2);
expect(hashB1 != hashB2);
};
}

View file

@ -1,35 +0,0 @@
#include <iomanip>
#include <lemonbuddy/utils/memory.hpp>
#include "../unit_test.hpp"
using namespace lemonbuddy;
class test_memory : public unit_test {
public:
CPPUNIT_TEST_SUITE(test_memory);
CPPUNIT_TEST(test_make_malloc_ptr);
CPPUNIT_TEST(test_countof);
CPPUNIT_TEST_SUITE_END();
struct mytype {
int x, y, z;
};
void test_make_malloc_ptr() {
auto ptr = memory_util::make_malloc_ptr<mytype>();
CPPUNIT_ASSERT_EQUAL(sizeof(mytype*), sizeof(ptr.get()));
ptr.reset();
CPPUNIT_ASSERT(ptr.get() == nullptr);
}
void test_countof() {
mytype A[3]{{}, {}, {}};
mytype B[8]{{}, {}, {}, {}, {}, {}, {}, {}};
CPPUNIT_ASSERT_EQUAL(size_t{3}, memory_util::countof(A));
CPPUNIT_ASSERT_EQUAL(size_t{8}, memory_util::countof(B));
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(test_memory);

View file

@ -1,30 +0,0 @@
#include <iomanip>
#include <lemonbuddy/utils/scope.hpp>
#include "../unit_test.hpp"
using namespace lemonbuddy;
class test_scope : public unit_test {
public:
CPPUNIT_TEST_SUITE(test_scope);
CPPUNIT_TEST(test_on_exit);
CPPUNIT_TEST_SUITE_END();
void test_on_exit() {
auto flag = false;
{
CPPUNIT_ASSERT_EQUAL(false, flag);
auto handler = scope_util::make_exit_handler<>([&] { flag = true; });
CPPUNIT_ASSERT_EQUAL(false, flag);
{
auto handler = scope_util::make_exit_handler<>([&] { flag = true; });
}
CPPUNIT_ASSERT_EQUAL(true, flag);
flag = false;
}
CPPUNIT_ASSERT_EQUAL(true, flag);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(test_scope);

View file

@ -1,109 +0,0 @@
#include <iomanip>
#include <lemonbuddy/utils/string.hpp>
#include "../unit_test.hpp"
using namespace lemonbuddy;
class test_string : public unit_test {
public:
CPPUNIT_TEST_SUITE(test_string);
CPPUNIT_TEST(test_upper);
CPPUNIT_TEST(test_lower);
CPPUNIT_TEST(test_compare);
CPPUNIT_TEST(test_replace);
CPPUNIT_TEST(test_replace_all);
CPPUNIT_TEST(test_squeeze);
CPPUNIT_TEST(test_strip);
CPPUNIT_TEST(test_trim);
CPPUNIT_TEST(test_join);
CPPUNIT_TEST(test_split_into);
CPPUNIT_TEST(test_split);
CPPUNIT_TEST(test_find_nth);
CPPUNIT_TEST(test_from_stream);
CPPUNIT_TEST(test_hash);
CPPUNIT_TEST_SUITE_END();
void test_upper() {
CPPUNIT_ASSERT_EQUAL(string{"FOO"}, string_util::upper("FOO"));
}
void test_lower() {
CPPUNIT_ASSERT_EQUAL(string{"bar"}, string_util::lower("BAR"));
}
void test_compare() {
CPPUNIT_ASSERT_EQUAL(true, string_util::compare("foo", "foo"));
CPPUNIT_ASSERT_EQUAL(false, string_util::compare("foo", "bar"));
}
void test_replace() {
CPPUNIT_ASSERT_EQUAL(string{"Foo bxr baz"}, string_util::replace("Foo bar baz", "a", "x"));
}
void test_replace_all() {
CPPUNIT_ASSERT_EQUAL(string{"Foo bxr bxz"}, string_util::replace_all("Foo bar baz", "a", "x"));
CPPUNIT_ASSERT_EQUAL(string{"hoohoohoo"}, string_util::replace_all("hehehe", "he", "hoo"));
CPPUNIT_ASSERT_EQUAL(string{"113113113"}, string_util::replace_all("131313", "3", "13"));
}
void test_squeeze() {
CPPUNIT_ASSERT_EQUAL(string{"Squeze"}, string_util::squeeze("Squeeeeeze", 'e'));
CPPUNIT_ASSERT_EQUAL(string{"bar baz foobar"}, string_util::squeeze("bar baz foobar", ' '));
}
void test_strip() {
CPPUNIT_ASSERT_EQUAL(string{"Strp"}, string_util::strip("Striip", 'i'));
CPPUNIT_ASSERT_EQUAL(string{"test\n"}, string_util::strip_trailing_newline("test\n\n"));
}
void test_trim() {
CPPUNIT_ASSERT_EQUAL(string{"testxx"}, string_util::ltrim("xxtestxx", 'x'));
CPPUNIT_ASSERT_EQUAL(string{"xxtest"}, string_util::rtrim("xxtestxx", 'x'));
CPPUNIT_ASSERT_EQUAL(string{"test"}, string_util::trim("xxtestxx", 'x'));
}
void test_join() {
CPPUNIT_ASSERT_EQUAL(string{"A, B, C"}, string_util::join({"A", "B", "C"}, ", "));
}
void test_split_into() {
vector<string> strings;
string_util::split_into("A,B,C", ',', strings);
CPPUNIT_ASSERT_EQUAL(size_t(3), strings.size());
CPPUNIT_ASSERT_EQUAL(string{"A"}, strings[0]);
CPPUNIT_ASSERT_EQUAL(string{"C"}, strings[2]);
}
void test_split() {
vector<string> strings{string{"foo"}, string{"bar"}};
vector<string> result{string_util::split(string{"foo,bar"}, ',')};
CPPUNIT_ASSERT(strings.size() == result.size());
CPPUNIT_ASSERT(strings[0] == result[0]);
CPPUNIT_ASSERT_EQUAL(string{"bar"}, result[1]);
}
void test_find_nth() {
CPPUNIT_ASSERT_EQUAL(size_t{0}, string_util::find_nth("foobarfoobar", 0, "f", 1));
CPPUNIT_ASSERT_EQUAL(size_t{6}, string_util::find_nth("foobarfoobar", 0, "f", 2));
CPPUNIT_ASSERT_EQUAL(size_t{7}, string_util::find_nth("foobarfoobar", 0, "o", 3));
}
void test_from_stream() {
CPPUNIT_ASSERT_EQUAL(string{"zzzfoobar"},
string_util::from_stream(std::stringstream() << std::setw(6) << std::setfill('z') << "foo"
<< "bar"));
}
void test_hash() {
unsigned long hashA1{string_util::hash("foo")};
unsigned long hashA2{string_util::hash("foo")};
unsigned long hashB1{string_util::hash("Foo")};
unsigned long hashB2{string_util::hash("Bar")};
CPPUNIT_ASSERT(hashA1 == hashA2);
CPPUNIT_ASSERT(hashA1 != hashB1 != hashB2);
CPPUNIT_ASSERT(hashB1 != hashB2);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(test_string);