use vc_xidopt2xid()
[util-vserver.git] / util-vserver / ensc_vector / testsuite / test1.c
1 // $Id$    --*- 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
27 int     wrapper_exit_code = 2;
28
29 static int
30 cmp(void const *lhs_v, void const *rhs_v)
31 {
32   int const * const     lhs = lhs_v;
33   int const * const     rhs = rhs_v;
34
35   return *lhs - *rhs;
36 }
37
38 struct Vector           v;
39
40 static void     I(int val)
41 {
42   *(int *)Vector_insert(&v, &val, cmp) = val;
43 }
44
45 static void     P(int val)
46 {
47   *(int *)Vector_pushback(&v) = val;
48 }
49
50 static int      E(size_t idx)
51 {
52   return ((int const *)Vector_begin_const(&v))[idx];
53 }
54
55 static int const *      S(int val)
56 {
57   return Vector_search_const(&v, &val, cmp);
58 }
59
60 int main()
61 {
62   Vector_init(&v, sizeof(int));
63
64   I(0); I(1); I(2); I(3);
65   assert(Vector_count(&v)==4);
66   assert(E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3);
67
68   // clear-test
69   Vector_clear(&v);
70   assert(Vector_count(&v)==0);
71   I(1);
72   assert(Vector_count(&v)==1);
73   assert(E(0)==1);
74
75
76   Vector_clear(&v);
77   I(3); I(0); I(2); I(1); I(5); I(4); I(7); I(6);
78   assert(Vector_count(&v)==8);
79   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3 &&
80           E(4)==4 && E(5)==5 && E(6)==6 && E(7)==7));
81
82   assert(S(0) && *S(0)==0);
83   
84   
85   Vector_clear(&v);
86   assert(Vector_count(&v)==0);
87
88   P(3); P(0); P(2); P(1); P(5); P(4); P(7); P(6);
89   assert(Vector_count(&v)==8);
90   assert((E(0)==3 && E(1)==0 && E(2)==2 && E(3)==1 &&
91           E(4)==5 && E(5)==4 && E(6)==7 && E(7)==6));
92   
93   Vector_sort(&v, cmp);
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   Vector_popback(&v);
99   assert(Vector_count(&v)==7);
100   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3 &&
101           E(4)==4 && E(5)==5 && E(6)==6));
102
103   Vector_unique(&v, cmp);
104   assert(Vector_count(&v)==7);
105   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3 &&
106           E(4)==4 && E(5)==5 && E(6)==6));
107
108   Vector_clear(&v);
109   assert(Vector_count(&v)==0);
110
111   Vector_clear(&v);
112   P(3); P(7); P(0); P(2); P(1); P(2); P(5); P(4); P(5); P(7); P(6);
113   assert(Vector_count(&v)==11);
114   Vector_sort(&v, cmp);
115   assert(Vector_count(&v)==11);
116   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==2 &&
117           E(4)==3 && E(5)==4 && E(6)==5 && E(7)==5 &&
118           E(8)==6 && E(9)==7 && E(10)==7));
119
120   Vector_unique(&v, cmp);
121   assert(Vector_count(&v)==8);
122   assert((E(0)==0 && E(1)==1 && E(2)==2 && E(3)==3 &&
123           E(4)==4 && E(5)==5 && E(6)==6 && E(7)==7)); 
124
125   Vector_free(&v);
126
127   return 0;
128 }