tests: move to subdir, move to meson

De-clutter the source directories a bit. Ensure tests build with meson, and
remove from autotools in a few places (no need to do things twice).
This commit is contained in:
Dirk-Jan C. Binnema
2021-11-07 11:41:55 +02:00
parent 67b16acbb2
commit 48d3f9cfab
87 changed files with 214 additions and 485 deletions

View File

@ -0,0 +1,50 @@
## Copyright (C) 2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software Foundation,
## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
################################################################################
# tests
#
test('test_command_parser',
executable('test-command-parser',
'test-command-parser.cc',
install: false,
dependencies: [glib_dep, lib_mu_utils_dep]))
test('test_mu_str',
executable('test-mu-str',
'test-mu-str.c',
install: false,
dependencies: [glib_dep, config_h_dep,lib_mu_utils_dep]))
test('test_mu_util',
executable('test-mu-util',
'test-mu-util.c',
install: false,
dependencies: [glib_dep,config_h_dep, lib_mu_utils_dep]))
test('test_option',
executable('test-option',
'test-option.cc',
install: false,
dependencies: [glib_dep, lib_mu_utils_dep]))
test('test_mu_utils',
executable('test-mu-utils',
'test-utils.cc',
install: false,
dependencies: [glib_dep, lib_mu_utils_dep]))
test('test_sexp',
executable('test-sexp',
'test-sexp.cc',
install: false,
dependencies: [glib_dep, lib_mu_utils_dep] ))

View File

@ -0,0 +1,148 @@
/*
** Copyright (C) 2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
** as published by the Free Software Foundation; either version 2.1
** of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free
** Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*/
#include <vector>
#include <glib.h>
#include <iostream>
#include <sstream>
#include "mu-command-parser.hh"
#include "mu-utils.hh"
using namespace Mu;
static void
test_param_getters()
{
const auto sexp{Sexp::make_parse(R"((foo :bar 123 :cuux "456" :boo nil :bah true))")};
if (g_test_verbose())
std::cout << sexp << "\n";
g_assert_cmpint(Command::get_int_or(sexp.list(), ":bar"), ==, 123);
assert_equal(Command::get_string_or(sexp.list(), ":bra", "bla"), "bla");
assert_equal(Command::get_string_or(sexp.list(), ":cuux"), "456");
g_assert_true(Command::get_bool_or(sexp.list(), ":boo") == false);
g_assert_true(Command::get_bool_or(sexp.list(), ":bah") == true);
}
static bool
call(const Command::CommandMap& cmap, const std::string& str)
try {
const auto sexp{Sexp::make_parse(str)};
invoke(cmap, sexp);
return true;
} catch (const Error& err) {
g_warning("%s", err.what());
return false;
}
static void
test_command()
{
using namespace Command;
allow_warnings();
CommandMap cmap;
cmap.emplace(
"my-command",
CommandInfo{ArgMap{{":param1", ArgInfo{Sexp::Type::String, true, "some string"}},
{":param2", ArgInfo{Sexp::Type::Number, false, "some integer"}}},
"My command,",
{}});
g_assert_true(call(cmap, "(my-command :param1 \"hello\")"));
g_assert_true(call(cmap, "(my-command :param1 \"hello\" :param2 123)"));
g_assert_false(call(cmap, "(my-command :param1 \"hello\" :param2 123 :param3 xxx)"));
}
static void
test_command2()
{
using namespace Command;
allow_warnings();
CommandMap cmap;
cmap.emplace("bla",
CommandInfo{ArgMap{
{":foo", ArgInfo{Sexp::Type::Number, false, "foo"}},
{":bar", ArgInfo{Sexp::Type::String, false, "bar"}},
},
"yeah",
[&](const auto& params) {}});
g_assert_true(call(cmap, "(bla :foo nil)"));
g_assert_false(call(cmap, "(bla :foo nil :bla nil)"));
}
static void
test_command_fail()
{
using namespace Command;
allow_warnings();
CommandMap cmap;
cmap.emplace(
"my-command",
CommandInfo{ArgMap{{":param1", ArgInfo{Sexp::Type::String, true, "some string"}},
{":param2", ArgInfo{Sexp::Type::Number, false, "some integer"}}},
"My command,",
{}});
g_assert_false(call(cmap, "(my-command)"));
g_assert_false(call(cmap, "(my-command2)"));
g_assert_false(call(cmap, "(my-command :param1 123 :param2 123)"));
g_assert_false(call(cmap, "(my-command :param1 \"hello\" :param2 \"123\")"));
}
static void
black_hole()
{
}
int
main(int argc, char* argv[])
try {
g_test_init(&argc, &argv, NULL);
g_test_add_func("/utils/command-parser/param-getters", test_param_getters);
g_test_add_func("/utils/command-parser/command", test_command);
g_test_add_func("/utils/command-parser/command2", test_command2);
g_test_add_func("/utils/command-parser/command-fail", test_command_fail);
g_log_set_handler(
NULL,
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
(GLogFunc)black_hole,
NULL);
return g_test_run();
} catch (const std::runtime_error& re) {
std::cerr << re.what() << "\n";
return 1;
}

