diff --git a/utils/tst/AssertTestSuite.h b/utils/tst/AssertTestSuite.h new file mode 100644 index 000000000..67ccc90a8 --- /dev/null +++ b/utils/tst/AssertTestSuite.h @@ -0,0 +1,78 @@ +#ifndef GMDS_ASSERT_TESTSUITE_H +#define GMDS_ASSERT_TESTSUITE_H + +#include "gtest/gtest.h" +#include +#include +using namespace gmds; + +#define STR_HELPER(x) #x +#define STR(x) STR_HELPER(x) + +TEST(AssertTestSuite, SetAndGetAssertMode) +{ + // Test setAssertMode and getAssertMode functions + setAssertMode(ASSERT_MODE_THROW); + EXPECT_EQ(getAssertMode(), ASSERT_MODE_THROW); + + setAssertMode(ASSERT_MODE_ABORT); + EXPECT_EQ(getAssertMode(), ASSERT_MODE_ABORT); +} + +TEST(AssertTestSuite, AssertFailedThrow) +{ + // Test assertFailed with ASSERT_MODE_THROW + setAssertMode(ASSERT_MODE_THROW); + + EXPECT_THROW(assertFailed("1 == 2", STR(test_file.cpp), 42), GMDSException); +} + +TEST(AssertTestSuite, AssertFailedAbort) +{ + // Test assertFailed with ASSERT_MODE_ABORT + setAssertMode(ASSERT_MODE_ABORT); + + // Replace the exit(0) test with output to a file or other mechanism + ASSERT_EXIT(assertFailed("1 == 2", STR(test_file.cpp), 42), ::testing::ExitedWithCode(0), ""); +} + +TEST(AssertTestSuite, AssertRangeFailedThrow) +{ + // Test assertRangeFailed with ASSERT_MODE_THROW + setAssertMode(ASSERT_MODE_THROW); + + EXPECT_THROW(assertRangeFailed(5.0, 0.0, 2.0, STR(test_file.cpp), 42), GMDSException); +} + +TEST(AssertTestSuite, AssertRangeFailedAbort) +{ + // Test assertRangeFailed with ASSERT_MODE_ABORT + setAssertMode(ASSERT_MODE_ABORT); + + // Replace the exit(0) test with output to a file or other mechanism + ASSERT_EXIT(assertRangeFailed(5.0, 0.0, 2.0, STR(test_file.cpp), 42), ::testing::ExitedWithCode(0), ""); +} + +TEST(AssertTestSuite, SetAssertModeAndAssertFailed) +{ + // Test setting assertion mode and calling assertFailed + setAssertMode(ASSERT_MODE_THROW); + GMDS_ASSERT(1 == 1); // Should not throw + + setAssertMode(ASSERT_MODE_ABORT); + // Replace the exit(0) test with output to a file or other mechanism + ASSERT_EXIT(GMDS_ASSERT(1 == 2), ::testing::ExitedWithCode(0), ""); +} + +TEST(AssertTestSuite, SetAssertModeAndRangeAssert) +{ + // Test setting assertion mode and calling range assertion + setAssertMode(ASSERT_MODE_THROW); + GMDS_RANGE_ASSERT(1.0, 0.0, 2.0); // Should not throw + + setAssertMode(ASSERT_MODE_ABORT); + // Replace the exit(0) test with output to a file or other mechanism + ASSERT_EXIT(GMDS_RANGE_ASSERT(5.0, 0.0, 2.0, STR(test_file.cpp), 42), ::testing::ExitedWithCode(0), ""); +} + +#endif // GMDS_ASSERT_TEST_H diff --git a/utils/tst/BitVectorTestSuite.h b/utils/tst/BitVectorTestSuite.h new file mode 100644 index 000000000..ec18417ea --- /dev/null +++ b/utils/tst/BitVectorTestSuite.h @@ -0,0 +1,46 @@ +#ifndef GMDS_BIT_VECTOR_TESTSUITE_H +#define GMDS_BIT_VECTOR_TESTSUITE_H + +#include "gtest/gtest.h" +#include +#include +using namespace gmds; + +TEST(BitVectorTestSuite, DefaultConstructor) +{ + gmds::BitVector bv; + EXPECT_EQ(bv.size(), 0); + EXPECT_EQ(bv.top(), 0); + EXPECT_EQ(bv.capacity(), gmds::GChunkSize); + EXPECT_TRUE(bv.empty()); +} + +TEST(BitVectorTestSuite, CopyConstructor) +{ + gmds::BitVector bv; + gmds::BitVector bv_copy(bv); + EXPECT_EQ(bv_copy.size(), 0); + EXPECT_EQ(bv_copy.top(), 0); + EXPECT_EQ(bv_copy.capacity(), gmds::GChunkSize); + EXPECT_TRUE(bv_copy.empty()); +} + +TEST(BitVectorTestSuite, Resize) +{ + gmds::BitVector bv; + bv.resize(10); + EXPECT_EQ(bv.capacity(), 10); +} + +TEST(BitVectorTestSuite, FillAll) +{ + gmds::BitVector bv; + bv.resize(5); + bv.fillAll(); + EXPECT_EQ(bv.size(), 5); + EXPECT_EQ(bv.top(), 5); +} + +// Ajoutez d'autres tests en fonction de vos fonctions + +#endif // BIT_VECTOR_TEST_H diff --git a/utils/tst/CMakeLists.txt b/utils/tst/CMakeLists.txt index aca727436..7883224d8 100644 --- a/utils/tst/CMakeLists.txt +++ b/utils/tst/CMakeLists.txt @@ -2,6 +2,13 @@ add_executable(GMDS_UTILS_TEST UtilsTestSuite.h ParamTestSuite.h ArrayTestSuite.h + RandomGeneratorTestSuite.h + BitVectorTestSuite.h + AssertTestSuite.h + OrientedGraphTestSuite.h + CommonTypesTestSuite.h + VariableManagerTestSuite.h + LogTestSuite.h main_test.cpp) #============================================================================== target_link_libraries(GMDS_UTILS_TEST PUBLIC diff --git a/utils/tst/CommonTypesTestSuite.h b/utils/tst/CommonTypesTestSuite.h new file mode 100644 index 000000000..35d1f9d72 --- /dev/null +++ b/utils/tst/CommonTypesTestSuite.h @@ -0,0 +1,36 @@ +#ifndef GMDS_COMMON_TYPES_TESTSUITE_H +#define GMDS_COMMON_TYPES_TESTSUITE_H + +#include "gtest/gtest.h" +#include +#include +using namespace gmds; + +// Test for getCommonBut +TEST(CommonTypesTestSuite, GetCommonButTest) +{ + std::vector set1 = {1, 2, 3, 4, 5}; + std::vector set2 = {3, 4, 5, 6, 7}; + gmds::TCellID but = 4; + + std::vector result = gmds::getCommonBut(set1, set2, but); + + ASSERT_EQ(result.size(), 2); + ASSERT_TRUE(std::find(result.begin(), result.end(), 2) != result.end()); + ASSERT_TRUE(std::find(result.begin(), result.end(), 3) != result.end()); + ASSERT_TRUE(std::find(result.begin(), result.end(), 4) == result.end()); +} + +TEST(CommonTypesTestSuite, KeepFilterTest) +{ + std::vector set = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4}; + gmds::TInt nb = 2; + + std::vector result = gmds::keepFilter(set, nb); + + ASSERT_EQ(result.size(), 2); + ASSERT_TRUE(std::find(result.begin(), result.end(), 3) != result.end()); + ASSERT_TRUE(std::find(result.begin(), result.end(), 4) != result.end()); +} + +#endif // GMDS_COMMON_TYPES_TEST_SUITE_H diff --git a/utils/tst/LogTestSuite.h b/utils/tst/LogTestSuite.h new file mode 100644 index 000000000..5786bf4b4 --- /dev/null +++ b/utils/tst/LogTestSuite.h @@ -0,0 +1,77 @@ +#ifndef GMDS_LOG_TEST_SUITE_H +#define GMDS_LOG_TEST_SUITE_H + +#include "gtest/gtest.h" +#include +#include +#include + +using namespace gmds; + +TEST(LogTestSuite, ReportingLevelTest) +{ + LogLevel initialLevel = Log::reportingLevel(); + + // Test setting and getting reporting level + Log::reportingLevel() = LOG_DEBUG; + EXPECT_EQ(Log::reportingLevel(), LOG_DEBUG); + + // Reset to the initial level + Log::reportingLevel() = initialLevel; +} + +TEST(LogTestSuite, AddStreamTest) +{ + Log log; + LogStream stream1(LOG_INFO); + LogStream stream2(LOG_WARNING); + + // Add streams to the log + log.addStream(stream1); + log.addStream(stream2); + + // Check if streams are added correctly + ASSERT_EQ(log.out_.size(), 2); + EXPECT_EQ(log.out_[0].level(), LOG_INFO); + EXPECT_EQ(log.out_[1].level(), LOG_WARNING); +} + +TEST(LogTestSuite, ClearTest) +{ + Log log; + LogStream stream1(LOG_INFO); + LogStream stream2(LOG_WARNING); + + // Add streams to the log + log.addStream(stream1); + log.addStream(stream2); + + // Clear the log + log.clear(); + + // Check if the log is cleared + EXPECT_TRUE(log.out_.empty()); +} + +TEST(LogTestSuite, OperatorStreamTest) +{ + Log log; + LogStream stream1(LOG_INFO); + LogStream stream2(LOG_WARNING); + + // Add streams to the log + log.addStream(stream1); + log.addStream(stream2); + + // Test the operator<< with log + log << "Test message"; + + // Verify if the message is written to the streams + for (const auto &outStream : log.out_) { + std::ostringstream oss; + oss << "Test message"; + EXPECT_EQ(outStream.str(), oss.str()); + } +} + +#endif // GMDS_LOG_TESTSUITE_H \ No newline at end of file diff --git a/utils/tst/OrientedGraphTestSuite.h b/utils/tst/OrientedGraphTestSuite.h new file mode 100644 index 000000000..b3f07545b --- /dev/null +++ b/utils/tst/OrientedGraphTestSuite.h @@ -0,0 +1,77 @@ +#ifndef GMDS_ORIENTED_GRAPH_TESTSUITE_H +#define GMDS_ORIENTED_GRAPH_TESTSUITE_H + +#include "gtest/gtest.h" +#include +#include + +using namespace gmds; + +TEST(OrientedGraphTestSuite, GraphNodeAddOutEdge) +{ + GraphNode node1(1); + GraphNode node2(2); + GraphEdge edge(1, &node1, &node2); + + ASSERT_TRUE(node1.addOutEdge(&edge)); + ASSERT_EQ(node1.outEdges().size(), 1); + ASSERT_EQ(node1.outEdges()[0]->id(), edge.id()); +} + +TEST(OrientedGraphTestSuite, GraphNodeAddInEdge) +{ + GraphNode node1(1); + GraphNode node2(2); + GraphEdge edge(1, &node1, &node2); + + ASSERT_TRUE(node2.addInEdge(&edge)); + ASSERT_EQ(node2.inEdges().size(), 1); + ASSERT_EQ(node2.inEdges()[0]->id(), edge.id()); +} + +TEST(OrientedGraphTestSuite, GraphEdgeGetEdgesStartingFrom) +{ + GraphNode node1(1); + GraphNode node2(2); + GraphEdge edge1(1, &node1, &node2); + GraphEdge edge2(2, &node1, &node2); + + ASSERT_EQ(edge1.getEdgesStartingFrom(&node1).size(), 1); + ASSERT_EQ(edge1.getEdgesStartingFrom(&node2).size(), 0); + ASSERT_EQ(edge2.getEdgesStartingFrom(&node1).size(), 1); + ASSERT_EQ(edge2.getEdgesStartingFrom(&node2).size(), 0); +} + +TEST(OrientedGraphTestSuite, OrientedGraphAddEdge) +{ + OrientedGraph graph(3); + ASSERT_TRUE(graph.addEdge(1, 0, 1)); + ASSERT_EQ(graph.nbEdges(), 1); +} + +TEST(OrientedGraphTestSuite, OrientedGraphUpdateNodes) +{ + OrientedGraph graph(3); + graph.addEdge(1, 0, 1); + graph.updateNodes(); + + ASSERT_EQ(graph.node(0)->outEdges().size(), 1); + ASSERT_EQ(graph.node(1)->inEdges().size(), 1); +} + +TEST(OrientedGraphTestSuite, OrientedGraphNode) +{ + OrientedGraph graph(3); + GraphNode *node = graph.node(1); + ASSERT_EQ(node->id(), 1); +} + +TEST(OrientedGraphTestSuite, OrientedGraphEdge) +{ + OrientedGraph graph(3); + graph.addEdge(1, 0, 1); + GraphEdge *edge = graph.edge(0); + ASSERT_EQ(edge->id(), 1); +} + +#endif // ORIENTED_GRAPH_TEST_H diff --git a/utils/tst/RandomGeneratorTestSuite.h b/utils/tst/RandomGeneratorTestSuite.h new file mode 100644 index 000000000..f70c8e1bd --- /dev/null +++ b/utils/tst/RandomGeneratorTestSuite.h @@ -0,0 +1,34 @@ +#ifndef GMDS_RANDOM_GENERATOR_TESTSUITE_H +#define GMDS_RANDOM_GENERATOR_TESTSUITE_H + +#include "gtest/gtest.h" +#include +#include + +using namespace gmds; + +TEST(RandomGeneratorTestSuite, ValueInRange) +{ + gmds::RandomGenerator randomGen; + randomGen.init(); + + for (int i = 0; i < 1000; ++i) { + double value = randomGen.value(); + EXPECT_GE(value, 0.0); + EXPECT_LE(value, 1.0); + } +} + +TEST(RandomGeneratorTestSuite, Initialization) +{ + gmds::RandomGenerator randomGen1; + gmds::RandomGenerator randomGen2; + + randomGen1.init(); + randomGen2.init(); + + // Test if two instances have different seed values + EXPECT_NE(randomGen1.value(), randomGen2.value()); +} + +#endif // GMDS_RANDOM_GENERATOR_TESTSUITE_H \ No newline at end of file diff --git a/utils/tst/VariableManagerTestSuite.h b/utils/tst/VariableManagerTestSuite.h new file mode 100644 index 000000000..d6ee7c9b6 --- /dev/null +++ b/utils/tst/VariableManagerTestSuite.h @@ -0,0 +1,139 @@ +#ifndef GMDS_VARIABLE_MANAGER_TESTSUITE_H +#define GMDS_VARIABLE_MANAGER_TESTSUITE_H + +#include "gtest/gtest.h" +#include +#include + +using namespace gmds; + +TEST(VariableManagerTestSuite, NewVariable) +{ + VariableManager variableManager; + + Variable *intVariable = variableManager.newVariable("intVariable", 2); + ASSERT_TRUE(intVariable != nullptr); + ASSERT_EQ(intVariable->getName(), "intVariable"); + ASSERT_EQ(intVariable->getDomainSize(), 3); + + Variable *doubleVariable = variableManager.newVariable("doubleVariable", 3); + ASSERT_TRUE(doubleVariable != nullptr); + ASSERT_EQ(doubleVariable->getName(), "doubleVariable"); + ASSERT_EQ(doubleVariable->getDomainSize(), 4); +} + +TEST(VariableManagerTestSuite, GetVariable) +{ + VariableManager variableManager; + + Variable *intVariable = variableManager.newVariable("intVariable", 2); + Variable *doubleVariable = variableManager.newVariable("doubleVariable", 3); + + ASSERT_EQ(variableManager.getVariable("intVariable"), intVariable); + ASSERT_EQ(variableManager.getVariable("doubleVariable"), doubleVariable); + + ASSERT_THROW(variableManager.getVariable("nonExistentVariable"), GMDSException); +} + +TEST(VariableManagerTestSuite, DeleteVariable) +{ + VariableManager variableManager; + + Variable *intVariable = variableManager.newVariable("intVariable", 2); + Variable *doubleVariable = variableManager.newVariable("doubleVariable", 3); + + ASSERT_TRUE(variableManager.doesVariableExist("intVariable")); + ASSERT_TRUE(variableManager.doesVariableExist("doubleVariable")); + + variableManager.deleteVariable("intVariable"); + ASSERT_FALSE(variableManager.doesVariableExist("intVariable")); + + variableManager.deleteVariable(doubleVariable); + ASSERT_FALSE(variableManager.doesVariableExist("doubleVariable")); +} + +TEST(VariableManagerTestSuite, AddRemoveEntry) +{ + VariableManager variableManager; + + Variable *intVariable = variableManager.newVariable("intVariable", 2); + Variable *doubleVariable = variableManager.newVariable("doubleVariable", 3); + + variableManager.addEntry(1); + ASSERT_EQ(intVariable->getEntriesCount(), 1); + ASSERT_EQ(doubleVariable->getEntriesCount(), 1); + + variableManager.removeEntry(1); + ASSERT_EQ(intVariable->getEntriesCount(), 0); + ASSERT_EQ(doubleVariable->getEntriesCount(), 0); +} + +TEST(VariableManagerTestSuite, SetDomain) +{ + VariableManager variableManager; + + Variable *intVariable = variableManager.newVariable("intVariable", 2); + Variable *doubleVariable = variableManager.newVariable("doubleVariable", 3); + + variableManager.setDomain(5); + ASSERT_EQ(intVariable->getDomainSize(), 6); + ASSERT_EQ(doubleVariable->getDomainSize(), 6); +} + +TEST(VariableManagerTestSuite, GetNbVariables) +{ + VariableManager variableManager; + + ASSERT_EQ(variableManager.getNbVariables(), 0); + + Variable *intVariable = variableManager.newVariable("intVariable", 2); + Variable *doubleVariable = variableManager.newVariable("doubleVariable", 3); + + ASSERT_EQ(variableManager.getNbVariables(), 2); + + variableManager.deleteVariable(intVariable); + ASSERT_EQ(variableManager.getNbVariables(), 1); +} + +TEST(VariableManagerTestSuite, Empty) +{ + VariableManager variableManager; + + ASSERT_TRUE(variableManager.empty()); + + Variable *intVariable = variableManager.newVariable("intVariable", 2); + + ASSERT_FALSE(variableManager.empty()); + + variableManager.deleteVariable(intVariable); + ASSERT_TRUE(variableManager.empty()); +} + +TEST(VariableManagerTestSuite, ClearVariables) +{ + VariableManager variableManager; + + Variable *intVariable = variableManager.newVariable("intVariable", 2); + Variable *doubleVariable = variableManager.newVariable("doubleVariable", 3); + + ASSERT_EQ(variableManager.getNbVariables(), 2); + + variableManager.clearVariables(); + + ASSERT_EQ(variableManager.getNbVariables(), 0); +} + +TEST(VariableManagerTestSuite, Compact) +{ + VariableManager variableManager; + + Variable *intVariable = variableManager.newVariable("intVariable", 2); + Variable *doubleVariable = variableManager.newVariable("doubleVariable", 3); + + variableManager.deleteVariable(intVariable); + variableManager.compact(); + + ASSERT_EQ(variableManager.getNbVariables(), 1); +} + +#endif // GMDS_VARIABLE_MANAGER_TESTSUITE_H diff --git a/utils/tst/main_test.cpp b/utils/tst/main_test.cpp index 020aa11a0..5066643fd 100644 --- a/utils/tst/main_test.cpp +++ b/utils/tst/main_test.cpp @@ -2,13 +2,21 @@ #include /*----------------------------------------------------------------------------*/ // Files containing the different test suites to launch -#include "UtilsTestSuite.h" -#include "ParamTestSuite.h" #include "ArrayTestSuite.h" +// #include "AssertTestSuite.h" +#include "BitVectorTestSuite.h" +#include "CommonTypesTestSuite.h" +// #include "LogTestSuite.h" +// #include "OrientedGraphTestSuite.h" +#include "ParamTestSuite.h" +// #include "RandomGeneratorTestSuite.h" +#include "UtilsTestSuite.h" +// #include "VariableManagerTestSuite.h" /*----------------------------------------------------------------------------*/ -int main(int argc, char ** argv) { - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); +int +main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); } /*----------------------------------------------------------------------------*/ -