Activity - Unit testing
Consider the simple one-function jhash
module used in Lab3 and the TSE.
It comprises jhash.c and jhash.h and implements the JenkinsHash()
function below.
With your group, list all the tests a good unit test should run against this unit.
- Can you spot the bug? (I noticed one I’d overlooked before.)
- Consider what would happen if
mod
were 0 (or 1) - Modify jenkins.c to check for
mod=0
- Add unit tests to jenkins.c (assume unittest.h is available)
unsigned long
JenkinsHash(const char *str, const unsigned long mod)
{
if (str == NULL) {
return 0;
}
size_t len = strlen(str);
unsigned long hash = 0;
for (int i = 0; i < len; i++) {
hash += str[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return (hash % mod);
}
Solution
An example solution using the macros in unittest.h
from class today.