1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "toku_list.h"
40 
41 
42 #include "test.h"
43 #include <memory.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 
47 struct testlist {
48     struct toku_list next;
49     int tag;
50 };
51 
testlist_init(struct testlist * tl,int tag)52 static void testlist_init (struct testlist *tl, int tag) {
53     tl->tag = tag;
54 }
55 
test_push_pop(int n)56 static void test_push_pop (int n) {
57     int i;
58     struct toku_list head;
59 
60     toku_list_init(&head);
61     for (i=0; i<n; i++) {
62         struct testlist *tl = (struct testlist *) toku_malloc(sizeof *tl);
63         assert(tl);
64         testlist_init(tl, i);
65         toku_list_push(&head, &tl->next);
66         assert(!toku_list_empty(&head));
67     }
68     for (i=n-1; i>=0; i--) {
69         struct toku_list *list;
70         struct testlist *tl;
71 
72         list = toku_list_head(&head);
73         tl  = toku_list_struct(list, struct testlist, next);
74         assert(tl->tag == 0);
75         list = toku_list_tail(&head);
76         tl = toku_list_struct(list, struct testlist, next);
77         assert(tl->tag == i);
78         list = toku_list_pop(&head);
79         tl = toku_list_struct(list, struct testlist, next);
80         assert(tl->tag == i);
81         toku_free(tl);
82     }
83     assert(toku_list_empty(&head));
84 }
85 
test_push_pop_head(int n)86 static void test_push_pop_head (int n) {
87     int i;
88     struct toku_list head;
89 
90     toku_list_init(&head);
91     for (i=0; i<n; i++) {
92         struct testlist *tl = (struct testlist *) toku_malloc(sizeof *tl);
93         assert(tl);
94         testlist_init(tl, i);
95         toku_list_push(&head, &tl->next);
96         assert(!toku_list_empty(&head));
97     }
98     for (i=0; i<n; i++) {
99         struct toku_list *list;
100         struct testlist *tl;
101 
102         list = toku_list_head(&head);
103         tl  = toku_list_struct(list, struct testlist, next);
104         assert(tl->tag == i);
105         list = toku_list_tail(&head);
106         tl = toku_list_struct(list, struct testlist, next);
107         assert(tl->tag == n-1);
108 
109         list = toku_list_pop_head(&head);
110         tl = toku_list_struct(list, struct testlist, next);
111         assert(tl->tag == i);
112         toku_free(tl);
113     }
114     assert(toku_list_empty(&head));
115 }
116 
test_push_head_pop(int n)117 static void test_push_head_pop (int n) {
118     int i;
119     struct toku_list head;
120 
121     toku_list_init(&head);
122     for (i=0; i<n; i++) {
123         struct testlist *tl = (struct testlist *) toku_malloc(sizeof *tl);
124         assert(tl);
125         testlist_init(tl, i);
126         toku_list_push_head(&head, &tl->next);
127         assert(!toku_list_empty(&head));
128     }
129     for (i=0; i<n; i++) {
130         struct toku_list *list;
131         struct testlist *tl;
132 
133         list = toku_list_head(&head);
134         tl  = toku_list_struct(list, struct testlist, next);
135         assert(tl->tag == n-1);
136         list = toku_list_tail(&head);
137         tl = toku_list_struct(list, struct testlist, next);
138         assert(tl->tag == i);
139 
140         list = toku_list_pop(&head);
141         tl = toku_list_struct(list, struct testlist, next);
142         assert(tl->tag == i);
143         toku_free(tl);
144     }
145     assert(toku_list_empty(&head));
146 }
147 
148 #if 0
149 // cant move an empty list
150 static void test_move_empty (void) {
151     struct toku_list h1, h2;
152 
153     toku_list_init(&h1);
154     toku_list_init(&h2);
155     toku_list_move(&h1, &h2);
156     assert(toku_list_empty(&h2));
157     assert(toku_list_empty(&h1));
158 }
159 #endif
160 
test_move(int n)161 static void test_move (int n) {
162     struct toku_list h1, h2;
163     int i;
164 
165     toku_list_init(&h1);
166     toku_list_init(&h2);
167     for (i=0; i<n; i++) {
168         struct testlist *tl = (struct testlist *) toku_malloc(sizeof *tl);
169         assert(tl);
170         testlist_init(tl, i);
171         toku_list_push(&h2, &tl->next);
172     }
173     toku_list_move(&h1, &h2);
174     assert(!toku_list_empty(&h1));
175     assert(toku_list_empty(&h2));
176     i = 0;
177     while (!toku_list_empty(&h1)) {
178         struct toku_list *list = toku_list_pop_head(&h1);
179         struct testlist *tl = toku_list_struct(list, struct testlist, next);
180         assert(tl->tag == i);
181         toku_free(tl);
182         i += 1;
183     }
184     assert(i == n);
185 }
186 
187 int
test_main(int argc,const char * argv[])188 test_main(int argc, const char *argv[]) {
189     default_parse_args(argc, argv);
190     test_push_pop(0);
191     test_push_pop(8);
192     test_push_pop_head(0);
193     test_push_pop_head(8);
194     test_push_head_pop(8);
195     test_move(1);
196     test_move(8);
197     //    test_move_empty();
198 
199     return 0;
200 }
201 
202