Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft : Issue 344 utils component code coverage #352

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
78 changes: 78 additions & 0 deletions utils/tst/AssertTestSuite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef GMDS_ASSERT_TESTSUITE_H
#define GMDS_ASSERT_TESTSUITE_H

#include "gtest/gtest.h"
#include <gmds/utils/Assert.h>
#include <unit_test_config.h>
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
46 changes: 46 additions & 0 deletions utils/tst/BitVectorTestSuite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef GMDS_BIT_VECTOR_TESTSUITE_H
#define GMDS_BIT_VECTOR_TESTSUITE_H

#include "gtest/gtest.h"
#include <gmds/utils/BitVector.h>
#include <unit_test_config.h>
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
7 changes: 7 additions & 0 deletions utils/tst/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions utils/tst/CommonTypesTestSuite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef GMDS_COMMON_TYPES_TESTSUITE_H
#define GMDS_COMMON_TYPES_TESTSUITE_H

#include "gtest/gtest.h"
#include <gmds/utils/CommonTypes.h>
#include <unit_test_config.h>
using namespace gmds;

// Test for getCommonBut
TEST(CommonTypesTestSuite, GetCommonButTest)
{
std::vector<gmds::TCellID> set1 = {1, 2, 3, 4, 5};
std::vector<gmds::TCellID> set2 = {3, 4, 5, 6, 7};
gmds::TCellID but = 4;

std::vector<gmds::TCellID> 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<gmds::TCellID> set = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
gmds::TInt nb = 2;

std::vector<gmds::TCellID> 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
77 changes: 77 additions & 0 deletions utils/tst/LogTestSuite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef GMDS_LOG_TEST_SUITE_H
#define GMDS_LOG_TEST_SUITE_H

#include "gtest/gtest.h"
#include <gmds/utils/Log.h>
#include <gmds/utils/LogStream.h>
#include <unit_test_config.h>

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
77 changes: 77 additions & 0 deletions utils/tst/OrientedGraphTestSuite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef GMDS_ORIENTED_GRAPH_TESTSUITE_H
#define GMDS_ORIENTED_GRAPH_TESTSUITE_H

#include "gtest/gtest.h"
#include <gmds/utils/OrientedGraph.h>
#include <unit_test_config.h>

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
34 changes: 34 additions & 0 deletions utils/tst/RandomGeneratorTestSuite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef GMDS_RANDOM_GENERATOR_TESTSUITE_H
#define GMDS_RANDOM_GENERATOR_TESTSUITE_H

#include "gtest/gtest.h"
#include <gmds/utils/RandomGenerator.h>
#include <unit_test_config.h>

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
Loading
Loading