Skip to content

Commit

Permalink
evol(Tests): add test on Icomp and And
Browse files Browse the repository at this point in the history
Signed-off-by: Mael-RABOT <[email protected]>
  • Loading branch information
Mael-RABOT committed Feb 19, 2024
1 parent 8b9bb7d commit 4fad731
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 25 deletions.
Binary file added nanotester
Binary file not shown.
1 change: 1 addition & 0 deletions src/Component/AComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ namespace nts {
void setState(std::size_t pin, Tristate state) override { _pins[pin].first = state; };
virtual void computeBehaviour(std::size_t tick) = 0;
void setValue(nts::Tristate) override {};
nts::Tristate getValue(std::size_t pin) override { return _pins[pin].first; };
};
}
1 change: 1 addition & 0 deletions src/Component/IComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ namespace nts {
virtual void setState(std::size_t pin, Tristate state) = 0;
virtual void computeBehaviour(std::size_t tick) = 0;
virtual void setValue(nts::Tristate value) = 0;
virtual Tristate getValue(std::size_t pin) = 0;
};
}
23 changes: 19 additions & 4 deletions src/Manager/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ namespace nts {
return this->_addComponent(label, this->factory(type, label));
}

IComponent *Manager::getComponent(const std::string &label) {
if (_components.find(label) == _components.end()) {
throw CustomError("Component not found: " + label);
}
return _components[label];
}

bool Manager::_addComponent(const std::string &label, IComponent *component) {
if (component == nullptr)
throw CustomError("Creation went wrong.");
Expand Down Expand Up @@ -135,10 +142,10 @@ namespace nts {

void Manager::simulate(std::size_t tick) {
_currentTick = tick;
// for (auto &output : _components) {
// if (output.second->getType() == ComponentType::Input)
// output.second->computeBehaviour(tick);
// }
for (auto &input : _components) {
if (input.second->getType() == ComponentType::Input)
input.second->computeBehaviour(tick);
}
// for (auto &output : _components) {
// if (output.second->getType() == ComponentType::Standard)
// output.second->computeBehaviour(tick);
Expand Down Expand Up @@ -624,4 +631,12 @@ namespace nts {
}
}
}

void Manager::clearComponents() {
for (auto &component : _components) {
delete component.second;
}
_components.clear();
_currentTick = 0;
}
}
4 changes: 3 additions & 1 deletion src/Manager/Manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace nts {
~Manager();

bool createComponent(const std::string &type, const std::string &label);
IComponent *getComponent(const std::string &label);

void addLink(
const std::string &source,
Expand All @@ -71,7 +72,7 @@ namespace nts {

void dump(bool = true, bool = true, bool = true, bool = false);

void simulate() { this->simulate(_currentTick++); };
void simulate() { this->simulate(++_currentTick); };
void simulate(std::size_t tick);

IComponent * factory(
Expand All @@ -89,5 +90,6 @@ namespace nts {

void display();
void run();
void clearComponents();
};
}
20 changes: 0 additions & 20 deletions test/TestAComponent.cpp

This file was deleted.

74 changes: 74 additions & 0 deletions test/TestAnd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <criterion/criterion.h>
#include <criterion/redirect.h>

#include "../include/paths.hpp"
#include "../src/Manager/Manager.hpp"

nts::Manager AndManager;

void AndSetup() {
AndManager.initializeTruthTables(TRUTH_TABLE_PATH);
AndManager.createComponent("and", "and1");
AndManager.createComponent("input", "in1");
AndManager.createComponent("input", "in2");
AndManager.createComponent("output", "out1");
AndManager.addLink("in1", 1, "and1", 1);
AndManager.addLink("in2", 1, "and1", 2);
AndManager.addLink("and1", 3, "out1", 1);
}

void AndTeardown() {
AndManager.clearComponents();
}

TestSuite(And, .init = AndSetup, .fini = AndTeardown);

Test(And, should_create_and) {
nts::IComponent *component = AndManager.getComponent("and1");

cr_assert_eq(component->getLabel(), "and1");
cr_assert_eq(component->getType(), nts::ComponentType::Standard);
}

Test(And, should_link_and) {
nts::IComponent *output = AndManager.getComponent("out1");
cr_assert_eq(output->getValue(1), nts::Tristate::Undefined);

AndManager.simulate(0);
cr_assert_eq(output->getValue(1), nts::Tristate::Undefined);

AndManager.getComponent("in1")->setValue(nts::Tristate::True);
AndManager.getComponent("in2")->setValue(nts::Tristate::True);
AndManager.simulate(1);

cr_assert_eq(output->getValue(1), nts::Tristate::True);

AndManager.getComponent("in2")->setValue(nts::Tristate::False);
AndManager.simulate(2);

cr_assert_eq(output->getValue(1), nts::Tristate::False);
}

Test(And, and_truth_table) {
std::vector<nts::Tristate> states = {nts::Tristate::True, nts::Tristate::False, nts::Tristate::Undefined};

for (auto state1 : states) {
for (auto state2 : states) {
AndManager.getComponent("in1")->setValue(state1);
AndManager.getComponent("in2")->setValue(state2);
AndManager.simulate();

nts::Tristate expectedOutput;
if (state1 == nts::Tristate::False || state2 == nts::Tristate::False) {
expectedOutput = nts::Tristate::False;
} else if (state1 == nts::Tristate::True && state2 == nts::Tristate::True) {
expectedOutput = nts::Tristate::True;
} else {
expectedOutput = nts::Tristate::Undefined;
}

nts::IComponent *output = AndManager.getComponent("out1");
cr_assert_eq(output->getValue(1), expectedOutput);
}
}
}
43 changes: 43 additions & 0 deletions test/TestIComponent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <criterion/criterion.h>
#include <criterion/redirect.h>

#include "../include/paths.hpp"
#include "../src/Manager/Manager.hpp"

nts::Manager IComponentManager;

void IComponentSetup() {
IComponentManager.initializeTruthTables(TRUTH_TABLE_PATH);
}

void IComponentTeardown() {
IComponentManager.clearComponents();
}

TestSuite(IComponent, .init = IComponentSetup, .fini = IComponentTeardown);

Test(IComponent, should_create_component)
{
IComponentManager.createComponent("input", "in1");
nts::IComponent *component = IComponentManager.getComponent("in1");

cr_assert_eq(component->getLabel(), "in1");
cr_assert_eq(component->getType(), nts::ComponentType::Input);
}

Test(IComponent, should_link_input_and_output)
{
IComponentManager.createComponent("input", "in1");
IComponentManager.createComponent("output", "out1");
IComponentManager.addLink("in1", 1, "out1", 1);

nts::IComponent *output = IComponentManager.getComponent("out1");
cr_assert_eq(output->getValue(1), nts::Tristate::Undefined);

IComponentManager.simulate(0);
cr_assert_eq(output->getValue(1), nts::Tristate::Undefined);

IComponentManager.getComponent("in1")->setValue(nts::Tristate::True);
IComponentManager.simulate(1);
cr_assert_eq(output->getValue(1), nts::Tristate::True);
}

0 comments on commit 4fad731

Please sign in to comment.