View File

@ -0,0 +1,169 @@
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
/*
** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <glib.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <locale.h>
#include "mu-str.h"
static void
assert_cmplst (GSList *lst, const char *items[])
{
int i;
if (!lst)
g_assert (!items);
for (i = 0; lst; lst = g_slist_next(lst), ++i)
g_assert_cmpstr ((char*)lst->data,==,items[i]);
g_assert (items[i] == NULL);
}
static GSList*
create_list (const char *items[])
{
GSList *lst;
lst = NULL;
while (items && *items) {
lst = g_slist_prepend (lst, g_strdup(*items));
++items;
}
return g_slist_reverse (lst);
}
static void
test_mu_str_from_list (void)
{
{
const char *strs[] = {"aap", "noot", "mies", NULL};
GSList *lst = create_list (strs);
gchar *str = mu_str_from_list (lst, ',');
g_assert_cmpstr ("aap,noot,mies", ==, str);
mu_str_free_list (lst);
g_free (str);
}
{
const char *strs[] = {"aap", "no,ot", "mies", NULL};
GSList *lst = create_list (strs);
gchar *str = mu_str_from_list (lst, ',');
g_assert_cmpstr ("aap,no,ot,mies", ==, str);
mu_str_free_list (lst);
g_free (str);
}
{
const char *strs[] = {NULL};
GSList *lst = create_list (strs);
gchar *str = mu_str_from_list (lst,'@');
g_assert_cmpstr (NULL, ==, str);
mu_str_free_list (lst);
g_free (str);
}
}
static void
test_mu_str_to_list (void)
{
{
const char *items[]= {"foo", "bar ", "cuux", NULL};
GSList *lst = mu_str_to_list ("foo@bar @cuux",'@', FALSE);
assert_cmplst (lst, items);
mu_str_free_list (lst);
}
{
GSList *lst = mu_str_to_list (NULL,'x',FALSE);
g_assert (lst == NULL);
mu_str_free_list (lst);
}
}
static void
test_mu_str_to_list_strip (void)
{
const char *items[]= {"foo", "bar", "cuux", NULL};
GSList *lst = mu_str_to_list ("foo@bar @cuux",'@', TRUE);
assert_cmplst (lst, items);
mu_str_free_list (lst);
}
static void
test_mu_str_remove_ctrl_in_place (void)
{
unsigned u;
struct {
char *str;
const char *exp;
} strings [] = {
{ g_strdup(""), ""},
{ g_strdup("hello, world!"), "hello, world!" },
{ g_strdup("hello,\tworld!"), "hello, world!" },
{ g_strdup("hello,\n\nworld!"), "hello, world!", },
{ g_strdup("hello,\x1f\x1e\x1ew\nor\nld!"), "hello,w or ld!" },
{ g_strdup("\x1ehello, world!\x1f"), "hello, world!" }
};
for (u = 0; u != G_N_ELEMENTS(strings); ++u) {
char *res;
res = mu_str_remove_ctrl_in_place (strings[u].str);
g_assert_cmpstr (res,==,strings[u].exp);
g_free (strings[u].str);
}
}
int
main (int argc, char *argv[])
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/mu-str/mu-str-from-list",
test_mu_str_from_list);
g_test_add_func ("/mu-str/mu-str-to-list",
test_mu_str_to_list);
g_test_add_func ("/mu-str/mu-str-to-list-strip",
test_mu_str_to_list_strip);
g_test_add_func ("/mu-str/mu_str_remove_ctrl_in_place",
test_mu_str_remove_ctrl_in_place);
return g_test_run ();
}

View File

@ -0,0 +1,246 @@
/*
** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <glib.h>
#include <glib/gstdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include "mu-util.h"
static void
test_mu_util_dir_expand_00 (void)
{
#ifdef HAVE_WORDEXP_H
gchar *got, *expected;
got = mu_util_dir_expand ("~/IProbablyDoNotExist");
expected = g_strdup_printf ("%s%cIProbablyDoNotExist",
getenv("HOME"), G_DIR_SEPARATOR);
g_assert_cmpstr (got,==,expected);
g_free (got);
g_free (expected);
#endif /*HAVE_WORDEXP_H*/
}
static void
test_mu_util_dir_expand_01 (void)
{
/* XXXX: the testcase does not work when using some dir
* setups; (see issue #585), although the code should still
* work. Turn of the test for now */
return;
#ifdef HAVE_WORDEXP_H
{
gchar *got, *expected;
got = mu_util_dir_expand ("~/Desktop");
expected = g_strdup_printf ("%s%cDesktop",
getenv("HOME"), G_DIR_SEPARATOR);
g_assert_cmpstr (got,==,expected);
g_free (got);
g_free (expected);
}
#endif /*HAVE_WORDEXP_H*/
}
static void
test_mu_util_guess_maildir_01 (void)
{
char *got;
const char *expected;
/* skip the test if there's no /tmp */
if (access ("/tmp", F_OK))
return;
g_setenv ("MAILDIR", "/tmp", TRUE);
got = mu_util_guess_maildir ();
expected = "/tmp";
g_assert_cmpstr (got,==,expected);
g_free (got);
}
static void
test_mu_util_guess_maildir_02 (void)
{
char *got, *mdir;
g_unsetenv ("MAILDIR");
mdir = g_strdup_printf ("%s%cMaildir",
getenv("HOME"), G_DIR_SEPARATOR);
got = mu_util_guess_maildir ();
if (access (mdir, F_OK) == 0)
g_assert_cmpstr (got, ==, mdir);
else
g_assert_cmpstr (got, == , NULL);
g_free (got);
g_free (mdir);
}
static void
test_mu_util_check_dir_01 (void)
{
if (g_access ("/usr/bin", F_OK) == 0) {
g_assert_cmpuint (
mu_util_check_dir ("/usr/bin", TRUE, FALSE) == TRUE,
==,
g_access ("/usr/bin", R_OK) == 0);
}
}
static void
test_mu_util_check_dir_02 (void)
{
if (g_access ("/tmp", F_OK) == 0) {
g_assert_cmpuint (
mu_util_check_dir ("/tmp", FALSE, TRUE) == TRUE,
==,
g_access ("/tmp", W_OK) == 0);
}
}
static void
test_mu_util_check_dir_03 (void)
{
if (g_access (".", F_OK) == 0) {
g_assert_cmpuint (
mu_util_check_dir (".", TRUE, TRUE) == TRUE,
==,
g_access (".", W_OK | R_OK) == 0);
}
}
static void
test_mu_util_check_dir_04 (void)
{
/* not a dir, so it must be false */
g_assert_cmpuint (
mu_util_check_dir ("test-util.c", TRUE, TRUE),
==,
FALSE);
}
static void
test_mu_util_get_dtype_with_lstat (void)
{
g_assert_cmpuint (
mu_util_get_dtype (MU_TESTMAILDIR, TRUE), ==, DT_DIR);
g_assert_cmpuint (
mu_util_get_dtype (MU_TESTMAILDIR2, TRUE), ==, DT_DIR);
g_assert_cmpuint (
mu_util_get_dtype (MU_TESTMAILDIR2 "/Foo/cur/mail5", TRUE),
==, DT_REG);
}
static void
test_mu_util_supports (void)
{
gboolean has_guile;
gchar *path;
has_guile = FALSE;
#ifdef BUILD_GUILE
has_guile = TRUE;
#endif /*BUILD_GUILE*/
g_assert_cmpuint (mu_util_supports (MU_FEATURE_GUILE), == ,has_guile);
path = g_find_program_in_path ("gnuplot");
g_free (path);
g_assert_cmpuint (mu_util_supports (MU_FEATURE_GNUPLOT),==,
path ? TRUE : FALSE);
g_assert_cmpuint (
mu_util_supports (MU_FEATURE_GNUPLOT|MU_FEATURE_GUILE),
==,
has_guile && path ? TRUE : FALSE);
}
static void
test_mu_util_program_in_path (void)
{
g_assert_cmpuint (mu_util_program_in_path("ls"),==,TRUE);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
/* mu_util_dir_expand */
g_test_add_func ("/mu-util/mu-util-dir-expand-00",
test_mu_util_dir_expand_00);
g_test_add_func ("/mu-util/mu-util-dir-expand-01",
test_mu_util_dir_expand_01);
/* mu_util_guess_maildir */
g_test_add_func ("/mu-util/mu-util-guess-maildir-01",
test_mu_util_guess_maildir_01);
g_test_add_func ("/mu-util/mu-util-guess-maildir-02",
test_mu_util_guess_maildir_02);
/* mu_util_check_dir */
g_test_add_func ("/mu-util/mu-util-check-dir-01",
test_mu_util_check_dir_01);
g_test_add_func ("/mu-util/mu-util-check-dir-02",
test_mu_util_check_dir_02);
g_test_add_func ("/mu-util/mu-util-check-dir-03",
test_mu_util_check_dir_03);
g_test_add_func ("/mu-util/mu-util-check-dir-04",
test_mu_util_check_dir_04);
g_test_add_func ("/mu-util/mu-util-get-dtype-with-lstat",
test_mu_util_get_dtype_with_lstat);
g_test_add_func ("/mu-util/mu-util-supports", test_mu_util_supports);
g_test_add_func ("/mu-util/mu-util-program-in-path",
test_mu_util_program_in_path);
return g_test_run ();
}

