xref: /haiku/src/tests/add-ons/kernel/file_systems/bfs/array/array.cpp (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 #include "Utility.h"
2 
3 #include <List.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 
7 
8 const int32 kBlockSize = 1024;
9 
10 int32 gNumber = 100000;
11 BList gList;
12 BlockArray gArray(kBlockSize);
13 
14 
15 void
16 dumpArray()
17 {
18 	printf("  items in array: %ld\n",gArray.CountItems());
19 	printf("  blocks used:    %ld\n",gArray.BlocksUsed());
20 	printf("  size:           %ld\n",gArray.Size());
21 }
22 
23 
24 int
25 main(int argc,char **argv)
26 {
27 	srand(42);
28 
29 	// insert numbers in the array
30 
31 	for (int32 i = 0;i < gNumber;i++) {
32 		int32 num = rand();
33 		if (num == 0)
34 			num++;
35 
36 		if (gArray.Insert(num) == B_OK)
37 			gList.AddItem((void *)num);
38 		else if (gArray.Find(num) < 0) {
39 			printf("Could not insert entry in array, but it's not in there either...\n");
40 			dumpArray();
41 		} else
42 			printf("hola\n");
43 	}
44 
45 	// check for numbers in the array
46 
47 	for (int32 i = 0;i < gNumber;i++) {
48 		int32 num = (int32)gList.ItemAt(i);
49 
50 		if (gArray.Find(num) < 0) {
51 			printf("could not found entry %ld in array!\n",num);
52 			dumpArray();
53 		}
54 	}
55 
56 	// iterate through the array
57 
58 	sorted_array *array = gArray.Array();
59 	for (int32 i = 0;i < array->count;i++) {
60 		if (!gList.HasItem((void *)array->values[i])) {
61 			printf("Could not find entry %lld at %ld in list!\n",array->values[i],i);
62 			dumpArray();
63 		}
64 	}
65 	return 0;
66 }
67