Skip to content

Commit

Permalink
Merge branch 'main' into version_format
Browse files Browse the repository at this point in the history
  • Loading branch information
ravic-rs authored Aug 25, 2023
2 parents dab04ff + f7f947a commit 8ac69be
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/DeviceModeling/device_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
class device_block;
class device_signal;

using namespace std;

/**
* @class device_net
* @brief Class representing a net in a device.
Expand Down
6 changes: 4 additions & 2 deletions src/DeviceModeling/rs_parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

#pragma once

#include <iostream>
#include <memory>
#include <optional>
#include <sstream>

#include "rs_parameter_type.h"

Expand Down Expand Up @@ -181,8 +184,7 @@ class Parameter {
std::string to_string() const {
std::ostringstream oss;
std::string tp;
std::string tn = std::string(typeid(T).name());
tp = (tn == "i") ? "int" : ((tn == "d") ? "double" : "string");
tp = TypeNameMapper::GetTypeName(T());
oss << "Parameter " << name_ << ": " << value_ << " of type " << tp;
if (has_address()) {
oss << " at address " << get_address();
Expand Down
15 changes: 15 additions & 0 deletions src/DeviceModeling/rs_parameter_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
#include <stdexcept>
#include <string>
#include <unordered_map>
class TypeNameMapper {
public:
template <typename T>
static std::string GetTypeName() {
return "Unknown";
}

// Not a specialization, but a function that's always there
static std::string GetTypeName(int) { return "int"; }

static std::string GetTypeName(double) { return "double"; }

static std::string GetTypeName(std::string) { return "string"; }
};

/**
* @class ParameterType
* @brief Template class to hold type information of a parameter.
Expand Down
4 changes: 3 additions & 1 deletion tests/unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if(MSVC)
endif()

if(MINGW)
add_compile_options(-Wa,-mbig-obj)
add_compile_options(-Wa,-mbig-obj)
endif()

set(CPP_LIST
Expand Down Expand Up @@ -61,6 +61,8 @@ set(CPP_LIST
DeviceModeling/rs_expression_test.cpp
DeviceModeling/rs_expression_evaluator_test.cpp
DeviceModeling/rs_parameter_type_test.cpp
DeviceModeling/rs_parameter_test.cpp
DeviceModeling/device_net_test.cpp
)
set(H_LIST
PinAssignment/TestLoader.h
Expand Down
99 changes: 99 additions & 0 deletions tests/unittest/DeviceModeling/device_net_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "DeviceModeling/device_net.h"

#include <gtest/gtest.h>

#include <memory>

// This fixture will help in setting up some common setup and teardown for each
// test
class DeviceNetTest : public ::testing::Test {
protected:
void SetUp() override {
// Common setup if required
}

void TearDown() override {
// Common cleanup if required
}
};

// Testing the constructor
TEST_F(DeviceNetTest, ConstructWithNameOnly) {
device_net net("test_net");
EXPECT_EQ(net.get_net_name(), "test_net");
EXPECT_EQ(net.get_signal(), nullptr);
}

// Testing the copy constructor
TEST_F(DeviceNetTest, CopyConstructor) {
device_net net_original("test_net");
device_net net_copy(net_original);
EXPECT_EQ(net_copy.get_net_name(), "test_net");
EXPECT_EQ(net_copy.get_signal(), nullptr);
}

// Testing setter and getter of net name
TEST_F(DeviceNetTest, SetAndGetNetName) {
device_net net("initial_name");
net.set_net_name("new_name");
EXPECT_EQ(net.get_net_name(), "new_name");
}

// Testing source and sink relationships
TEST_F(DeviceNetTest, SetAndGetSource) {
device_net net("test_net");
std::shared_ptr<device_net> source_net =
std::make_shared<device_net>("source_net");
net.set_source(source_net);
EXPECT_EQ(net.get_source(), source_net);
}

TEST_F(DeviceNetTest, AddAndGetSinks) {
device_net net("test_net");
std::shared_ptr<device_net> sink1 = std::make_shared<device_net>("sink1");
std::shared_ptr<device_net> sink2 = std::make_shared<device_net>("sink2");

net.add_sink(sink1);
net.add_sink(sink2);

const auto& sinks = net.get_sink_set();
EXPECT_EQ(sinks.size(), 2);
EXPECT_NE(sinks.find(sink1), sinks.end());
EXPECT_NE(sinks.find(sink2), sinks.end());
}

// Testing equality functions
TEST_F(DeviceNetTest, EqualityOperators) {
device_net net1("test_net");
device_net net2("test_net");
device_net net3("another_net");

EXPECT_FALSE(net1 == net2); // Since your == checks for same object reference
EXPECT_TRUE(net1 != net2); // Inverse
EXPECT_FALSE(net1 == net3);
EXPECT_TRUE(net1 != net3);
}

TEST_F(DeviceNetTest, CheckEqualityFunction) {
device_net net1("test_net");
device_net net2("test_net");
device_net net3("another_net");

EXPECT_TRUE(net1.equal(net2)); // Since they are separate instances with
// potentially separate sinks, sources etc.
EXPECT_FALSE(net1.equal(net3));
}

// Testing string representation functions
TEST_F(DeviceNetTest, ToStringFunction) {
device_net net("test_net");
std::string expected_start = "Net Name: test_net";
EXPECT_TRUE(net.to_string().find(expected_start) != std::string::npos);
}

TEST_F(DeviceNetTest, FormattedStringFunction) {
device_net net("test_net");
std::string expected_start = "device_net: Net Name: test_net";
EXPECT_TRUE(net.to_formatted_string().find(expected_start) !=
std::string::npos);
}
74 changes: 74 additions & 0 deletions tests/unittest/DeviceModeling/rs_parameter_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @file rs_parameter_test.cpp
* @author Manadher Kharroubi ([email protected])
* @brief
* @version 0.1
* @date 2023-05-18
*
* @copyright Copyright (c) 2023
*
*/

