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