1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #include <iostream>
- #include "utils/HashMap.h"
- constexpr int MAP_MIN_CAPACITY = 5;
- typedef HashMap<int, int, MAP_MIN_CAPACITY> IntMap;
- const char* RED = "\033[0;31m";
- const char* GREEN = "\033[0;32m";
- int tests = 0;
- int successTests = 0;
- void finalizeTest() {
- std::cout << ((successTests == tests) ? GREEN : RED);
- std::cout << successTests << " / " << tests << " succeeded\n";
- tests = 0;
- successTests = 0;
- }
- template<typename T>
- void checkEqual(const T& wanted, const T& actual, const char* text) {
- if(wanted == actual) {
- tests++;
- successTests++;
- std::cout << GREEN << tests << ": " << text << "\n";
- } else {
- tests++;
- std::cout << RED << tests << ": " << text << " - ";
- std::cout << RED << "expected '" << wanted << "' got '" << actual << "'\n";
- }
- }
- namespace Map {
- void testAdd() {
- IntMap map;
- map.add(5, 4);
- checkEqual(true, map.contains(5), "map contains added value");
- checkEqual(true, map.search(5, -1) == 4, "map search finds added value");
- }
- void testMultipleAdd() {
- IntMap map;
- map.add(5, 4);
- map.add(10, 3);
- map.add(15, 2);
- checkEqual(true, map.contains(5), "map contains added value 1");
- checkEqual(true, map.contains(10), "map contains added value 2");
- checkEqual(true, map.contains(15), "map contains added value 3");
- checkEqual(true, map.search(5, -1) == 4, "map search finds added value 1");
- checkEqual(true, map.search(10, -1) == 3, "map search finds added value 2");
- checkEqual(true, map.search(15, -1) == 2, "map search finds added value 3");
- }
- void testAddReplace() {
- IntMap map;
- map.add(5, 4);
- map.add(5, 10);
- checkEqual(true, map.contains(5), "map contains replaced value");
- checkEqual(true, map.search(5, -1) == 10, "map search finds replaced value");
- }
- void testClear() {
- IntMap map;
- map.add(5, 4);
- map.add(4, 10);
- map.clear();
- checkEqual(false, map.contains(5), "map does not contain cleared values");
- checkEqual(false, map.contains(4), "map does not contain cleared values");
- }
- void testOverflow() {
- IntMap map;
- for(int i = 0; i < 1000000; i++) {
- map.add(i, i);
- }
- for(int i = 0; i < MAP_MIN_CAPACITY; i++) {
- checkEqual(true, map.contains(i), "map still contains values after overflow");
- }
- checkEqual(true, true, "map survives overflow");
- }
- void test() {
- testAdd();
- testMultipleAdd();
- testAddReplace();
- testClear();
- testOverflow();
- finalizeTest();
- }
- }
- int main() {
- Map::test();
- return 0;
- }
|