2 Copyright © 2008 The AROS Development Team. All rights reserved.
5 Desc: Test AVL balanced tree interface.
9 #include <aros/debug.h>
11 #include <exec/memory.h>
13 #include <dos/exall.h>
14 #include <dos/datetime.h>
15 #include <proto/dos.h>
16 #include <proto/utility.h>
17 #include <utility/tagitem.h>
18 #include <utility/utility.h>
20 #include <proto/alib.h>
21 #include <proto/exec.h>
22 #include <proto/dos.h>
29 static const char version
[] = "$VER: avltest.c 45.0 (25.2.2008)\n";
31 #define MAX(x, y) ((x)>(y) ? (x) : (y))
32 #define RCOUNT (100000)
44 AROS_UFH2S(LONG
, nodecmp_int
,
45 AROS_UFHA(const struct AVLNode
*, avlnode1
, A0
),
46 AROS_UFHA(const struct AVLNode
*, avlnode2
, A1
))
50 const struct testnode
*n1
= (const struct testnode
*)avlnode1
;
51 const struct testnode
*n2
= (const struct testnode
*)avlnode2
;
53 return n1
->key
- n2
->key
;
58 static LONG
nodecmp_int2(const struct AVLNode
* avlnode1
, const struct AVLNode
*avlnode2
)
60 const struct testnode
*n1
= (const struct testnode
*)avlnode1
;
61 const struct testnode
*n2
= (const struct testnode
*)avlnode2
;
65 return n1
->key
- n2
->key
;
68 AROS_UFH2S(LONG
, keycmp_int
,
69 AROS_UFHA(const struct AVLNode
*, avlnode
, A0
),
70 AROS_UFHA(AVLKey
, avlkey
, A1
))
74 int key
= (int)avlkey
;
75 const struct testnode
*n
= (const struct testnode
*)avlnode
;
83 static int checkbalance(struct AVLNode
*root
)
90 left
= checkbalance(root
->avl_link
[0]);
91 right
= checkbalance(root
->avl_link
[1]);
93 if (right
- left
!= root
->avl_balance
) {
94 printf("** Tree not balanced at %p\n", root
);
100 return MAX(left
, right
)+1;
103 static void dumporder(struct AVLNode
*root
)
105 struct AVLNode
*scan
= AVL_FindFirstNode(root
);
107 printf("keys in order:\n");
108 while (scan
!= NULL
) {
109 struct testnode
*s
= (struct testnode
*)scan
;
111 printf(" %d\n", s
->key
);
112 scan
= AVL_FindNextNodeByAddress(scan
);
116 static void dumprorder(struct AVLNode
*root
)
118 struct AVLNode
*scan
= AVL_FindLastNode(root
);
120 printf("keys in reverse order:\n");
121 while (scan
!= NULL
) {
122 struct testnode
*s
= (struct testnode
*)scan
;
124 printf(" %d\n", s
->key
);
125 scan
= AVL_FindPrevNodeByAddress(scan
);
129 struct testnode
*newnode()
131 struct testnode
*node
= AllocPooled(mempool
, sizeof(struct testnode
));
134 fprintf(stdout
, "AllocPooled failed\n");
144 struct AVLNode
*resetTree()
148 mempool
= CreatePool(0, sizeof(struct testnode
)*32, sizeof(struct testnode
)*32);
150 if (mempool
== NULL
) {
151 fprintf(stdout
, "CreatePool failed\n");
159 int main(int argc
, char **argv
)
163 shuffle
= malloc(sizeof(shuffle
[0]) * RCOUNT
);
169 for (i
=0;i
<10;i
++) {
175 printf("Add node %d\n", i
);
176 AVL_AddNode(&root
, &n
->node
, nodecmp_int2
);
182 printf("remove by key\n");
183 for (i
= 0;i
<10;i
++) {
184 AVL_RemNodeByKey(&root
, (AVLKey
)i
, keycmp_int
);
190 printf("find next node by key\n");
192 for (i
=0;i
<20;i
+=2) {
198 printf("Add node %d\n", i
);
199 AVL_AddNode(&root
, &n
->node
, nodecmp_int
);
202 for (i
=0;i
<20;i
++) {
203 struct testnode
*n
= (struct testnode
*)AVL_FindNextNodeByKey(root
, (AVLKey
)i
, keycmp_int
);
206 printf("next node %d = %d\n", i
, n
? n
->key
: -1);
213 if ((n
!= NULL
&& n
->key
!= next
)
214 || (n
== NULL
&& i
!= 19))
215 printf("next node by key is wrong! got %d expected %d\n", (n
== NULL
? -1 : n
->key
), i
+1);
220 printf("insert reverse order\n");
221 for (i
=9;i
>=0;i
--) {
227 AVL_AddNode(&root
, &n
->node
, nodecmp_int
);
233 printf("remove by key\n");
234 for (i
= 0;i
<10;i
++) {
235 AVL_RemNodeByKey(&root
, (AVLKey
)i
, keycmp_int
);
241 // root should now be empty
243 printf("tree not empty!?");
248 printf("insert random order\n");
249 for (i
= 0;i
<RCOUNT
;i
++)
251 for (i
= 0;i
<RCOUNT
;i
++) {
252 int f
= random() % RCOUNT
;
253 int t
= random() % RCOUNT
;
257 shuffle
[f
] = shuffle
[t
];
260 //for (i=0;i<RCOUNT;i++) {
261 //printf(" %d\n", shuffle[i]);
264 for (i
=0;i
<RCOUNT
;i
++) {
270 AVL_AddNode(&root
, &n
->node
, nodecmp_int
);
279 printf("remove random order\n");
280 for (i
= 0;i
<RCOUNT
;i
++) {
288 AVL_AddNode(&root
, &n
->node
, nodecmp_int
);
292 for (i
= 0;i
<RCOUNT
;i
++) {
293 int f
= random() % RCOUNT
;
294 int t
= random() % RCOUNT
;
298 shuffle
[f
] = shuffle
[t
];
302 for (i
=0;i
<RCOUNT
;i
++) {
303 AVL_RemNodeByKey(&root
, (AVLKey
)shuffle
[i
], keycmp_int
);