View Javadoc
1   package com.puppycrawl.tools.checkstyle.checks.metrics.npathcomplexity;
2   // Advise: for lack of ambiguity try to make all factors prime numbers
3   public class InputNPathComplexity {
4       //NP = 5
5       void testIfWithExpression() {
6           // NP = (if-range=1) + 1 + (expr=3) = 5
7           if (true && true || (true || true)) { }
8       }
9       
10      //NP = 5
11      void testIfElseWithExpression() {
12          // NP = (if-range=1) + (else-range=1) + (expr=3) = 5
13          if (true && true || (true || true)) { }
14          else { }
15      }
16      
17      //NP = 4
18      int testSimpleSwitch() {
19          int a = 0;
20          // NP = (case-range[1]=1) + (case-range[2]=1) + (case-range[3]=1)
21          //         + (default-range=1) + (expr=0) = 4
22          switch(a) {
23          case 1:
24              break;
25          case 2:
26          case 3:
27              break;
28          }
29          return a;
30      }
31      
32      //NP = 4
33      void testSimpleSwitchWithDefault() {
34          int a = 0;
35          // NP = (case-range[1]=1) + (case-range[2]=1) + (case-range[3]=1)
36          //         + (default-range=1) + (expr=0) = 4
37          switch(a) {
38          case 1:
39              break;
40          case 2:
41          case 3:
42              break;
43          default:
44              break;
45          }
46      }
47      
48      //NP = 6
49      void testSwitchWithExpression() {
50          int a = 0;
51          // NP = (case-range[1]=1) + (case-range[2]=1) + (case-range[3]=1)
52          //         + (default-range=1) + (expr=2) = 6
53          switch(true ? a : a) {
54          case 1:
55              break;
56          case 2:
57          case 3:
58              break;
59          default:
60              break;
61          }
62      }
63      
64      //NP = 15
65      void testComplexSwitch() {
66          int a = 0;
67          // NP = (case-range[1]=2) + (case-range[2]=5*2) + (case-range[3]=2)
68          //         + (default-range=1) + (expr=0) = 15
69          switch(a) {
70          case 1:
71              // NP(case-range) = (if-range=1) + 1 + (expr=0) = 2
72              if (true) { }
73              break;
74          case 2:
75              // NP(case-range) = (if-range=1) + (else-range=1) + (expr=3) = 5
76              if (true && true || (true || true)) { }
77              else { }
78              // NP(case-range) = (if-range=1) + 1 + (expr=0) = 2
79              if (true) { }
80          case 3:
81              // NP(case-range) = (if-range=1) + 1 + (expr=0) = 2
82              if (true) { }
83              break;
84          default:
85              break;
86          }
87      }
88      
89      // NP = 11   
90      void testComplexIfElse() {
91          // NP = (if-range=1) + (else-range=9) + (expr=1) = 11
92          if (true && true) { }
93          // NP(else-range) = (if-range=1) + (else-range=6) + (expr=2) = 9
94          else if (true || true || true) { }
95          // NP(else-range) = (if-range=1) + 1 + (expr=4) = 6
96          else if (true && true && true || true || true) { }
97      }
98      
99      // NP = 8
100     boolean testComplexReturn() {
101         // NP = (if-range=3) + (else-range=4) + (expr=1) = 8
102         if (true && true) {
103             // NP(if-range) = 3
104             return true && true || (true && true);
105         } else {
106             // NP(else-range) = (expr(1)=0) + (expr(2)=1) + (expr(3)=1) + 2 = 4
107             return true ? true && true : true || true;
108         }
109     }
110     
111     // NP = (for-statement[1]=2) * (for-statement[2]=3)
112     //         * (for-statement[3]=4) * (for-statement[4]=5) = 120
113     void testForCyclesComplex() {
114         // NP(for-statement) = (for-range=1) + (expr(1)=0) + (expr(2)=0) + (expr(3)=0) + 1 = 2
115         for (int i = 0; i < 10; i++);
116         // NP(for-statement) = (for-range=1) + (expr(1)=0) + (expr(2)=1) + (expr(3)=0) + 1 = 3
117         for (int i = 0; i < 10 && true; i++);
118         // NP(for-statement) = (for-range=1) + (expr(1)=2) + (expr(2)=0) + (expr(3)=0) + 1 = 4
119         for (int i = true ? 0 : 0; i < 10; i++);
120         // NP(for-statement) = (for-range=1) + (expr(1)=0) + (expr(2)=1+2) + (expr(3)=0) + 1 = 5
121         for (int i = 0; true ? i < 10 : true || true; i++);
122     }
123     
124     // NP = (while-statement[1]=2) * (while-statement[2]=3) = 6
125     boolean testWhileCyclesComplex() {
126         int a = 0;
127         // NP(while-statement) = (while-range=1) + (expr=0) + 1 = 2
128         while (a != 0) { }
129         // NP(while-statement) = (while-range=1) + (expr=1) + 1 = 3
130         while (a != 0 && a == 0) { return a == 0 || a == 0; }
131         return true;
132     }
133     
134     // NP = (do-statement[1]=6) * (do-statement[2]=3) = 21
135     void testDoWhileCyclesComplex() {
136         int a = 0;
137         // NP(do-statement) = (do-range=1) + (expr=1) + 1 = 3
138         do { } while (a < 10 && true);
139         // NP(do-statement) = 
140         //         (do-range=3) + ((expr(1)=0) + (expr(2)=0) + (expr(3)=1) + 2) + 1 = 7
141         do {
142             // NP(do-range) = (do-range=1) + (expr=1) + 1 = 3
143             do { } while (a < 10 || true);
144         } while (true ? a > 10 : (a < 10 || true));
145     }
146     
147     // NP = (question-statement[1]=5) * (question-statement[2]=7) = 35
148     void testComplexTernaryOperator() {
149         // NP(question-statement) = (expr(1)=0) + (expr(2)=2) + (expr(3)=1+2) + 2 = 7
150         boolean a = true ? (true ? true : true) : (false ? (true || false) : true);
151         // NP(question-statement) = (expr(1)=0) + (expr(2)=2) + (expr(3)=1) + 2 = 5;
152         boolean b = true ? (true ? true : true) : true || true;
153     }
154     
155     // NP = (if-expression[1]=5) * (if-expression[2]=5) = 25
156     void testSimpleTernaryBadFormatting() {
157         // NP(if-expression) = (if-range=2) + 1 + (expr=2) = 5
158         if(
159            true ? true : true
160                 ) { boolean a = true ? true
161                         : true;
162         }
163         // NP(if-expression) = (if-range=2) + 1 + (expr=2) = 5
164         if(
165                 true ? true : true) { boolean b = true ? true : true;
166              }
167     }
168 
169     //Calculation for try-catch is wrong now
170     //See issue #3814 https://github.com/checkstyle/checkstyle/issues/3814
171     void testTryCatch() {
172        try {
173        }
174        catch (Exception e) {
175        }
176     }
177 
178 }