Skip to content

Commit

Permalink
Issue #37, #41, #24, #20, #5 - Improve unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
vldtecno committed May 12, 2024
1 parent c937840 commit cb3d342
Show file tree
Hide file tree
Showing 46 changed files with 2,501 additions and 934 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if(CMAKE_COMPILER_IS_GNUCXX)
#set(MULTITHREADED_BUILD 8 CACHE STRING "How many threads are used to build the project")
#set(CMAKE_MAKE_PROGRAM "${CMAKE_MAKE_PROGRAM} -j${MULTITHREADED_BUILD}")
set(GCC_PTHREAD_COMPILE_FLAGS "-pthread")
set(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage")
set(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage -fprofile-update=atomic")
set(GCC_COVERAGE_LINK_FLAGS "-lgcov")
set(GCC_PTHREAD_LINK_FLAGS "-lpthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_PTHREAD_COMPILE_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
Expand Down
17 changes: 8 additions & 9 deletions Examples/Elevator/Controller/ElevatorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void ElevatorController::mergeMinGoingUp()
{
return;
}
const int minWaiting = *ranges::min_element(m_floorsWaitingToGoUp.begin(), m_floorsWaitingToGoUp.end());
const int minWaiting = *ranges::min_element(m_floorsWaitingToGoUp);
m_destinations.insert(minWaiting);
m_floorsWaitingToGoUp.erase(minWaiting);

Expand All @@ -240,7 +240,7 @@ void ElevatorController::mergeMaxGoingDown()
{
return;
}
const int maxWaiting = *ranges::max_element(m_floorsWaitingToGoDown.begin(), m_floorsWaitingToGoDown.end());
const int maxWaiting = *ranges::max_element(m_floorsWaitingToGoDown);
m_destinations.insert(maxWaiting);
m_floorsWaitingToGoDown.erase(maxWaiting);

Expand Down Expand Up @@ -270,9 +270,8 @@ void ElevatorController::mergeToDestinations(unordered_set<int> &toAdd, const bo
{
deleted = false;
auto floorIt =
ranges::find_if(toAdd.cbegin(), toAdd.cend(),
[this, &lessThan](const int floor)
{ return (lessThan && floor < m_currentFloor) || (!lessThan && floor > m_currentFloor); });
ranges::find_if(toAdd, [this, &lessThan](const int floor)
{ return (lessThan && floor < m_currentFloor) || (!lessThan && floor > m_currentFloor); });
if (floorIt != toAdd.cend())
{
auto &floor = *floorIt;
Expand Down Expand Up @@ -337,7 +336,7 @@ bool ElevatorController::isMinSmallerThanCurrent() const
{
return false;
}
int minFloor = *ranges::min_element(m_destinations.cbegin(), m_destinations.cend());
int minFloor = *ranges::min_element(m_destinations);
return minFloor < m_currentFloor;
}

Expand All @@ -348,7 +347,7 @@ bool ElevatorController::isMinGreaterThanCurrent() const
{
return false;
}
int minFloor = *ranges::min_element(m_destinations.cbegin(), m_destinations.cend());
int minFloor = *ranges::min_element(m_destinations);
return minFloor > m_currentFloor;
}

Expand All @@ -359,7 +358,7 @@ bool ElevatorController::isMaxSmallerThanCurrent() const
{
return false;
}
int maxFloor = *ranges::max_element(m_destinations.cbegin(), m_destinations.cend());
int maxFloor = *ranges::max_element(m_destinations);
return maxFloor < m_currentFloor;
}

Expand All @@ -370,7 +369,7 @@ bool ElevatorController::isMaxGreaterThanCurrent() const
{
return false;
}
int maxFloor = *ranges::max_element(m_destinations.cbegin(), m_destinations.cend());
int maxFloor = *ranges::max_element(m_destinations);
return maxFloor > m_currentFloor;
}

Expand Down
5 changes: 0 additions & 5 deletions PTN_Engine/ImportExport/IExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ void IFileExporter::_export(const PTN_Engine &ptnEngine)
{
exportTransition(transitionProperties);
}

for (const auto &arcProperties : ptnEngine.getArcsProperties())
{
exportArc(arcProperties);
}
}

} // namespace ptne
51 changes: 16 additions & 35 deletions PTN_Engine/ImportExport/XML/src/XML/XML_FileExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

#include "PTN_Engine/PTN_Engine.h"
#include "PTN_Engine/PTN_Exception.h"
#include "XML/XML_FileExporter.h"
#include <pugixml.hpp>
#include <vector>
Expand Down Expand Up @@ -72,46 +71,28 @@ void XML_FileExporter::exportTransition(const TransitionProperties &transitionPr

xml_node requireNoActionsInExecution = transitionNode.append_child("RequireNoActionsInExecution");
requireNoActionsInExecution.append_attribute("value").set_value(transitionProperties.requireNoActionsInExecution? "true" : "false");
}