View File

@ -0,0 +1,59 @@
/*
** Copyright (C) 2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
** as published by the Free Software Foundation; either version 2.1
** of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free
** Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*/
#include "mu-utils.hh"
#include "mu-option.hh"
using namespace Mu;
static Option<int>
get_opt_int(bool b)
{
if (b)
return Some(123);
else
return Nothing;
}
static void
test_option()
{
{
const auto oi{get_opt_int(true)};
g_assert_true(!!oi);
g_assert_cmpint(oi.value(), ==, 123);
}
{
const auto oi{get_opt_int(false)};
g_assert_false(!!oi);
g_assert_false(oi.has_value());
g_assert_cmpint(oi.value_or(456), ==, 456);
}
}
int
main(int argc, char* argv[])
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/option/option", test_option);
return g_test_run();
}

View File

@ -0,0 +1,150 @@
/*
** Copyright (C) 2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
** as published by the Free Software Foundation; either version 2.1
** of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free
** Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*/
#include <vector>
#include <glib.h>
#include <iostream>
#include <sstream>
#include "mu-command-parser.hh"
#include "mu-utils.hh"
using namespace Mu;
static bool
check_parse(const std::string& expr, const std::string& expected)
{
try {
const auto parsed{to_string(Sexp::make_parse(expr))};
assert_equal(parsed, expected);
return true;
} catch (const Error& err) {
g_warning("caught exception parsing '%s': %s", expr.c_str(), err.what());
return false;
}
}
static void
test_parser()
{
check_parse(":foo-123", ":foo-123");
check_parse("foo", "foo");
check_parse(R"(12345)", "12345");
check_parse(R"(-12345)", "-12345");
check_parse(R"((123 bar "cuux"))", "(123 bar \"cuux\")");
check_parse(R"("foo\"bar\"cuux")", "\"foo\\\"bar\\\"cuux\"");
check_parse(R"("foo
bar")",
"\"foo\nbar\"");
}
static void
test_list()
{
const auto nstr{Sexp::make_string("foo")};
g_assert_true(nstr.value() == "foo");
g_assert_true(nstr.type() == Sexp::Type::String);
assert_equal(nstr.to_sexp_string(), "\"foo\"");
const auto nnum{Sexp::make_number(123)};
g_assert_true(nnum.value() == "123");
g_assert_true(nnum.type() == Sexp::Type::Number);
assert_equal(nnum.to_sexp_string(), "123");
const auto nsym{Sexp::make_symbol("blub")};
g_assert_true(nsym.value() == "blub");
g_assert_true(nsym.type() == Sexp::Type::Symbol);
assert_equal(nsym.to_sexp_string(), "blub");
Sexp::List list;
list.add(Sexp::make_string("foo"))
.add(Sexp::make_number(123))
.add(Sexp::make_symbol("blub"));
const auto nlst = Sexp::make_list(std::move(list));
g_assert_true(nlst.list().size() == 3);
g_assert_true(nlst.type() == Sexp::Type::List);
g_assert_true(nlst.list().at(1).value() == "123");
assert_equal(nlst.to_sexp_string(), "(\"foo\" 123 blub)");
}
static void
test_prop_list()
{
Sexp::List l1;
l1.add_prop(":foo", Sexp::make_string("bar"));
Sexp s2{Sexp::make_list(std::move(l1))};
assert_equal(s2.to_sexp_string(), "(:foo \"bar\")");
Sexp::List l2;
const std::string x{"bar"};
l2.add_prop(":foo", Sexp::make_string(x));
l2.add_prop(":bar", Sexp::make_number(77));
Sexp::List l3;
l3.add_prop(":cuux", Sexp::make_list(std::move(l2)));
Sexp s3{Sexp::make_list(std::move(l3))};
assert_equal(s3.to_sexp_string(), "(:cuux (:foo \"bar\" :bar 77))");
}
static void
test_props()
{
auto sexp2 = Sexp::make_list(Sexp::make_string("foo"),
Sexp::make_number(123),
Sexp::make_symbol("blub"));
auto sexp = Sexp::make_prop_list(":foo",
Sexp::make_string("bär"),
":cuux",
Sexp::make_number(123),
":flub",
Sexp::make_symbol("fnord"),
":boo",
std::move(sexp2));
assert_equal(sexp.to_sexp_string(),
"(:foo \"b\303\244r\" :cuux 123 :flub fnord :boo (\"foo\" 123 blub))");
}
int
main(int argc, char* argv[])
try {
g_test_init(&argc, &argv, NULL);
if (argc == 2) {
std::cout << Sexp::make_parse(argv[1]) << '\n';
return 0;
}
g_test_add_func("/utils/sexp/parser", test_parser);
g_test_add_func("/utils/sexp/list", test_list);
g_test_add_func("/utils/sexp/proplist", test_prop_list);
g_test_add_func("/utils/sexp/props", test_props);
return g_test_run();
} catch (const std::runtime_error& re) {
std::cerr << re.what() << "\n";
return 1;
}

