KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > CoverSwitch


1 package org.hanseltest;
2
3 /**
4  * Switch statements to be covered by the testcases.
5  * @author Niklas Mehner
6  */

7 public class CoverSwitch {
8     
9     /**
10      * Simple switch.
11      * @param i Switch parameter.
12      * @return 1 if i=0, 2 if i=1, 3 if i=5, 4 otherwise.
13      */

14     public int coverSimpleSwitch(int i) {
15         switch (i) {
16             case 0: return 1;
17             case 1: return 2;
18             case 5: return 3;
19             default: return 4;
20         }
21     }
22
23     /**
24      * Switch without default.
25      * @param i Switch parameter.
26      * @return 2, or 3 if i=0.
27      */

28     public int coverNoDefault(int i) {
29         int result = 2;
30
31         switch (i) {
32             case 0: result++;
33         }
34
35         return result;
36     }
37
38     /**
39      * Switch translated to a tableswitch.
40      * @param i Switch parameter.
41      * @return 1 if i in [1..3], 2 if i in [4..6], 3 else.
42      */

43     public int coverSimpleSwitch2(int i) {
44         int result;
45
46         switch (i) {
47             case 1 :
48             case 2 :
49             case 3 :
50                 result = 1;
51                 break;
52             case 4 :
53             case 5 :
54             case 6 :
55                 result = 2;
56                 break;
57             default:
58                 result = 3;
59         }
60
61         return result;
62     }
63 }
64
Popular Tags