Code

19b2cc081e4027665ef9310b06d6df1d148a2409
[sysdb.git] / t / unit / core / time_test.c
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 #include "core/time.h"
29 #include "libsysdb_test.h"
31 #include <check.h>
33 #define YEAR  3652425L   * 24L * 3600L * 100000L
34 #define MONTH  30436875L * 24L * 3600L * 1000L
35 #define DAY                24L * 3600L * 1000000000L
36 #define HOUR                     3600L * 1000000000L
37 #define MINUTE                     60L * 1000000000L
38 #define SECOND                           1000000000L
39 #define MS                                  1000000L
40 #define US                                     1000L
41 #define NS                                        1L
43 struct {
44         sdb_time_t  interval;
45         const char *expected;
46 } strfinterval_data[] = {
47         { 0,                    "0s" },
48         { 4711,                 ".000004711s" },
49         { 1000123400,           "1.0001234s" },
50         { 47940228000000000L,   "1Y6M7D" },
51         { YEAR,                 "1Y" },
52         { MONTH,                "1M" },
53         { DAY,                  "1D" },
54         { HOUR,                 "1h" },
55         { MINUTE,               "1m" },
56         { SECOND,               "1s" },
57         { YEAR
58           + MONTH
59           + DAY
60           + HOUR
61           + MINUTE
62           + SECOND
63           + 1234,               "1Y1M1D1h1m1.000001234s" },
64 };
66 START_TEST(test_strfinterval)
67 {
68         char buf[1024];
69         size_t check;
71         /* this should return the number of bytes which would have been written;
72          * in fact, it might return a bit more because it cannot detect trailing
73          * zeroes; even more importantly, it should not segfault ;-) */
74         check = sdb_strfinterval(NULL, 0, strfinterval_data[_i].interval);
75         fail_unless(check >= strlen(strfinterval_data[_i].expected),
76                         "sdb_strfinterval(NULL, 0, %"PRIsdbTIME") = %zu; expected: %zu",
77                         strfinterval_data[_i].interval, check,
78                         strlen(strfinterval_data[_i].expected));
80         check = sdb_strfinterval(buf, sizeof(buf), strfinterval_data[_i].interval);
81         fail_unless(check > 0,
82                         "sdb_strfinterval(<buf>, <size>, %"PRIsdbTIME") = %zu; "
83                         "expected: >0", strfinterval_data[_i].interval, check);
84         fail_unless(!strcmp(buf, strfinterval_data[_i].expected),
85                         "sdb_strfinterval(<buf>, <size>, %"PRIsdbTIME") did not "
86                         "format interval correctly; got: '%s'; expected: '%s'",
87                         strfinterval_data[_i].interval, buf, strfinterval_data[_i].expected);
88         fail_unless(check == strlen(strfinterval_data[_i].expected),
89                         "sdb_strfinterval(<buf>, <size>, %"PRIsdbTIME") = %zu; "
90                         "expected: %zu", strfinterval_data[_i].interval, check,
91                         strlen(strfinterval_data[_i].expected));
92 }
93 END_TEST
95 struct {
96         const char *s;
97         sdb_time_t expected;
98 } strpunit_data[] = {
99         { "Y",  YEAR },
100         { "M",  MONTH },
101         { "D",  DAY },
102         { "h",  HOUR },
103         { "m",  MINUTE },
104         { "s",  SECOND },
105         { "ms", MS },
106         { "us", US },
107         { "ns", NS },
108         /* invalid units */
109         { "y",  0 },
110         { "d",  0 },
111         { "H",  0 },
112         { "S",  0 },
113         { "ps", 0 },
114 };
116 START_TEST(test_strpunit)
118         sdb_time_t check = sdb_strpunit(strpunit_data[_i].s);
120         fail_unless(check == strpunit_data[_i].expected,
121                         "sdb_strpunit(%s) = %"PRIsdbTIME"; expected: %"PRIsdbTIME,
122                         strpunit_data[_i].s, check, strpunit_data[_i].expected);
124 END_TEST
126 Suite *
127 core_time_suite(void)
129         Suite *s = suite_create("core::time");
130         TCase *tc;
132         tc = tcase_create("core");
133         TC_ADD_LOOP_TEST(tc, strfinterval);
134         TC_ADD_LOOP_TEST(tc, strpunit);
135         suite_add_tcase(s, tc);
137         return s;
138 } /* core_time_suite */
140 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */