2021-01-29 21:32:57 +08:00
|
|
|
// eventpp library
|
|
|
|
// Copyright (C) 2018 Wang Qi (wqking)
|
|
|
|
// Github: https://github.com/wqking/eventpp
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "test.h"
|
2021-01-30 19:17:49 +08:00
|
|
|
#include "eventpp/utilities/anyid.h"
|
2021-01-29 21:32:57 +08:00
|
|
|
#include "eventpp/eventqueue.h"
|
|
|
|
#include "eventpp/eventdispatcher.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2021-01-30 19:17:49 +08:00
|
|
|
#include <any>
|
2021-01-29 21:32:57 +08:00
|
|
|
|
2021-01-31 16:54:25 +08:00
|
|
|
TEST_CASE("AnyId, default, unordered_map")
|
2021-01-29 21:32:57 +08:00
|
|
|
{
|
2021-01-30 19:17:49 +08:00
|
|
|
eventpp::EventQueue<eventpp::AnyHashableId, void()> eventQueue;
|
2021-01-29 21:32:57 +08:00
|
|
|
|
|
|
|
std::vector<int> dataList(3);
|
|
|
|
|
2021-01-30 19:17:49 +08:00
|
|
|
eventQueue.appendListener(3, [&dataList]() {
|
2021-01-29 21:32:57 +08:00
|
|
|
++dataList[0];
|
|
|
|
});
|
2021-01-30 19:17:49 +08:00
|
|
|
eventQueue.appendListener(std::string("hello"), [&dataList]() {
|
2021-01-29 21:32:57 +08:00
|
|
|
++dataList[1];
|
|
|
|
});
|
2021-01-30 19:17:49 +08:00
|
|
|
eventQueue.appendListener(std::vector<bool>{ true, false, true }, [&dataList]() {
|
2021-01-29 21:32:57 +08:00
|
|
|
++dataList[2];
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 0, 0});
|
|
|
|
|
2021-01-30 19:17:49 +08:00
|
|
|
eventQueue.dispatch(std::string("hello"));
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 1, 0});
|
|
|
|
eventQueue.dispatch(3);
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 0});
|
|
|
|
eventQueue.enqueue(std::vector<bool>{ true, false, true });
|
|
|
|
eventQueue.process();
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 1});
|
|
|
|
|
|
|
|
eventQueue.dispatch(std::string("hellox"));
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 1});
|
|
|
|
eventQueue.dispatch(2);
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 1});
|
|
|
|
eventQueue.dispatch(std::vector<bool>{ true, false, false });
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 1});
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MyEventPolicies
|
|
|
|
{
|
|
|
|
template <typename Key, typename T>
|
|
|
|
using Map = std::map <Key, T>;
|
|
|
|
};
|
2021-01-31 16:54:25 +08:00
|
|
|
TEST_CASE("AnyId, default, map")
|
2021-01-30 19:17:49 +08:00
|
|
|
{
|
2021-01-31 16:54:25 +08:00
|
|
|
eventpp::EventDispatcher<eventpp::AnyHashableId, void(), MyEventPolicies> dispatcher;
|
2021-01-30 19:17:49 +08:00
|
|
|
|
|
|
|
std::vector<int> dataList(3);
|
|
|
|
|
|
|
|
dispatcher.appendListener(3, [&dataList]() {
|
|
|
|
++dataList[0];
|
|
|
|
});
|
|
|
|
dispatcher.appendListener(std::string("hello"), [&dataList]() {
|
|
|
|
++dataList[1];
|
|
|
|
});
|
|
|
|
dispatcher.appendListener(std::vector<bool>{ true, false, true }, [&dataList]() {
|
|
|
|
++dataList[2];
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 0, 0});
|
|
|
|
|
|
|
|
dispatcher.dispatch(std::string("hello"));
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 1, 0});
|
|
|
|
dispatcher.dispatch(3);
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 0});
|
|
|
|
dispatcher.dispatch(std::vector<bool>{ true, false, true });
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 1});
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:54:25 +08:00
|
|
|
TEST_CASE("AnyId<std::hash, std::any>")
|
2021-01-30 19:17:49 +08:00
|
|
|
{
|
2021-01-31 16:54:25 +08:00
|
|
|
using MyAnyId = eventpp::AnyId<std::hash, std::any>;
|
|
|
|
|
|
|
|
eventpp::EventQueue<MyAnyId, void(const MyAnyId &)> eventQueue;
|
2021-01-30 19:17:49 +08:00
|
|
|
|
|
|
|
std::vector<int> dataList(3);
|
|
|
|
|
2021-01-31 16:54:25 +08:00
|
|
|
eventQueue.appendListener(3, [&dataList](const MyAnyId & e) {
|
2021-01-30 19:17:49 +08:00
|
|
|
REQUIRE(std::any_cast<int>(e.getValue()) == 3);
|
|
|
|
++dataList[0];
|
|
|
|
});
|
2021-01-31 16:54:25 +08:00
|
|
|
eventQueue.appendListener(std::string("hello"), [&dataList](const MyAnyId & e) {
|
2021-01-30 19:17:49 +08:00
|
|
|
REQUIRE(std::any_cast<std::string>(e.getValue()) == "hello");
|
|
|
|
++dataList[1];
|
|
|
|
});
|
2021-01-31 16:54:25 +08:00
|
|
|
eventQueue.appendListener(std::vector<bool>{ true, false, true }, [&dataList](const MyAnyId & e) {
|
2021-01-30 19:17:49 +08:00
|
|
|
REQUIRE(std::any_cast<std::vector<bool>>(e.getValue()) == std::vector<bool>{ true, false, true });
|
|
|
|
++dataList[2];
|
|
|
|
});
|
|
|
|
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 0, 0});
|
|
|
|
|
2021-01-29 21:32:57 +08:00
|
|
|
eventQueue.dispatch(std::string("hello"));
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 1, 0});
|
|
|
|
eventQueue.enqueue(3);
|
|
|
|
eventQueue.process();
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 0});
|
|
|
|
eventQueue.enqueue(std::vector<bool>{ true, false, true });
|
|
|
|
eventQueue.process();
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 1});
|
|
|
|
|
|
|
|
eventQueue.dispatch(std::string("hellox"));
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 1});
|
|
|
|
eventQueue.dispatch(2);
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 1});
|
|
|
|
eventQueue.dispatch(std::vector<bool>{ true, false, false });
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 1});
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:54:25 +08:00
|
|
|
struct StringStorage
|
2021-01-29 21:32:57 +08:00
|
|
|
{
|
2021-01-31 16:54:25 +08:00
|
|
|
StringStorage(const int value) : value(std::to_string(value)) {}
|
|
|
|
StringStorage(const std::string & value) : value(value) {}
|
|
|
|
|
|
|
|
std::string value;
|
2021-01-29 21:32:57 +08:00
|
|
|
};
|
2021-01-31 16:54:25 +08:00
|
|
|
|
|
|
|
TEST_CASE("AnyId<std::hash, StringStorage>")
|
2021-01-29 21:32:57 +08:00
|
|
|
{
|
2021-01-31 16:54:25 +08:00
|
|
|
using MyAnyId = eventpp::AnyId<std::hash, StringStorage>;
|
|
|
|
|
2021-02-01 09:24:15 +08:00
|
|
|
eventpp::EventDispatcher<MyAnyId, void(const MyAnyId &)> dispatcher;
|
2021-01-29 21:32:57 +08:00
|
|
|
|
|
|
|
std::vector<int> dataList(3);
|
|
|
|
|
2021-02-01 09:24:15 +08:00
|
|
|
dispatcher.appendListener(3, [&dataList](const MyAnyId & e) {
|
|
|
|
REQUIRE(e.getValue().value == "3");
|
2021-01-29 21:32:57 +08:00
|
|
|
++dataList[0];
|
2021-01-31 16:54:25 +08:00
|
|
|
});
|
2021-02-01 09:24:15 +08:00
|
|
|
dispatcher.appendListener(std::string("hello"), [&dataList](const MyAnyId & e) {
|
|
|
|
REQUIRE(e.getValue().value == "hello");
|
2021-01-29 21:32:57 +08:00
|
|
|
++dataList[1];
|
2021-01-31 16:54:25 +08:00
|
|
|
});
|
2021-01-29 21:32:57 +08:00
|
|
|
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 0, 0});
|
|
|
|
|
|
|
|
dispatcher.dispatch(std::string("hello"));
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 0, 1, 0});
|
|
|
|
dispatcher.dispatch(3);
|
|
|
|
REQUIRE(dataList == std::vector<int>{ 1, 1, 0});
|
|
|
|
}
|
|
|
|
|