View File

@ -0,0 +1,206 @@
/*
** Copyright (C) 2017 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
** as published by the Free Software Foundation; either version 2.1
** of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free
** Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*/
#include <vector>
#include <glib.h>
#include <iostream>
#include <sstream>
#include <functional>
#include "mu-utils.hh"
using namespace Mu;
struct Case {
const std::string expr;
bool is_first{};
const std::string expected;
};
using CaseVec = std::vector<Case>;
using ProcFunc = std::function<std::string(std::string, bool)>;
static void
test_cases(const CaseVec& cases, ProcFunc proc)
{
for (const auto& casus : cases) {
const auto res = proc(casus.expr, casus.is_first);
if (g_test_verbose()) {
std::cout << "\n";
std::cout << casus.expr << ' ' << casus.is_first << std::endl;
std::cout << "exp: '" << casus.expected << "'" << std::endl;
std::cout << "got: '" << res << "'" << std::endl;
}
g_assert_true(casus.expected == res);
}
}
static void
test_date_basic()
{
g_setenv("TZ", "Europe/Helsinki", TRUE);
CaseVec cases = {{"2015-09-18T09:10:23", true, "1442556623"},
{"1972-12-14T09:10:23", true, "0093165023"},
{"1854-11-18T17:10:23", true, "0000000000"},
{"2000-02-31T09:10:23", true, "0951861599"},
{"2000-02-29T23:59:59", true, "0951861599"},
{"2016", true, "1451599200"},
{"2016", false, "1483221599"},
{"fnorb", true, "0000000000"},
{"fnorb", false, "9999999999"},
{"", false, "9999999999"},
{"", true, "0000000000"}};
test_cases(cases, [](auto s, auto f) { return date_to_time_t_string(s, f); });
}
static void
test_date_ymwdhMs(void)
{
struct {
std::string expr;
long diff;
int tolerance;
} tests[] = {{"3h", 3 * 60 * 60, 1},
{"21d", 21 * 24 * 60 * 60, 3600 + 1},
{"2w", 2 * 7 * 24 * 60 * 60, 3600 + 1},
{"2y", 2 * 365 * 24 * 60 * 60, 24 * 3600 + 1},
{"3m", 3 * 30 * 24 * 60 * 60, 3 * 24 * 3600 + 1}};
for (auto i = 0; i != G_N_ELEMENTS(tests); ++i) {
const auto diff =
time(NULL) -
strtol(Mu::date_to_time_t_string(tests[i].expr, true).c_str(), NULL, 10);
if (g_test_verbose())
std::cerr << tests[i].expr << ' ' << diff << ' ' << tests[i].diff
<< std::endl;
g_assert_true(tests[i].diff - diff <= tests[i].tolerance);
}
g_assert_true(strtol(Mu::date_to_time_t_string("-1y", true).c_str(), NULL, 10) == 0);
}
static void
test_size()
{
CaseVec cases = {
{"456", true, "0000000456"},
{"", false, "9999999999"},
{"", true, "0000000000"},
};
test_cases(cases, [](auto s, auto f) { return size_to_string(s, f); });
}
static void
test_flatten()
{
CaseVec cases = {
{"Менделе́ев", true, "менделеев"},
{"", false, ""},
{"Ångström", true, "angstrom"},
};
test_cases(cases, [](auto s, auto f) { return utf8_flatten(s); });
}
static void
test_remove_ctrl()
{
CaseVec cases = {
{"Foo\n\nbar", true, "Foo bar"},
{"", false, ""},
{" ", false, " "},
{"Hello World ", false, "Hello World "},
{"Ångström", false, "Ångström"},
};
test_cases(cases, [](auto s, auto f) { return remove_ctrl(s); });
}
static void
test_clean()
{
CaseVec cases = {
{"\t a\t\nb ", true, "a b"},
{"", false, ""},
{"Ångström", true, "Ångström"},
};
test_cases(cases, [](auto s, auto f) { return utf8_clean(s); });
}
static void
test_format()
{
g_assert_true(format("hello %s", "world") == "hello world");
g_assert_true(format("hello %s, %u", "world", 123) == "hello world, 123");
}
enum struct Bits { None = 0, Bit1 = 1 << 0, Bit2 = 1 << 1 };
MU_ENABLE_BITOPS(Bits);
static void
test_define_bitmap()
{
g_assert_cmpuint((guint)Bits::None, ==, (guint)0);
g_assert_cmpuint((guint)Bits::Bit1, ==, (guint)1);
g_assert_cmpuint((guint)Bits::Bit2, ==, (guint)2);
g_assert_cmpuint((guint)(Bits::Bit1 | Bits::Bit2), ==, (guint)3);
g_assert_cmpuint((guint)(Bits::Bit1 & Bits::Bit2), ==, (guint)0);
g_assert_cmpuint((guint)(Bits::Bit1 & (~Bits::Bit2)), ==, (guint)1);
{
Bits b{Bits::Bit1};
b |= Bits::Bit2;
g_assert_cmpuint((guint)b, ==, (guint)3);
}
{
Bits b{Bits::Bit1};
b &= Bits::Bit1;
g_assert_cmpuint((guint)b, ==, (guint)1);
}
}
int
main(int argc, char* argv[])
{
g_test_init(&argc, &argv, NULL);
g_test_add_func("/utils/date-basic", test_date_basic);
g_test_add_func("/utils/date-ymwdhMs", test_date_ymwdhMs);
g_test_add_func("/utils/size", test_size);
g_test_add_func("/utils/flatten", test_flatten);
g_test_add_func("/utils/remove-ctrl", test_remove_ctrl);
g_test_add_func("/utils/clean", test_clean);
g_test_add_func("/utils/format", test_format);
g_test_add_func("/utils/define-bitmap", test_define_bitmap);
return g_test_run();
}