diff --git a/src/tests/BUILD b/src/tests/BUILD index 012cfda..34410fc 100644 --- a/src/tests/BUILD +++ b/src/tests/BUILD @@ -38,7 +38,7 @@ cc_test( cc_test( name = "attention_broker_server_test", - srcs = ["attention_broker_server_test.cc"], + srcs = ["attention_broker_server_test.cc", "test_utils.cc", "test_utils.h"], size = "small", copts = [ "-Iexternal/gtest/googletest/include", @@ -50,6 +50,7 @@ cc_test( "@com_github_grpc_grpc//:grpc++", "@com_github_grpc_grpc//:grpc++_reflection", "//attention_broker_server:attention_broker_server_lib", + "//utils:utils_lib", "@mbedcrypto//:lib", ], linkstatic = 1 @@ -107,7 +108,7 @@ cc_test( "@com_github_grpc_grpc//:grpc++", "@com_github_grpc_grpc//:grpc++_reflection", "//attention_broker_server:attention_broker_server_lib", - "//commons:commons_lib", + "//utils:utils_lib", "@mbedcrypto//:lib", ], linkstatic = 1 @@ -152,8 +153,8 @@ cc_test( ) cc_test( - name = "leadership_broker_test", - srcs = ["leadership_broker_test.cc", "test_utils.cc", "test_utils.h"], + name = "link_template_test", + srcs = ["link_template_test.cc", "test_utils.cc", "test_utils.h"], size = "small", copts = [ "-Iexternal/gtest/googletest/include", @@ -161,15 +162,87 @@ cc_test( ], deps = [ "@com_github_google_googletest//:gtest_main", - "//atom_space_node:atom_space_node_lib", - "//hasher:hasher_lib", + "//query_engine:query_engine_lib", + ], + linkopts = [ + #"-L/opt/3rd-party/mbedcrypto", + "-lmbedcrypto", + "-L/usr/local/lib", + "-lhiredis_cluster", + "-lhiredis", + "-lmongocxx", + "-lbsoncxx", + ], + linkstatic = 1 +) + +cc_test( + name = "nested_link_template_test", + srcs = ["nested_link_template_test.cc", "test_utils.cc", "test_utils.h"], + size = "small", + copts = [ + "-Iexternal/gtest/googletest/include", + "-Iexternal/gtest/googletest", + ], + deps = [ + "@com_github_google_googletest//:gtest_main", + "//query_engine:query_engine_lib", + ], + linkopts = [ + "-lmbedcrypto", + "-L/usr/local/lib", + "-lhiredis_cluster", + "-lhiredis", + "-lmongocxx", + "-lbsoncxx", + ], + linkstatic = 1 +) + +cc_test( + name = "query_answer_test", + srcs = ["query_answer_test.cc", "test_utils.cc", "test_utils.h"], + size = "small", + copts = [ + "-Iexternal/gtest/googletest/include", + "-Iexternal/gtest/googletest", + ], + deps = [ + "@com_github_google_googletest//:gtest_main", + "//query_engine:query_engine_lib", + ], + linkopts = [ ], linkstatic = 1 ) cc_test( - name = "message_broker_test", - srcs = ["message_broker_test.cc", "test_utils.cc", "test_utils.h"], + name = "and_operator_test", + srcs = ["and_operator_test.cc", "test_utils.cc", "test_utils.h"], + size = "medium", + copts = [ + "-Iexternal/gtest/googletest/include", + "-Iexternal/gtest/googletest", + ], + deps = [ + "@com_github_google_googletest//:gtest_main", + "//query_engine:query_engine_lib", + ], + linkopts = [ + #"-L/opt/3rd-party/mbedcrypto", + "-lmbedcrypto", + "-L/usr/local/lib", + "-lhiredis_cluster", + "-lhiredis", + "-lmongocxx", + "-lbsoncxx", + ], + linkstatic = 1 +) + +cc_test( + name = "iterator_test", + srcs = ["iterator_test.cc", "test_utils.cc", "test_utils.h"], size = "small", copts = [ "-Iexternal/gtest/googletest/include", @@ -177,16 +250,46 @@ cc_test( ], deps = [ "@com_github_google_googletest//:gtest_main", - "//cache_node:cache_node_lib", - "//atom_space_node:atom_space_node_lib", - "//hasher:hasher_lib", + "//query_engine:query_engine_lib", + ], + linkopts = [ + #"-L/opt/3rd-party/mbedcrypto", + "-lmbedcrypto", + "-L/usr/local/lib", + "-lhiredis_cluster", + "-lhiredis", + "-lmongocxx", + "-lbsoncxx", + ], + linkstatic = 1 +) + +cc_test( + name = "remote_sink_iterator_test", + srcs = ["remote_sink_iterator_test.cc", "test_utils.cc", "test_utils.h"], + size = "small", + copts = [ + "-Iexternal/gtest/googletest/include", + "-Iexternal/gtest/googletest", + ], + deps = [ + "@com_github_google_googletest//:gtest_main", + "//query_engine:query_engine_lib", + ], + linkopts = [ + "-lmbedcrypto", + "-L/usr/local/lib", + "-lhiredis_cluster", + "-lhiredis", + "-lmongocxx", + "-lbsoncxx", ], linkstatic = 1 ) cc_test( - name = "cache_node_test", - srcs = ["cache_node_test.cc", "test_utils.cc", "test_utils.h"], + name = "query_node_test", + srcs = ["query_node_test.cc"], size = "small", copts = [ "-Iexternal/gtest/googletest/include", @@ -194,9 +297,7 @@ cc_test( ], deps = [ "@com_github_google_googletest//:gtest_main", - "//cache_node:cache_node_lib", - "//atom_space_node:atom_space_node_lib", - "//hasher:hasher_lib", + "//query_engine:query_engine_lib", ], linkstatic = 1 ) diff --git a/src/tests/and_operator_test.cc b/src/tests/and_operator_test.cc new file mode 100644 index 0000000..29a05b7 --- /dev/null +++ b/src/tests/and_operator_test.cc @@ -0,0 +1,250 @@ +#include +#include +#include "gtest/gtest.h" + +#include "Source.h" +#include "Sink.h" +#include "QueryAnswer.h" +#include "And.h" +#include "test_utils.h" + +using namespace query_engine; +using namespace query_element; + +#define SLEEP_DURATION ((unsigned int) 500) + +class TestSource : public Source { + + public: + + TestSource(unsigned int count) { + this->id = "TestSource_" + to_string(count); + } + + ~TestSource() { + } + + void add( + const char *handle, + double importance, + const array &labels, + const array &values, + bool sleep_flag = true) { + + QueryAnswer *query_answer = new QueryAnswer(handle, importance); + for (unsigned int i = 0; i < labels.size(); i++) { + query_answer->assignment.assign(labels[i], values[i]); + } + this->output_buffer->add_query_answer(query_answer); + if (sleep_flag) { + Utils::sleep(SLEEP_DURATION); + } + } + + void query_answers_finished() { + return this->output_buffer->query_answers_finished(); + } +}; + +class TestSink : public Sink { + public: + TestSink(QueryElement *precedent) : + Sink(precedent, "TestSink(" + precedent->id + ")") { + } + ~TestSink() { + } + bool empty() { return this->input_buffer->is_query_answers_empty(); } + bool finished() { return this->input_buffer->is_query_answers_finished(); } + QueryAnswer *pop() { return this->input_buffer->pop_query_answer(); } +}; + +void check_query_answer( + string tag, + QueryAnswer *query_answer, + double importance, + unsigned int handles_size, + const array &handles) { + + cout << "check_query_answer(" + tag + ")" << endl; + EXPECT_TRUE(double_equals(query_answer->importance, importance)); + EXPECT_EQ(query_answer->handles_size, 2); + for (unsigned int i = 0; i < handles_size; i++) { + EXPECT_TRUE(strcmp(query_answer->handles[i], handles[i]) == 0); + } +} + +TEST(AndOperator, basics) { + + TestSource source1(1); + TestSource source2(2); + And<2> and_operator({&source1, &source2}); + TestSink sink(&and_operator); + QueryAnswer *query_answer; + + EXPECT_TRUE(sink.empty()); EXPECT_FALSE(sink.finished()); + + // -------------------------------------------------- + // Expected processing order: + // h1: 0.5, 0.4, 0.3 + // h2: 0.3, 0.2, 0.1 + // 0 0 - 0.15 + // 1 0 - 0.12 INVALID + // 0 1 - 0.10 + // 2 0 - 0.09 + // 1 1 - 0.08 + // 2 1 - 0.06 + // 0 2 - 0.05 + // 1 2 - 0.04 + // 2 2 - 0.03 + // -------------------------------------------------- + + source1.add("h1_0", 0.5, {"v1_0"}, {"1"}); + source2.add("h2_0", 0.3, {"v1_1"}, {"2"}); + source2.add("h2_1", 0.2, {"v2_1"}, {"1"}); + EXPECT_TRUE(sink.empty()); EXPECT_FALSE(sink.finished()); + source1.add("h1_1", 0.4, {"v1_1"}, {"1"}); + EXPECT_FALSE(sink.empty()); EXPECT_FALSE(sink.finished()); + EXPECT_FALSE((query_answer = sink.pop()) == NULL); + EXPECT_TRUE(sink.empty()); EXPECT_FALSE(sink.finished()); + check_query_answer("1", query_answer, 0.5, 2, {"h1_0", "h2_0"}); + EXPECT_TRUE(strcmp(query_answer->assignment.get("v1_0"), "1") == 0); + EXPECT_TRUE(strcmp(query_answer->assignment.get("v1_1"), "2") == 0); + source1.add("h1_2", 0.3, {"v1_2"}, {"1"}); + EXPECT_TRUE(sink.empty()); EXPECT_FALSE(sink.finished()); + source2.add("h2_2", 0.1, {"v2_2"}, {"1"}); + EXPECT_TRUE(sink.empty()); EXPECT_FALSE(sink.finished()); + source1.query_answers_finished(); + EXPECT_TRUE(sink.empty()); EXPECT_FALSE(sink.finished()); + source2.query_answers_finished(); + Utils::sleep(SLEEP_DURATION); + EXPECT_FALSE(sink.empty()); EXPECT_TRUE(sink.finished()); + + // {"h1_1", "h2_0"} is not popped because it's invalid + + EXPECT_FALSE((query_answer = sink.pop()) == NULL); + check_query_answer("3", query_answer, 0.5, 2, {"h1_0", "h2_1"}); + + EXPECT_FALSE((query_answer = sink.pop()) == NULL); + check_query_answer("4", query_answer, 0.3, 2, {"h1_2", "h2_0"}); + + EXPECT_FALSE((query_answer = sink.pop()) == NULL); + check_query_answer("5", query_answer, 0.4, 2, {"h1_1", "h2_1"}); + + EXPECT_FALSE((query_answer = sink.pop()) == NULL); + check_query_answer("6", query_answer, 0.3, 2, {"h1_2", "h2_1"}); + + EXPECT_FALSE((query_answer = sink.pop()) == NULL); + check_query_answer("7", query_answer, 0.5, 2, {"h1_0", "h2_2"}); + + EXPECT_FALSE((query_answer = sink.pop()) == NULL); + check_query_answer("8", query_answer, 0.4, 2, {"h1_1", "h2_2"}); + + EXPECT_FALSE((query_answer = sink.pop()) == NULL); + EXPECT_TRUE(sink.empty()); + check_query_answer("9", query_answer, 0.3, 2, {"h1_2", "h2_2"}); + + EXPECT_TRUE(sink.empty()); EXPECT_TRUE(sink.finished()); +} + +TEST(AndOperator, operation_logic) { + + class ImportanceFitnessPair { + public: + double importance; + double fitness; + ImportanceFitnessPair() {} + ImportanceFitnessPair(const ImportanceFitnessPair &other) { + this->importance = other.importance; + this->fitness = other.fitness; + } + ImportanceFitnessPair& operator=(const ImportanceFitnessPair &other) { + this->importance = other.importance; + this->fitness = other.fitness; + return *this; + } + bool operator<(const ImportanceFitnessPair &other) const { + return this->fitness < other.fitness; + } + bool operator>(const ImportanceFitnessPair &other) const { + return this->fitness > other.fitness; + } + }; + + cout << "SETUP" << endl; + + unsigned int clause_count = 3; + unsigned int link_count = 100; + array, 3> importance; + priority_queue fitness_heap; + ImportanceFitnessPair pair; + QueryAnswer *query_answer; + TestSource *source[3]; + for (unsigned int clause = 0; clause < clause_count; clause++) { + source[clause] = new TestSource(clause); + } + And<3> *and_operator = new And<3>((QueryElement **) source); + TestSink *sink = new TestSink(and_operator); + + for (unsigned int clause = 0; clause < clause_count; clause++) { + for (unsigned int link = 0; link < link_count; link++) { + importance[clause][link] = random_importance(); + } + std::sort( + std::begin(importance[clause]), + std::end(importance[clause]), + std::greater{}); + } + + cout << "QUEUES POPULATION" << endl; + + for (unsigned int clause = 0; clause < clause_count; clause++) { + for (unsigned int link = 0; link < link_count; link++) { + source[clause]->add( + random_handle().c_str(), + importance[clause][link], + {"v"}, + {"1"}, + false); + } + source[clause]->query_answers_finished(); + } + + cout << "MATRIX POPULATION" << endl; + + for (unsigned int i = 0; i < link_count; i++) { + for (unsigned int j = 0; j < link_count; j++) { + for (unsigned int k = 0; k < link_count; k++) { + pair.importance = importance[0][i] > importance[1][j] ? importance[0][i] : importance[1][j]; + pair.importance = importance[2][k] > pair.importance ? importance[2][k] : pair.importance; + pair.fitness = importance[0][i] * importance[1][j] * importance[2][k]; + fitness_heap.push(pair); + } + } + } + + Utils::sleep(5000); + cout << "TEST CHECKS" << endl; + + unsigned int count = 0; + while (! (sink->empty() && sink->finished())) { + if (sink->empty()) { + Utils::sleep(); + continue; + } + EXPECT_FALSE((query_answer = sink->pop()) == NULL); + pair = fitness_heap.top(); + cout << count << " CHECK: " << query_answer->importance << " " << pair.importance << " (" << pair.fitness << ")" << endl; + EXPECT_TRUE(double_equals(query_answer->importance, pair.importance)); + fitness_heap.pop(); + count++; + } + + Utils::sleep(5000); + cout << "TEAR DOWN" << endl; + + delete sink; + delete and_operator; + for (unsigned int clause = 0; clause < clause_count; clause++) { + delete source[clause]; + } +} diff --git a/src/tests/attention_broker_server_test.cc b/src/tests/attention_broker_server_test.cc index 05d8489..42856a7 100644 --- a/src/tests/attention_broker_server_test.cc +++ b/src/tests/attention_broker_server_test.cc @@ -12,8 +12,16 @@ #include "attention_broker.pb.h" #include "AttentionBrokerServer.h" +#include "Utils.h" +#include "test_utils.h" using namespace attention_broker_server; +using namespace commons; + +bool importance_equals(ImportanceType importance, double v2) { + double v1 = (double) importance; + return fabs(v2 - v1) < 0.001; +} TEST(AttentionBrokerTest, basics) { @@ -22,6 +30,7 @@ TEST(AttentionBrokerTest, basics) { dasproto::HandleCount handle_count; dasproto::HandleList handle_list; dasproto::Ack ack; + dasproto::ImportanceList importance_list; ServerContext *context = NULL; service.ping(context, &empty, &ack); @@ -30,4 +39,46 @@ TEST(AttentionBrokerTest, basics) { EXPECT_EQ(ack.msg(), "STIMULATE"); service.correlate(context, &handle_list, &ack); EXPECT_EQ(ack.msg(), "CORRELATE"); + service.get_importance(context, &handle_list, &importance_list); + EXPECT_EQ(importance_list.list_size(), 0); +} + +TEST(AttentionBrokerTest, get_importance) { + + string *handles = build_handle_space(4); + + AttentionBrokerServer service; + dasproto::HandleList handle_list0; + dasproto::HandleList handle_list1; + dasproto::HandleList handle_list2; + dasproto::HandleCount handle_count; + dasproto::Ack ack; + dasproto::ImportanceList importance_list1; + dasproto::ImportanceList importance_list2; + ServerContext *context = NULL; + + (*handle_count.mutable_map())[handles[0]] = 1; + (*handle_count.mutable_map())[handles[1]] = 1; + (*handle_count.mutable_map())["SUM"] = 2; + + handle_list0.add_list(handles[0]); + handle_list0.add_list(handles[1]); + handle_list0.add_list(handles[2]); + handle_list0.add_list(handles[3]); + handle_list1.add_list(handles[0]); + handle_list1.add_list(handles[1]); + handle_list2.add_list(handles[2]); + handle_list2.add_list(handles[3]); + + service.correlate(context, &handle_list0, &ack); + Utils::sleep(1000); + service.stimulate(context, &handle_count, &ack); + Utils::sleep(1000); + service.get_importance(context, &handle_list1, &importance_list1); + service.get_importance(context, &handle_list2, &importance_list2); + + EXPECT_TRUE(importance_list1.list(0) > 0.4); + EXPECT_TRUE(importance_list1.list(1) > 0.4); + EXPECT_TRUE(importance_list2.list(0) < 0.1); + EXPECT_TRUE(importance_list2.list(1) < 0.1); } diff --git a/src/tests/cache_node_test.cc b/src/tests/cache_node_test.cc deleted file mode 100644 index 0155770..0000000 --- a/src/tests/cache_node_test.cc +++ /dev/null @@ -1,58 +0,0 @@ -#include "gtest/gtest.h" -#include "CacheNode.h" - -using namespace atom_space_node; -using namespace cache_node; -using namespace std; - -TEST(CacheNode, basics) { - - string server_id = "localhost:35700"; - string client1_id = "localhost:35701"; - string client2_id = "localhost:35702"; - CacheNodeServer server(server_id); - CacheNodeClient client1(client1_id, server_id); - CacheNodeClient client2(client2_id, server_id); - - // Check properties before joining the network - - EXPECT_FALSE(server.is_leader()); - EXPECT_FALSE(client1.is_leader()); - EXPECT_FALSE(client2.is_leader()); - - EXPECT_FALSE(server.has_leader()); - EXPECT_FALSE(client1.has_leader()); - EXPECT_FALSE(client2.has_leader()); - - EXPECT_TRUE(server.leader_id() == ""); - EXPECT_TRUE(client1.leader_id() == ""); - EXPECT_TRUE(client2.leader_id() == ""); - - EXPECT_TRUE(server.node_id() == server_id); - EXPECT_TRUE(client1.node_id() == client1_id); - EXPECT_TRUE(client2.node_id() == client2_id); - - // Create network - - server.join_network(); - client1.join_network(); - client2.join_network(); - - // Check properties after joining the network - - EXPECT_TRUE(server.is_leader()); - EXPECT_FALSE(client1.is_leader()); - EXPECT_FALSE(client2.is_leader()); - - EXPECT_TRUE(server.has_leader()); - EXPECT_TRUE(client1.has_leader()); - EXPECT_TRUE(client2.has_leader()); - - EXPECT_TRUE(server.leader_id() == server_id); - EXPECT_TRUE(client1.leader_id() == server_id); - EXPECT_TRUE(client2.leader_id() == server_id); - - EXPECT_TRUE(server.node_id() == server_id); - EXPECT_TRUE(client1.node_id() == client1_id); - EXPECT_TRUE(client2.node_id() == client2_id); -} diff --git a/src/tests/hebbian_network_updater_test.cc b/src/tests/hebbian_network_updater_test.cc index 2b6ff85..5fd4db7 100644 --- a/src/tests/hebbian_network_updater_test.cc +++ b/src/tests/hebbian_network_updater_test.cc @@ -21,10 +21,10 @@ TEST(HebbianNetworkUpdater, correlation) { request = new dasproto::HandleList(); request->set_hebbian_network((unsigned long) network); - request->add_handle_list(handles[0]); - request->add_handle_list(handles[1]); - request->add_handle_list(handles[2]); - request->add_handle_list(handles[3]); + request->add_list(handles[0]); + request->add_list(handles[1]); + request->add_list(handles[2]); + request->add_list(handles[3]); updater->correlation(request); EXPECT_TRUE(network->get_node_count(handles[0]) == 1); @@ -48,10 +48,10 @@ TEST(HebbianNetworkUpdater, correlation) { request = new dasproto::HandleList(); request->set_hebbian_network((unsigned long) network); - request->add_handle_list(handles[1]); - request->add_handle_list(handles[2]); - request->add_handle_list(handles[4]); - request->add_handle_list(handles[5]); + request->add_list(handles[1]); + request->add_list(handles[2]); + request->add_list(handles[4]); + request->add_list(handles[5]); updater->correlation(request); EXPECT_TRUE(network->get_node_count(handles[0]) == 1); diff --git a/src/tests/iterator_test.cc b/src/tests/iterator_test.cc new file mode 100644 index 0000000..12879f1 --- /dev/null +++ b/src/tests/iterator_test.cc @@ -0,0 +1,123 @@ +#include +#include "gtest/gtest.h" + +#include "QueryNode.h" +#include "LinkTemplate.h" +#include "AtomDBSingleton.h" +#include "test_utils.h" +#include "Iterator.h" + +using namespace query_engine; +using namespace query_element; +using namespace query_node; + +class TestQueryElement : public QueryElement { + public: + TestQueryElement(const string &id) { + this->id = id; + } + void setup_buffers() {} + void graceful_shutdown() {} +}; + +TEST(Iterator, basics) { + string client_id = "no_query_element"; + TestQueryElement dummy(client_id); + Iterator query_answer_iterator(&dummy); + string server_id = query_answer_iterator.id; + EXPECT_FALSE(server_id == ""); + QueryNodeClient client_node(client_id, query_answer_iterator.id); + + EXPECT_FALSE(query_answer_iterator.finished()); + + QueryAnswer *qa; + QueryAnswer qa0("h0", 0.0); + QueryAnswer qa1("h1", 0.1); + QueryAnswer qa2("h2", 0.2); + + client_node.add_query_answer(&qa0); + client_node.add_query_answer(&qa1); + Utils::sleep(1000); + + EXPECT_FALSE(query_answer_iterator.finished()); + qa = query_answer_iterator.pop(); + EXPECT_TRUE(strcmp(qa->handles[0], "h0") == 0); + EXPECT_TRUE(double_equals(qa->importance, 0.0)); + + EXPECT_FALSE(query_answer_iterator.finished()); + qa = query_answer_iterator.pop(); + EXPECT_TRUE(strcmp(qa->handles[0], "h1") == 0); + EXPECT_TRUE(double_equals(qa->importance, 0.1)); + + qa = query_answer_iterator.pop(); + EXPECT_TRUE(qa == NULL); + EXPECT_FALSE(query_answer_iterator.finished()); + + client_node.add_query_answer(&qa2); + EXPECT_FALSE(client_node.is_query_answers_finished()); + EXPECT_FALSE(query_answer_iterator.finished()); + client_node.query_answers_finished(); + EXPECT_TRUE(client_node.is_query_answers_finished()); + EXPECT_FALSE(query_answer_iterator.finished()); + Utils::sleep(1000); + + EXPECT_FALSE(query_answer_iterator.finished()); + qa = query_answer_iterator.pop(); + EXPECT_TRUE(strcmp(qa->handles[0], "h2") == 0); + EXPECT_TRUE(double_equals(qa->importance, 0.2)); + EXPECT_TRUE(query_answer_iterator.finished()); +} + +TEST(Iterator, link_template_integration) { + + setenv("DAS_REDIS_HOSTNAME", "ninjato", 1); + setenv("DAS_REDIS_PORT", "29000", 1); + setenv("DAS_USE_REDIS_CLUSTER", "false", 1); + setenv("DAS_MONGODB_HOSTNAME", "ninjato", 1); + setenv("DAS_MONGODB_PORT", "28000", 1); + setenv("DAS_MONGODB_USERNAME", "dbadmin", 1); + setenv("DAS_MONGODB_PASSWORD", "dassecret", 1); + + AtomDBSingleton::init(); + string expression = "Expression"; + string symbol = "Symbol"; + + Variable v1("v1"); + Variable v2("v2"); + Variable v3("v3"); + Node similarity(symbol, "Similarity"); + Node human(symbol, "\"human\""); + + LinkTemplate<3> link_template("Expression", {&similarity, &human, &v1}); + Iterator query_answer_iterator(&link_template); + + string monkey_handle = string(terminal_hash((char *) symbol.c_str(), (char *) "\"monkey\"")); + string chimp_handle = string(terminal_hash((char *) symbol.c_str(), (char *) "\"chimp\"")); + string ent_handle = string(terminal_hash((char *) symbol.c_str(), (char *) "\"ent\"")); + bool monkey_flag = false; + bool chimp_flag = false; + bool ent_flag = false; + QueryAnswer *query_answer; + while (! query_answer_iterator.finished()) { + query_answer = query_answer_iterator.pop(); + if (query_answer != NULL) { + string var = string(query_answer->assignment.get("v1")); + EXPECT_TRUE(double_equals(query_answer->importance, 0.0)); + if (var == monkey_handle) { + // TODO: perform extra checks + monkey_flag = true; + } else if (var == chimp_handle) { + // TODO: perform extra checks + chimp_flag = true; + } else if (var == ent_handle) { + // TODO: perform extra checks + ent_flag = true; + } else { + FAIL(); + } + } + } + EXPECT_TRUE(monkey_flag); + EXPECT_TRUE(chimp_flag); + EXPECT_TRUE(ent_flag); +} diff --git a/src/tests/leadership_broker_test.cc b/src/tests/leadership_broker_test.cc deleted file mode 100644 index e66627a..0000000 --- a/src/tests/leadership_broker_test.cc +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -#include "gtest/gtest.h" -#include "test_utils.h" -#include "LeadershipBroker.h" - -using namespace atom_space_node; - -TEST(LeadershipBroker, basics) { - - try { - LeadershipBroker::factory((LeadershipBrokerType) -1); - FAIL() << "Expected exception"; - } catch(std::runtime_error const &error) { - } catch(...) { - FAIL() << "Expected std::runtime_error"; - } - - SingleMasterServer *leadership_broker = (SingleMasterServer *) - LeadershipBroker::factory(LeadershipBrokerType::SINGLE_MASTER_SERVER); - - EXPECT_EQ(leadership_broker->leader_id(), ""); - EXPECT_FALSE(leadership_broker->has_leader()); - leadership_broker->start_leader_election("blah"); - EXPECT_EQ(leadership_broker->leader_id(), "blah"); - EXPECT_TRUE(leadership_broker->has_leader()); -} diff --git a/src/tests/link_template_test.cc b/src/tests/link_template_test.cc new file mode 100644 index 0000000..5b0dbd6 --- /dev/null +++ b/src/tests/link_template_test.cc @@ -0,0 +1,67 @@ +#include +#include "gtest/gtest.h" + +#include "QueryNode.h" +#include "LinkTemplate.h" +#include "AtomDBSingleton.h" +#include "test_utils.h" + +using namespace query_engine; +using namespace query_element; + +TEST(LinkTemplate, basics) { + + setenv("DAS_REDIS_HOSTNAME", "ninjato", 1); + setenv("DAS_REDIS_PORT", "29000", 1); + setenv("DAS_USE_REDIS_CLUSTER", "false", 1); + setenv("DAS_MONGODB_HOSTNAME", "ninjato", 1); + setenv("DAS_MONGODB_PORT", "28000", 1); + setenv("DAS_MONGODB_USERNAME", "dbadmin", 1); + setenv("DAS_MONGODB_PASSWORD", "dassecret", 1); + + string server_node_id = "SERVER"; + QueryNodeServer server_node(server_node_id); + + AtomDBSingleton::init(); + string expression = "Expression"; + string symbol = "Symbol"; + + Variable v1("v1"); + Variable v2("v2"); + Variable v3("v3"); + Node similarity(symbol, "Similarity"); + Node human(symbol, "\"human\""); + + LinkTemplate<3> link_template1("Expression", {&similarity, &human, &v1}); + link_template1.subsequent_id = server_node_id; + link_template1.setup_buffers(); + //link_template1.fetch_links(); + Utils::sleep(1000); + + string monkey_handle = string(terminal_hash((char *) symbol.c_str(), (char *) "\"monkey\"")); + string chimp_handle = string(terminal_hash((char *) symbol.c_str(), (char *) "\"chimp\"")); + string ent_handle = string(terminal_hash((char *) symbol.c_str(), (char *) "\"ent\"")); + bool monkey_flag = false; + bool chimp_flag = false; + bool ent_flag = false; + QueryAnswer *query_answer; + while ((query_answer = server_node.pop_query_answer()) != NULL) { + string var = string(query_answer->assignment.get("v1")); + EXPECT_TRUE(double_equals(query_answer->importance, 0.0)); + if (var == monkey_handle) { + // TODO: perform extra checks + monkey_flag = true; + } else if (var == chimp_handle) { + // TODO: perform extra checks + chimp_flag = true; + } else if (var == ent_handle) { + // TODO: perform extra checks + ent_flag = true; + } else { + FAIL(); + } + } + EXPECT_TRUE(monkey_flag); + EXPECT_TRUE(chimp_flag); + EXPECT_TRUE(ent_flag); +} diff --git a/src/tests/message_broker_test.cc b/src/tests/message_broker_test.cc deleted file mode 100644 index f8abc21..0000000 --- a/src/tests/message_broker_test.cc +++ /dev/null @@ -1,196 +0,0 @@ -#include -#include - -#include "gtest/gtest.h" -#include "test_utils.h" -#include "Utils.h" -#include "MessageBroker.h" -#include "CacheNode.h" - -using namespace atom_space_node; -using namespace cache_node; - -class TestNode { -public: - string command; - vector args; - unsigned int node_joined_network_count; -}; - -class TestCacheNodeClient; - -class TestMessage : public Message { -public: - string command; - vector args; - TestMessage(string command, vector args) { - this->command = command; - this->args = args; - } - void act(AtomSpaceNode *_node); -}; - -class TestCacheNodeServer : public TestNode, public CacheNodeServer { -public: - TestCacheNodeServer(const string &node_id) : CacheNodeServer(node_id) { - this->command = ""; - this->node_joined_network_count = 0; - } - void node_joined_network(const string &node_id) { - CacheNodeServer::node_joined_network(node_id); - this->node_joined_network_count += 1; - } - Message *message_factory(string &command, vector &args) { - Message *message = CacheNode::message_factory(command, args); - if (message != NULL) { - return message; - } - if (command == "c1" || command == "c2" || command == "c3") { - return new TestMessage(command, args); - } - return NULL; - } -}; - -class TestCacheNodeClient : public TestNode, public CacheNodeClient { -public: - TestCacheNodeClient(const string &node_id, string &server_id) : CacheNodeClient(node_id, server_id) { - this->command = ""; - this->node_joined_network_count = 0; - } - void node_joined_network(const string &node_id) { - CacheNodeClient::node_joined_network(node_id); - this->node_joined_network_count += 1; - } - Message *message_factory(string &command, vector &args) { - Message *message = CacheNode::message_factory(command, args); - if (message != NULL) { - return message; - } - if (command == "c1" || command == "c2" || command == "c3") { - return new TestMessage(command, args); - } - return NULL; - } -}; - -void TestMessage::act(AtomSpaceNode *_node) { - TestCacheNodeClient *node = (TestCacheNodeClient *) _node; - ((TestNode *) node)->command = this->command; - ((TestNode *) node)->args = this->args; -} - -TEST(MessageBroker, basics) { - - try { - MessageBroker::factory((MessageBrokerType) -1, NULL, ""); - FAIL() << "Expected exception"; - } catch(std::runtime_error const &error) { - } catch(...) { - FAIL() << "Expected std::runtime_error"; - } - - SynchronousGRPC *message_broker = (SynchronousGRPC *) - MessageBroker::factory(MessageBrokerType::GRPC, NULL, ""); - EXPECT_TRUE(message_broker != NULL); -} - -TEST(CacheNode, basics) { - - string server_id = "localhost:30700"; - string client1_id = "localhost:30701"; - string client2_id = "localhost:30702"; - - CacheNodeServer server(server_id); - EXPECT_FALSE(server.is_leader()); - EXPECT_FALSE(server.has_leader()); - EXPECT_EQ(server.leader_id(), ""); - EXPECT_EQ(server.node_id(), server_id); - server.join_network(); - EXPECT_TRUE(server.is_leader()); - EXPECT_TRUE(server.has_leader()); - EXPECT_EQ(server.leader_id(), server_id); - EXPECT_EQ(server.node_id(), server_id); - - CacheNodeClient client1(client1_id, server_id); - CacheNodeClient client2(client2_id, server_id); - - EXPECT_FALSE(client1.is_leader()); - EXPECT_FALSE(client2.is_leader()); - - EXPECT_FALSE(client1.has_leader()); - EXPECT_FALSE(client2.has_leader()); - - EXPECT_EQ(client1.leader_id(), ""); - EXPECT_EQ(client2.leader_id(), ""); - - EXPECT_EQ(client1.node_id(), client1_id); - EXPECT_EQ(client2.node_id(), client2_id); - - client1.join_network(); - client2.join_network(); - - EXPECT_FALSE(client1.is_leader()); - EXPECT_FALSE(client2.is_leader()); - - EXPECT_TRUE(client1.has_leader()); - EXPECT_TRUE(client2.has_leader()); - - EXPECT_EQ(client1.leader_id(), server_id); - EXPECT_EQ(client2.leader_id(), server_id); - - EXPECT_EQ(client1.node_id(), client1_id); - EXPECT_EQ(client2.node_id(), client2_id); -} - -TEST(MessageBroker, communication) { - string server_id = "localhost:31700"; - string client1_id = "localhost:31701"; - string client2_id = "localhost:31702"; - - TestCacheNodeServer server(server_id); - server.join_network(); - Utils::sleep(1000); - - TestCacheNodeClient client1(client1_id, server_id); - client1.join_network(); - Utils::sleep(1000); - - TestCacheNodeClient client2(client2_id, server_id); - client2.join_network(); - Utils::sleep(1000); - - EXPECT_EQ(server.command, ""); - EXPECT_EQ(server.args.size(), 0); - EXPECT_EQ(client1.command, ""); - EXPECT_EQ(client1.args.size(), 0); - EXPECT_EQ(client2.command, ""); - EXPECT_EQ(client2.args.size(), 0); - - EXPECT_EQ(server.node_joined_network_count, 2); - EXPECT_EQ(client1.node_joined_network_count, 1); - EXPECT_EQ(client2.node_joined_network_count, 0); // TODO Fix this. This count should be 2 - - vector args1 = {"a", "b"}; - server.broadcast("c1", args1); - Utils::sleep(1000); - - EXPECT_EQ(server.command, ""); - EXPECT_EQ(server.args.size(), 0); - EXPECT_EQ(client1.command, "c1"); - EXPECT_EQ(client1.args, args1); - EXPECT_EQ(client2.command, "c1"); - EXPECT_EQ(client2.args, args1); - - vector args2 = {"a2", "b2"}; - server.send("c2", args2, client1_id); - Utils::sleep(1000); - - EXPECT_EQ(server.command, ""); - EXPECT_EQ(server.args.size(), 0); - EXPECT_EQ(client1.command, "c2"); - EXPECT_EQ(client1.args, args2); - EXPECT_EQ(client2.command, "c1"); - EXPECT_EQ(client2.args, args1); -} - diff --git a/src/tests/nested_link_template_test.cc b/src/tests/nested_link_template_test.cc new file mode 100644 index 0000000..8268fd1 --- /dev/null +++ b/src/tests/nested_link_template_test.cc @@ -0,0 +1,84 @@ +#include +#include "gtest/gtest.h" + +#include "QueryNode.h" +#include "AtomDB.h" +#include "LinkTemplate.h" +#include "AtomDBSingleton.h" +#include "test_utils.h" + +using namespace query_engine; +using namespace query_element; + +TEST(LinkTemplate, basics) { + + setenv("DAS_REDIS_HOSTNAME", "ninjato", 1); + setenv("DAS_REDIS_PORT", "29000", 1); + setenv("DAS_USE_REDIS_CLUSTER", "false", 1); + setenv("DAS_MONGODB_HOSTNAME", "ninjato", 1); + setenv("DAS_MONGODB_PORT", "28000", 1); + setenv("DAS_MONGODB_USERNAME", "dbadmin", 1); + setenv("DAS_MONGODB_PASSWORD", "dassecret", 1); + + string server_node_id = "SERVER"; + QueryNodeServer server_node(server_node_id); + + AtomDBSingleton::init(); + string expression = "Expression"; + string symbol = "Symbol"; + + Variable v1("v1"); + Variable v2("v2"); + Variable v3("v3"); + Node similarity(symbol, "Similarity"); + Node odd_link(symbol, "OddLink"); + + LinkTemplate<3> inner_template("Expression", {&similarity, &v1, &v2}); + LinkTemplate<2> outter_template("Expression", {&odd_link, &inner_template}); + + outter_template.subsequent_id = server_node_id; + outter_template.setup_buffers(); + Utils::sleep(5000); + + QueryAnswer *query_answer; + unsigned int count = 0; + while ((query_answer = server_node.pop_query_answer()) != NULL) { + EXPECT_TRUE(double_equals(query_answer->importance, 0.0)); + count++; + } + EXPECT_EQ(count, 8); +} + +TEST(LinkTemplate, nested_variables) { + + string server_node_id = "SERVER"; + QueryNodeServer server_node(server_node_id); + + string expression = "Expression"; + string symbol = "Symbol"; + + Variable v1("v1"); + Variable v2("v2"); + Variable v3("v3"); + Node similarity(symbol, "Similarity"); + Node odd_link(symbol, "OddLink"); + Node human(symbol, "\"human\""); + + LinkTemplate<3> inner_template("Expression", {&similarity, &v1, &v2}); + LinkTemplate<2> outter_template("Expression", {&odd_link, &inner_template}); + LinkTemplate<3> human_template("Expression", {&similarity, &v1, &human}); + And<2> and_operator({&human_template, &outter_template}); + + and_operator.subsequent_id = server_node_id; + and_operator.setup_buffers(); + Utils::sleep(5000); + + QueryAnswer *query_answer; + shared_ptr document; + unsigned int count = 0; + while ((query_answer = server_node.pop_query_answer()) != NULL) { + EXPECT_TRUE(double_equals(query_answer->importance, 0.0)); + count++; + } + EXPECT_EQ(count, 1); +} diff --git a/src/tests/query_answer_test.cc b/src/tests/query_answer_test.cc new file mode 100644 index 0000000..446c1ef --- /dev/null +++ b/src/tests/query_answer_test.cc @@ -0,0 +1,113 @@ +#include +#include +#include "gtest/gtest.h" + +#include "QueryAnswer.h" +#include "test_utils.h" + +using namespace query_engine; + +TEST(QueryAnswer, assignments_basics) { + + Assignment mapping0; + + // Tests assign() + Assignment mapping1; + EXPECT_TRUE(mapping1.assign("v1", "1")); + EXPECT_TRUE(mapping1.assign("v2", "2")); + EXPECT_TRUE(mapping1.assign("v2", "2")); + EXPECT_FALSE(mapping1.assign("v2", "3")); + Assignment mapping2; + EXPECT_TRUE(mapping1.assign("v1", "1")); + EXPECT_TRUE(mapping1.assign("v3", "3")); + Assignment mapping3; + EXPECT_TRUE(mapping3.assign("v1", "1")); + EXPECT_TRUE(mapping3.assign("v2", "3")); + + // Tests get() + EXPECT_TRUE(strcmp(mapping1.get("v1"), "1") == 0); + EXPECT_TRUE(strcmp(mapping1.get("v2"), "2") == 0); + EXPECT_TRUE(mapping1.get("blah") == NULL); + EXPECT_TRUE(mapping1.get("v11") == NULL); + EXPECT_TRUE(mapping1.get("v") == NULL); + EXPECT_TRUE(mapping1.get("") == NULL); + + // Tests is_compatible() + EXPECT_TRUE(mapping1.is_compatible(mapping0)); + EXPECT_TRUE(mapping2.is_compatible(mapping0)); + EXPECT_TRUE(mapping3.is_compatible(mapping0)); + EXPECT_TRUE(mapping0.is_compatible(mapping1)); + EXPECT_TRUE(mapping0.is_compatible(mapping2)); + EXPECT_TRUE(mapping0.is_compatible(mapping3)); + EXPECT_TRUE(mapping1.is_compatible(mapping2)); + EXPECT_TRUE(mapping2.is_compatible(mapping1)); + EXPECT_TRUE(mapping2.is_compatible(mapping3)); + EXPECT_TRUE(mapping3.is_compatible(mapping2)); + EXPECT_FALSE(mapping1.is_compatible(mapping3)); + EXPECT_FALSE(mapping3.is_compatible(mapping1)); + + // Tests copy_from() + Assignment mapping4; + mapping4.copy_from(mapping1); + EXPECT_TRUE(strcmp(mapping4.get("v1"), "1") == 0); + EXPECT_TRUE(strcmp(mapping4.get("v2"), "2") == 0); + EXPECT_TRUE(mapping4.is_compatible(mapping2)); + EXPECT_TRUE(mapping2.is_compatible(mapping4)); + EXPECT_FALSE(mapping4.is_compatible(mapping3)); + EXPECT_FALSE(mapping3.is_compatible(mapping4)); + + // Tests add_assignments() + mapping4.add_assignments(mapping1); + mapping4.add_assignments(mapping2); + EXPECT_TRUE(strcmp(mapping4.get("v1"), "1") == 0); + EXPECT_TRUE(strcmp(mapping4.get("v2"), "2") == 0); + EXPECT_TRUE(strcmp(mapping4.get("v3"), "3") == 0); + EXPECT_TRUE(mapping1.is_compatible(mapping4)); + EXPECT_TRUE(mapping2.is_compatible(mapping4)); + EXPECT_FALSE(mapping3.is_compatible(mapping4)); + EXPECT_TRUE(mapping4.is_compatible(mapping1)); + EXPECT_TRUE(mapping4.is_compatible(mapping2)); + EXPECT_FALSE(mapping4.is_compatible(mapping3)); + + // Tests to_string(): + EXPECT_TRUE(mapping0.to_string() != ""); + EXPECT_TRUE(mapping1.to_string() != ""); + EXPECT_TRUE(mapping4.to_string() != ""); +} + +TEST(QueryAnswer, query_answer_basics) { + + // Tests add_handle() + QueryAnswer query_answer1("h1", 0); + query_answer1.assignment.assign("v1", "1"); + EXPECT_EQ(query_answer1.handles_size, 1); + EXPECT_TRUE(strcmp(query_answer1.handles[0], "h1") == 0); + query_answer1.add_handle("hx"); + EXPECT_EQ(query_answer1.handles_size, 2); + EXPECT_TRUE(strcmp(query_answer1.handles[0], "h1") == 0); + EXPECT_TRUE(strcmp(query_answer1.handles[1], "hx") == 0); + + // Tests merge() + QueryAnswer query_answer2("h2", 0); + query_answer2.assignment.assign("v2", "2"); + query_answer2.add_handle("hx"); + query_answer2.merge(&query_answer1); + EXPECT_EQ(query_answer2.handles_size, 3); + EXPECT_TRUE(strcmp(query_answer2.handles[0], "h2") == 0); + EXPECT_TRUE(strcmp(query_answer2.handles[1], "hx") == 0); + EXPECT_TRUE(strcmp(query_answer2.handles[2], "h1") == 0); + EXPECT_FALSE(query_answer2.assignment.assign("v1", "x")); + EXPECT_FALSE(query_answer2.assignment.assign("v2", "x")); + EXPECT_TRUE(query_answer2.assignment.assign("v3", "x")); + + // Tests copy() + QueryAnswer *query_answer3 = QueryAnswer::copy(&query_answer2); + EXPECT_EQ(query_answer3->handles_size, 3); + EXPECT_TRUE(strcmp(query_answer3->handles[0], "h2") == 0); + EXPECT_TRUE(strcmp(query_answer3->handles[1], "hx") == 0); + EXPECT_TRUE(strcmp(query_answer3->handles[2], "h1") == 0); + EXPECT_FALSE(query_answer3->assignment.assign("v1", "x")); + EXPECT_FALSE(query_answer3->assignment.assign("v2", "x")); + EXPECT_FALSE(query_answer3->assignment.assign("v3", "y")); + EXPECT_TRUE(query_answer3->assignment.assign("v4", "x")); +} diff --git a/src/tests/query_node_test.cc b/src/tests/query_node_test.cc new file mode 100644 index 0000000..5c08c4f --- /dev/null +++ b/src/tests/query_node_test.cc @@ -0,0 +1,69 @@ +#include "gtest/gtest.h" + +#include "Utils.h" +#include "QueryNode.h" +#include "QueryAnswer.h" + +using namespace commons; +using namespace query_node; + +TEST(QueryNode, basics) { + + string server_id = "server"; + string client1_id = "client1"; + string client2_id = "client2"; + + QueryNodeServer server(server_id); + QueryNodeClient client1(client1_id, server_id); + QueryNodeClient client2(client2_id, server_id); + + EXPECT_TRUE(server.is_query_answers_empty()); + EXPECT_FALSE(server.is_query_answers_finished()); + EXPECT_TRUE(client1.is_query_answers_empty()); + EXPECT_FALSE(client1.is_query_answers_finished()); + EXPECT_TRUE(client2.is_query_answers_empty()); + EXPECT_FALSE(client2.is_query_answers_finished()); + + ASSERT_TRUE(server.pop_query_answer() == (QueryAnswer *) 0); + + client1.add_query_answer((QueryAnswer *) 1); + client1.add_query_answer((QueryAnswer *) 2); + Utils::sleep(1000); + client2.add_query_answer((QueryAnswer *) 3); + client2.add_query_answer((QueryAnswer *) 4); + Utils::sleep(1000); + client1.add_query_answer((QueryAnswer *) 5); + Utils::sleep(1000); + + EXPECT_FALSE(server.is_query_answers_empty()); + EXPECT_FALSE(server.is_query_answers_finished()); + EXPECT_TRUE(client1.is_query_answers_empty()); + EXPECT_FALSE(client1.is_query_answers_finished()); + EXPECT_TRUE(client2.is_query_answers_empty()); + EXPECT_FALSE(client2.is_query_answers_finished()); + + client1.query_answers_finished(); + client2.query_answers_finished(); + Utils::sleep(1000); + + EXPECT_FALSE(server.is_query_answers_empty()); + EXPECT_TRUE(server.is_query_answers_finished()); + EXPECT_TRUE(client1.is_query_answers_empty()); + EXPECT_TRUE(client1.is_query_answers_finished()); + EXPECT_TRUE(client2.is_query_answers_empty()); + EXPECT_TRUE(client2.is_query_answers_finished()); + + ASSERT_TRUE(server.pop_query_answer() == (QueryAnswer *) 1); + ASSERT_TRUE(server.pop_query_answer() == (QueryAnswer *) 2); + ASSERT_TRUE(server.pop_query_answer() == (QueryAnswer *) 3); + ASSERT_TRUE(server.pop_query_answer() == (QueryAnswer *) 4); + ASSERT_TRUE(server.pop_query_answer() == (QueryAnswer *) 5); + ASSERT_TRUE(server.pop_query_answer() == (QueryAnswer *) 0); + + EXPECT_TRUE(server.is_query_answers_empty()); + EXPECT_TRUE(server.is_query_answers_finished()); + EXPECT_TRUE(client1.is_query_answers_empty()); + EXPECT_TRUE(client1.is_query_answers_finished()); + EXPECT_TRUE(client2.is_query_answers_empty()); + EXPECT_TRUE(client2.is_query_answers_finished()); +} diff --git a/src/tests/remote_sink_iterator_test.cc b/src/tests/remote_sink_iterator_test.cc new file mode 100644 index 0000000..c275ca5 --- /dev/null +++ b/src/tests/remote_sink_iterator_test.cc @@ -0,0 +1,72 @@ +#include +#include "gtest/gtest.h" + +#include "RemoteSink.h" +#include "Source.h" +#include "RemoteIterator.h" +#include "AtomDBSingleton.h" +#include "test_utils.h" + +using namespace query_engine; +using namespace query_element; +using namespace query_node; + +class TestSource : public Source { + public: + TestSource(const string &id) { + this->id = id; + } + void add(QueryAnswer *qa) { + this->output_buffer->add_query_answer(qa); + Utils::sleep(1000); + } + void finished() { + this->output_buffer->query_answers_finished(); + Utils::sleep(1000); + } +}; + +TEST(RemoteSinkIterator, basics) { + string producer_id = "localhost:30700"; + string consumer_id = "localhost:30701"; + + string input_element_id = "test_source"; + TestSource input(input_element_id); + RemoteIterator consumer(consumer_id); + RemoteSink producer(&input, producer_id, consumer_id); + producer.setup_buffers(); + Utils::sleep(1000); + + EXPECT_FALSE(consumer.finished()); + + QueryAnswer *qa; + QueryAnswer qa0("h0", 0.0); + QueryAnswer qa1("h1", 0.1); + QueryAnswer qa2("h2", 0.2); + + input.add(&qa0); + input.add(&qa1); + + EXPECT_FALSE(consumer.finished()); + EXPECT_FALSE((qa = consumer.pop()) == NULL); + EXPECT_TRUE(strcmp(qa->handles[0], "h0") == 0); + EXPECT_TRUE(double_equals(qa->importance, 0.0)); + + EXPECT_FALSE(consumer.finished()); + EXPECT_FALSE((qa = consumer.pop()) == NULL); + EXPECT_TRUE(strcmp(qa->handles[0], "h1") == 0); + EXPECT_TRUE(double_equals(qa->importance, 0.1)); + + EXPECT_TRUE((qa = consumer.pop()) == NULL); + EXPECT_FALSE(consumer.finished()); + + input.add(&qa2); + input.finished(); + EXPECT_FALSE(consumer.finished()); + + EXPECT_FALSE(consumer.finished()); + EXPECT_FALSE((qa = consumer.pop()) == NULL); + EXPECT_TRUE(strcmp(qa->handles[0], "h2") == 0); + EXPECT_TRUE(double_equals(qa->importance, 0.2)); + EXPECT_TRUE(consumer.finished()); +} diff --git a/src/tests/stimulus_spreader_test.cc b/src/tests/stimulus_spreader_test.cc index 6a71aae..6e99f89 100644 --- a/src/tests/stimulus_spreader_test.cc +++ b/src/tests/stimulus_spreader_test.cc @@ -35,12 +35,12 @@ TEST(TokenSpreader, distribute_wages) { tokens_to_spread = 1.0; request = new dasproto::HandleCount(); - (*request->mutable_handle_count())[handles[0]] = 2; - (*request->mutable_handle_count())[handles[1]] = 1; - (*request->mutable_handle_count())[handles[2]] = 2; - (*request->mutable_handle_count())[handles[3]] = 1; - (*request->mutable_handle_count())[handles[4]] = 2; - (*request->mutable_handle_count())["SUM"] = 8; + (*request->mutable_map())[handles[0]] = 2; + (*request->mutable_map())[handles[1]] = 1; + (*request->mutable_map())[handles[2]] = 2; + (*request->mutable_map())[handles[3]] = 1; + (*request->mutable_map())[handles[4]] = 2; + (*request->mutable_map())["SUM"] = 8; data.importance_changes = new HandleTrie(HANDLE_HASH_SIZE - 1); spreader->distribute_wages(request, tokens_to_spread, &data); @@ -68,18 +68,18 @@ static HebbianNetwork *build_test_network(string *handles) { request = new dasproto::HandleList(); request->set_hebbian_network((unsigned long) network); - request->add_handle_list(handles[0]); - request->add_handle_list(handles[1]); - request->add_handle_list(handles[2]); - request->add_handle_list(handles[3]); + request->add_list(handles[0]); + request->add_list(handles[1]); + request->add_list(handles[2]); + request->add_list(handles[3]); updater->correlation(request); request = new dasproto::HandleList(); request->set_hebbian_network((unsigned long) network); - request->add_handle_list(handles[1]); - request->add_handle_list(handles[2]); - request->add_handle_list(handles[4]); - request->add_handle_list(handles[5]); + request->add_list(handles[1]); + request->add_list(handles[2]); + request->add_list(handles[4]); + request->add_list(handles[5]); updater->correlation(request); return network; @@ -132,14 +132,14 @@ TEST(TokenSpreader, spread_stimuli) { request = new dasproto::HandleCount(); request->set_hebbian_network((unsigned long) network); - (*request->mutable_handle_count())[handles[0]] = 1; - (*request->mutable_handle_count())[handles[1]] = 1; - (*request->mutable_handle_count())[handles[2]] = 1; - (*request->mutable_handle_count())[handles[3]] = 1; - (*request->mutable_handle_count())[handles[4]] = 1; - (*request->mutable_handle_count())[handles[5]] = 1; - (*request->mutable_handle_count())["SUM"] = 6; - unsigned int SUM = (*request->mutable_handle_count())["SUM"]; + (*request->mutable_map())[handles[0]] = 1; + (*request->mutable_map())[handles[1]] = 1; + (*request->mutable_map())[handles[2]] = 1; + (*request->mutable_map())[handles[3]] = 1; + (*request->mutable_map())[handles[4]] = 1; + (*request->mutable_map())[handles[5]] = 1; + (*request->mutable_map())["SUM"] = 6; + unsigned int SUM = (*request->mutable_map())["SUM"]; spreader->spread_stimuli(request); // ---------------------------------------------------------- @@ -165,7 +165,7 @@ TEST(TokenSpreader, spread_stimuli) { double wages[6]; for (unsigned int i = 0; i < 6; i++) { - wages[i] = ((double) ((*request->mutable_handle_count())[handles[i]])) / SUM * total_wages; + wages[i] = ((double) ((*request->mutable_map())[handles[i]])) / SUM * total_wages; } double updated[6]; diff --git a/src/tests/test_utils.cc b/src/tests/test_utils.cc index 822892a..6c5ab25 100644 --- a/src/tests/test_utils.cc +++ b/src/tests/test_utils.cc @@ -23,6 +23,10 @@ static char REVERSE_TLB[16] = { 'f', }; +double random_importance() { + return ((double) rand()) / RAND_MAX; +} + string random_handle() { char buffer[HANDLE_HASH_SIZE]; unsigned int key_size = HANDLE_HASH_SIZE - 1; @@ -63,3 +67,7 @@ string *build_handle_space(unsigned int size, bool sort) { } return answer; } + +bool double_equals(double v1, double v2) { + return fabs(v2 - v1) < 0.001; +} diff --git a/src/tests/test_utils.h b/src/tests/test_utils.h index cc915cf..1c35a80 100644 --- a/src/tests/test_utils.h +++ b/src/tests/test_utils.h @@ -2,10 +2,14 @@ #define _ATTENTION_BROKER_SERVER_TESTS_TESTUTILS #include +#include + using namespace std; +double random_importance(); string random_handle(); string prefixed_random_handle(string prefix); string *build_handle_space(unsigned int size, bool sort=false); +bool double_equals(double v1, double v2); #endif diff --git a/src/tests/worker_threads_test.cc b/src/tests/worker_threads_test.cc index 13508f3..de3477a 100644 --- a/src/tests/worker_threads_test.cc +++ b/src/tests/worker_threads_test.cc @@ -72,37 +72,37 @@ TEST(WorkerThreads, hebbian_network_updater_basics) { string h2 = random_handle(); string h3 = random_handle(); string h4 = random_handle(); - handle_list->add_handle_list(h1); - handle_list->add_handle_list(h2); - handle_list->add_handle_list(h3); - handle_list->add_handle_list(h4); + handle_list->add_list(h1); + handle_list->add_list(h2); + handle_list->add_list(h3); + handle_list->add_list(h4); handle_list->set_hebbian_network((long) &network); correlation->enqueue(handle_list); handle_list = new dasproto::HandleList(); string h5 = random_handle(); - handle_list->add_handle_list(h1); - handle_list->add_handle_list(h2); - handle_list->add_handle_list(h5); + handle_list->add_list(h1); + handle_list->add_list(h2); + handle_list->add_list(h5); handle_list->set_hebbian_network((long) &network); correlation->enqueue(handle_list); handle_list = new dasproto::HandleList(); - handle_list->add_handle_list(h2); - handle_list->add_handle_list(h5); + handle_list->add_list(h2); + handle_list->add_list(h5); handle_list->set_hebbian_network((long) &network); correlation->enqueue(handle_list); string h6 = random_handle(); handle_list = new dasproto::HandleList(); - handle_list->add_handle_list(h6); - handle_list->add_handle_list(h6); + handle_list->add_list(h6); + handle_list->add_list(h6); handle_list->set_hebbian_network((long) &network); correlation->enqueue(handle_list); handle_list = new dasproto::HandleList(); - handle_list->add_handle_list(h1); - handle_list->add_handle_list(h1); + handle_list->add_list(h1); + handle_list->add_list(h1); handle_list->set_hebbian_network((long) &network); correlation->enqueue(handle_list); @@ -170,14 +170,14 @@ TEST(WorkerThreads, hebbian_network_updater_stress) { unsigned int num_handles = (rand() % (max_handles_per_request - 1)) + 2; for (unsigned int j = 0; j < num_handles; j++) { string h = handles[rand() % HANDLE_SPACE_SIZE]; - handle_list->add_handle_list(h); + handle_list->add_list(h); if (node_count.find(h) == node_count.end()) { node_count[h] = 0; } node_count[h] = node_count[h] + 1; } - for (const string &h1: handle_list->handle_list()) { - for (const string &h2: handle_list->handle_list()) { + for (const string &h1: handle_list->list()) { + for (const string &h2: handle_list->list()) { string composite; if (h1.compare(h2) < 0) { composite = h1 + h2;