65
67
for (int i = 0; i < 5; i++)
68
70
assertEquals(0.2, a.getBranchCoverageRate(), 0);
69
71
assertEquals(5, a.getNumberOfValidBranches(), 0);
70
72
assertEquals(1, a.getNumberOfCoveredBranches(), 0);
74
76
assertEquals(0.4, a.getBranchCoverageRate(), 0);
75
77
assertEquals(5, a.getNumberOfValidBranches(), 0);
76
78
assertEquals(2, a.getNumberOfCoveredBranches(), 0);
79
81
assertEquals(0.6, a.getBranchCoverageRate(), 0);
80
82
assertEquals(5, a.getNumberOfValidBranches(), 0);
81
83
assertEquals(3, a.getNumberOfCoveredBranches(), 0);
84
86
assertEquals(0.8, a.getBranchCoverageRate(), 0);
85
87
assertEquals(5, a.getNumberOfValidBranches(), 0);
86
88
assertEquals(4, a.getNumberOfCoveredBranches(), 0);
89
91
assertEquals(1, a.getBranchCoverageRate(), 0);
90
92
assertEquals(5, a.getNumberOfValidBranches(), 0);
91
93
assertEquals(5, a.getNumberOfCoveredBranches(), 0);
96
98
assertEquals(0, a.getHits(0));
97
99
for (int i = 0; i < 400; i++)
99
101
assertEquals(400, a.getHits(0));
101
103
assertEquals(0, a.getHits(1));
102
104
for (int i = 0; i < 4500; i++)
104
106
assertEquals(4500, a.getHits(1));
106
108
assertEquals(0, a.getHits(2));
107
109
for (int i = 0; i < 300; i++)
109
111
assertEquals(300, a.getHits(2));
111
113
assertEquals(0, a.getHits(3));
112
114
for (int i = 0; i < 800; i++)
114
116
assertEquals(800, a.getHits(3));
116
118
assertEquals(0, a.getDefaultHits());
117
119
for (int i = 0; i < 200; i++)
119
121
assertEquals(200, a.getDefaultHits());
122
124
public void testMerge()
128
130
SwitchData x = new SwitchData(0);
132
134
assertEquals(2, a.getHits(0));
133
135
assertEquals(0, a.getHits(1));
147
149
assertEquals(1, a.getHits(5));
148
150
assertEquals(2, a.getDefaultHits());
153
private static void touchIteratively(SwitchData data, int num)
156
* When this test fails, it usually does so well before 2000 iterations. If it
157
* gets past 2000, it will usually pass, so there is not much need in going much
160
for (int i=0; i<2000; i++)
163
* The following yield is needed to make sure the other thread gets
164
* some CPU. Otherwise, this thread will get too much of a jump ahead
165
* of the other thread.
169
data.touchBranch(i,1);
173
private void runTestWithTwoThreads() throws Throwable
175
final SwitchData data = new SwitchData(2);
176
final AtomicReference<Throwable> possibleThrowable = new AtomicReference<Throwable>();
178
ThreadGroup threadGroup = new ThreadGroup("TestThreadGroup") {
179
public void uncaughtException(Thread thread, Throwable t)
182
* Save the Throwable for later use and interrupt this thread so it exits
184
possibleThrowable.set(t);
190
* Create two threads using the above thread group
192
Thread thread1 = new Thread(threadGroup, "1") {
195
touchIteratively(data, 0);
198
Thread thread2 = new Thread(threadGroup, "2") {
201
touchIteratively(data, 1);
207
* Wait for the threads to exit
209
if (thread1.isAlive()) thread1.join();
210
if (thread2.isAlive()) thread2.join();
211
Throwable t = possibleThrowable.get();
218
public void testMultiThreaded() throws Throwable
221
* This test will often pass with only one iteration.
222
* It passes once in a while with 4. It never passes
225
for (int i=0; i<10; i++)
227
runTestWithTwoThreads();