// 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" #include "eventpp/eventqueue.h" #include "eventpp/utilities/orderedqueuelist.h" #include #include TEST_CASE("detectDataListOrder") { std::vector a { 1, 2, 3, 4, 5 }; REQUIRE(detectDataListOrder(a.begin(), a.end()) == 1); std::vector b { 5, 4, 3, 2, 1 }; REQUIRE(detectDataListOrder(b.begin(), b.end()) == -1); std::vector c { 4, 5, 3, 2, 1 }; REQUIRE(detectDataListOrder(c.begin(), c.end()) == 0); } TEST_CASE("EventQueue, non-ordered list") { constexpr int count = 100; eventpp::EventQueue queue; std::vector eventList(count); std::iota(eventList.begin(), eventList.end(), 1); for(int i = 0; i < count; i += 2) { std::swap(eventList[i], eventList[i + 1]); } REQUIRE(detectDataListOrder(eventList.begin(), eventList.end()) == 0); std::vector dataList(count); int processedCount = 0; for(const int e : eventList) { queue.appendListener(e, [&dataList, e, &processedCount]() { dataList[processedCount] = e; ++processedCount; }); } for(const int e : eventList) { queue.enqueue(e); } queue.process(); REQUIRE(dataList == eventList); } using MyCompareAscend = eventpp::OrderedQueueListCompare; struct MyPolicyAscend { template using QueueList = eventpp::OrderedQueueList; }; TEST_CASE("EventQueue, ordered list, ascend") { constexpr int count = 100; eventpp::EventQueue queue; std::vector eventList(count); std::iota(eventList.begin(), eventList.end(), 1); for(int i = 0; i < count; i += 2) { std::swap(eventList[i], eventList[i + 1]); } REQUIRE(detectDataListOrder(eventList.begin(), eventList.end()) == 0); std::vector dataList(count); int processedCount = 0; for(const int e : eventList) { queue.appendListener(e, [&dataList, e, &processedCount]() { dataList[processedCount] = e; ++processedCount; }); } for(const int e : eventList) { queue.enqueue(e); } queue.process(); REQUIRE(dataList != eventList); REQUIRE(detectDataListOrder(dataList.begin(), dataList.end()) == 1); // Be sure OrderedQueueList is compilable in below calls. queue.processOne(); queue.processIf([](){ return true; }); } struct MyCompareDescend { template bool operator() (const T & a, const T & b) const { return a.event > b.event; } }; struct MyPolicyDescend { template using QueueList = eventpp::OrderedQueueList; }; TEST_CASE("EventQueue, ordered list, descend") { constexpr int count = 100; eventpp::EventQueue queue; std::vector eventList(count); std::iota(eventList.begin(), eventList.end(), 1); for(int i = 0; i < count; i += 2) { std::swap(eventList[i], eventList[i + 1]); } REQUIRE(detectDataListOrder(eventList.begin(), eventList.end()) == 0); std::vector dataList(count); int processedCount = 0; for(const int e : eventList) { queue.appendListener(e, [&dataList, e, &processedCount]() { dataList[processedCount] = e; ++processedCount; }); } for(const int e : eventList) { queue.enqueue(e); } queue.process(); REQUIRE(dataList != eventList); REQUIRE(detectDataListOrder(dataList.begin(), dataList.end()) == -1); }