1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
/*
* Copyright (C) Matthias Klumpp <matthias@tenstral.net>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#include <glib.h>
#include "btd-utils.h"
/**
* test_duration_parser:
*/
static void
test_duration_parser (void)
{
g_assert_cmpint (btd_parse_duration_string ("1h"), ==, 3600);
g_assert_cmpint (btd_parse_duration_string ("2h"), ==, 3600 * 2);
g_assert_cmpint (btd_parse_duration_string ("3"), ==, 3600 * 3);
g_assert_cmpint (btd_parse_duration_string ("1d"), ==, 86400);
g_assert_cmpint (btd_parse_duration_string ("4d"), ==, 86400 * 4);
g_assert_cmpint (btd_parse_duration_string ("1w"), ==, 604800);
g_assert_cmpint (btd_parse_duration_string ("4w"), ==, 604800 * 4);
g_assert_cmpint (btd_parse_duration_string ("1M"), ==, 2630016);
g_assert_cmpint (btd_parse_duration_string ("3M"), ==, 2630016 * 3);
g_assert_cmpint (btd_parse_duration_string ("notvalid"), ==, 0);
g_assert_cmpint (btd_parse_duration_string ("2u"), ==, 0);
}
/**
* test_render_template:
*/
static void
test_render_template (void)
{
const gchar
*template1 = "This is a {{key1}} template\n"
"All strings need to be {{action}} correctly for the {{test_name}} to pass.";
const gchar *
result1 = "This is a good template\n"
"All strings need to be rendered correctly for the render_template test to pass.";
g_autofree gchar *tmp = NULL;
tmp = btd_render_template (template1,
"key1",
"good",
"action",
"rendered",
"test_name",
"render_template test",
NULL);
g_assert_cmpstr (tmp, ==, result1);
}
/**
* test_path_escape:
*/
static void
test_path_escape (void)
{
gchar *tmp;
tmp = btd_path_to_filename ("/");
g_assert_cmpstr (tmp, ==, "-");
g_free (tmp);
tmp = btd_path_to_filename ("");
g_assert_cmpstr (tmp, ==, "-");
g_free (tmp);
tmp = btd_path_to_filename ("/this/is/a path with/spaces/.txt");
g_assert_cmpstr (tmp, ==, "this-is-a path with-spaces-.txt_4128569403");
g_free (tmp);
tmp = btd_path_to_filename ("..");
g_assert_cmpstr (tmp, ==, "-");
g_free (tmp);
tmp = btd_path_to_filename ("/../../.");
g_assert_cmpstr (tmp, ==, "-");
g_free (tmp);
tmp = btd_path_to_filename ("/a/cräzü/path----/x/../txt");
g_assert_cmpstr (tmp, ==, "a-cräzü-path-----txt_3474729208");
g_free (tmp);
tmp = btd_path_to_filename ("/a-b/c");
g_assert_cmpstr (tmp, ==, "a-b-c_2088179606");
g_free (tmp);
tmp = btd_path_to_filename ("/a/b/c");
g_assert_cmpstr (tmp, ==, "a-b-c_2088251480");
g_free (tmp);
}
/**
* test_humanize_time:
*/
static void
test_humanize_time (void)
{
g_autofree gchar *result = NULL;
result = btd_humanize_time (0);
g_assert_cmpstr (result, ==, "Never");
g_clear_pointer (&result, g_free);
result = btd_humanize_time (5);
g_assert_cmpstr (result, ==, "5 seconds");
g_clear_pointer (&result, g_free);
result = btd_humanize_time (1);
g_assert_cmpstr (result, ==, "1 second");
g_clear_pointer (&result, g_free);
result = btd_humanize_time (70);
g_assert_cmpstr (result, ==, "1 minute 10 seconds");
g_clear_pointer (&result, g_free);
result = btd_humanize_time (120);
g_assert_cmpstr (result, ==, "2 minutes");
g_clear_pointer (&result, g_free);
result = btd_humanize_time (3600);
g_assert_cmpstr (result, ==, "1 hour");
g_clear_pointer (&result, g_free);
result = btd_humanize_time (3660);
g_assert_cmpstr (result, ==, "1 hour 1 minute");
g_clear_pointer (&result, g_free);
result = btd_humanize_time (SECONDS_IN_A_DAY);
g_assert_cmpstr (result, ==, "1 day");
g_clear_pointer (&result, g_free);
result = btd_humanize_time (SECONDS_IN_A_DAY + SECONDS_IN_AN_HOUR);
g_assert_cmpstr (result, ==, "1 day 1 hour");
g_clear_pointer (&result, g_free);
result = btd_humanize_time (SECONDS_IN_A_MONTH);
g_assert_cmpstr (result, ==, "1 month");
g_clear_pointer (&result, g_free);
result = btd_humanize_time (SECONDS_IN_A_MONTH + SECONDS_IN_A_DAY);
g_assert_cmpstr (result, ==, "1 month 1 day");
g_clear_pointer (&result, g_free);
}
int
main (int argc, char **argv)
{
int ret;
if (argc == 0) {
g_error ("No test directory specified!");
return 1;
}
g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
g_test_init (&argc, &argv, NULL);
/* only critical and error are fatal */
g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
g_test_add_func ("/Btrfsd/Misc/DurationParser", test_duration_parser);
g_test_add_func ("/Btrfsd/Misc/RenderTemplate", test_render_template);
g_test_add_func ("/Btrfsd/Misc/PathEscape", test_path_escape);
g_test_add_func ("/Btrfsd/Misc/HumanizeTime", test_humanize_time);
ret = g_test_run ();
return ret;
}
|