blob: 28b6f59d8c70807c96357c3a200f2d08f07ba596 [file] [log] [blame]
Radek Krejcie84f12f2018-09-18 14:11:50 +02001/*
2 * @file set.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from set.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#define _BSD_SOURCE
16#define _DEFAULT_SOURCE
17#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include <string.h>
23
24#include "libyang.h"
25#include "../../src/set.c"
26
27#define BUFSIZE 1024
28char logbuf[BUFSIZE] = {0};
29
30static void
31logger(LY_LOG_LEVEL level, const char *msg, const char *path)
32{
33 (void) level; /* unused */
34 (void) path; /* unused */
35
36 strncpy(logbuf, msg, BUFSIZE - 1);
37}
38
39static int
40logger_setup(void **state)
41{
42 (void) state; /* unused */
43
44 ly_set_log_clb(logger, 0);
45
46 return 0;
47}
48
49static void
50test_basics(void **state)
51{
52 (void) state; /* unused */
53
54 struct ly_set *set;
55 char *str;
56 unsigned int u;
57 void *ptr;
58
59 /* creation - everything is empty */
60 set = ly_set_new();
61 assert_non_null(set);
62 assert_int_equal(0, set->count);
63 assert_int_equal(0, set->size);
64 assert_null(set->objs);
65
66 /* add a testing object */
67 str = strdup("test string");
68 assert_non_null(str);
69
70 ly_set_add(set, str, 0);
71 assert_int_not_equal(0, set->size);
72 assert_int_equal(1, set->count);
73 assert_non_null(set->objs);
74 assert_non_null(set->objs[0]);
75
76 /* check the presence of the testing data */
77 assert_int_equal(0, ly_set_contains(set, str));
78 assert_int_equal(-1, ly_set_contains(set, str - 1));
79
80 /* remove data, but keep the set */
81 u = set->size;
82 ptr = set->objs;
83 ly_set_clean(set, free);
84 assert_int_equal(0, set->count);
85 assert_int_equal(u, set->size);
86 assert_ptr_equal(ptr, set->objs);
87
88 /* remove buffer, but keep the set object */
89 ly_set_erase(set, NULL);
90 assert_int_equal(0, set->count);
91 assert_int_equal(0, set->size);
92 assert_ptr_equal(NULL, set->objs);
93
94 /* final cleanup */
95 ly_set_free(set, NULL);
96}
97
98static void
99test_inval(void **state)
100{
101 struct ly_set set;
102 memset(&set, 0, sizeof set);
103
104 ly_set_clean(NULL, NULL);
105 assert_string_equal(logbuf, "Invalid argument set (ly_set_clean()).");
106
107 ly_set_erase(NULL, NULL);
108 assert_string_equal(logbuf, "Invalid argument set (ly_set_erase()).");
109
110 ly_set_free(NULL, NULL);
111 assert_string_equal(logbuf, "Invalid argument set (ly_set_free()).");
112
113 assert_null(ly_set_dup(NULL));
114 assert_string_equal(logbuf, "Invalid argument set (ly_set_dup()).");
115
116 assert_int_equal(-1, ly_set_add(NULL, NULL, 0));
117 assert_string_equal(logbuf, "Invalid argument set (ly_set_add()).");
118 assert_int_equal(-1, ly_set_add(&set, NULL, 0));
119 assert_string_equal(logbuf, "Invalid argument object (ly_set_add()).");
120
121 assert_int_equal(-1, ly_set_merge(NULL, NULL, 0));
122 assert_string_equal(logbuf, "Invalid argument trg (ly_set_merge()).");
123 assert_int_equal(0, ly_set_merge(&set, NULL, 0));
124 assert_string_equal(logbuf, "Invalid argument src (ly_set_merge()).");
125
126 assert_int_equal(LY_EINVAL, ly_set_rm_index(NULL, 0));
127 assert_string_equal(logbuf, "Invalid argument set (ly_set_rm_index()).");
128 assert_int_equal(LY_EINVAL, ly_set_rm_index(&set, 1));
129 assert_string_equal(logbuf, "Invalid argument index (ly_set_rm_index()).");
130
131 assert_int_equal(LY_EINVAL, ly_set_rm(NULL, NULL));
132 assert_string_equal(logbuf, "Invalid argument set (ly_set_rm()).");
133 assert_int_equal(LY_EINVAL, ly_set_rm(&set, NULL));
134 assert_string_equal(logbuf, "Invalid argument object (ly_set_rm()).");
135 assert_int_equal(LY_EINVAL, ly_set_rm(&set, &state));
136 assert_string_equal(logbuf, "Invalid argument object (ly_set_rm()).");
137}
138
139int main(void)
140{
141 const struct CMUnitTest tests[] = {
142 cmocka_unit_test(test_basics),
143 cmocka_unit_test_setup(test_inval, logger_setup),
144 };
145
146 return cmocka_run_group_tests(tests, NULL, NULL);
147}