Code

data: Correctly format NULL values.
[sysdb.git] / t / core / data_test.c
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");
108 END_TEST
110 START_TEST(test_format)
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.data.string = NULL;
159         sdb_strbuf_clear(buf);
160         check = sdb_data_format(&datum, buf);
161         fail_unless(! check,
162                         "sdb_data_format(STRING) = %d; expected: 0", check);
163         string = sdb_strbuf_string(buf);
164         expected = "\"NULL\"";
165         fail_unless(! strcmp(string, expected),
166                         "sdb_data_format() used wrong format: %s; expected: %s",
167                         string, expected);
169         datum.type = SDB_TYPE_DATETIME;
170         datum.data.datetime = 471147114711471100;
171         sdb_strbuf_clear(buf);
172         check = sdb_data_format(&datum, buf);
173         fail_unless(! check,
174                         "sdb_data_format(DATETIME) = %d; expected: 0", check);
175         string = sdb_strbuf_string(buf);
176         expected = "\"1984-12-06 02:11:54 +0000\"";
177         fail_unless(! strcmp(string, expected),
178                         "sdb_data_format() used wrong format: %s; expected: %s",
179                         string, expected);
181         datum.type = SDB_TYPE_BINARY;
182         datum.data.binary.datum = (unsigned char *)"binary\0crap\x42";
183         datum.data.binary.length = 12;
184         sdb_strbuf_clear(buf);
185         check = sdb_data_format(&datum, buf);
186         fail_unless(! check,
187                         "sdb_data_format(BINARY) = %d; expected: 0", check);
188         string = sdb_strbuf_string(buf);
189         expected =
190                 "\"\\x62\\x69\\x6e\\x61\\x72\\x79\\x0\\x63\\x72\\x61\\x70\\x42\"";
191         fail_unless(! strcmp(string, expected),
192                         "sdb_data_format() used wrong format: %s; expected: %s",
193                         string, expected);
195         datum.data.binary.datum = NULL;
196         datum.data.binary.length = 0;
197         sdb_strbuf_clear(buf);
198         check = sdb_data_format(&datum, buf);
199         fail_unless(! check,
200                         "sdb_data_format(BINARY) = %d; expected: 0", check);
201         string = sdb_strbuf_string(buf);
202         expected = "\"\"";
203         fail_unless(! strcmp(string, expected),
204                         "sdb_data_format() used wrong format: %s; expected: %s",
205                         string, expected);
207 END_TEST
209 Suite *
210 core_data_suite(void)
212         Suite *s = suite_create("core::data");
213         TCase *tc;
215         tc = tcase_create("core");
216         tcase_add_test(tc, test_data);
217         tcase_add_test(tc, test_format);
218         suite_add_tcase(s, tc);
220         return s;
221 } /* core_data_suite */
223 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */