Skip to content

Commit 12bc47f

Browse files
committed
Fix for each is valid coco for super types of iterable
1 parent 0742e45 commit 12bc47f

14 files changed

Lines changed: 239 additions & 45 deletions

File tree

generators/ma2jsim-it/main/cd2pojo/montiarc/types.cd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ classdiagram types {
4848
public static void call(Object o, Object p);
4949
}
5050

51+
public class ItInt implements java.lang.Iterable<Integer> { }
52+
5153
// ======= Associations ======== //
5254

5355
public interface InterfaceLeft { }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* (c) https://github.com/MontiCore/monticore */
2+
package montiarc.types;
3+
4+
import org.codehaus.commons.nullanalysis.NotNull;
5+
6+
import java.util.Arrays;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
10+
public class ItInt extends ItIntTOP {
11+
12+
public ItInt(Integer... i) {
13+
list = Arrays.asList(i);
14+
}
15+
16+
private final List<Integer> list;
17+
18+
@Override @NotNull
19+
public Iterator<Integer> iterator() {
20+
return list.iterator();
21+
}
22+
}

generators/ma2jsim-it/main/montiarc/montiarc/statements/ForEachWithList.arc renamed to generators/ma2jsim-it/main/montiarc/montiarc/statements/ForEachWithList1.arc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
package montiarc.statements;
33

44
import java.util.List;
5-
import montiarc.types.OnOff;
65