#include "DeviceModeling/rs_parameter.h"

#include <gtest/gtest.h>

TEST(ParameterTest, Constructor) {
auto type = std::make_shared<ParameterType<int>>();
Parameter<int> param("test", 5, type);

EXPECT_EQ(param.get_name(), "test");
EXPECT_EQ(param.get_value(), 5);
}

TEST(ParameterTest, SetValue) {
auto type = std::make_shared<ParameterType<int>>();
Parameter<int> param("test", 5, type);
param.set_value(10);

EXPECT_EQ(param.get_value(), 10);
}

TEST(ParameterTest, InvalidValue) {
auto type = std::make_shared<ParameterType<int>>();

type->set_lower_bound(0);
type->set_upper_bound(10);
EXPECT_THROW(Parameter<int>("test", -1, type), std::runtime_error);
}

TEST(ParameterTest, SetAddress) {
auto type = std::make_shared<ParameterType<int>>();
Parameter<int> param("test", 5, type);
param.set_address(123);

EXPECT_EQ(param.get_address(), 123);
}

TEST(ParameterTest, IntToString) {
auto type = std::make_shared<ParameterType<int>>();
Parameter<int> param("test", 5, type);

EXPECT_EQ(param.to_string(), "Parameter test: 5 of type int");
}

TEST(ParameterTest, DoubleToString) {
auto type = std::make_shared<ParameterType<double>>();
Parameter<double> param("test", 5.5, type);

EXPECT_EQ(param.to_string(), "Parameter test: 5.5 of type double");
}

TEST(ParameterTest, StringToString) {
auto type = std::make_shared<ParameterType<std::string>>();
Parameter<std::string> param("test", "Test", type);

EXPECT_EQ(param.to_string(), "Parameter test: Test of type string");
}

TEST(ParameterTest, InvalidSetAddress) {
auto type = std::make_shared<ParameterType<double>>();
Parameter<double> param("test", 5.5, type);

EXPECT_THROW(param.set_address(123), std::runtime_error);
}

0 comments on commit 8ac69be

Please sign in to comment.