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 Suite *
111 core_data_suite(void)
112 {
113 Suite *s = suite_create("core::data");
114 TCase *tc;
116 tc = tcase_create("core");
117 tcase_add_test(tc, test_data);
118 suite_add_tcase(s, tc);
120 return s;
121 } /* core_data_suite */
123 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */