~ubuntu-branches/ubuntu/trusty/cobertura/trusty

« back to all changes in this revision

Viewing changes to test/net/sourceforge/cobertura/coveragedata/SwitchDataTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-05-11 19:21:46 UTC
  • mfrom: (0.1.4 sid) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100511192146-j742v5jsl89ztndu
Tags: 1.9.4.1+dfsg-2
* Now Build-Depends on libservlet2.5-java and add a missing Depends
  on the same package. (Closes: #580842). 
* Simplify list of JRE dependences for cobertura and drop JRE dependences for
  libcobertura-java as Java libraries are no longer required to depend on a
  JVM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
package net.sourceforge.cobertura.coveragedata;
23
23
 
 
24
import java.util.concurrent.atomic.AtomicReference;
 
25
 
24
26
import junit.framework.TestCase;
25
27
 
26
28
public class SwitchDataTest extends TestCase
33
35
        public void testEquals()
34
36
        {
35
37
                assertFalse(a.equals(null));
36
 
                assertFalse(a.equals(new Integer(4)));
 
38
                assertFalse(a.equals(Integer.valueOf(4)));
37
39
 
38
40
                assertTrue(a.equals(a));
39
41
                assertFalse(a.equals(b));
64
66
 
65
67
                for (int i = 0; i < 5; i++)
66
68
                {
67
 
                        a.touchBranch(1);
 
69
                        a.touchBranch(1,1);
68
70
                        assertEquals(0.2, a.getBranchCoverageRate(), 0);
69
71
                        assertEquals(5, a.getNumberOfValidBranches(), 0);
70
72
                        assertEquals(1, a.getNumberOfCoveredBranches(), 0);
71
73
                }
72
74
 
73
 
                a.touchBranch(-1);
 
75
                a.touchBranch(-1,1);
74
76
                assertEquals(0.4, a.getBranchCoverageRate(), 0);
75
77
                assertEquals(5, a.getNumberOfValidBranches(), 0);
76
78
                assertEquals(2, a.getNumberOfCoveredBranches(), 0);
77
79
 
78
 
                a.touchBranch(0);
 
80
                a.touchBranch(0,1);
79
81
                assertEquals(0.6, a.getBranchCoverageRate(), 0);
80
82
                assertEquals(5, a.getNumberOfValidBranches(), 0);
81
83
                assertEquals(3, a.getNumberOfCoveredBranches(), 0);
82
84
 
83
 
                a.touchBranch(2);
 
85
                a.touchBranch(2,1);
84
86
                assertEquals(0.8, a.getBranchCoverageRate(), 0);
85
87
                assertEquals(5, a.getNumberOfValidBranches(), 0);
86
88
                assertEquals(4, a.getNumberOfCoveredBranches(), 0);
87
89
 
88
 
                a.touchBranch(3);
 
90
                a.touchBranch(3,1);
89
91
                assertEquals(1, a.getBranchCoverageRate(), 0);
90
92
                assertEquals(5, a.getNumberOfValidBranches(), 0);
91
93
                assertEquals(5, a.getNumberOfCoveredBranches(), 0);
95
97
        {
96
98
                assertEquals(0, a.getHits(0));
97
99
                for (int i = 0; i < 400; i++)
98
 
                        a.touchBranch(0);
 
100
                        a.touchBranch(0,1);
99
101
                assertEquals(400, a.getHits(0));
100
102
 
101
103
                assertEquals(0, a.getHits(1));
102
104
                for (int i = 0; i < 4500; i++)
103
 
                        a.touchBranch(1);
 
105
                        a.touchBranch(1,1);
104
106
                assertEquals(4500, a.getHits(1));
105
107
 
106
108
                assertEquals(0, a.getHits(2));
107
109
                for (int i = 0; i < 300; i++)
108
 
                        a.touchBranch(2);
 
110
                        a.touchBranch(2,1);
109
111
                assertEquals(300, a.getHits(2));
110
112
 
111
113
                assertEquals(0, a.getHits(3));
112
114
                for (int i = 0; i < 800; i++)
113
 
                        a.touchBranch(3);
 
115
                        a.touchBranch(3,1);
114
116
                assertEquals(800, a.getHits(3));
115
117
 
116
118
                assertEquals(0, a.getDefaultHits());
117
119
                for (int i = 0; i < 200; i++)
118
 
                        a.touchBranch(-1);
 
120
                        a.touchBranch(-1,1);
119
121
                assertEquals(200, a.getDefaultHits());
120
122
        }
121
123
 
122
124
        public void testMerge()
123
125
        {
124
 
                a.touchBranch(0);
125
 
                a.touchBranch(0);
126
 
                a.touchBranch(2);
127
 
                a.touchBranch(-1);
 
126
                a.touchBranch(0,1);
 
127
                a.touchBranch(0,1);
 
128
                a.touchBranch(2,1);
 
129
                a.touchBranch(-1,1);
128
130
                SwitchData x = new SwitchData(0);
129
 
                x.touchBranch(3);
130
 
                x.touchBranch(3);
 
131
                x.touchBranch(3,1);
 
132
                x.touchBranch(3,1);
131
133
                a.merge(x);
132
134
                assertEquals(2, a.getHits(0));
133
135
                assertEquals(0, a.getHits(1));
136
138
                assertEquals(1, a.getDefaultHits());
137
139
 
138
140
                x = new SwitchData(0);
139
 
                x.touchBranch(5);
140
 
                x.touchBranch(-1);
 
141
                x.touchBranch(5,1);
 
142
                x.touchBranch(-1,1);
141
143
                a.merge(x);
142
144
                assertEquals(2, a.getHits(0));
143
145
                assertEquals(0, a.getHits(1));
147
149
                assertEquals(1, a.getHits(5));
148
150
                assertEquals(2, a.getDefaultHits());
149
151
        }
 
152
        
 
153
        private static void touchIteratively(SwitchData data, int num)
 
154
        {
 
155
                /*
 
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
 
158
                 * past 2000.
 
159
                 */
 
160
                for (int i=0; i<2000; i++)
 
161
                {
 
162
                        /*
 
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.
 
166
                         */
 
167
                        Thread.yield(); 
 
168
                        
 
169
                        data.touchBranch(i,1);
 
170
                }
 
171
        }
 
172
        
 
173
        private void runTestWithTwoThreads() throws Throwable
 
174
        {
 
175
                final SwitchData data = new SwitchData(2);
 
176
                final AtomicReference<Throwable> possibleThrowable = new AtomicReference<Throwable>();
 
177
                
 
178
                ThreadGroup threadGroup = new ThreadGroup("TestThreadGroup") {
 
179
                        public void uncaughtException(Thread thread, Throwable t)
 
180
                        {
 
181
                                /*
 
182
                                 * Save the Throwable for later use and interrupt this thread so it exits
 
183
                                 */
 
184
                                possibleThrowable.set(t);
 
185
                                thread.interrupt();
 
186
                        }
 
187
                };
 
188
                
 
189
                /*
 
190
                 * Create two threads using the above thread group
 
191
                 */
 
192
                Thread thread1 = new Thread(threadGroup, "1") {
 
193
                        public void run()
 
194
                        {
 
195
                                touchIteratively(data, 0);
 
196
                        }
 
197
                };
 
198
                Thread thread2 = new Thread(threadGroup, "2") {
 
199
                        public void run()
 
200
                        {
 
201
                                touchIteratively(data, 1);
 
202
                        }
 
203
                };
 
204
                thread1.start();
 
205
                thread2.start();
 
206
                /*
 
207
                 * Wait for the threads to exit
 
208
                 */
 
209
                if (thread1.isAlive()) thread1.join();
 
210
                if (thread2.isAlive()) thread2.join();
 
211
                Throwable t = possibleThrowable.get();
 
212
                if (t != null)
 
213
                {
 
214
                        throw t;
 
215
                }
 
216
        }
150
217
 
 
218
        public void testMultiThreaded() throws Throwable
 
219
        {
 
220
                /*
 
221
                 * This test will often pass with only one iteration.
 
222
                 * It passes once in a while with 4.   It never passes
 
223
                 * with 10 (I hope).
 
224
                 */
 
225
                for (int i=0; i<10; i++)
 
226
                {
 
227
                        runTestWithTwoThreads();
 
228
                }
 
229
        }
151
230
}