Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: regehr/rb_tree_demo
base: step_7
...
head fork: regehr/rb_tree_demo
compare: step_8c
  • 5 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Showing with 61 additions and 40 deletions.
  1. +2 −2 Makefile
  2. +19 −18 container.c
  3. +7 −2 container.h
  4. +33 −18 fuzz_red_black_tree.c
View
4 Makefile
@@ -17,8 +17,8 @@ OBJS2 = red_black_tree.o stack.o fuzz_red_black_tree.o misc.o container.o
CC = gcc
#CC = clang -fsanitize=integer
-CFLAGS = -g -O0 -coverage -fprofile-arcs -Wall -pedantic
-#CFLAGS = -O3 -Wall -pedantic
+#CFLAGS = -g -O0 -coverage -fprofile-arcs -Wall -pedantic
+CFLAGS = -O3 -Wall -pedantic
PROGRAM = test_rb
View
37 container.c
@@ -3,20 +3,21 @@
#include <stdio.h>
#include "container.h"
-#define MAX_SIZE 1000
+#define MAX_SIZE 10000
static int size;
-static int array[MAX_SIZE];
+static struct elt_t array[MAX_SIZE];
void containerCreate (void)
{
size = 0;
}
-void containerInsert (int val)
+void containerInsert (int val, void *info)
{
assert (size < MAX_SIZE);
- array[size] = val;
+ array[size].val = val;
+ array[size].info = info;
size++;
}
@@ -24,7 +25,7 @@ int containerFind (int val)
{
int i;
for (i=0; i<size; i++) {
- if (array[i] == val) return 1;
+ if (array[i].val == val) return 1;
}
return 0;
}
@@ -32,7 +33,7 @@ int containerFind (int val)
int containerRandom (int *res)
{
if (size==0) return 0;
- *res = array[rand()%size];
+ *res = array[rand()%size].val;
return 1;
}
@@ -40,7 +41,7 @@ void containerDelete (int val)
{
int i, j;
for (i=0; i<size; i++) {
- if (array[i] == val) goto remove;
+ if (array[i].val == val) goto remove;
}
assert (0);
remove:
@@ -52,14 +53,14 @@ void containerDelete (int val)
static int compar (const void *a, const void *b)
{
- int ai = * ((int *) a);
- int bi = * ((int *) b);
+ int ai = ((struct elt_t *) a)->val;
+ int bi = ((struct elt_t *) b)->val;
return ai > bi;
}
void containerSort (void)
{
- qsort (array, size, sizeof (int), compar);
+ qsort (array, size, sizeof (struct elt_t), compar);
}
int containerPred (int val, int *ret)
@@ -67,12 +68,12 @@ int containerPred (int val, int *ret)
int i;
containerSort();
for (i=0; i<size; i++) {
- if (array[i] == val) goto found;
+ if (array[i].val == val) goto found;
}
return KEY_NOT_FOUND;
found:
if (i==0) return NO_PRED_OR_SUCC;
- *ret = array[i-1];
+ *ret = array[i-1].val;
return FOUND;
}
@@ -81,12 +82,12 @@ int containerSucc (int val, int *ret)
int i;
containerSort();
for (i=0; i<size; i++) {
- if (array[i] == val) goto found;
+ if (array[i].val == val) goto found;
}
return KEY_NOT_FOUND;
found:
if (i==(size-1)) return NO_PRED_OR_SUCC;
- *ret = array[i+1];
+ *ret = array[i+1].val;
return FOUND;
}
@@ -95,7 +96,7 @@ int containerStartVal (int val, int val2)
int i;
containerSort();
for (i=0; i<size; i++) {
- if (array[i]>=val && array[i]<=val2) {
+ if (array[i].val >= val && array[i].val <= val2) {
return i;
}
}
@@ -106,7 +107,7 @@ int containerNextVal (int val, int i)
{
i++;
if (i==size) return -1;
- if (array[i] <= val) return i;
+ if (array[i].val <= val) return i;
return -1;
}
@@ -124,7 +125,7 @@ int containerStart (void)
return 0;
}
-int containerGet (int i)
+struct elt_t containerGet (int i)
{
assert (i>=0);
assert (i<size);
@@ -136,7 +137,7 @@ void containerPrint (void)
int i;
containerSort();
for (i=0; i<size; i++) {
- printf ("%d ", array[i]);
+ printf ("%d ", array[i].val);
}
printf ("\n");
}
View
9 container.h
@@ -4,8 +4,13 @@ enum result {
FOUND
};
+struct elt_t {
+ int val;
+ void *info;
+};
+
void containerCreate (void);
-void containerInsert (int);
+void containerInsert (int, void *);
int containerFind (int);
void containerDelete (int);
int containerPred (int, int *);
@@ -15,5 +20,5 @@ int containerStart (void);
int containerStartVal (int, int);
int containerNextVal (int, int);
int containerNext (int);
-int containerGet (int);
+struct elt_t containerGet (int);
int containerRandom (int *);
View
51 fuzz_red_black_tree.c
@@ -3,6 +3,7 @@
#include<ctype.h>
#include <assert.h>
#include <time.h>
+#include <stdint.h>
#include "container.h"
#define META_REPS 1000
@@ -42,14 +43,28 @@ int randomInt (void)
return rand()%FUZZ_RANGE;
}
+void *randomVoidP (void)
+{
+ uintptr_t p = 0;
+ int i;
+ for (i=0; i<sizeof(p); i++) {
+ p <<= 8;
+ p |= rand()%256;
+ }
+ return (void *)p;
+}
+
int idx;
+int nodups;
+
void InorderTreeVerify(rb_red_blk_tree* tree, rb_red_blk_node* x) {
if (x != tree->nil) {
- int res;
+ struct elt_t e;
InorderTreeVerify(tree,x->left);
- res = containerGet (idx);
- assert (res==*(int *)x->key);
+ e = containerGet (idx);
+ assert (e.val == *(int *)x->key);
+ if (nodups) assert (e.info == x->info);
idx = containerNext (idx);
InorderTreeVerify(tree,x->right);
}
@@ -70,23 +85,20 @@ static void fuzzit (void)
rb_red_blk_node* newNode;
rb_red_blk_tree* tree;
int i;
- int nodups;
+ int fuzz_reps;
+
+ fuzz_reps = 1+rand()%FUZZ_REPS;
tree=RBTreeCreate(IntComp,IntDest,InfoDest,IntPrint,InfoPrint);
containerCreate ();
nodups = rand()%2;
- switch (rand()%2) {
- case 0:
- FUZZ_RANGE = 1 + rand()%FUZZ_REPS;
- break;
- case 1:
+ if (rand()%2 == 0) {
+ FUZZ_RANGE = 1 + rand()%fuzz_reps;
+ } else {
FUZZ_RANGE = 1 + rand()%RAND_MAX;
- break;
- default:
- assert (0);
}
- for (i=0; i<FUZZ_REPS; i++) {
+ for (i=0; i<fuzz_reps; i++) {
checkRep (tree);
@@ -96,6 +108,7 @@ static void fuzzit (void)
{
case 1:
{
+ void *p;
newKey = randomInt();
if (nodups) {
@@ -104,8 +117,9 @@ static void fuzzit (void)
newInt=(int*) malloc(sizeof(int));
*newInt=newKey;
- RBTreeInsert(tree,newInt,0);
- containerInsert(newKey);
+ p = randomVoidP();
+ RBTreeInsert(tree,newInt,p);
+ containerInsert(newKey,p);
}
break;
@@ -181,10 +195,11 @@ static void fuzzit (void)
i = containerStartVal (newKey,newKey2);
enumResult=RBEnumerate(tree,&newKey,&newKey2);
while ( (newNode = StackPop(enumResult)) ) {
- int k;
+ struct elt_t e;
assert (i != -1);
- k = containerGet(i);
- assert (k == *(int *)newNode->key);
+ e = containerGet(i);
+ assert (e.val == *(int *)newNode->key);
+ if (nodups) assert (e.info == newNode->info);
i = containerNextVal (newKey2, i);
}
assert (i==-1);

No commit comments for this range

Something went wrong with that request. Please try again.