From f7f947a1276f5855daefce881749a07eb5fd84a9 Mon Sep 17 00:00:00 2001 From: Manadher Kharroubi <99300024+ManadherRS@users.noreply.github.com> Date: Thu, 24 Aug 2023 17:54:30 -0700 Subject: [PATCH] Add Gtests for nets and parameters and fix rs type name generation (across platforms) (#1242) * Adding Gtests for RS parameters * Adding GTests for device net * debugging typeid names on msvc * Replacing the type naming method * More elegant solution to type naming * Incremented patch version --------- Co-authored-by: ManadherRS --- CMakeLists.txt | 2 +- src/DeviceModeling/device_net.h | 2 + src/DeviceModeling/rs_parameter.h | 6 +- src/DeviceModeling/rs_parameter_type.h | 15 +++ tests/unittest/CMakeLists.txt | 4 +- .../DeviceModeling/device_net_test.cpp | 99 +++++++++++++++++++ .../DeviceModeling/rs_parameter_test.cpp | 74 ++++++++++++++ 7 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 tests/unittest/DeviceModeling/device_net_test.cpp create mode 100644 tests/unittest/DeviceModeling/rs_parameter_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index dab55d596..23a741f30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,7 +40,7 @@ set(VERSION_MINOR 0) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third_party/spdlog/include ${CMAKE_CURRENT_SOURCE_DIR}/third_party/exprtk) -set(VERSION_PATCH 233) +set(VERSION_PATCH 234) option( WITH_LIBCXX diff --git a/src/DeviceModeling/device_net.h b/src/DeviceModeling/device_net.h index ba46752e2..8513fcb85 100644 --- a/src/DeviceModeling/device_net.h +++ b/src/DeviceModeling/device_net.h @@ -17,6 +17,8 @@ class device_block; class device_signal; +using namespace std; + /** * @class device_net * @brief Class representing a net in a device. diff --git a/src/DeviceModeling/rs_parameter.h b/src/DeviceModeling/rs_parameter.h index ceac7f6b6..220a0ff49 100644 --- a/src/DeviceModeling/rs_parameter.h +++ b/src/DeviceModeling/rs_parameter.h @@ -11,7 +11,10 @@ #pragma once +#include +#include #include +#include #include "rs_parameter_type.h" @@ -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(); diff --git a/src/DeviceModeling/rs_parameter_type.h b/src/DeviceModeling/rs_parameter_type.h index 7b8033eea..22405f760 100644 --- a/src/DeviceModeling/rs_parameter_type.h +++ b/src/DeviceModeling/rs_parameter_type.h @@ -15,6 +15,21 @@ #include #include #include +class TypeNameMapper { + public: + template + 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. diff --git a/tests/unittest/CMakeLists.txt b/tests/unittest/CMakeLists.txt index 79bac1d4c..5eadbc65e 100644 --- a/tests/unittest/CMakeLists.txt +++ b/tests/unittest/CMakeLists.txt @@ -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 @@ -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 diff --git a/tests/unittest/DeviceModeling/device_net_test.cpp b/tests/unittest/DeviceModeling/device_net_test.cpp new file mode 100644 index 000000000..15378b1c9 --- /dev/null +++ b/tests/unittest/DeviceModeling/device_net_test.cpp @@ -0,0 +1,99 @@ +#include "DeviceModeling/device_net.h" + +#include + +#include + +// 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 source_net = + std::make_shared("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 sink1 = std::make_shared("sink1"); + std::shared_ptr sink2 = std::make_shared("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); +} diff --git a/tests/unittest/DeviceModeling/rs_parameter_test.cpp b/tests/unittest/DeviceModeling/rs_parameter_test.cpp new file mode 100644 index 000000000..d3833a536 --- /dev/null +++ b/tests/unittest/DeviceModeling/rs_parameter_test.cpp @@ -0,0 +1,74 @@ +/** + * @file rs_parameter_test.cpp + * @author Manadher Kharroubi (manadher@rapidsilicon.com) + * @brief + * @version 0.1 + * @date 2023-05-18 + * + * @copyright Copyright (c) 2023 + * + */ + +#include "DeviceModeling/rs_parameter.h" + +#include + +TEST(ParameterTest, Constructor) { + auto type = std::make_shared>(); + Parameter param("test", 5, type); + + EXPECT_EQ(param.get_name(), "test"); + EXPECT_EQ(param.get_value(), 5); +} + +TEST(ParameterTest, SetValue) { + auto type = std::make_shared>(); + Parameter param("test", 5, type); + param.set_value(10); + + EXPECT_EQ(param.get_value(), 10); +} + +TEST(ParameterTest, InvalidValue) { + auto type = std::make_shared>(); + + type->set_lower_bound(0); + type->set_upper_bound(10); + EXPECT_THROW(Parameter("test", -1, type), std::runtime_error); +} + +TEST(ParameterTest, SetAddress) { + auto type = std::make_shared>(); + Parameter param("test", 5, type); + param.set_address(123); + + EXPECT_EQ(param.get_address(), 123); +} + +TEST(ParameterTest, IntToString) { + auto type = std::make_shared>(); + Parameter param("test", 5, type); + + EXPECT_EQ(param.to_string(), "Parameter test: 5 of type int"); +} + +TEST(ParameterTest, DoubleToString) { + auto type = std::make_shared>(); + Parameter 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>(); + Parameter param("test", "Test", type); + + EXPECT_EQ(param.to_string(), "Parameter test: Test of type string"); +} + +TEST(ParameterTest, InvalidSetAddress) { + auto type = std::make_shared>(); + Parameter param("test", 5.5, type); + + EXPECT_THROW(param.set_address(123), std::runtime_error); +}