forked from Ericsson/CodeCompass
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added type McCabe tests (Ericsson#683)
- Loading branch information
Seeker04
committed
May 1, 2024
1 parent
ffe0038
commit ece08a1
Showing
7 changed files
with
484 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
plugins/cpp_metrics/test/sources/service/typemccabe.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.