Skip to content

Commit 224fda1

Browse files
author
Marc Geilen
committed
add mcm test dg
1 parent c169d04 commit 224fda1

File tree

9 files changed

+196
-6
lines changed

9 files changed

+196
-6
lines changed

.gitlab-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ build:
1010
script:
1111
- cmake -DCODE_COVERAGE=ON .
1212
- make
13-
- ctest
13+
- ctest --output-on-failure
1414
- make maxpluslibcoverage
1515
artifacts:
1616
expire_in: 14d

include/maxplus/base/analysis/mcm/mcmdg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace Graphs {
5454
* @todo
5555
* check if algorithm can be generalized to float edge weights
5656
*/
57-
CDouble mcmDG(MCMgraph *mcmGraph);
57+
CDouble mcmDG(MCMgraph &mcmGraph);
5858

5959
} // namespace Graphs
6060

src/base/analysis/mcm/mcmdg.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ namespace Graphs {
5454
* @todo
5555
* check if algorithm can be generalized to float edge weights
5656
*/
57-
CDouble mcmDG(MCMgraph *mcmGraph) {
57+
CDouble mcmDG(MCMgraph &mcmGraph) {
5858
// Allocate memory
59-
const unsigned int n = mcmGraph->nrVisibleNodes();
59+
const unsigned int n = mcmGraph.nrVisibleNodes();
6060
std::vector<int> level(n);
6161
std::vector<std::vector<int>> pi(n + 1, std::vector<int>(n));
6262
std::vector<std::vector<int>> d(n + 1, std::vector<int>(n));
@@ -71,7 +71,7 @@ CDouble mcmDG(MCMgraph *mcmGraph) {
7171
std::list<int> Q_k;
7272
Q_k.push_back(0);
7373
std::list<MCMnode*> Q_u;
74-
Q_u.push_back(&(mcmGraph->getNodes().front()));
74+
Q_u.push_back(&(mcmGraph.getNodes().front()));
7575

7676
// Compute the distances
7777
unsigned int k = Q_k.front();
@@ -100,7 +100,7 @@ CDouble mcmDG(MCMgraph *mcmGraph) {
100100

101101
// Compute lambda using Karp's theorem
102102
CDouble l = -INT_MAX;
103-
for (const auto & u : mcmGraph->getNodes()) {
103+
for (const auto & u : mcmGraph.getNodes()) {
104104

105105
if (level[u.id] == n) {
106106
CDouble ld = INT_MAX;

src/testbench/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
include_directories(${CMAKE_SOURCE_DIR}/src)
22

33
add_subdirectory(algebra)
4+
add_subdirectory(base)
45
add_subdirectory(game)
56
add_subdirectory(graph)

src/testbench/base/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
add_test(
2+
NAME base
3+
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/testing_base
4+
)
5+
6+
add_executable(testing_base
7+
testing.cc
8+
mcmtest.cc
9+
)
10+
11+
target_link_libraries(testing_base maxplus)
12+
13+
set(MAXPLUSLIB_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
14+
include_directories(
15+
${CMAKE_CURRENT_SOURCE_DIR}
16+
${MAXPLUSLIB_INCLUDE_DIR}/maxplus
17+
)

src/testbench/base/mcmtest.cc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <algorithm>
2+
3+
#include "algebra/mptype.h"
4+
#include "base/analysis/mcm/mcmdg.h"
5+
#include "base/analysis/mcm/mcmgraph.h"
6+
#include "mcmtest.h"
7+
#include "testing.h"
8+
9+
10+
using namespace MaxPlus;
11+
using namespace Graphs;
12+
13+
void MCMTest::Run() {
14+
this->test_dg();
15+
this->test_howard();
16+
this->test_karp();
17+
this->test_yto();
18+
};
19+
20+
// Test mcmdg.
21+
void MCMTest::test_dg() {
22+
std::cout << "Running test: MCM-dg" << std::endl;
23+
MCMgraph g;
24+
25+
MCMnode& n0 = *g.addNode(0);
26+
MCMnode& n1 = *g.addNode(1);
27+
MCMnode& n2 = *g.addNode(2);
28+
MCMnode& n3 = *g.addNode(3);
29+
g.addEdge(0, n0, n1, 2.0, 1.0);
30+
g.addEdge(0, n1, n2, 2.0, 0.0);
31+
g.addEdge(0, n2, n3, 2.0, 0.0);
32+
g.addEdge(0, n3, n0, 2.0, 0.0);
33+
34+
CDouble result = mcmDG(g);
35+
ASSERT_APPROX_EQUAL(1.0, result, 1e-5);
36+
}
37+
38+
/// Test MCM Howard.
39+
void MCMTest::test_howard() {
40+
std::cout << "Running test: MCM-Howard" << std::endl;
41+
ASSERT_APPROX_EQUAL(1.0, 1.0, 1e-5);
42+
}
43+
44+
/// Test MCM Howard.
45+
void MCMTest::test_karp() {
46+
std::cout << "Running test: MCM-Karp" << std::endl;
47+
ASSERT_APPROX_EQUAL(1.0, 1.0, 1e-5);
48+
}
49+
50+
/// Test MCM Howard.
51+
void MCMTest::test_yto() {
52+
std::cout << "Running test: MCM-YTO" << std::endl;
53+
ASSERT_APPROX_EQUAL(1.0, 1.0, 1e-5);
54+
}

src/testbench/base/mcmtest.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <algorithm>
4+
5+
#include "algebra/mptype.h"
6+
#include "testing.h"
7+
8+
class MCMTest : public ::testing::Test {
9+
10+
public:
11+
MCMTest() {}
12+
virtual void Run();
13+
virtual void SetUp(){};
14+
virtual void TearDown(){};
15+
16+
void test_dg();
17+
void test_howard();
18+
void test_karp();
19+
void test_yto();
20+
};

src/testbench/base/testing.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "mcmtest.h"
2+
3+
int main() {
4+
5+
std::cout << "Running Tests Maxplus - Base" << std::endl;
6+
7+
MCMTest T1;
8+
T1.Run();
9+
10+
return 0;
11+
}

src/testbench/base/testing.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#pragma once
2+
3+
#include <cmath>
4+
5+
namespace testing {
6+
7+
#define ASSERT_THROW( condition) \
8+
{ \
9+
if( !( condition ) ) \
10+
{ \
11+
throw std::runtime_error( std::string( "Assertion violated." ) \
12+
+ std::string( "\nIn:" ) \
13+
+ std::string( __FILE__ ) \
14+
+ std::string( ":" ) \
15+
+ std::to_string( __LINE__ ) \
16+
+ std::string( " in " ) \
17+
+ std::string( __FUNCTION__ ) \
18+
); \
19+
} \
20+
}
21+
22+
#define ASSERT_EQUAL( x, y ) \
23+
{ \
24+
if( ( x ) != ( y ) ) \
25+
{ \
26+
throw std::runtime_error( std::string( "Asserted equality violated." ) \
27+
+ std::string( "\nIn:" ) \
28+
+ std::string( __FILE__ ) \
29+
+ std::string( ":" ) \
30+
+ std::to_string( __LINE__ ) \
31+
+ std::string( " in " ) \
32+
+ std::string( __FUNCTION__ ) \
33+
+ std::string( ": " ) \
34+
+ std::to_string( ( x ) ) \
35+
+ std::string( " != " ) \
36+
+ std::to_string( ( y ) ) \
37+
); \
38+
} \
39+
}
40+
41+
#define ASSERT_APPROX_EQUAL( x, y, eps ) \
42+
{ \
43+
if( abs(( x ) - ( y )) > eps ) \
44+
{ \
45+
throw std::runtime_error( std::string( "Asserted approximate equality violated." ) \
46+
+ std::string( "\nIn:" ) \
47+
+ std::string( __FILE__ ) \
48+
+ std::string( ":" ) \
49+
+ std::to_string( __LINE__ ) \
50+
+ std::string( " in " ) \
51+
+ std::string( __FUNCTION__ ) \
52+
+ std::string( ": " ) \
53+
+ std::to_string( ( x ) ) \
54+
+ std::string( " != " ) \
55+
+ std::to_string( ( y ) ) \
56+
); \
57+
} \
58+
}
59+
60+
61+
#define ASSERT_MP_MINUSINFINITY( x ) \
62+
{ \
63+
if( ! MP_ISMINUSINFINITY(x) ) \
64+
{ \
65+
throw std::runtime_error( std::string( "Asserted equality to minus infinity violated." ) \
66+
+ std::string( "\nIn:" ) \
67+
+ std::string( __FILE__ ) \
68+
+ std::string( ":" ) \
69+
+ std::to_string( __LINE__ ) \
70+
+ std::string( " in " ) \
71+
+ std::string( __FUNCTION__ ) \
72+
+ std::string( ": " ) \
73+
+ std::to_string( ( x ) ) \
74+
+ std::string( " != -inf" ) \
75+
); \
76+
} \
77+
}
78+
79+
class Test {
80+
81+
public:
82+
virtual void SetUp() = 0;
83+
virtual void TearDown() = 0;
84+
virtual void Run() = 0;
85+
};
86+
87+
} // namespace testing

0 commit comments

Comments
 (0)