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