1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 
3 /**
4  * @copyright 2013 Couchbase, Inc.
5  *
6  * @author Filipe Manana  <filipe@couchbase.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9  * use this file except in compliance with the License. You may obtain a copy of
10  * the License at
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17  * License for the specific language governing permissions and limitations under
18  * the License.
19  **/
20 
21 #include "view_tests.h"
22 
23 
int_cmp_fun(const void *a, const void *b)24 static int int_cmp_fun(const void *a, const void *b)
25 {
26     return *((int *) a) - *((int *) b);
27 }
28 
29 
test_sorted_listsnull30 void test_sorted_lists()
31 {
32     int elements[] = { 100, 10, 9, 30, 40, 20, 11, 12 };
33     int *sorted_elements;
34     int num_elements = sizeof(elements) / sizeof(elements[0]);
35     int non_elements[] = { 666, 0, -4, 999, 555 };
36     int num_non_elements = sizeof(non_elements) / sizeof(non_elements[0]);
37     void *list = sorted_list_create(int_cmp_fun);
38     int i;
39     void *iterator;
40 
41     fprintf(stderr, "Running view sorted_list tests\n");
42     sorted_elements = (int *) malloc(sizeof(elements));
43     assert(sorted_elements != NULL);
44     memcpy(sorted_elements, elements, sizeof(elements));
45     qsort(sorted_elements, num_elements, sizeof(sorted_elements[0]), int_cmp_fun);
46 
47     assert(list != NULL);
48     assert(sorted_list_size(list) == 0);
49 
50     for (i = 0; i < num_elements; ++i) {
51         int el = elements[i];
52         int *copy, *copy2;
53 
54         assert(sorted_list_add(list, &el, sizeof(el)) == 0);
55         copy = sorted_list_get(list, &el);
56         assert(copy != NULL);
57         assert(*copy == el);
58         assert(copy != &el);
59 
60         assert(sorted_list_size(list) == (i + 1));
61 
62         /* Insert existing element replaces existing element. */
63         assert(sorted_list_add(list, &el, sizeof(el)) == 0);
64         copy2 = sorted_list_get(list, &el);
65         assert(copy2 != NULL);
66         assert(*copy2 == el);
67         assert(copy2 != &el);
68         assert(copy2 != copy);
69     }
70 
71     /* Add same elements again. */
72     for (i = 0; i < num_elements; ++i) {
73         int el = elements[i];
74 
75         assert(sorted_list_add(list, &el, sizeof(el)) == 0);
76         assert(sorted_list_size(list) == num_elements);
77     }
78 
79     for (i = num_elements - 1; i >= 0; --i) {
80         int el = elements[i];
81         int *copy;
82 
83         copy = sorted_list_get(list, &el);
84         assert(copy != NULL);
85         assert(*copy == el);
86         assert(copy != &el);
87     }
88 
89     assert(sorted_list_size(list) == num_elements);
90 
91     for (i = 0; i < num_non_elements; ++i) {
92         assert(sorted_list_get(list, &non_elements[i]) == NULL);
93     }
94 
95     assert(sorted_list_size(list) == num_elements);
96 
97     for (i = 0; i < num_non_elements; ++i) {
98         sorted_list_remove(list, &non_elements[i]);
99     }
100 
101     assert(sorted_list_size(list) == num_elements);
102 
103     for (i = 0; i < num_elements; ++i) {
104         int el = elements[i];
105         int *copy;
106 
107         copy = sorted_list_get(list, &el);
108         assert(copy != NULL);
109         assert(*copy == el);
110         assert(copy != &el);
111     }
112 
113     assert(sorted_list_size(list) == num_elements);
114 
115     iterator = sorted_list_iterator(list);
116     assert(iterator != NULL);
117     for (i = 0; i < num_elements; ++i) {
118         int *e = sorted_list_next(iterator);
119 
120         assert(e != NULL);
121         assert(*e == sorted_elements[i]);
122     }
123     assert(sorted_list_next(iterator) == NULL);
124     sorted_list_free_iterator(iterator);
125 
126     for (i = 0; i < num_elements; ++i) {
127         int j;
128 
129         sorted_list_remove(list, &elements[i]);
130 
131         for (j = i + 1; j < num_elements; ++j) {
132             int *copy;
133 
134             copy = sorted_list_get(list, &elements[j]);
135             assert(copy != NULL);
136             assert(*copy == elements[j]);
137             assert(copy != &elements[j]);
138         }
139     }
140 
141     assert(sorted_list_size(list) == 0);
142 
143     for (i = 0; i < num_elements; ++i) {
144         assert(sorted_list_get(list, &elements[i]) == NULL);
145     }
146 
147     iterator = sorted_list_iterator(list);
148     assert(iterator != NULL);
149     assert(sorted_list_next(iterator) == NULL);
150     sorted_list_free_iterator(iterator);
151 
152     sorted_list_free(list);
153 }
154