e3e7f255d12715cd212632bc5eb85457a73a42a2
[util-vserver.git] / ensc_vector / testsuite / test1.c
1 // $Id: test1.c,v 1.4 2005/03/17 14:49:01 ensc Exp $    --*- c -*--
2
3 // Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22 #undef NDEBUG
23
24 #include "ensc_vector/vector.h"
25 #include <assert.h>
26 #include <stdbool.h>
27
28 int     wrapper_exit_code = 2;
29
30 static int
31 cmp(void const *lhs_v, void const *rhs_v)
32 {
33   int const * const     lhs = lhs_v;
34   int const * const     rhs = rhs_v;
35
36   return *lhs - *rhs;
37 }
38
39 struct Vector           v;
40
41 static void     I(int val)
42 {
43   *(int *)Vector_insert(&v, &val, cmp) = val;
44 }
45
46 static void     P(int val)
47 {
48   *(int *)Vector_pushback(&v) = val;
49 }
50
51 static int      E(size_t idx)
52 {
53   return ((int const *)Vector_begin_const(&v))[idx];
54 }
55
56 static int const *      S(int val)
57 {
58   return Vector_search_const(&v, &val, cmp);
59 }
60
61 static int const *      S_F(int val)
62 {
63   return Vector_searchSelfOrg(&v, &val, cmp, vecMOVE_FRONT);
64 }
65
66 static int const *      S_S(int val)
67 {
68   return Vector_searchSelfOrg(&v, &val, cmp, vecSHIFT_ONCE);
69 }
70
71 static bool             CMP(int const *lhs, int val)
72 {
73   return (lhs!=0 && val==*lhs) || (lhs==0 && val==-1);
74 }
75
76 int main()
77 {
78   Vector_init(&v, sizeof(int));
79
80   I(0); I(1); I(2); I(3);
81   assert(Vector_count(&v)==4);
82   assert(E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3);
83
84   // clear-test
85   Vector_clear(&v);
86   assert(Vector_count(&v)==0);
87   I(1);
88   assert(Vector_count(&v)==1);
89   assert(E(0)==1);
90
91
92   Vector_clear(&v);
93   I(3); I(0); I(2); I(1); I(5); I(4); I(7); I(6);
94   assert(Vector_count(&v)==8);
95   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3 &&
96           E(4)==4 && E(5)==5 && E(6)==6 && E(7)==7));
97
98   assert(S(0) && *S(0)==0);
99   
100   
101   Vector_clear(&v);
102   assert(Vector_count(&v)==0);
103
104   P(3); P(0); P(2); P(1); P(5); P(4); P(7); P(6);
105   assert(Vector_count(&v)==8);
106   assert((E(0)==3 && E(1)==0 && E(2)==2 && E(3)==1 &&
107           E(4)==5 && E(5)==4 && E(6)==7 && E(7)==6));
108   
109   Vector_sort(&v, cmp);
110   assert(Vector_count(&v)==8);
111   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3 &&
112           E(4)==4 && E(5)==5 && E(6)==6 && E(7)==7));
113
114   Vector_popback(&v);
115   assert(Vector_count(&v)==7);
116   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3 &&
117           E(4)==4 && E(5)==5 && E(6)==6));
118
119   Vector_unique(&v, cmp);
120   assert(Vector_count(&v)==7);
121   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3 &&
122           E(4)==4 && E(5)==5 && E(6)==6));
123
124   Vector_clear(&v);
125   assert(Vector_count(&v)==0);
126
127   Vector_clear(&v);
128   P(3); P(7); P(0); P(2); P(1); P(2); P(5); P(4); P(5); P(7); P(6);
129   assert(Vector_count(&v)==11);
130   Vector_sort(&v, cmp);
131   assert(Vector_count(&v)==11);
132   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==2 &&
133           E(4)==3 && E(5)==4 && E(6)==5 && E(7)==5 &&
134           E(8)==6 && E(9)==7 && E(10)==7));
135
136   Vector_unique(&v, cmp);
137   assert(Vector_count(&v)==8);
138   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3 &&
139           E(4)==4 && E(5)==5 && E(6)==6 && E(7)==7));
140
141   assert(CMP(S_F(0),0));
142   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3 && E(4)==4 && E(5)==5 && E(6)==6 && E(7)==7));
143
144   assert(CMP(S_F(1),1));
145   assert((E(0)==1 && E(1)==0 && E(2)==2 && E(3)==3 && E(4)==4 && E(5)==5 && E(6)==6 && E(7)==7));
146
147   assert(CMP(S_F(7),7));
148   assert((E(0)==7 && E(1)==1 && E(2)==0 && E(3)==2 && E(4)==3 && E(5)==4 && E(6)==5 && E(7)==6));
149
150   assert(CMP(S_F(3),3));
151   assert((E(0)==3 && E(1)==7 && E(2)==1 && E(3)==0 && E(4)==2 && E(5)==4 && E(6)==5 && E(7)==6));
152
153   assert(CMP(S_F(3),3));
154   assert((E(0)==3 && E(1)==7 && E(2)==1 && E(3)==0 && E(4)==2 && E(5)==4 && E(6)==5 && E(7)==6));
155
156   assert(CMP(S_F(42), -1));
157   assert((E(0)==3 && E(1)==7 && E(2)==1 && E(3)==0 && E(4)==2 && E(5)==4 && E(6)==5 && E(7)==6));
158
159
160   assert(CMP(S_S(6), 6));
161   assert((E(0)==3 && E(1)==7 && E(2)==1 && E(3)==0 && E(4)==2 && E(5)==4 && E(6)==6 && E(7)==5));
162
163   assert(CMP(S_S(6), 6));
164   assert((E(0)==3 && E(1)==7 && E(2)==1 && E(3)==0 && E(4)==2 && E(5)==6 && E(6)==4 && E(7)==5));
165
166   assert(CMP(S_S(6), 6));
167   assert((E(0)==3 && E(1)==7 && E(2)==1 && E(3)==0 && E(4)==6 && E(5)==2 && E(6)==4 && E(7)==5));
168
169   assert(CMP(S_S(7), 7));
170   assert((E(0)==7 && E(1)==3 && E(2)==1 && E(3)==0 && E(4)==6 && E(5)==2 && E(6)==4 && E(7)==5));
171
172   assert(CMP(S_S(7), 7));
173   assert((E(0)==7 && E(1)==3 && E(2)==1 && E(3)==0 && E(4)==6 && E(5)==2 && E(6)==4 && E(7)==5));
174
175   assert(CMP(S_S(42), -1));
176   assert((E(0)==7 && E(1)==3 && E(2)==1 && E(3)==0 && E(4)==6 && E(5)==2 && E(6)==4 && E(7)==5));
177
178   
179   Vector_free(&v);
180
181   return 0;
182 }