PageRenderTime 68ms CodeModel.GetById 56ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 16package com.google.test.metric.cpp;
 17
 18import com.google.test.metric.cpp.dom.FunctionDefinition;
 19import com.google.test.metric.cpp.dom.TranslationUnit;
 20
 21import junit.framework.TestCase;
 22
 23public class CyclomaticComplexityTest extends TestCase {
 24
 25  private CyclomaticComplexityAnalyzer analyzer;
 26
 27  @Override
 28  protected void setUp() throws Exception {
 29    analyzer = new CyclomaticComplexityAnalyzer();
 30  }
 31
 32  private TranslationUnit parse(String source) throws Exception {
 33    return new Parser().parse(source);
 34  }
 35
 36  public void testEmptyFunction() throws Exception {
 37    TranslationUnit unit = parse(
 38        "void foo() {                  " +
 39        "}"
 40    );
 41    FunctionDefinition functionFoo = unit.getChild(0);
 42    functionFoo.accept(analyzer);
 43    assertEquals(1, analyzer.getScore());
 44  }
 45
 46  public void testSimpleFunction() throws Exception {
 47    TranslationUnit unit = parse(
 48        "void foo() {                  " +
 49        "  int i = 0;                  " +
 50        "  i += 1;                     " +
 51        "}                             "
 52    );
 53    FunctionDefinition functionFoo = unit.getChild(0);
 54    functionFoo.accept(analyzer);
 55    assertEquals(1, analyzer.getScore());
 56  }
 57
 58  public void testIfFunction() throws Exception {
 59    TranslationUnit unit = parse(
 60        "void foo() {                  " +
 61        "  int a = 0;                  " +
 62        "  if (a < 0) {                " +
 63        "    a++;                      " +
 64        "  } else {                    " +
 65        "    a--;                      " +
 66        "  }                           " +
 67        "}                             "
 68    );
 69    FunctionDefinition functionFoo = unit.getChild(0);
 70    functionFoo.accept(analyzer);
 71    assertEquals(2, analyzer.getScore());
 72  }
 73
 74  public void testIfFunctionNoElse() throws Exception {
 75    TranslationUnit unit = parse(
 76        "void foo() {                  " +
 77        "  int a = 0;                  " +
 78        "  if (a < 0) {                " +
 79        "    a++;                      " +
 80        "  }                           " +
 81        "}                             "
 82    );
 83    FunctionDefinition functionFoo = unit.getChild(0);
 84    functionFoo.accept(analyzer);
 85    assertEquals(2, analyzer.getScore());
 86  }
 87
 88  public void testEmptySwitch() throws Exception {
 89    TranslationUnit unit = parse(
 90        "void foo() {                  " +
 91        "  int a = 0;                  " +
 92        "  switch(a) {                 " +
 93        "  case 0:                     " +
 94        "    a = 0;                    " +
 95        "  }                           " +
 96        "}                             "
 97    );
 98    FunctionDefinition functionFoo = unit.getChild(0);
 99    functionFoo.accept(analyzer);
100    assertEquals(2, analyzer.getScore());
101  }
102
103  public void testLongerSwitch() throws Exception {
104    TranslationUnit unit = parse(
105        "void foo() {                  " +
106        "  int a = 0;                  " +
107        "  switch(a) {                 " +
108        "  case 0:                     " +
109        "    a = 0;                    " +
110        "    break;                    " +
111        "  case 1:                     " +
112        "    a = 1;                    " +
113        "    break;                    " +
114        "  }                           " +
115        "}                             "
116    );
117    FunctionDefinition functionFoo = unit.getChild(0);
118    functionFoo.accept(analyzer);
119    assertEquals(3, analyzer.getScore());
120  }
121
122  public void testSwitchWithDefault() throws Exception {
123    TranslationUnit unit = parse(
124        "void foo() {                  " +
125        "  int a = 0;                  " +
126        "  switch(a) {                 " +
127        "  case 0:                     " +
128        "    a = 0;                    " +
129        "    break;                    " +
130        "  default:                    " +
131        "    a = 3;                    " +
132        "  }                           " +
133        "}                             "
134    );
135    FunctionDefinition functionFoo = unit.getChild(0);
136    functionFoo.accept(analyzer);
137    assertEquals(2, analyzer.getScore());
138  }
139
140  public void testLoopFunction() throws Exception {
141    TranslationUnit unit = parse(
142        "int foo() {                   " +
143        "  int a = 0;                  " +
144        "  while(true) {               " +
145        "    ++a;                      " +
146        "  }                           " +
147        "  return a;                   " +
148        "}                             "
149    );
150    FunctionDefinition functionFoo = unit.getChild(0);
151    functionFoo.accept(analyzer);
152    assertEquals(2, analyzer.getScore());
153  }
154
155  public void testTernaryOperator() throws Exception {
156    TranslationUnit unit = parse(
157        "int foo(int a, int b) {       " +
158        "  return a > 0 ? b : 1;       " +
159        "}                             "
160    );
161    FunctionDefinition functionFoo = unit.getChild(0);
162    functionFoo.accept(analyzer);
163    assertEquals(2, analyzer.getScore());
164  }
165
166  public void testNestedTernaryOperator() throws Exception {
167    TranslationUnit unit = parse(
168        "int foo(int a, int b) {                  " +
169        "  return a > 0 ? (b < 0 ? -1 : 0) : 1;   " +
170        "}                                        "
171    );
172    FunctionDefinition functionFoo = unit.getChild(0);
173    functionFoo.accept(analyzer);
174    assertEquals(3, analyzer.getScore());
175  }
176}