void XML_FileExporter::exportArc(const vector<ArcProperties> &arcsPropereties)
{
for (const auto &arcProperties : arcsPropereties)
auto exportArcs = [this](const vector<ArcProperties> arcsProperties, const string &typeStr)
{
xml_node arcNode = m_arcsNode.append_child("Arc");
xml_node place = arcNode.append_child("Place");
place.append_attribute("value").set_value(arcProperties.placeName.c_str());

xml_node transition = arcNode.append_child("Transition");
transition.append_attribute("value").set_value(arcProperties.transitionName.c_str());
for (const auto &arcProperties : arcsProperties)
{
xml_node arcNode = m_arcsNode.append_child("Arc");
xml_node place = arcNode.append_child("Place");
place.append_attribute("value").set_value(arcProperties.placeName.c_str());

xml_node weight = arcNode.append_child("Weight");
weight.append_attribute("value").set_value(to_string(arcProperties.weight).c_str());
xml_node transition = arcNode.append_child("Transition");
transition.append_attribute("value").set_value(arcProperties.transitionName.c_str());

xml_node type = arcNode.append_child("Type");
xml_node weight = arcNode.append_child("Weight");
weight.append_attribute("value").set_value(to_string(arcProperties.weight).c_str());

using enum ArcProperties::Type;
string typeStr;
switch (arcProperties.type)
{
default:
throw PTN_Exception("Unsupported enum");
break;
case ACTIVATION:
typeStr = "Activation";
break;
case BIDIRECTIONAL:
typeStr = "Bidirectional";
break;
case DESTINATION:
typeStr = "Destination";
break;
case INHIBITOR:
typeStr = "Inhibitor";
break;
xml_node type = arcNode.append_child("Type");
type.append_attribute("value").set_value(typeStr.c_str());
}
type.append_attribute("value").set_value(typeStr.c_str());
}
};
exportArcs(transitionProperties.activationArcs, "Activation");
exportArcs(transitionProperties.destinationArcs, "Destination");
exportArcs(transitionProperties.inhibitorArcs, "Inhibitor");
}

void XML_FileExporter::saveFile() const
Expand Down
2 changes: 0 additions & 2 deletions PTN_Engine/ImportExport/XML/src/XML/XML_FileExporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class XML_FileExporter : public IFileExporter

void exportTransition(const TransitionProperties &transitionProperties) override;

void exportArc(const std::vector<ArcProperties> &arcPropereties) override;

void saveFile() const;

pugi::xml_node m_arcsNode;
Expand Down
22 changes: 11 additions & 11 deletions PTN_Engine/ImportExport/XML/src/XML/XML_FileImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
* limitations under the License.
*/

#include "XML/XML_FileImporter.h"
#include "PTN_Engine/PTN_Engine.h"
#include "PTN_Engine/PTN_Exception.h"
#include "XML/XML_FileImporter.h"
#include <algorithm>
#include <charconv>
#include <iterator>
#include <algorithm>

using namespace pugi;
using namespace std;
Expand All @@ -38,12 +38,13 @@ vector<string> collectTransitionAttributes(const xml_node &transition, const str
{
vector<string> activationConditions;
const auto &childAttributes = transition.child(attribute.c_str());
ranges::transform(begin(childAttributes), end(childAttributes), back_inserter(activationConditions),
[](const auto &activationCondition) { return activationCondition.attribute("name").value(); });
ranges::transform(childAttributes, back_inserter(activationConditions), [](const auto &activationCondition)
{ return activationCondition.attribute("name").value(); });
return activationConditions;
}

vector<ArcProperties> collectArcAttributes(const xml_node &transition, const string &attribute, const ArcProperties::Type type)
vector<ArcProperties>
collectArcAttributes(const xml_node &transition, const string &attribute, const ArcProperties::Type type)
{
vector<ArcProperties> arcsProperties;
for (xml_node activationCondition : transition.child(attribute.c_str()))
Expand All @@ -56,10 +57,8 @@ vector<ArcProperties> collectArcAttributes(const xml_node &transition, const str
weight = static_cast<size_t>(atol(weightStr.c_str()));
}

arcsProperties.emplace_back(weight,
activationCondition.attribute("name").value(),
activationConditionName,
type);
arcsProperties.emplace_back(weight, activationCondition.attribute("name").value(), activationConditionName,
type);
}
return arcsProperties;
}
Expand Down Expand Up @@ -134,7 +133,7 @@ vector<PlaceProperties> XML_FileImporter::importPlaces() const
isInput = isInputStr == "true" ? true : false;
}

