Refactors sequential&parallel test cases
[junction.git] / test / test.h
diff --git a/test/test.h b/test/test.h
new file mode 100644 (file)
index 0000000..1b54d13
--- /dev/null
@@ -0,0 +1,84 @@
+#ifndef _JUNCTION_TEST_H
+#define _JUNCTION_TEST_H
+
+#include <junction/ConcurrentMap_Crude.h>
+#include <junction/ConcurrentMap_Grampa.h>
+#include <junction/ConcurrentMap_Leapfrog.h>
+#include <junction/ConcurrentMap_Linear.h>
+
+#include <cds_test/stress_test.h>
+#include <cds_test/stress_test_util.h>
+
+#include <algorithm>
+#include <iostream>
+#include <memory>
+#include <random>
+#include <thread>
+
+namespace junction_test {
+
+typedef junction::ConcurrentMap_Grampa<size_t, size_t> GrampaMap;
+typedef junction::ConcurrentMap_Linear<size_t, size_t> LinearMap;
+typedef junction::ConcurrentMap_Leapfrog<size_t, size_t> LeapfrogMap;
+typedef junction::ConcurrentMap_Crude<size_t, size_t> CrudeMap;
+
+template <typename Map, typename Key, typename Value>
+static bool map_insert(Map* map, Key key, Value value) {
+  auto iter = map->insertOrFind(key);
+  if (!iter.getValue() || iter.getValue() != value) {
+    // Insert/update the <key,value> pair
+    iter.assignValue(value);
+    return true;
+  } else {
+    return false;
+  }
+}
+
+template <typename Map, typename Key>
+static bool map_delete(Map* map, Key key) {
+  auto iter = map->find(key);
+  if (iter.getValue()) {
+    iter.eraseValue();
+    return true;
+  } else {
+    return false;
+  }
+}
+
+template <typename Map, typename Key> static bool map_find(Map* map, Key key) {
+  auto iter = map->find(key);
+  if (iter.getValue()) {
+    return true;
+  } else {
+    return false;
+  }
+}
+
+// Specialization for CrudeMap
+template <typename Key, typename Value>
+static bool map_insert(CrudeMap* map, Key key, Value value) {
+  auto old_val = map->get(key);
+  if (!old_val || old_val != value) {
+    map->assign(key, value);
+    return true;
+  } else {
+    return false;
+  }
+}
+
+template <typename Key> static bool map_delete(CrudeMap* map, Key key) {
+  if (map->get(key)) {
+    map->assign(key, ((Key)0));
+    return true;
+  } else {
+    return false;
+  }
+}
+
+template <typename Key> static bool map_find(CrudeMap* map, Key key) {
+  return map->get(key) != ((Key)0);
+}
+
+} // namespace junction_test
+
+#endif