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>
28 static const char version
[] = "$VER: avltest.c 45.0 (25.2.2008)\n";
30 #define MAX(x, y) ((x)>(y) ? (x) : (y))
31 #define RCOUNT (100000)
43 AROS_UFH2S(LONG
, nodecmp_int
,
44 AROS_UFHA(const struct AVLNode
*, avlnode1
, A0
),
45 AROS_UFHA(const struct AVLNode
*, avlnode2
, A1
))
49 const struct testnode
*n1
= (const struct testnode
*)avlnode1
;
50 const struct testnode
*n2
= (const struct testnode
*)avlnode2
;
52 return n1
->key
- n2
->key
;
57 static LONG
nodecmp_int2(const struct AVLNode
* avlnode1
, const struct AVLNode
*avlnode2
)
59 const struct testnode
*n1
= (const struct testnode
*)avlnode1
;
60 const struct testnode
*n2
= (const struct testnode
*)avlnode2
;
64 return n1
->key
- n2
->key
;
67 AROS_UFH2S(LONG
, keycmp_int
,
68 AROS_UFHA(const struct AVLNode
*, avlnode
, A0
),
69 AROS_UFHA(AVLKey
, avlkey
, A1
))
73 int key
= (int)(IPTR
)avlkey
;
74 const struct testnode
*n
= (const struct testnode
*)avlnode
;
82 static int checkbalance(struct AVLNode
*root
)
89 left
= checkbalance(root
->avl_link
[0]);
90 right
= checkbalance(root
->avl_link
[1]);
92 if (right
- left
!= root
->avl_balance
) {
93 printf("** Tree not balanced at %p\n", root
);
99 return MAX(left
, right
)+1;
102 static void dumporder(struct AVLNode
*root
)
104 struct AVLNode
*scan
= AVL_FindFirstNode(root
);
106 printf("keys in order:\n");
107 while (scan
!= NULL
) {
108 struct testnode
*s
= (struct testnode
*)scan
;
110 printf(" %d\n", s
->key
);
111 scan
= AVL_FindNextNodeByAddress(scan
);
115 static void dumprorder(struct AVLNode
*root
)
117 struct AVLNode
*scan
= AVL_FindLastNode(root
);
119 printf("keys in reverse order:\n");
120 while (scan
!= NULL
) {
121 struct testnode
*s
= (struct testnode
*)scan
;
123 printf(" %d\n", s
->key
);
124 scan
= AVL_FindPrevNodeByAddress(scan
);
128 struct testnode
*newnode()
130 struct testnode
*node
= AllocPooled(mempool
, sizeof(struct testnode
));
133 fprintf(stdout
, "AllocPooled failed\n");
143 struct AVLNode
*resetTree()
147 mempool
= CreatePool(0, sizeof(struct testnode
)*32, sizeof(struct testnode
)*32);
149 if (mempool
== NULL
) {
150 fprintf(stdout
, "CreatePool failed\n");
158 int main(int argc
, char **argv
)
162 shuffle
= malloc(sizeof(shuffle
[0]) * RCOUNT
);
168 for (i
=0;i
<10;i
++) {
174 printf("Add node %d\n", i
);
175 AVL_AddNode(&root
, &n
->node
, (AVLNODECOMP
)nodecmp_int2
);
181 printf("remove by key\n");
182 for (i
= 0;i
<10;i
++) {
183 AVL_RemNodeByKey(&root
, (AVLKey
)(IPTR
)i
, keycmp_int
);
189 printf("find next node by key\n");
191 for (i
=0;i
<20;i
+=2) {
197 printf("Add node %d\n", i
);
198 AVL_AddNode(&root
, &n
->node
, nodecmp_int
);
201 for (i
=0;i
<20;i
++) {
202 struct testnode
*n
= (struct testnode
*)AVL_FindNextNodeByKey(root
, (AVLKey
)(IPTR
)i
, keycmp_int
);
205 printf("next node %d = %d\n", i
, n
? n
->key
: -1);
212 if ((n
!= NULL
&& n
->key
!= next
)
213 || (n
== NULL
&& i
!= 19))
214 printf("next node by key is wrong! got %d expected %d\n", (n
== NULL
? -1 : n
->key
), i
+1);
219 printf("insert reverse order\n");
220 for (i
=9;i
>=0;i
--) {
226 AVL_AddNode(&root
, &n
->node
, nodecmp_int
);
232 printf("remove by key\n");
233 for (i
= 0;i
<10;i
++) {
234 AVL_RemNodeByKey(&root
, (AVLKey
)(IPTR
)i
, keycmp_int
);
240 // root should now be empty
242 printf("tree not empty!?");
247 printf("insert random order\n");
248 for (i
= 0;i
<RCOUNT
;i
++)
250 for (i
= 0;i
<RCOUNT
;i
++) {
251 int f
= random() % RCOUNT
;
252 int t
= random() % RCOUNT
;
256 shuffle
[f
] = shuffle
[t
];
259 //for (i=0;i<RCOUNT;i++) {
260 //printf(" %d\n", shuffle[i]);
263 for (i
=0;i
<RCOUNT
;i
++) {
269 AVL_AddNode(&root
, &n
->node
, nodecmp_int
);
278 printf("remove random order\n");
279 for (i
= 0;i
<RCOUNT
;i
++) {
287 AVL_AddNode(&root
, &n
->node
, nodecmp_int
);
291 for (i
= 0;i
<RCOUNT
;i
++) {
292 int f
= random() % RCOUNT
;
293 int t
= random() % RCOUNT
;
297 shuffle
[f
] = shuffle
[t
];
301 for (i
=0;i
<RCOUNT
;i
++) {
302 AVL_RemNodeByKey(&root
, (AVLKey
)(IPTR
)shuffle
[i
], keycmp_int
);