size_t numberOfTokens = 0;
size_t numberOfTokens = 0;
if (const string numberOfTokensStr = getAttributeValue(place, "tokens"); !numberOfTokensStr.empty())
{
numberOfTokens = static_cast<size_t>(atol(numberOfTokensStr.c_str()));
Expand Down Expand Up @@ -165,7 +164,8 @@ vector<TransitionProperties> XML_FileImporter::importTransitions() const
transitionProperties.destinationArcs = collectArcAttributes(transition, "DestinationPlaces", DESTINATION);
transitionProperties.inhibitorArcs = collectArcAttributes(transition, "InhibitorPlaces", INHIBITOR);
transitionProperties.additionalConditionsNames = activationConditions;
transitionProperties.requireNoActionsInExecution = getNodeValue<bool>("RequireNoActionsInExecution", transition);
transitionProperties.requireNoActionsInExecution =
getNodeValue<bool>("RequireNoActionsInExecution", transition);
transitionInfoCollection.emplace_back(transitionProperties);
}
return transitionInfoCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include "PTN_Engine/PTN_Engine.h"
#include "PTN_Engine/Utilities/Explicit.h"
#include <vector>

namespace ptne
{
Expand Down Expand Up @@ -51,8 +50,6 @@ class DLL_PUBLIC IFileExporter
virtual void exportPlace(const PlaceProperties &placeProperties) = 0;

virtual void exportTransition(const TransitionProperties &transitionPropereties) = 0;

virtual void exportArc(const std::vector<ArcProperties> &arcPropereties) = 0;
};

} // namespace ptne
3 changes: 3 additions & 0 deletions PTN_Engine/JobQueue/JobQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ namespace ptne
{
using namespace std;

JobQueue::~JobQueue() = default;
JobQueue::JobQueue() = default;

void JobQueue::activate()
{
if (m_isJobQueueActive)
Expand Down
13 changes: 7 additions & 6 deletions PTN_Engine/JobQueue/JobQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace ptne
class JobQueue
{
public:
~JobQueue() = default;
JobQueue() = default;
~JobQueue();
JobQueue();
JobQueue(const JobQueue &) = delete;
JobQueue(JobQueue &&) = delete;
JobQueue &operator=(const JobQueue &) = delete;
Expand All @@ -58,17 +58,18 @@ class JobQueue
//!
bool isActive() const;

//!
//! \brief Run the job queue. Should be executed in its own thread.
//!
void run(std::stop_token stopToken);

private:
//!
//! \brief Launch the job queue.
//!
void launch();

//!
//! \brief Run the job queue. Should be executed in its own thread.
//!
void run(std::stop_token stopToken);

//! Whether the job queue is active or not.
std::atomic<bool> m_isJobQueueActive = true;

Expand Down
8 changes: 4 additions & 4 deletions PTN_Engine/ManagedContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
*/
#pragma once

#include "PTN_Engine/PTN_Engine.h"
#include "PTN_Engine/PTN_Exception.h"
#include <algorithm>
#include <mutex>
#include <shared_mutex>
#include <unordered_map>
#include <vector>

namespace ptne
{
Expand Down Expand Up @@ -94,7 +95,7 @@ class ManagedContainer
{
std::shared_lock lock(m_mutex);
std::vector<std::pair<std::string, T>> items;
std::ranges::for_each(names.cbegin(), names.cend(),
std::ranges::for_each(names,
[&](const std::string &name)
{
if (name.empty())
Expand All @@ -106,8 +107,7 @@ class ManagedContainer
{
throw InvalidFunctionNameException(name);
}
items.push_back(
std::pair<std::string, ConditionFunction>(name, m_items.at(name)));
items.emplace_back(name, m_items.at(name));
});
return items;
}
Expand Down
10 changes: 9 additions & 1 deletion PTN_Engine/ManagerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ class ManagerBase

void insert(const std::shared_ptr<T> &item)
{
if (item == nullptr)
{
throw PTN_Exception("Tried to insert nullptr item");
}
const auto &itemName = item->getName();
if (m_items.contains(itemName))
if (itemName.empty())
{
throw PTN_Exception("Empty item names are not supported.");
}
else if (m_items.contains(itemName))
{
throw RepeatedPlaceException(itemName);
}
Expand Down
5 changes: 0 additions & 5 deletions PTN_Engine/PTN_Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ vector<TransitionProperties> PTN_Engine::getTransitionsProperties() const
return m_impProxy->getTransitionsProperties();
}

vector<vector<ArcProperties>> PTN_Engine::getArcsProperties() const
{
return m_impProxy->getArcsProperties();
}

void PTN_Engine::createTransition(const TransitionProperties &transitionProperties)
{
m_impProxy->createTransition(transitionProperties);
Expand Down
Loading

0 comments on commit cb3d342

Please sign in to comment.