954201451b35c88bd9fbe29a4ba3f038edd635df
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_strbuf_t *buf;
113 size_t i;
115 struct {
116 sdb_data_t datum;
117 const char *expected;
118 } golden_data[] = {
119 {
120 { SDB_TYPE_INTEGER, { .integer = 4711 } },
121 "4711",
122 },
123 {
124 { SDB_TYPE_DECIMAL, { .decimal = 65536.0 } },
125 "0x1p+16",
126 },
127 {
128 { SDB_TYPE_STRING, { .string = NULL } },
129 "\"NULL\"",
130 },
131 {
132 { SDB_TYPE_STRING, { .string = "this is a test" } },
133 "\"this is a test\"",
134 },
135 {
136 { SDB_TYPE_STRING, { .string = "special \\ \" characters" } },
137 "\"special \\\\ \\\" characters\"",
138 },
139 {
140 { SDB_TYPE_DATETIME, { .datetime= 471147114711471100 } },
141 "\"1984-12-06 02:11:54 +0000\"",
142 },
143 {
144 { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
145 "\"\"",
146 },
147 {
148 {
149 SDB_TYPE_BINARY,
150 { .binary = { 12, (unsigned char *)"binary\0crap\x42" } },
151 },
152 "\"\\x62\\x69\\x6e\\x61\\x72\\x79\\x0\\x63\\x72\\x61\\x70\\x42\"",
153 },
154 };
156 buf = sdb_strbuf_create(1024);
157 fail_unless(buf != NULL,
158 "INTERNAL ERROR: Failed to allocate string buffer");
160 for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
161 const char *string;
162 int check;
164 check = sdb_data_format(&golden_data[i].datum, buf);
165 fail_unless(! check,
166 "sdb_data_format() = %d; expected: 0", check);
167 string = sdb_strbuf_string(buf);
168 fail_unless(! strcmp(string, golden_data[i].expected),
169 "sdb_data_format() used wrong format: %s; expected: %s",
170 string, golden_data[i].expected);
171 sdb_strbuf_clear(buf);
172 }
174 sdb_strbuf_destroy(buf);
175 }
176 END_TEST
178 Suite *
179 core_data_suite(void)
180 {
181 Suite *s = suite_create("core::data");
182 TCase *tc;
184 tc = tcase_create("core");
185 tcase_add_test(tc, test_data);
186 tcase_add_test(tc, test_format);
187 suite_add_tcase(s, tc);
189 return s;
190 } /* core_data_suite */
192 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */