Created
March 24, 2012 00:46
-
-
Save crazy2be/2176833 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string intToString(int number) { | |
char * buffer = new char[10]; | |
sprintf(buffer, "%d", number); | |
return *(new string(buffer)); | |
} | |
void debugPrint(ostream& out, HashTable* table, map<int, string> expected) { | |
out << "**My map state:" << endl; | |
table->debugPrint(out); | |
out << "**Expected map state:" << endl; | |
for (auto it = expected.begin(); it != expected.end(); it++) { | |
out << "(" << it->first << ", " << it->second << ") "; | |
} | |
out << endl; | |
} | |
template<typename T> | |
void bold(ostream& out, const T& t) { | |
out << t; | |
} | |
template<typename T, typename... Args> | |
void bold(ostream& out, const T& t, const Args&... args) { | |
out << "\033[1;34m"; | |
bold(out, t); | |
bold(out, args...); | |
out << "\033[0m"; | |
} | |
bool test(ostream& out, HashTable* table, map<int, string>& expected) { | |
int exp, res, size = 5; | |
out << "===== Before:" << endl; | |
debugPrint(out, table, expected); | |
int key = rand() % (size) + 1; | |
string val = intToString(key + rand()); | |
int action = rand() % 7; | |
int bucket = key % size; | |
try { | |
switch (action) { | |
case 0: | |
case 1: | |
case 4: | |
case 5: | |
case 6: | |
exp = expected.erase(key); | |
res = table->remove(key); | |
bold(out, "Removing ", key, " (Bucket ", bucket, ")"); | |
break; | |
case 2: | |
exp = expected.insert(pair<int, string>(key, val)).second; | |
res = table->insert(key, val); | |
bold(out, "Adding (", key, ", ", val, ") (Bucket ", bucket, ")"); | |
break; | |
case 3: | |
bold(out, "Accessing index ", key, " (Bucket ", bucket, ") and setting it to ", val); | |
exp = 0; | |
res = expected.size() >= size && expected.find(key) == expected.end() ? 1 : 0; | |
expected[key] = val; | |
(*table)[key] = val; | |
break; | |
} | |
} catch(string error) { | |
cout << " (error thrown from " << key << ")"; | |
exp = 1; | |
} | |
if(exp != res) { | |
out << ", \033[1;31mgot unexpected value\033[0m " << res << " (expected " << exp << ")"; | |
} | |
out << endl; | |
out << "===== After:" << endl; | |
debugPrint(out, table, expected); | |
if (exp != res) return false; | |
return true; | |
} | |
void test(HashTable* table) { | |
// printf("%s", ((*table)[5]).c_str()); | |
bool errorThrown = false; | |
map<int, string> expected; | |
string val; | |
ostringstream out; | |
bool failed = false; | |
for (int i = 0; i < 50; i++) { | |
if (test(out, table, expected)) { | |
failed = true; | |
} | |
} | |
if (failed) { | |
cout << out.str(); | |
} | |
} | |
int main (int argc, char* argv[]) { | |
// test(new HashTableClosed(5)); | |
test(new HashTableOpen(5)); | |
return 0; | |
HashTable* table = new HashTableClosed(4); | |
delete table; | |
for(int i=0; i<2; i++) { | |
switch(i) { | |
case 0: table = new HashTableClosed(4); break; | |
case 1: table = new HashTableOpen(4); break; | |
} | |
cout << "------------------------------" << endl; | |
cout << " Running test " << (i == 0 ? "HashTableClosed" : "HashTableOpen") << endl; | |
cout << "------------------------------" << endl; | |
cout << "5660:" << table->insert(5660, "Ali") << " "; | |
cout << "1223:" << table->insert(1223, "Gia") << " "; | |
cout << "9020:" << table->insert(9020, "Jon") << " "; | |
cout << "9020b:" << table->insert(9020, "Jon") << " "; | |
cout << "3447:" << table->insert(3447, "Mei") << endl; | |
table->print(); | |
cout << table->remove(9020) << " "; | |
// << (*table)[9020] << " " | |
cout << table->remove(3405) << " "; | |
cout << table->remove(9020) << " "; | |
cout << (*table)[9020] << " "; | |
cout << table->remove(5660) << " "; | |
cout << table->remove(5660) << " "; | |
cout << table->insert(5660, "TROLL") << " "; | |
cout << table->insert(1223, "TROLL") << endl; | |
(*table)[4444] = "Rin"; | |
cout << "uw_ids[" << 4444 << "] = " << (*table)[4444] << endl; | |
try { | |
(*table)[1111] = "Uri"; | |
} catch (string e) { | |
cout << e << endl; | |
} | |
table->print(); | |
cout << endl; | |
delete table; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment