1 /*
2  * Copyright (c) 2010 Red Hat, Inc.
3  *
4  * All rights reserved.
5  *
6  * Author: Angus Salkeld <asalkeld@redhat.com>
7  *
8  * This file is part of libqb.
9  *
10  * libqb is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation, either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * libqb is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with libqb.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "os_base.h"
25 
26 #include "check_common.h"
27 
28 #include <qb/qbdefs.h>
29 #include <qb/qblog.h>
30 #include <qb/qbarray.h>
31 
32 struct test_my_st {
33 	int32_t a;
34 	int32_t b;
35 	int32_t c;
36 	int32_t d;
37 };
38 
39 
START_TEST(test_array_limits)40 START_TEST(test_array_limits)
41 {
42 	qb_array_t *a;
43 	int32_t res;
44 	struct test_my_st *st;
45 
46 	a = qb_array_create(INT_MAX, sizeof(struct test_my_st));
47 	ck_assert(a == NULL);
48 	a = qb_array_create(-56, sizeof(struct test_my_st));
49 	ck_assert(a == NULL);
50 	a = qb_array_create(67, 0);
51 	ck_assert(a == NULL);
52 
53 	/* working array */
54 	a = qb_array_create(10, sizeof(struct test_my_st));
55 	ck_assert(a != NULL);
56 
57 	/* out-of-bounds */
58 	res = qb_array_index(a, 10, (void**)&st);
59 	ck_assert_int_eq(res, -ERANGE);
60 	res = qb_array_index(a, -10, (void**)&st);
61 	ck_assert_int_eq(res, -ERANGE);
62 	res = qb_array_index(NULL, 1, (void**)&st);
63 	ck_assert_int_eq(res, -EINVAL);
64 	res = qb_array_index(a, -10, NULL);
65 	ck_assert_int_eq(res, -EINVAL);
66 
67 	qb_array_free(a);
68 }
69 END_TEST
70 
START_TEST(test_array_alloc_free)71 START_TEST(test_array_alloc_free)
72 {
73 	qb_array_t *a;
74 	a = qb_array_create(65536, sizeof(struct test_my_st));
75 	qb_array_free(a);
76 }
77 END_TEST
78 
START_TEST(test_array_correct_retrieval)79 START_TEST(test_array_correct_retrieval)
80 {
81 	qb_array_t *a;
82 	int32_t i;
83 	int32_t res;
84 	struct test_my_st *st;
85 
86 	a = qb_array_create(112, sizeof(struct test_my_st));
87 
88 	for (i = 0; i < 112; i++) {
89 		res = qb_array_index(a, i, (void**)&st);
90 		ck_assert_int_eq(res, 0);
91 		st->a = i;
92 		st->b = i+1;
93 		st->c = i+2;
94 		st->d = i+3;
95 	}
96 	/* read back */
97 	for (i = 0; i < 112; i++) {
98 		res = qb_array_index(a, i, (void**)&st);
99 		ck_assert_int_eq(res, 0);
100 		ck_assert_int_eq(st->a, i);
101 		ck_assert_int_eq(st->b, i+1);
102 		ck_assert_int_eq(st->c, i+2);
103 		ck_assert_int_eq(st->d, i+3);
104 	}
105 
106 	qb_array_free(a);
107 }
108 END_TEST
109 
START_TEST(test_array_static_memory)110 START_TEST(test_array_static_memory)
111 {
112 	qb_array_t *a;
113 	int32_t res;
114 	struct test_my_st *st_old;
115 	struct test_my_st *st;
116 
117 	a = qb_array_create(112, sizeof(struct test_my_st));
118 
119 	res = qb_array_index(a, 99, (void**)&st_old);
120 	ck_assert_int_eq(res, 0);
121 
122 	res = qb_array_grow(a, 1453);
123 	ck_assert_int_eq(res, 0);
124 
125 	res = qb_array_index(a, 345, (void**)&st);
126 	ck_assert_int_eq(res, 0);
127 	st->a = 411;
128 
129 	/* confirm the pointer is the same after a grow */
130 	res = qb_array_index(a, 99, (void**)&st);
131 	ck_assert_int_eq(res, 0);
132 	ck_assert(st == st_old);
133 
134 	qb_array_free(a);
135 }
136 END_TEST
137 
array_suite(void)138 static Suite *array_suite(void)
139 {
140 	TCase *tc;
141 	Suite *s = suite_create("qb_array");
142 
143 	add_tcase(s, tc, test_array_limits);
144 	add_tcase(s, tc, test_array_alloc_free);
145 	add_tcase(s, tc, test_array_correct_retrieval);
146 	add_tcase(s, tc, test_array_static_memory);
147 
148 	return s;
149 }
150 
main(void)151 int32_t main(void)
152 {
153 	int32_t number_failed;
154 
155 	Suite *s = array_suite();
156 	SRunner *sr = srunner_create(s);
157 
158 	qb_log_init("check", LOG_USER, LOG_EMERG);
159 	atexit(qb_log_fini);
160 	qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
161 	qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
162 			  QB_LOG_FILTER_FILE, "*", LOG_INFO);
163 	qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
164 
165 	srunner_run_all(sr, CK_VERBOSE);
166 	number_failed = srunner_ntests_failed(sr);
167 	srunner_free(sr);
168 	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
169 }
170