fb3b1145bac8001458b95b598a348c92dc15ca20
1 /*
2 * SysDB - t/core/data_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/data.h"
29 #include "libsysdb_test.h"
31 #include <check.h>
33 START_TEST(test_data)
34 {
35 sdb_data_t d1, d2;
36 int check;
38 d2.type = SDB_TYPE_INTEGER;
39 d2.data.integer = 4711;
40 check = sdb_data_copy(&d1, &d2);
41 fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
42 fail_unless(d1.type == d2.type,
43 "sdb_data_copy() didn't copy type; got: %i; expected: %i",
44 d1.type, d2.type);
45 fail_unless(d1.data.integer == d2.data.integer,
46 "sdb_data_copy() didn't copy integer data: got: %d; expected: %d",
47 d1.data.integer, d2.data.integer);
49 d2.type = SDB_TYPE_DECIMAL;
50 d2.data.decimal = 47.11;
51 check = sdb_data_copy(&d1, &d2);
52 fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
53 fail_unless(d1.type == d2.type,
54 "sdb_data_copy() didn't copy type; got: %i; expected: %i",
55 d1.type, d2.type);
56 fail_unless(d1.data.decimal == d2.data.decimal,
57 "sdb_data_copy() didn't copy decimal data: got: %f; expected: %f",
58 d1.data.decimal, d2.data.decimal);
60 d2.type = SDB_TYPE_STRING;
61 d2.data.string = "some string";
62 check = sdb_data_copy(&d1, &d2);
63 fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
64 fail_unless(d1.type == d2.type,
65 "sdb_data_copy() didn't copy type; got: %i; expected: %i",
66 d1.type, d2.type);
67 fail_unless(!strcmp(d1.data.string, d2.data.string),
68 "sdb_data_copy() didn't copy string data: got: %s; expected: %s",
69 d1.data.string, d2.data.string);
71 sdb_data_free_datum(&d1);
72 fail_unless(d1.data.string == NULL,
73 "sdb_data_free_datum() didn't free string data");
75 d2.type = SDB_TYPE_DATETIME;
76 d2.data.datetime = 4711;
77 check = sdb_data_copy(&d1, &d2);
78 fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
79 fail_unless(d1.type == d2.type,
80 "sdb_data_copy() didn't copy type; got: %i; expected: %i",
81 d1.type, d2.type);
82 fail_unless(d1.data.datetime == d2.data.datetime,
83 "sdb_data_copy() didn't copy datetime data: got: %d; expected: %d",
84 d1.data.datetime, d2.data.datetime);
86 d2.type = SDB_TYPE_BINARY;
87 d2.data.binary.datum = (unsigned char *)"some string";
88 d2.data.binary.length = strlen((const char *)d2.data.binary.datum);
89 check = sdb_data_copy(&d1, &d2);
90 fail_unless(!check, "sdb_data_copy() = %i; expected: 0", check);
91 fail_unless(d1.type == d2.type,
92 "sdb_data_copy() didn't copy type; got: %i; expected: %i",
93 d1.type, d2.type);
94 fail_unless(d1.data.binary.length == d2.data.binary.length,
95 "sdb_data_copy() didn't copy length; got: %d; expected: 5d",
96 d1.data.binary.length, d2.data.binary.length);
97 fail_unless(!memcmp(d1.data.binary.datum, d2.data.binary.datum,
98 d2.data.binary.length),
99 "sdb_data_copy() didn't copy binary data: got: %s; expected: %s",
100 d1.data.string, d2.data.string);
102 sdb_data_free_datum(&d1);
103 fail_unless(d1.data.binary.length == 0,
104 "sdb_data_free_datum() didn't reset binary datum length");
105 fail_unless(d1.data.binary.datum == NULL,
106 "sdb_data_free_datum() didn't free binary datum");
107 }
108 END_TEST
110 START_TEST(test_format)
111 {
112 sdb_data_t datum;
113 sdb_strbuf_t *buf;
114 const char *string;
115 const char *expected;
117 int check;
119 buf = sdb_strbuf_create(1024);
120 fail_unless(buf != NULL,
121 "INTERNAL ERROR: Failed to allocate string buffer");
123 datum.type = SDB_TYPE_INTEGER;
124 datum.data.integer = 4711;
125 check = sdb_data_format(&datum, buf);
126 fail_unless(! check,
127 "sdb_data_format(INTEGER) = %d; expected: 0", check);
128 string = sdb_strbuf_string(buf);
129 expected = "4711";
130 fail_unless(! strcmp(string, expected),
131 "sdb_data_format() used wrong format: %s; expected: %s",
132 string, expected);
134 datum.type = SDB_TYPE_DECIMAL;
135 datum.data.decimal = 65536.0;
136 sdb_strbuf_clear(buf);
137 check = sdb_data_format(&datum, buf);
138 fail_unless(! check,
139 "sdb_data_format(DECIMAL) = %d; expected: 0", check);
140 string = sdb_strbuf_string(buf);
141 expected = "0x1p+16";
142 fail_unless(! strcmp(string, expected),
143 "sdb_data_format() used wrong format: %s; expected: %s",
144 string, expected);
146 datum.type = SDB_TYPE_STRING;
147 datum.data.string = "this is a test";
148 sdb_strbuf_clear(buf);
149 check = sdb_data_format(&datum, buf);
150 fail_unless(! check,
151 "sdb_data_format(STRING) = %d; expected: 0", check);
152 string = sdb_strbuf_string(buf);
153 expected = "\"this is a test\"";
154 fail_unless(! strcmp(string, expected),
155 "sdb_data_format() used wrong format: %s; expected: %s",
156 string, expected);
158 datum.type = SDB_TYPE_DATETIME;
159 datum.data.datetime = 471147114711471100;
160 sdb_strbuf_clear(buf);
161 check = sdb_data_format(&datum, buf);
162 fail_unless(! check,
163 "sdb_data_format(DATETIME) = %d; expected: 0", check);
164 string = sdb_strbuf_string(buf);
165 expected = "1984-12-06 02:11:54 +0000";
166 fail_unless(! strcmp(string, expected),
167 "sdb_data_format() used wrong format: %s; expected: %s",
168 string, expected);
170 datum.type = SDB_TYPE_BINARY;
171 datum.data.binary.datum = (unsigned char *)"binary\0crap\x42";
172 datum.data.binary.length = 12;
173 sdb_strbuf_clear(buf);
174 check = sdb_data_format(&datum, buf);
175 fail_unless(! check,
176 "sdb_data_format(BINARY) = %d; expected: 0", check);
177 string = sdb_strbuf_string(buf);
178 expected = "\"\\62\\69\\6e\\61\\72\\79\\0\\63\\72\\61\\70\\42\"";
179 fail_unless(! strcmp(string, expected),
180 "sdb_data_format() used wrong format: %s; expected: %s",
181 string, expected);
182 }
183 END_TEST
185 Suite *
186 core_data_suite(void)
187 {
188 Suite *s = suite_create("core::data");
189 TCase *tc;
191 tc = tcase_create("core");
192 tcase_add_test(tc, test_data);
193 tcase_add_test(tc, test_format);
194 suite_add_tcase(s, tc);
196 return s;
197 } /* core_data_suite */
199 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */