Permalink
Browse files

first cut at a fuzzer

  • Loading branch information...
1 parent 17f2dc5 commit a7c9e23d82ec9cf3cf269f946af06583dede831f @regehr committed Feb 13, 2013
Showing with 24 additions and 30 deletions.
  1. +24 −30 fuzz_red_black_tree.c
View
@@ -1,6 +1,11 @@
#include"red_black_tree.h"
#include<stdio.h>
#include<ctype.h>
+#include <assert.h>
+#include <time.h>
+
+#define FUZZ_REPS 1000
+#define FUZZ_RANGE 100
/* this file has functions to test a red-black tree of integers */
@@ -29,28 +34,30 @@ void InfoDest(void *a){
;
}
+int randomInt (void)
+{
+ return rand()%FUZZ_RANGE;
+}
+
int main() {
stk_stack* enumResult;
int option=0;
int newKey,newKey2;
int* newInt;
rb_red_blk_node* newNode;
rb_red_blk_tree* tree;
+ int i;
+
+ srand (time(NULL));
tree=RBTreeCreate(IntComp,IntDest,InfoDest,IntPrint,InfoPrint);
- while(option!=8) {
- printf("choose one of the following:\n");
- printf("(1) add to tree\n(2) delete from tree\n(3) query\n");
- printf("(4) find predecessor\n(5) find sucessor\n(6) enumerate\n");
- printf("(7) print tree\n(8) quit\n");
- do option=fgetc(stdin); while(-1 != option && isspace(option));
- option-='0';
+ for (i=0; i<FUZZ_REPS; i++) {
+ option = 1 + rand()%7;
switch(option)
{
case 1:
{
- printf("type key for new node\n");
- scanf("%i",&newKey);
+ newKey = randomInt();
newInt=(int*) malloc(sizeof(int));
*newInt=newKey;
RBTreeInsert(tree,newInt,0);
@@ -59,17 +66,15 @@ int main() {
case 2:
{
- printf("type key of node to remove\n");
- scanf("%i",&newKey);
+ newKey = randomInt();
if ( ( newNode=RBExactQuery(tree,&newKey ) ) ) RBDelete(tree,newNode);/*assignment*/
else printf("key not found in tree, no action taken\n");
}
break;
case 3:
{
- printf("type key of node to query for\n");
- scanf("%i",&newKey);
+ newKey = randomInt();
if ( ( newNode = RBExactQuery(tree,&newKey) ) ) {/*assignment*/
printf("data found in tree at location %p\n", (void *)newNode);
} else {
@@ -79,8 +84,7 @@ int main() {
break;
case 4:
{
- printf("type key of node to find predecessor of\n");
- scanf("%i",&newKey);
+ newKey = randomInt();
if ( ( newNode = RBExactQuery(tree,&newKey) ) ) {/*assignment*/
newNode=TreePredecessor(tree,newNode);
if(tree->nil == newNode) {
@@ -95,8 +99,7 @@ int main() {
break;
case 5:
{
- printf("type key of node to find successor of\n");
- scanf("%i",&newKey);
+ newKey = randomInt();
if ( (newNode = RBExactQuery(tree,&newKey) ) ) {
newNode=TreeSuccessor(tree,newNode);
if(tree->nil == newNode) {
@@ -111,8 +114,8 @@ int main() {
break;
case 6:
{
- printf("type low and high keys to see all keys between them\n");
- scanf("%i %i",&newKey,&newKey2);
+ newKey = randomInt();
+ newKey2 = randomInt();
enumResult=RBEnumerate(tree,&newKey,&newKey2);
while ( (newNode = StackPop(enumResult)) ) {
tree->PrintKey(newNode->key);
@@ -126,19 +129,10 @@ int main() {
RBTreePrint(tree);
}
break;
- case 8:
- {
- RBTreeDestroy(tree);
- return 0;
- }
- break;
default:
- printf("Invalid input; Please try again.\n");
+ assert (0);
}
}
+ RBTreeDestroy(tree);
return 0;
}
-
-
-
-

0 comments on commit a7c9e23

Please sign in to comment.