Skip to content

Commit e90cf44

Browse files
committed
add more tests
1 parent a1f7f8a commit e90cf44

11 files changed

+223
-5
lines changed

spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/FindCompoundOperationsOnSharedVariablesTest.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ void happyPath_compoundOperation_onSharedAtomicVariable() {
1212
assertBugTypeCount(BUG_TYPE, 0);
1313
}
1414

15+
@Test
16+
void happyPath_compoundOperation_onSharedVariable_volatileReadSyncWrite() {
17+
performAnalysis("multithreaded/compoundOperationOnSharedVariables/CompoundAdditionOnSharedVolatileReadSyncWrite.class");
18+
assertBugTypeCount(BUG_TYPE, 0);
19+
}
20+
21+
@Test
22+
void happyPath_compoundOperation_readWriteLock() {
23+
performAnalysis("multithreaded/compoundOperationOnSharedVariables/CompoundNegateReadWriteLock.class");
24+
assertBugTypeCount(BUG_TYPE, 0);
25+
}
26+
1527
@Test
1628
void happyPath_compoundOperationInsideSynchronizedBlock_onSharedVariable() {
1729
performAnalysis("multithreaded/compoundOperationOnSharedVariables/SynchronizedBlockCompoundOperationOnSharedVariable.class");
@@ -51,9 +63,10 @@ void reportsBugFor_compoundPreIncrementation_onSharedVariable() {
5163
// num++
5264
@Test
5365
void reportsBugFor_compoundPostIncrementation_onSharedVariable() {
66+
// The order of the functions is reversed
5467
performAnalysis("multithreaded/compoundOperationOnSharedVariables/CompoundPostIncrementationOnSharedVariable.class");
5568
assertBugTypeCount(BUG_TYPE, 1);
56-
assertBugInMethodAtLine(BUG_TYPE, "CompoundPostIncrementationOnSharedVariable", "getNum", 11);
69+
assertBugInMethodAtLine(BUG_TYPE, "CompoundPostIncrementationOnSharedVariable", "toggle", 11);
5770
}
5871

5972
// &=
@@ -152,4 +165,60 @@ void reportsBugFor_compoundXOROperation_onSharedVariable() {
152165
assertBugTypeCount(BUG_TYPE, 1);
153166
assertBugInMethodAtLine(BUG_TYPE, "CompoundXOROperationOnSharedVariable", "getFlag", 16);
154167
}
168+
169+
// num = num + 2
170+
@Test
171+
void reportsBugFor_simpleAddition_onSharedVariable() {
172+
performAnalysis("multithreaded/compoundOperationOnSharedVariables/AdditionOnSharedVariable.class");
173+
assertBugTypeCount(BUG_TYPE, 1);
174+
assertBugInMethodAtLine(BUG_TYPE, "AdditionOnSharedVariable", "getNum", 11);
175+
}
176+
177+
// num = -num
178+
@Test
179+
void reportsBugFor_negate_onSharedVariable() {
180+
performAnalysis("multithreaded/compoundOperationOnSharedVariables/NegateSharedVariable.class");
181+
assertBugTypeCount(BUG_TYPE, 1);
182+
assertBugInMethodAtLine(BUG_TYPE, "NegateSharedVariable", "getNum", 11);
183+
}
184+
185+
// num -= num + 2
186+
@Test
187+
void reportsBugFor_compoundSubtraction_onSharedVariable_complex() {
188+
performAnalysis("multithreaded/compoundOperationOnSharedVariables/CompoundSubstractComplexExpression.class");
189+
assertBugTypeCount(BUG_TYPE, 1);
190+
assertBugInMethodAtLine(BUG_TYPE, "CompoundSubstractComplexExpression", "getNum", 11);
191+
}
192+
193+
// num += num2 + 5
194+
@Test
195+
void reportsBugFor_compoundAddition_onSharedVariable_complex_withAnotherVar() {
196+
performAnalysis("multithreaded/compoundOperationOnSharedVariables/CompoundAdditionComplexExpressionWithAnotherVar.class");
197+
assertBugTypeCount(BUG_TYPE, 1);
198+
assertBugInMethodAtLine(BUG_TYPE, "CompoundAdditionComplexExpressionWithAnotherVar", "getNum", 12);
199+
}
200+
201+
// num2 = num; num += 1
202+
@Test
203+
void reportsBugFor_compoundAddition_onSharedVariable_withAnotherVar() {
204+
performAnalysis("multithreaded/compoundOperationOnSharedVariables/CompoundAdditionWithAnotherVar.class");
205+
assertBugTypeCount(BUG_TYPE, 1);
206+
assertBugInMethodAtLine(BUG_TYPE, "CompoundAdditionWithAnotherVar", "getNum", 13);
207+
}
208+
209+
// num += param
210+
@Test
211+
void reportsBugFor_compoundSubstractionOfArg_onSharedVariable() {
212+
performAnalysis("multithreaded/compoundOperationOnSharedVariables/CompoundSubstractionOfArg.class");
213+
assertBugTypeCount(BUG_TYPE, 1);
214+
assertBugInMethodAtLine(BUG_TYPE, "CompoundSubstractionOfArg", "getNum", 11);
215+
}
216+
217+
// num += getOne()
218+
@Test
219+
void reportsBugFor_compoundSubstractionOfFunCall_onSharedVariable() {
220+
performAnalysis("multithreaded/compoundOperationOnSharedVariables/CompoundSubstractionOfMethodReturnValue.class");
221+
assertBugTypeCount(BUG_TYPE, 1);
222+
assertBugInMethodAtLine(BUG_TYPE, "CompoundSubstractionOfMethodReturnValue", "getNum", 11);
223+
}
155224
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package multithreaded.compoundOperationOnSharedVariables;
2+
3+
public class AdditionOnSharedVariable extends Thread {
4+
private int num = 0;
5+
6+
public void toggle() {
7+
num = num + 2;
8+
}
9+
10+
public Integer getNum() {
11+
return num;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package multithreaded.compoundOperationOnSharedVariables;
2+
3+
public class CompoundAdditionComplexExpressionWithAnotherVar extends Thread {
4+
private int num = 0;
5+
private int num2 = 2;
6+
7+
public void toggle() {
8+
num += num2 + 5;
9+
}
10+
11+
public Integer getNum() {
12+
return num;
13+
}
14+
15+
public Integer getNum2() {
16+
return num2;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package multithreaded.compoundOperationOnSharedVariables;
2+
3+
public class CompoundAdditionOnSharedVolatileReadSyncWrite extends Thread {
4+
private volatile int num = 0;
5+
6+
public synchronized void toggle() {
7+
num += 2;
8+
}
9+
10+
public Integer getNum() {
11+
return num;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package multithreaded.compoundOperationOnSharedVariables;
2+
3+
public class CompoundAdditionWithAnotherVar extends Thread {
4+
private int num = 0;
5+
private int num2 = 2;
6+
7+
public void toggle() {
8+
num2 = num;
9+
num += 1;
10+
}
11+
12+
public Integer getNum() {
13+
return num;
14+
}
15+
16+
public Integer getNum2() {
17+
return num2;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package multithreaded.compoundOperationOnSharedVariables;
2+
3+
import java.util.concurrent.locks.Lock;
4+
import java.util.concurrent.locks.ReadWriteLock;
5+
import java.util.concurrent.locks.ReentrantReadWriteLock;
6+
7+
public class CompoundNegateReadWriteLock extends Thread {
8+
private boolean flag = true;
9+
private final ReadWriteLock lock = new ReentrantReadWriteLock();
10+
private final Lock readLock = lock.readLock();
11+
private final Lock writeLock = lock.writeLock();
12+
13+
public void toggle() {
14+
writeLock.lock();
15+
try {
16+
flag ^= true; // Same as flag = !flag;
17+
} finally {
18+
writeLock.unlock();
19+
}
20+
}
21+
22+
public boolean getFlag() {
23+
readLock.lock();
24+
try {
25+
return flag;
26+
} finally {
27+
readLock.unlock();
28+
}
29+
}
30+
}

spotbugsTestCases/src/java/multithreaded/compoundOperationOnSharedVariables/CompoundPostIncrementationOnSharedVariable.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
public class CompoundPostIncrementationOnSharedVariable extends Thread {
44
private double num = 3.0;
55

6-
public void toggle() {
7-
num++;
8-
}
9-
106
public double getNum() {
117
return num;
128
}
9+
10+
public void toggle() {
11+
num++;
12+
}
1313
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package multithreaded.compoundOperationOnSharedVariables;
2+
3+
public class CompoundSubstractComplexExpression extends Thread {
4+
private int num = 0;
5+
6+
public void toggle() {
7+
num -= num + 2;
8+
}
9+
10+
public Integer getNum() {
11+
return num;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package multithreaded.compoundOperationOnSharedVariables;
2+
3+
public class CompoundSubstractionOfArg extends Thread {
4+
private int num = 0;
5+
6+
public void toggle(int param) {
7+
num += param;
8+
}
9+
10+
public Integer getNum() {
11+
return num;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package multithreaded.compoundOperationOnSharedVariables;
2+
3+
public class CompoundSubstractionOfMethodReturnValue extends Thread {
4+
private int num = 0;
5+
6+
public void toggle() {
7+
num += getOne();
8+
}
9+
10+
public Integer getNum() {
11+
return num;
12+
}
13+
14+
private int getOne() {
15+
return 1;
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package multithreaded.compoundOperationOnSharedVariables;
2+
3+
public class NegateSharedVariable extends Thread {
4+
private int num = 3;
5+
6+
public void toggle() {
7+
num = -num;
8+
}
9+
10+
public Integer getNum() {
11+
return num;
12+
}
13+
}

0 commit comments

Comments
 (0)