Message ID | 1427061825-27470-4-git-send-email-emil.l.velikov@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
On Sun, 2015-03-22 at 22:03 +0000, Emil Velikov wrote: > Get the test from completely broken to working like a charm. > > - Use the same variable type for both HashInsert and HashLookup. > - Use correct storage type for the HashLookup return value. > - Remove useless backward iteration of HashLookup(i). > > Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> > --- > tests/hash.c | 31 ++++++++++++++----------------- > 1 file changed, 14 insertions(+), 17 deletions(-) > > diff --git a/tests/hash.c b/tests/hash.c > index d57d2dc..902919f 100644 > --- a/tests/hash.c > +++ b/tests/hash.c > @@ -139,27 +139,27 @@ static void compute_dist(HashTablePtr table) > static void check_table(HashTablePtr table, > unsigned long key, unsigned long value) I think we should use void* for value here. > { > - unsigned long retval = 0; > - int retcode = drmHashLookup(table, key, &retval); > + unsigned long *retval; > + int retcode = drmHashLookup(table, key, (void **)&retval); I don't think this is correct. If the entry is found, it stores address to stack variable i in retval, which at this point in iteration incidentally contains the value we are looking for. > > switch (retcode) { > case -1: > printf("Bad magic = 0x%08lx:" > " key = %lu, expected = %lu, returned = %lu\n", > - table->magic, key, value, retval); > + table->magic, key, value, *retval); > break; > case 1: > - printf("Not found: key = %lu, expected = %lu returned = %lu\n", > - key, value, retval); > + printf("Not found: key = %lu, expected = %lu, returned = %lu\n", > + key, value, *retval); > break; > case 0: > - if (value != retval) > + if (value != *retval) > printf("Bad value: key = %lu, expected = %lu, returned = %lu\n", > - key, value, retval); > + key, value, *retval); > break; > default: > printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n", > - retcode, key, value, retval); > + retcode, key, value, *retval); > break; > } > } > @@ -167,36 +167,33 @@ static void check_table(HashTablePtr table, > int main(void) > { > HashTablePtr table; > - int i; > + unsigned long i; > > printf("\n***** 256 consecutive integers ****\n"); > table = drmHashCreate(); > - for (i = 0; i < 256; i++) drmHashInsert(table, i, i); > + for (i = 0; i < 256; i++) drmHashInsert(table, i, (void *)&i); This changes the entries inserted. previously it would insert values [0, 256). Now it inserts address of i 256 times. I think we should change the inserted values to be different from the key (offset should be enough), to catch the kind of scenario this tests creates. > for (i = 0; i < 256; i++) check_table(table, i, i); > - for (i = 256; i >= 0; i--) check_table(table, i, i); > compute_dist(table); > drmHashDestroy(table); > > printf("\n***** 1024 consecutive integers ****\n"); > table = drmHashCreate(); > - for (i = 0; i < 1024; i++) drmHashInsert(table, i, i); > + for (i = 0; i < 1024; i++) drmHashInsert(table, i, (void *)&i); > for (i = 0; i < 1024; i++) check_table(table, i, i); > - for (i = 1024; i >= 0; i--) check_table(table, i, i); > compute_dist(table); > drmHashDestroy(table); > > printf("\n***** 1024 consecutive page addresses (4k pages) ****\n"); > table = drmHashCreate(); > - for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, i); > + for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, (void *)&i); > for (i = 0; i < 1024; i++) check_table(table, i*4096, i); > - for (i = 1024; i >= 0; i--) check_table(table, i*4096, i); > compute_dist(table); > drmHashDestroy(table); > > printf("\n***** 1024 random integers ****\n"); > table = drmHashCreate(); > srandom(0xbeefbeef); > - for (i = 0; i < 1024; i++) drmHashInsert(table, random(), i); > + for (i = 0; i < 1024; i++) drmHashInsert(table, random(), (void *)&i); > srandom(0xbeefbeef); > for (i = 0; i < 1024; i++) check_table(table, random(), i); > srandom(0xbeefbeef); > @@ -207,7 +204,7 @@ int main(void) > printf("\n***** 5000 random integers ****\n"); > table = drmHashCreate(); > srandom(0xbeefbeef); > - for (i = 0; i < 5000; i++) drmHashInsert(table, random(), i); > + for (i = 0; i < 5000; i++) drmHashInsert(table, random(), (void *)&i); > srandom(0xbeefbeef); > for (i = 0; i < 5000; i++) check_table(table, random(), i); > srandom(0xbeefbeef);
diff --git a/tests/hash.c b/tests/hash.c index d57d2dc..902919f 100644 --- a/tests/hash.c +++ b/tests/hash.c @@ -139,27 +139,27 @@ static void compute_dist(HashTablePtr table) static void check_table(HashTablePtr table, unsigned long key, unsigned long value) { - unsigned long retval = 0; - int retcode = drmHashLookup(table, key, &retval); + unsigned long *retval; + int retcode = drmHashLookup(table, key, (void **)&retval); switch (retcode) { case -1: printf("Bad magic = 0x%08lx:" " key = %lu, expected = %lu, returned = %lu\n", - table->magic, key, value, retval); + table->magic, key, value, *retval); break; case 1: - printf("Not found: key = %lu, expected = %lu returned = %lu\n", - key, value, retval); + printf("Not found: key = %lu, expected = %lu, returned = %lu\n", + key, value, *retval); break; case 0: - if (value != retval) + if (value != *retval) printf("Bad value: key = %lu, expected = %lu, returned = %lu\n", - key, value, retval); + key, value, *retval); break; default: printf("Bad retcode = %d: key = %lu, expected = %lu, returned = %lu\n", - retcode, key, value, retval); + retcode, key, value, *retval); break; } } @@ -167,36 +167,33 @@ static void check_table(HashTablePtr table, int main(void) { HashTablePtr table; - int i; + unsigned long i; printf("\n***** 256 consecutive integers ****\n"); table = drmHashCreate(); - for (i = 0; i < 256; i++) drmHashInsert(table, i, i); + for (i = 0; i < 256; i++) drmHashInsert(table, i, (void *)&i); for (i = 0; i < 256; i++) check_table(table, i, i); - for (i = 256; i >= 0; i--) check_table(table, i, i); compute_dist(table); drmHashDestroy(table); printf("\n***** 1024 consecutive integers ****\n"); table = drmHashCreate(); - for (i = 0; i < 1024; i++) drmHashInsert(table, i, i); + for (i = 0; i < 1024; i++) drmHashInsert(table, i, (void *)&i); for (i = 0; i < 1024; i++) check_table(table, i, i); - for (i = 1024; i >= 0; i--) check_table(table, i, i); compute_dist(table); drmHashDestroy(table); printf("\n***** 1024 consecutive page addresses (4k pages) ****\n"); table = drmHashCreate(); - for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, i); + for (i = 0; i < 1024; i++) drmHashInsert(table, i*4096, (void *)&i); for (i = 0; i < 1024; i++) check_table(table, i*4096, i); - for (i = 1024; i >= 0; i--) check_table(table, i*4096, i); compute_dist(table); drmHashDestroy(table); printf("\n***** 1024 random integers ****\n"); table = drmHashCreate(); srandom(0xbeefbeef); - for (i = 0; i < 1024; i++) drmHashInsert(table, random(), i); + for (i = 0; i < 1024; i++) drmHashInsert(table, random(), (void *)&i); srandom(0xbeefbeef); for (i = 0; i < 1024; i++) check_table(table, random(), i); srandom(0xbeefbeef); @@ -207,7 +204,7 @@ int main(void) printf("\n***** 5000 random integers ****\n"); table = drmHashCreate(); srandom(0xbeefbeef); - for (i = 0; i < 5000; i++) drmHashInsert(table, random(), i); + for (i = 0; i < 5000; i++) drmHashInsert(table, random(), (void *)&i); srandom(0xbeefbeef); for (i = 0; i < 5000; i++) check_table(table, random(), i); srandom(0xbeefbeef);
Get the test from completely broken to working like a charm. - Use the same variable type for both HashInsert and HashLookup. - Use correct storage type for the HashLookup return value. - Remove useless backward iteration of HashLookup(i). Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> --- tests/hash.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-)