| | 1 | | #pragma rtGlobals=3 |
| | 2 | | #pragma TextEncoding="UTF-8" |
| | 3 | | #pragma rtFunctionErrors=1 |
| | 4 | | #pragma version=1.10 |
| | 5 | | #pragma ModuleName=TEST_Tracing_Complexity |
| | 6 | |
|
| | 7 | | #undef UTF_ALLOW_TRACING |
| | 8 | | #if Exists("TUFXOP_Version") |
| | 9 | |
|
| | 10 | | #if IgorVersion() >= 10.00 |
| | 11 | | #define UTF_ALLOW_TRACING |
| | 12 | | #elif (IgorVersion() >= 9.00) && (NumberByKey("BUILD", IgorInfo(0)) >= 38812) |
| | 13 | | #define UTF_ALLOW_TRACING |
| | 14 | | #endif |
| | 15 | |
|
| | 16 | | #endif |
| | 17 | |
|
| | 18 | | #ifdef UTF_ALLOW_TRACING |
| | 19 | |
|
| 1 | 20 | | static Function Test_Complexity_Simple() |
| 1 | 21 | | // statements that have no influence into the complexity |
| 1 | 22 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("#pragma ModuleName=abc")) |
| 1 | 23 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("#if (IgorVersion() >= 9.00)")) |
| 1 | 24 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("")) |
| 1 | 25 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("string abc = \"def\"")) |
| 1 | 26 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("\t\tFoo#Bar(42)")) |
| 1 | 27 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("\tswitch(foo)")) |
| 1 | 28 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("\ttry")) |
| | 29 | |
|
| 1 | 30 | | // statements that have an influence of 1 |
| 1 | 31 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("static Function Test()")) |
| 1 | 32 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("\tif(a < b)")) |
| 1 | 33 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("\telseif(a < b)")) |
| 1 | 34 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("while(1)")) |
| 1 | 35 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("for(i = 0; i < length; i += 1)")) |
| 1 | 36 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("case MAGIC_CONSTANT:")) |
| 1 | 37 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("SelectString(ParamIsDefault(op), op, \"\")")) |
| 1 | 38 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("SelectNumber(a < b, b, a)")) |
| 1 | 39 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("\tcatch")) |
| 1 | 40 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("variable a = mode1 || mode2")) |
| 1 | 41 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("variable a = mode1 && mode2")) |
| 1 | 42 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("variable op = ParamIsDefault(op) ? 0 : op")) |
| 1 | 43 | | End |
| | 44 | |
|
| 1 | 45 | | static Function Test_Complexity_Faulty() |
| 1 | 46 | | // malicious code that shouldn't match |
| 1 | 47 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("FunctionCaller")) |
| 1 | 48 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("CallFunction")) |
| 1 | 49 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("awhile")) |
| 1 | 50 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("while_constant")) |
| 1 | 51 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("for0")) |
| 1 | 52 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("WrongCase")) |
| 1 | 53 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("MySelectString")) |
| 1 | 54 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("CustomSelectNumber")) |
| 1 | 55 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("string mode = \"a || b\"")) |
| 1 | 56 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("printf \"check if okay\"")) |
| 1 | 57 | | CHECK_EQUAL_VAR(0, IUTF_Tracing#GetCyclomaticComplexity("// wrong case")) |
| 1 | 58 | | End |
| | 59 | |
|
| 1 | 60 | | static Function Test_Complexity_Combined() |
| 1 | 61 | | // combined keywords that increase the complexity |
| 1 | 62 | | CHECK_EQUAL_VAR(2, IUTF_Tracing#GetCyclomaticComplexity("if(a || b < 5)")) |
| 1 | 63 | | CHECK_EQUAL_VAR(3, IUTF_Tracing#GetCyclomaticComplexity("if(a || (b < 5 && !error))")) |
| 1 | 64 | | CHECK_EQUAL_VAR(2, IUTF_Tracing#GetCyclomaticComplexity("try; Call(err); catch; if(err)")) |
| 1 | 65 | | End |
| | 66 | |
|
| 1 | 67 | | static Function Test_Complexity_Casing() |
| 1 | 68 | | // Igor is case insensitive |
| 1 | 69 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("static FunCtion Test()")) |
| 1 | 70 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("\tIF(a < b)")) |
| 1 | 71 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("\tElsEIF(a < b)")) |
| 1 | 72 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("whILE(1)")) |
| 1 | 73 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("FOr(i = 0; i < length; i += 1)")) |
| 1 | 74 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("cASe MAGIC_CONSTANT:")) |
| 1 | 75 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("selectstring(ParamIsDefault(op), op, \"\")")) |
| 1 | 76 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("selectnumber(a < b, b, a)")) |
| 1 | 77 | | CHECK_EQUAL_VAR(1, IUTF_Tracing#GetCyclomaticComplexity("\tcatcH")) |
| 1 | 78 | | End |
| | 79 | |
|
| | 80 | | #endif // UTF_ALLOW_TRACING |