Skip to content

Commit 5b0b005

Browse files
committed
use adjacentPairsFrom()
1 parent 6443b7b commit 5b0b005

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

mug-concurrent-testing/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,15 @@
7575
<artifactId>truth</artifactId>
7676
<scope>test</scope>
7777
</dependency>
78-
7978
<dependency>
8079
<groupId>com.google.truth.extensions</groupId>
8180
<artifactId>truth-java8-extension</artifactId>
8281
<scope>test</scope>
8382
</dependency>
83+
<dependency>
84+
<groupId>com.google.guava</groupId>
85+
<artifactId>guava-testlib</artifactId>
86+
<scope>test</scope>
87+
</dependency>
8488
</dependencies>
8589
</project>

mug-concurrent-testing/src/main/java/com/google/mu/testing/concurrent/Happenstance.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static <K> Builder<K> builder(K... sequencePoints) {
9898
public static <K> Builder<K> builder(Iterable<? extends K> sequencePoints) {
9999
Builder<K> builder = new Builder<>();
100100
for (K point : sequencePoints) {
101-
builder.happenInOrder(point);
101+
builder.declareSequencePoint(point);
102102
}
103103
return builder;
104104
}
@@ -128,25 +128,24 @@ public static final class Builder<K> {
128128
* graph.
129129
*/
130130
@CanIgnoreReturnValue
131-
public Builder<K> happenInOrder(K... sequencePoints) {
131+
@SafeVarargs
132+
public final Builder<K> happenInOrder(K... sequencePoints) {
132133
for (K point : sequencePoints) {
133134
declareSequencePoint(point);
134135
}
135-
for (int i = 0; i < sequencePoints.length - 1; i++) {
136-
int u = pointToIndex.get(sequencePoints[i]);
137-
int v = pointToIndex.get(sequencePoints[i + 1]);
138-
if (u == v) {
139-
continue;
140-
}
141-
if (successors.get(u).add(v)) {
142-
checkArgument(
143-
!isReachable(v, u),
144-
"Adding edge %s -> %s creates a cycle",
145-
sequencePoints[i + 1],
146-
sequencePoints[i]);
147-
predecessors.get(v).add(u);
148-
}
149-
}
136+
BiStream.adjacentPairsFrom(sequencePoints)
137+
.forEach((predecessor, successor) -> {
138+
int u = pointToIndex.get(predecessor);
139+
int v = pointToIndex.get(successor);
140+
if (u != v && successors.get(u).add(v)) {
141+
checkArgument(
142+
!isReachable(v, u),
143+
"Adding edge %s -> %s creates a cycle",
144+
predecessor,
145+
successor);
146+
predecessors.get(v).add(u);
147+
}
148+
});
150149
return this;
151150
}
152151

mug-concurrent-testing/src/test/java/com/google/mu/testing/concurrent/HappenstanceTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.junit.runner.RunWith;
2222
import org.junit.runners.JUnit4;
2323

24+
import com.google.common.testing.NullPointerTester;
25+
2426
@RunWith(JUnit4.class)
2527
public class HappenstanceTest {
2628

@@ -663,6 +665,11 @@ public void checkpoint_noUnintendedMemoryBarrier() throws Exception {
663665
}
664666
assertThat(races).isNotEmpty();
665667
}
668+
669+
@Test public void testNulls() {
670+
new NullPointerTester().testAllPublicStaticMethods(Happenstance.class);
671+
new NullPointerTester().testAllPublicInstanceMethods(Happenstance.builder());
672+
}
666673

667674
private static class BuggySut {
668675
boolean ready = false;

0 commit comments

Comments
 (0)