7-
component ForEachWithList {
6+
component ForEachWithList1 {
87

9-
port in List<OnOff> i;
10-
port out OnOff o;
8+
port in List<int> i;
9+
port out int o;
1110

1211
automaton {
1312
initial state S;
1413
S -> S i / {
15-
for (OnOff e : i) {
14+
for (int e : i) {
1615
o = e;
1716
}
1817
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* (c) https://github.com/MontiCore/monticore */
2+
package montiarc.statements;
3+
4+
import montiarc.types.Signal;
5+
6+
component ForEachWithList2 {
7+
8+
port in Signal i;
9+
port out int o;
10+
11+
automaton {
12+
initial state S;
13+
S -> S i / {
14+
for (int e : [0, 1]) {
15+
o = e;
16+
}
17+
}
18+
}
19+
}

generators/ma2jsim-it/main/montiarc/montiarc/statements/ForEachWithSet.arc renamed to generators/ma2jsim-it/main/montiarc/montiarc/statements/ForEachWithSet1.arc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
package montiarc.statements;
33

44
import java.util.Set;
5-
import montiarc.types.OnOff;
65

7-
component ForEachWithSet {
6+
component ForEachWithSet1 {
87

9-
port in Set<OnOff> i;
10-
port out OnOff o;
8+
port in Set<int> i;
9+
port out int o;
1110

1211
automaton {
1312
initial state S;
1413
S -> S i / {
15-
for (OnOff e : i) {
14+
for (int e : i) {
1615
o = e;
1716
}
1817
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* (c) https://github.com/MontiCore/monticore */
2+
package montiarc.statements;
3+
4+
import montiarc.types.Signal;
5+
6+
component ForEachWithSet2 {
7+
8+
port in Signal i;
9+
port out int o;
10+
11+
automaton {
12+
initial state S;
13+
S -> S i / {
14+
for (int e : {0, 1}) {
15+
o = e;
16+
}
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* (c) https://github.com/MontiCore/monticore */
2+
package montiarc.statements;
3+
4+
import montiarc.types.ItInt;
5+
6+
component ForEachWithSubTypeOfIterable {
7+
8+
port in ItInt i;
9+
port out int o;
10+
11+
automaton {
12+
initial state S;
13+
S -> S i / {
14+
for (int e : i) {
15+
o = e;
16+
}
17+
}
18+
}
19+
}

generators/ma2jsim-it/test/java/montiarc/statements/ForEachWithListTest.java renamed to generators/ma2jsim-it/test/java/montiarc/statements/ForEachWithList1Test.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import montiarc.rte.port.PortObserver;
55
import montiarc.rte.tests.JSimTest;
6-
import montiarc.types.OnOff;
76
import org.junit.jupiter.api.Test;
87

98
import java.util.List;
@@ -12,23 +11,23 @@
1211
import static org.assertj.core.api.Assertions.assertThat;
1312

1413
@JSimTest
15-
class ForEachWithListTest {
14+
class ForEachWithList1Test {
1615

1716
@Test
1817
void testIO() {
1918
// Given
20-
ForEachWithListComp sut = new ForEachWithListCompBuilder().setName("sut").build();
19+
ForEachWithList1Comp sut = new ForEachWithList1CompBuilder().setName("sut").build();
2120

22-
PortObserver<OnOff> port_o = new PortObserver<>();
21+
PortObserver<Integer> port_o = new PortObserver<>();
2322

2423
sut.port_o().connect(port_o);
2524

2625
// When
27-
sut.port_i().receive(msg(List.of(OnOff.ON, OnOff.OFF)));
26+
sut.port_i().receive(msg(List.of(0, 1)));
2827

2928
sut.runToCompletion();
3029

3130
// Then
32-
assertThat(port_o.getObservedMessages()).containsExactly(msg(OnOff.ON), msg(OnOff.OFF));
31+
assertThat(port_o.getObservedMessages()).containsExactly(msg(0), msg(1));
3332
}
3433
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* (c) https://github.com/MontiCore/monticore */
2+
package montiarc.statements;
3+
4+
import montiarc.rte.port.PortObserver;
5+
import montiarc.rte.tests.JSimTest;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static montiarc.rte.msg.MessageFactory.msg;
9+
import static montiarc.types.Signal.SIGNAL;
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
@JSimTest
13+
class ForEachWithList2Test {
14+
15+
@Test
16+
void testIO() {
17+
// Given
18+
ForEachWithList2Comp sut = new ForEachWithList2CompBuilder().setName("sut").build();
19+
20+
PortObserver<Integer> port_o = new PortObserver<>();
21+
22+
sut.port_o().connect(port_o);
23+
24+
// When
25+
sut.port_i().receive(msg(SIGNAL));
26+
27+
sut.runToCompletion();
28+
29+
// Then
30+
assertThat(port_o.getObservedMessages()).containsExactly(msg(0), msg(1));
31+
}
32+
}

generators/ma2jsim-it/test/java/montiarc/statements/ForEachWithSetTest.java renamed to generators/ma2jsim-it/test/java/montiarc/statements/ForEachWithSet1Test.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import montiarc.rte.port.PortObserver;
55
import montiarc.rte.tests.JSimTest;
6-
import montiarc.types.OnOff;
76
import org.junit.jupiter.api.Test;
87

98
import java.util.LinkedHashSet;
@@ -13,27 +12,27 @@
1312
import static org.assertj.core.api.Assertions.assertThat;
1413

1514
@JSimTest
16-
class ForEachWithSetTest {
15+
class ForEachWithSet1Test {
1716

1817
@Test
1918
void testIO() {
2019
// Given
21-
ForEachWithSetComp sut = new ForEachWithSetCompBuilder().setName("sut").build();
20+
ForEachWithSet1Comp sut = new ForEachWithSet1CompBuilder().setName("sut").build();
2221

23-
PortObserver<OnOff> port_o = new PortObserver<>();
22+
PortObserver<Integer> port_o = new PortObserver<>();
2423

2524
sut.port_o().connect(port_o);
2625

27-
Set<OnOff> input = new LinkedHashSet<>(2);
28-
input.add(OnOff.ON);
29-
input.add(OnOff.OFF);
26+
Set<Integer> input = new LinkedHashSet<>(2);
27+
input.add(0);
28+
input.add(1);
3029

3130
// When
3231
sut.port_i().receive(msg(input));
3332

3433
sut.runToCompletion();
3534

3635
// Then
37-
assertThat(port_o.getObservedMessages()).containsExactly(msg(OnOff.ON), msg(OnOff.OFF));
36+
assertThat(port_o.getObservedMessages()).containsExactly(msg(0), msg(1));
3837
}
3938
}

0 commit comments

Comments
 (0)