Skip to content

Commit 1519c8a

Browse files
author
Barnabás Domozi
committed
Added test cases for relational cohesion metric.
1 parent 410c5e3 commit 1519c8a

File tree

10 files changed

+142
-0
lines changed

10 files changed

+142
-0
lines changed

plugins/cpp_metrics/test/sources/parser/modulemetrics.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@
44
#include "./module_b/b2.h"
55
#include "./module_c/c1.h"
66
#include "./module_c/c2.h"
7+
8+
#include "./rc_module_a/a1.h"
9+
#include "./rc_module_a/a2.h"
10+
#include "./rc_module_a/a3.h"
11+
#include "./rc_module_b/b1.h"
12+
#include "./rc_module_c/c1.h"
13+
#include "./rc_module_c/c2.h"
14+
#include "./rc_module_c/c3.h"
15+
#include "./rc_module_c/c4.h"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef CC_CPP_RC_MODULE_METRICS_TEST_A1
2+
#define CC_CPP_RC_MODULE_METRICS_TEST_A1
3+
4+
#include "./a2.h"
5+
#include "./a3.h"
6+
7+
namespace CC_CPP_RC_MODULE_METRICS_TEST
8+
{
9+
class A1 {
10+
A2 a2;
11+
A3 a3;
12+
};
13+
}
14+
15+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CC_CPP_RC_MODULE_METRICS_TEST_A2
2+
#define CC_CPP_RC_MODULE_METRICS_TEST_A2
3+
4+
#include "./a3.h"
5+
6+
namespace CC_CPP_RC_MODULE_METRICS_TEST
7+
{
8+
class A2 {
9+
A3 a3;
10+
};
11+
}
12+
13+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef CC_CPP_RC_MODULE_METRICS_TEST_A3
2+
#define CC_CPP_RC_MODULE_METRICS_TEST_A3
3+
4+
namespace CC_CPP_RC_MODULE_METRICS_TEST
5+
{
6+
class A3 {};
7+
}
8+
9+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef CC_CPP_RC_MODULE_METRICS_TEST_B1
2+
#define CC_CPP_RC_MODULE_METRICS_TEST_B1
3+
4+
namespace CC_CPP_RC_MODULE_METRICS_TEST
5+
{
6+
class B1 {};
7+
}
8+
9+
#endif
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef CC_CPP_RC_MODULE_METRICS_TEST_C1
2+
#define CC_CPP_RC_MODULE_METRICS_TEST_C1
3+
4+
#include "../rc_module_a/a1.h"
5+
#include "../rc_module_a/a2.h"
6+
#include "../rc_module_a/a3.h"
7+
8+
namespace CC_CPP_RC_MODULE_METRICS_TEST
9+
{
10+
class C1 {
11+
A1 a1;
12+
A2 a2;
13+
A3 a3;
14+
};
15+
}
16+
17+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef CC_CPP_RC_MODULE_METRICS_TEST_C2
2+
#define CC_CPP_RC_MODULE_METRICS_TEST_C2
3+
4+
#include "./c1.h"
5+
6+
namespace CC_CPP_RC_MODULE_METRICS_TEST
7+
{
8+
class C2 {
9+
C1 c1;
10+
};
11+
}
12+
13+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef CC_CPP_RC_MODULE_METRICS_TEST_C3
2+
#define CC_CPP_RC_MODULE_METRICS_TEST_C3
3+
4+
namespace CC_CPP_RC_MODULE_METRICS_TEST
5+
{
6+
class C3 {};
7+
}
8+
9+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef CC_CPP_RC_MODULE_METRICS_TEST_C4
2+
#define CC_CPP_RC_MODULE_METRICS_TEST_C4
3+
4+
namespace CC_CPP_RC_MODULE_METRICS_TEST
5+
{
6+
class C4 {};
7+
}
8+
9+
#endif

plugins/cpp_metrics/test/src/cppmetricsparsertest.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ using namespace cc;
2424
extern char* dbConnectionString;
2525

2626
typedef std::pair<std::string, unsigned int> StringUintParam;
27+
typedef std::pair<std::string, double> StringDoubleParam;
2728

2829
class CppMetricsParserTest : public ::testing::Test
2930
{
@@ -353,3 +354,41 @@ INSTANTIATE_TEST_SUITE_P(
353354
ParameterizedEfferentModuleCouplingTest,
354355
::testing::ValuesIn(paramEfferentModule)
355356
);
357+
358+
// Relational cohesion at module level
359+
360+
class ParameterizedRelationalCohesionTest
361+
: public CppMetricsParserTest,
362+
public ::testing::WithParamInterface<StringDoubleParam>
363+
{};
364+
365+
// Relational cohesion formula:
366+
// H = (R + 1)/ N
367+
// where R is the number of type relationships that are internal to a module,
368+
// N is the number of types within a module.
369+
370+
std::vector<StringDoubleParam> paramRelationalCohesion = {
371+
{"%/test/sources/parser/rc_module_a", (3 + 1) / 3.0},
372+
{"%/test/sources/parser/rc_module_b", (0 + 1) / 1.0},
373+
{"%/test/sources/parser/rc_module_c", (1 + 1) / 4.0},
374+
};
375+
376+
TEST_P(ParameterizedRelationalCohesionTest, RelationalCohesionTest) {
377+
_transaction([&, this]() {
378+
379+
typedef odb::query<model::CppModuleMetricsForPathView> CppModuleMetricsQuery;
380+
381+
const auto metric = _db->query_value<model::CppModuleMetricsForPathView>(
382+
CppModuleMetricsQuery::CppFileMetrics::type == model::CppFileMetrics::Type::RELATIONAL_COHESION_MODULE &&
383+
CppModuleMetricsQuery::File::path.like(GetParam().first));
384+
385+
constexpr double tolerance = 1e-8;
386+
EXPECT_NEAR(GetParam().second, metric.value, tolerance);
387+
});
388+
}
389+
390+
INSTANTIATE_TEST_SUITE_P(
391+
ParameterizedRelationalCohesionTestSuite,
392+
ParameterizedRelationalCohesionTest,
393+
::testing::ValuesIn(paramRelationalCohesion)
394+
);

0 commit comments

Comments
 (0)