1 /*
2 * SysDB - t/unit/core/time_test.c
3 * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "core/time.h"
33 #include "testutils.h"
35 #include <check.h>
37 #define YEAR 3652425L * 24L * 3600L * 100000L
38 #define MONTH 30436875L * 24L * 3600L * 1000L
39 #define DAY 24L * 3600L * 1000000000L
40 #define HOUR 3600L * 1000000000L
41 #define MINUTE 60L * 1000000000L
42 #define SECOND 1000000000L
43 #define MS 1000000L
44 #define US 1000L
45 #define NS 1L
47 struct {
48 sdb_time_t interval;
49 const char *expected;
50 } strfinterval_data[] = {
51 { 0, "0s" },
52 { 4711, ".000004711s" },
53 { 1000123400, "1.0001234s" },
54 { 47940228000000000L, "1Y6M7D" },
55 { YEAR, "1Y" },
56 { MONTH, "1M" },
57 { DAY, "1D" },
58 { HOUR, "1h" },
59 { MINUTE, "1m" },
60 { SECOND, "1s" },
61 { YEAR
62 + MONTH
63 + DAY
64 + HOUR
65 + MINUTE
66 + SECOND
67 + 1234, "1Y1M1D1h1m1.000001234s" },
68 };
70 START_TEST(test_strfinterval)
71 {
72 char buf[1024];
73 size_t check;
75 /* this should return the number of bytes which would have been written;
76 * in fact, it might return a bit more because it cannot detect trailing
77 * zeroes; even more importantly, it should not segfault ;-) */
78 check = sdb_strfinterval(NULL, 0, strfinterval_data[_i].interval);
79 fail_unless(check >= strlen(strfinterval_data[_i].expected),
80 "sdb_strfinterval(NULL, 0, %"PRIsdbTIME") = %zu; expected: %zu",
81 strfinterval_data[_i].interval, check,
82 strlen(strfinterval_data[_i].expected));
84 check = sdb_strfinterval(buf, sizeof(buf), strfinterval_data[_i].interval);
85 fail_unless(check > 0,
86 "sdb_strfinterval(<buf>, <size>, %"PRIsdbTIME") = %zu; "
87 "expected: >0", strfinterval_data[_i].interval, check);
88 fail_unless(!strcmp(buf, strfinterval_data[_i].expected),
89 "sdb_strfinterval(<buf>, <size>, %"PRIsdbTIME") did not "
90 "format interval correctly; got: '%s'; expected: '%s'",
91 strfinterval_data[_i].interval, buf, strfinterval_data[_i].expected);
92 fail_unless(check == strlen(strfinterval_data[_i].expected),
93 "sdb_strfinterval(<buf>, <size>, %"PRIsdbTIME") = %zu; "
94 "expected: %zu", strfinterval_data[_i].interval, check,
95 strlen(strfinterval_data[_i].expected));
96 }
97 END_TEST
99 struct {
100 const char *s;
101 sdb_time_t expected;
102 } strpunit_data[] = {
103 { "Y", YEAR },
104 { "M", MONTH },
105 { "D", DAY },
106 { "h", HOUR },
107 { "m", MINUTE },
108 { "s", SECOND },
109 { "ms", MS },
110 { "us", US },
111 { "ns", NS },
112 /* invalid units */
113 { "y", 0 },
114 { "d", 0 },
115 { "H", 0 },
116 { "S", 0 },
117 { "ps", 0 },
118 };
120 START_TEST(test_strpunit)
121 {
122 sdb_time_t check = sdb_strpunit(strpunit_data[_i].s);
124 fail_unless(check == strpunit_data[_i].expected,
125 "sdb_strpunit(%s) = %"PRIsdbTIME"; expected: %"PRIsdbTIME,
126 strpunit_data[_i].s, check, strpunit_data[_i].expected);
127 }
128 END_TEST
130 TEST_MAIN("core::time")
131 {
132 TCase *tc = tcase_create("core");
133 TC_ADD_LOOP_TEST(tc, strfinterval);
134 TC_ADD_LOOP_TEST(tc, strpunit);
135 ADD_TCASE(tc);
136 }
137 TEST_MAIN_END
139 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */