liblash

A bianry tree implementation used for game development from scratch
git clone git://holbrook.no/liblash.git
Log | Files | Refs | LICENSE

tree3_shifttest.c (1987B)


      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 #include <limits.h>
      4 #include <time.h>
      5 
      6 #include "../tree.h"
      7 #include "../tree_dump.h"
      8 
      9 #define TREE_CAPACITY 100
     10 #define HEAP_RANGE 1000
     11 
     12 typedef struct teststructure {
     13 	lash_tree_key_t key;
     14 	unsigned int localvalue;
     15 } teststructure;
     16 
     17 typedef struct teststructure2 {
     18 	unsigned int localvalue;
     19 	lash_tree_key_t key;
     20 } teststructure2;
     21 
     22 
     23 // insert TREE_CAPACITY number of items with random sort values into the tree, while shifting every second item
     24 
     25 /// \todo write alternating inserts with teststructure 2, to test the movement of items when the addresses of key and first address of item are not the same (see comment in lash_treeProcess())
     26 
     27 int main(int argc, char **argv) {
     28 	time_t t;
     29 	lash_tree_t *tree = NULL;
     30 	int i;
     31 	int j;
     32 	char pushvaluestring[255];
     33 	long int lastnumber;
     34 	teststructure *result;
     35 	
     36 	srand((unsigned) time(&t));
     37 	
     38 	tree = lash_treeInit(tree, TREE_CAPACITY);
     39 	if (tree == NULL)
     40 		return 1;
     41 	
     42 	teststructure *ts = (teststructure*)malloc(sizeof(teststructure) * TREE_CAPACITY);
     43 	if (ts == NULL)
     44 		return 1;
     45 
     46 	lash_treeDumpInit(1);
     47 	lash_treeDumpAdd(tree, "tree");
     48 	
     49 	i = 0;
     50 	for (j = 0; j < TREE_CAPACITY; j++) {
     51 		(ts+j)->key = -1;
     52 		(ts+j)->localvalue = j;
     53 		
     54 		if (j > 0)
     55 			(ts+j)->key = rand() % HEAP_RANGE;
     56 			
     57 		if (i % 2 == 0 && i > 0) {
     58 			fprintf(stderr, "Shifting!\n");
     59 			lash_treeShift(tree, (void**)&result);
     60 			fprintf(stderr, "Shift yield n%li l%d\n", result->key, result->localvalue);
     61 			j--;
     62 		}
     63 		
     64 		sprintf(pushvaluestring, "before push [%li]", (ts+j)->key);
     65 		fprintf(stderr, "pushing %li\n", (ts+j)->key);
     66 		lash_treeDump(tree, pushvaluestring);
     67 		lash_treePush(tree, (ts+j), NULL);
     68 		lash_treeDump(tree, "after push");
     69 		i++;
     70 	}
     71 	
     72 	printf("Unpacking\n");
     73 	
     74 	lastnumber = -1;
     75 	for (i = 0; i < TREE_CAPACITY; i++) {
     76 		lash_treeShift(tree, (void**)&result);
     77 		printf("%li ", result->key);
     78 		if (result->key < lastnumber)
     79 			printf("SORTERROR! ");
     80 		lastnumber = result->key;
     81 	}
     82 	printf("\n");
     83 	
     84 	return 0;
     85 }