/cpp/src/test/java/com/google/test/metric/cpp/CyclomaticComplexityTest.java

http://testability-explorer.googlecode.com/ · Java · 176 lines · 145 code · 16 blank · 15 comment · 0 complexity · 48cf74420e07039b71fb08f1aa02c981 MD5 · raw file

  1. /*
  2. * Copyright 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.cpp;
  17. import com.google.test.metric.cpp.dom.FunctionDefinition;
  18. import com.google.test.metric.cpp.dom.TranslationUnit;
  19. import junit.framework.TestCase;
  20. public class CyclomaticComplexityTest extends TestCase {
  21. private CyclomaticComplexityAnalyzer analyzer;
  22. @Override
  23. protected void setUp() throws Exception {
  24. analyzer = new CyclomaticComplexityAnalyzer();
  25. }
  26. private TranslationUnit parse(String source) throws Exception {
  27. return new Parser().parse(source);
  28. }
  29. public void testEmptyFunction() throws Exception {
  30. TranslationUnit unit = parse(
  31. "void foo() { " +
  32. "}"
  33. );
  34. FunctionDefinition functionFoo = unit.getChild(0);
  35. functionFoo.accept(analyzer);
  36. assertEquals(1, analyzer.getScore());
  37. }
  38. public void testSimpleFunction() throws Exception {
  39. TranslationUnit unit = parse(
  40. "void foo() { " +
  41. " int i = 0; " +
  42. " i += 1; " +
  43. "} "
  44. );
  45. FunctionDefinition functionFoo = unit.getChild(0);
  46. functionFoo.accept(analyzer);
  47. assertEquals(1, analyzer.getScore());
  48. }
  49. public void testIfFunction() throws Exception {
  50. TranslationUnit unit = parse(
  51. "void foo() { " +
  52. " int a = 0; " +
  53. " if (a < 0) { " +
  54. " a++; " +
  55. " } else { " +
  56. " a--; " +
  57. " } " +
  58. "} "
  59. );
  60. FunctionDefinition functionFoo = unit.getChild(0);
  61. functionFoo.accept(analyzer);
  62. assertEquals(2, analyzer.getScore());
  63. }
  64. public void testIfFunctionNoElse() throws Exception {
  65. TranslationUnit unit = parse(
  66. "void foo() { " +
  67. " int a = 0; " +
  68. " if (a < 0) { " +
  69. " a++; " +
  70. " } " +
  71. "} "
  72. );
  73. FunctionDefinition functionFoo = unit.getChild(0);
  74. functionFoo.accept(analyzer);
  75. assertEquals(2, analyzer.getScore());
  76. }
  77. public void testEmptySwitch() throws Exception {
  78. TranslationUnit unit = parse(
  79. "void foo() { " +
  80. " int a = 0; " +
  81. " switch(a) { " +
  82. " case 0: " +
  83. " a = 0; " +
  84. " } " +
  85. "} "
  86. );
  87. FunctionDefinition functionFoo = unit.getChild(0);
  88. functionFoo.accept(analyzer);
  89. assertEquals(2, analyzer.getScore());
  90. }
  91. public void testLongerSwitch() throws Exception {
  92. TranslationUnit unit = parse(
  93. "void foo() { " +
  94. " int a = 0; " +
  95. " switch(a) { " +
  96. " case 0: " +
  97. " a = 0; " +
  98. " break; " +
  99. " case 1: " +
  100. " a = 1; " +
  101. " break; " +
  102. " } " +
  103. "} "
  104. );
  105. FunctionDefinition functionFoo = unit.getChild(0);
  106. functionFoo.accept(analyzer);
  107. assertEquals(3, analyzer.getScore());
  108. }
  109. public void testSwitchWithDefault() throws Exception {
  110. TranslationUnit unit = parse(
  111. "void foo() { " +
  112. " int a = 0; " +
  113. " switch(a) { " +
  114. " case 0: " +
  115. " a = 0; " +
  116. " break; " +
  117. " default: " +
  118. " a = 3; " +
  119. " } " +
  120. "} "
  121. );
  122. FunctionDefinition functionFoo = unit.getChild(0);
  123. functionFoo.accept(analyzer);
  124. assertEquals(2, analyzer.getScore());
  125. }
  126. public void testLoopFunction() throws Exception {
  127. TranslationUnit unit = parse(
  128. "int foo() { " +
  129. " int a = 0; " +
  130. " while(true) { " +
  131. " ++a; " +
  132. " } " +
  133. " return a; " +
  134. "} "
  135. );
  136. FunctionDefinition functionFoo = unit.getChild(0);
  137. functionFoo.accept(analyzer);
  138. assertEquals(2, analyzer.getScore());
  139. }
  140. public void testTernaryOperator() throws Exception {
  141. TranslationUnit unit = parse(
  142. "int foo(int a, int b) { " +
  143. " return a > 0 ? b : 1; " +
  144. "} "
  145. );
  146. FunctionDefinition functionFoo = unit.getChild(0);
  147. functionFoo.accept(analyzer);
  148. assertEquals(2, analyzer.getScore());
  149. }
  150. public void testNestedTernaryOperator() throws Exception {
  151. TranslationUnit unit = parse(
  152. "int foo(int a, int b) { " +
  153. " return a > 0 ? (b < 0 ? -1 : 0) : 1; " +
  154. "} "
  155. );
  156. FunctionDefinition functionFoo = unit.getChild(0);
  157. functionFoo.accept(analyzer);
  158. assertEquals(3, analyzer.getScore());
  159. }
  160. }