Skip to content

Instantly share code, notes, and snippets.

@crazy2be
Created March 23, 2012 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy2be/2166130 to your computer and use it in GitHub Desktop.
Save crazy2be/2166130 to your computer and use it in GitHub Desktop.
void test(HashTable* table) {
int size = 5;
printf("%s", ((*table)[5]).c_str());
bool errorThrown = false;
map<int, string> expected;
int exp;
int res;
for (int i = 0; i < 50; i++) {
int var = rand() % 10 + 1;
int action = rand() % 3;
try {
switch (action) {
case 0:
exp = expected.erase(var);
res = table->remove(var);
if(exp != res) {
cout << "When removing " << var << "(" << var % size << "), got unexpected value " << res << " (expected " << exp << ")" << endl;
table->debugPrint();
}
break;
case 1:
exp = expected.insert(pair<int, string>(var, intToString(var))).second;
res = table->insert(var, intToString(var));
if(exp != res) {
cout << "When adding " << var << " (" << var % size << ")" << "), got unexpected value " << res << " (expected " << exp << ")" << endl;
cout << " (not added)";
table->debugPrint();
}
break;
case 2:
break;
cout << "accessing index " << var << "(" << var % size << ") and setting it to " << (var + 5);
(*table)[var] = intToString(var + 5);
}
} catch(string error) {
cout << " (error thrown from " << var << ")";
errorThrown = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment