1 /*
2  * Copyright (c) 2018 Red Hat, Inc.
3  *
4  * All rights reserved.
5  *
6  * Author: Jan Pokorny <jpokorny@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/qblist.h>
29 #include <qb/qblog.h>
30 
31 typedef struct {
32 	struct qb_list_head list;
33 	size_t i;
34 } enlistable_num_t;
35 
36 #define DIMOF(_a) sizeof(_a)/sizeof(*(_a))
37 
START_TEST(test_list_iter)38 START_TEST(test_list_iter)
39 {
40 	QB_LIST_DECLARE(mylist);
41 	enlistable_num_t reference_head[] = { {.i=0}, {.i=1}, {.i=2}, {.i=3} };
42 	enlistable_num_t reference_tail[] = { {.i=4}, {.i=5}, {.i=6}, {.i=7} };
43 	enlistable_num_t *iter, replacement = {.i=8};
44 	size_t iter_i;
45 
46 	for (iter_i = DIMOF(reference_head); iter_i > 0; iter_i--) {
47 		/* prepends in reverse order */
48 		qb_list_add(&reference_head[iter_i-1].list, &mylist);
49 	}
50 	for (iter_i = 0; iter_i < DIMOF(reference_tail); iter_i++) {
51 		/* appends in natural order */
52 		qb_list_add_tail(&reference_tail[iter_i].list, &mylist);
53 	}
54 
55 	/* assert the constructed list corresponds to ordered sequence... */
56 
57 	/* ... increasing when iterating forward */
58 	iter_i = 0;
59 	qb_list_for_each_entry(iter, &mylist, list) {
60 		ck_assert_int_eq(iter->i, iter_i);
61 		iter_i++;
62 	}
63 
64 	/* ... and decreasing when iterating backward */
65 	qb_list_for_each_entry_reverse(iter, &mylist, list) {
66 		ck_assert_int_gt(iter_i, 0);
67 		ck_assert_int_eq(iter->i, iter_i-1);
68 		iter_i--;
69 	}
70 	ck_assert_int_eq(iter_i, 0);
71 
72 	/* also check qb_list_replace and qb_list_first_entry */
73 	qb_list_replace(mylist.next, &replacement.list);
74 	ck_assert_int_eq(qb_list_first_entry(&mylist, enlistable_num_t, list)->i,
75 	                  replacement.i);
76 }
77 END_TEST
78 
array_suite(void)79 static Suite *array_suite(void)
80 {
81 	TCase *tc;
82 	Suite *s = suite_create("qb_list");
83 
84 	add_tcase(s, tc, test_list_iter);
85 
86 	return s;
87 }
88 
main(void)89 int32_t main(void)
90 {
91 	int32_t number_failed;
92 
93 	Suite *s = array_suite();
94 	SRunner *sr = srunner_create(s);
95 
96 	qb_log_init("check", LOG_USER, LOG_EMERG);
97 	atexit(qb_log_fini);
98 	qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
99 	qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
100 			  QB_LOG_FILTER_FILE, "*", LOG_INFO);
101 	qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
102 
103 	srunner_run_all(sr, CK_VERBOSE);
104 	number_failed = srunner_ntests_failed(sr);
105 	srunner_free(sr);
106 	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
107 }
108