Skip to content

Commit

Permalink
Added type McCabe tests (Ericsson#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seeker04 committed May 1, 2024
1 parent ffe0038 commit ece08a1
Show file tree
Hide file tree
Showing 7 changed files with 484 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/cpp_metrics/test/sources/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ project(CppMetricsTestProject)

add_library(CppMetricsTestProject STATIC
mccabe.cpp
typemccabe.cpp
lackofcohesion.cpp
bumpyroad.cpp)
135 changes: 135 additions & 0 deletions plugins/cpp_metrics/test/sources/parser/typemccabe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "typemccabe.h"

void ClassMethodsOutside::conditionals(int arg1, bool arg2) { // +1
if (arg2) { } // +1
else { }

if ((arg1 > 5 && arg2) || (arg1 < 13 && !arg2)) { } // +4

int local1 = arg2 ? arg1 / 2 : arg1 * 2; // + 1
int local2 = local1 ?: arg1 + 5; // +1
} // 8

void ClassMethodsOutside::loops1() { // +1
for (int i = 0; i < 5; ++i) {} // +1

int j = 0;
while(j < 5) { ++j; } // +1

do
{
++j;
} while (j < 10); // +1

char str[] = "hello";

for(char c : str) // +1
{
if (c == 'h') {} // +1
}
} // 6

void ClassMethodsOutside::loops2(int arg1, bool arg2) // +1
{
while(arg2) // +1
{
if (arg1 < 5) // +1
{
arg1 *= 2;
continue; // +1
}
else if (arg1 > 30) // +1
{
arg1 /= 2;
continue; // +1
}
break;
}
} // 6

int ClassMethodsOutside::loops3(int arg1) // +1
{
const int LIMIT = 42;
int result = 0;
for (int i = 0; i < arg1 * 2; ++i) // +1
{
++result;
for (int j = i; j < arg1 * 3; ++j) // +1
{
result -= j/5;
if (result >= LIMIT) // +1
goto endfor; // +1
}
}
endfor:
return result;
} // 5

void ClassMethodsOutside::switchcase(int arg1) // +1
{
switch(arg1)
{
case 1: // +1
break;
case 2: // +1
break;
case 3: // +1
break;
default: // +1
switch(arg1 * 5) {
case 85: // +1
break;
case 90: // +1
break;
}
break;
}
} // 7

void ClassMethodsOutside::fragile(int arg1) // +1
{} // 1

void ClassMethodsOutside::trycatch(int arg1) // +1
{
try
{
fragile(arg1);
}
catch (int err) // +1
{

}
catch (float err) // +1
{

}
} // 3

void ClassMethodsOutside::method1(int arg1) // +1
{
for (unsigned int i = arg1; i < 10; ++i) // +1
{
switch(arg1)
{
case -1: // +1
goto endfor; // +1
case 0: // +1
break;
case 1: // +1
break;
default: // +1
continue; // +1
}
arg1 *= 2;
}
endfor:;
} // 8

int ClassMethodsInsideAndOutside::baz() // +1
{
if (true) // +1
{
return 42;
}
} // 2

85 changes: 85 additions & 0 deletions plugins/cpp_metrics/test/sources/parser/typemccabe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifndef TYPE_MCCABE__H
#define TYPE_MCCABE__H

class Empty {};

class ClassMethodsInside {
public:
ClassMethodsInside(bool b) // +1
{
if (b) // +1
{
a = 1;
}
else
{
a = 0;
}
} // 2

void conditionals(int arg1, bool arg2) { // +1
if (arg2) { } // +1
else { }

if ((arg1 > 5 && arg2) || (arg1 < 13 && !arg2)) { } // +4

int local1 = arg2 ? arg1 / 2 : arg1 * 2; // + 1
int local2 = local1 ?: arg1 + 5; // +1
} // 8

void loops1() { // +1
for (int i = 0; i < 5; ++i) {} // +1

int j = 0;
while(j < 5) { ++j; } // +1

do
{
++j;
} while (j < 10); // +1

char str[] = "hello";

for(char c : str) // +1
{
if (c == 'h') {} // +1
}
} // 6
private:
int a;
char *p;
}; // 16

class ClassMethodsOutside { // definitions are in typemccabe.cpp
void conditionals(int arg1, bool arg2); // 8
void loops1(); // 6
void loops2(int arg1, bool arg2); // 6
int loops3(int arg1); // 5
void switchcase(int arg1); // 7
void fragile(int arg1); // 1
void trycatch(int arg1); // 3
void method1(int arg1); // 8
}; // 44

class ClassMethodsInsideAndOutside {
void foo() // +1
{
for (int i=0; i<3; ++i) // +1
{
for (int j=0; j<3; ++j) // +1
{
int x;
}
}
} // 3

bool bar(bool b1, bool b2, bool b3) // +1
{
return b1 && b2 && b3; // +2
} // 3

int baz(); // 2 (defined in typemccabe.cpp)
}; // 8

#endif // TYPE_MCCABE__H

1 change: 1 addition & 0 deletions plugins/cpp_metrics/test/sources/service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ project(CppMetricsTestProject)

add_library(CppMetricsTestProject STATIC
mccabe.cpp
typemccabe.cpp
lackofcohesion.cpp
bumpyroad.cpp)
135 changes: 135 additions & 0 deletions plugins/cpp_metrics/test/sources/service/typemccabe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "typemccabe.h"

void ClassMethodsOutside::conditionals(int arg1, bool arg2) { // +1
if (arg2) { } // +1
else { }

if ((arg1 > 5 && arg2) || (arg1 < 13 && !arg2)) { } // +4

int local1 = arg2 ? arg1 / 2 : arg1 * 2; // + 1
int local2 = local1 ?: arg1 + 5; // +1
} // 8

void ClassMethodsOutside::loops1() { // +1
for (int i = 0; i < 5; ++i) {} // +1

int j = 0;
while(j < 5) { ++j; } // +1

do
{
++j;
} while (j < 10); // +1

char str[] = "hello";

for(char c : str) // +1
{
if (c == 'h') {} // +1
}
} // 6

void ClassMethodsOutside::loops2(int arg1, bool arg2) // +1
{
while(arg2) // +1
{
if (arg1 < 5) // +1
{
arg1 *= 2;
continue; // +1
}
else if (arg1 > 30) // +1
{
arg1 /= 2;
continue; // +1
}
break;
}
} // 6

int ClassMethodsOutside::loops3(int arg1) // +1
{
const int LIMIT = 42;
int result = 0;
for (int i = 0; i < arg1 * 2; ++i) // +1
{
++result;
for (int j = i; j < arg1 * 3; ++j) // +1
{
result -= j/5;
if (result >= LIMIT) // +1
goto endfor; // +1
}
}
endfor:
return result;
} // 5

void ClassMethodsOutside::switchcase(int arg1) // +1
{
switch(arg1)
{
case 1: // +1
break;
case 2: // +1
break;
case 3: // +1
break;
default: // +1
switch(arg1 * 5) {
case 85: // +1
break;
case 90: // +1
break;
}
break;
}
} // 7

void ClassMethodsOutside::fragile(int arg1) // +1
{} // 1

void ClassMethodsOutside::trycatch(int arg1) // +1
{
try
{
fragile(arg1);
}
catch (int err) // +1
{

}
catch (float err) // +1
{

}
} // 3

void ClassMethodsOutside::method1(int arg1) // +1
{
for (unsigned int i = arg1; i < 10; ++i) // +1
{
switch(arg1)
{
case -1: // +1
goto endfor; // +1
case 0: // +1
break;
case 1: // +1
break;
default: // +1
continue; // +1
}
arg1 *= 2;
}
endfor:;
} // 8

int ClassMethodsInsideAndOutside::baz() // +1
{
if (true) // +1
{
return 42;
}
} // 2

Loading

0 comments on commit ece08a1

Please sign in to comment.