initial checkin
[util-vserver.git] / util-vserver / ensc_vector / testsuite / test2.c
1 // $Id$    --*- c -*--
2
3 // Copyright (C) 2005 Enrico Scholz <enrico.scholz@sigma-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/list.h"
24 #include "ensc_vector/list-internal.h"
25
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29
30 int     wrapper_exit_code = 2;
31
32 static int
33 cmp(void const *lhs_v, void const *rhs_v)
34 {
35   int const * const     lhs = lhs_v;
36   int const * const     rhs = rhs_v;
37
38   return *lhs - *rhs;
39 }
40
41 struct List             l;
42
43
44 static void     A(int val)
45 {
46   int *         res = List_add(&l, &val);
47
48   assert(*res == val);
49 }
50
51 static int const *      S(int val)
52 {
53   return List_search(&l, &val, cmp);
54 }
55
56 static int const *      SSO_F(int val)
57 {
58   return List_searchSelfOrg(&l, &val, cmp, listMOVE_FRONT);
59 }
60
61 static int const *      SSO_S(int val)
62 {
63   return List_searchSelfOrg(&l, &val, cmp, listSHIFT_ONCE);
64 }
65
66 static int              P(size_t idx)
67 {
68   int const     *res = List_at_const(&l, idx);
69
70   assert(res!=0);
71   return *res;
72 }
73
74 static bool             P0(size_t idx)
75 {
76   return List_at_const(&l, idx) == 0;
77 }
78
79 static bool             CMP(int const *lhs, int rhs)
80 {
81   return (lhs!=0 && *lhs==rhs) || (lhs==0 && rhs==-1);
82 }
83
84
85 int main()
86 {
87   List_init(&l, sizeof(int));
88
89   A(5); A(4); A(3); A(2); A(1); A(0);
90   assert(P(0)==0 && P(1)==1 && P(2)==2 && P(3)==3 && P(4)==4 && P(5)==5 && P0(6));
91
92   assert(CMP(S(5), 5) && CMP(S(2), 2) && CMP(S(0), 0));
93   assert(CMP(S(42),-1));
94
95   assert(CMP(SSO_F(5), 5));
96   assert(P(0)==5 && P(1)==0 && P(2)==1 && P(3)==2 && P(4)==3 && P(5)==4 && P0(6));
97
98   assert(CMP(SSO_F(5), 5));
99   assert(P(0)==5 && P(1)==0 && P(2)==1 && P(3)==2 && P(4)==3 && P(5)==4 && P0(6));
100
101   assert(CMP(SSO_F(0), 0));
102   assert(P(0)==0 && P(1)==5 && P(2)==1 && P(3)==2 && P(4)==3 && P(5)==4 && P0(6));
103
104   assert(CMP(SSO_F(4), 4));
105   assert(P(0)==4 && P(1)==0 && P(2)==5 && P(3)==1 && P(4)==2 && P(5)==3 && P0(6));
106
107   assert(CMP(SSO_F(5), 5));
108   assert(P(0)==5 && P(1)==4 && P(2)==0 && P(3)==1 && P(4)==2 && P(5)==3 && P0(6));
109   
110   assert(CMP(SSO_F(42),-1));
111   assert(P(0)==5 && P(1)==4 && P(2)==0 && P(3)==1 && P(4)==2 && P(5)==3 && P0(6));
112
113
114   
115   assert(CMP(SSO_S(3), 3));
116   assert(P(0)==5 && P(1)==4 && P(2)==0 && P(3)==1 && P(4)==3 && P(5)==2 && P0(6));
117   
118   assert(CMP(SSO_S(3), 3));
119   assert(P(0)==5 && P(1)==4 && P(2)==0 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6));
120
121   assert(CMP(SSO_S(5), 5));
122   assert(P(0)==5 && P(1)==4 && P(2)==0 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6));
123
124   assert(CMP(SSO_S(4), 4));
125   assert(P(0)==4 && P(1)==5 && P(2)==0 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6));
126
127   assert(CMP(SSO_S(0), 0));
128   assert(P(0)==4 && P(1)==0 && P(2)==5 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6));
129
130   assert(CMP(SSO_S(0), 0));
131   assert(P(0)==0 && P(1)==4 && P(2)==5 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6));
132
133   assert(CMP(SSO_S(0), 0));
134   assert(P(0)==0 && P(1)==4 && P(2)==5 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6));
135   
136   assert(CMP(SSO_S(42), -1));
137   assert(P(0)==0 && P(1)==4 && P(2)==5 && P(3)==3 && P(4)==1 && P(5)==2 && P0(6));
138
139   List_free(&l);
140
141   return EXIT_SUCCESS;
142 }