Skip to content

Commit b140cc5

Browse files
committed
refactor tests
1 parent 75e9556 commit b140cc5

File tree

3 files changed

+252
-190
lines changed

3 files changed

+252
-190
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* JavaPermutationTools: A Java library for computation on permutations and sequences
3+
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
4+
*
5+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
6+
*
7+
* JavaPermutationTools is free software: you can
8+
* redistribute it and/or modify it under the terms of the GNU
9+
* General Public License as published by the Free Software
10+
* Foundation, either version 3 of the License, or (at your
11+
* option) any later version.
12+
*
13+
* JavaPermutationTools is distributed in the hope
14+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
15+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
16+
* PARTICULAR PURPOSE. See the GNU General Public License for more
17+
* details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
package org.cicirello.permutations;
23+
24+
import org.junit.jupiter.api.*;
25+
import static org.junit.jupiter.api.Assertions.*;
26+
import java.util.NoSuchElementException;
27+
28+
/**
29+
* JUnit tests for the PermutationIterator.
30+
*/
31+
public class PermutationIteratorTests {
32+
33+
@Test
34+
public void testPermutationIterator() {
35+
int fact = 1;
36+
for (int n = 1; n <= 5; n++) {
37+
Permutation p = new Permutation(n);
38+
boolean checkedFirst = false;
39+
fact *= n;
40+
boolean[] found = new boolean[fact];
41+
int count = 0;
42+
for (Permutation pPrime : p) {
43+
if (!checkedFirst) {
44+
assertEquals(p, pPrime);
45+
checkedFirst = true;
46+
}
47+
int permID = pPrime.toInteger();
48+
assertFalse(found[permID]);
49+
found[permID] = true;
50+
count++;
51+
}
52+
assertEquals(fact, count);
53+
}
54+
final PermutationIterator iter = new PermutationIterator(4);
55+
fact = 24;
56+
boolean[] found = new boolean[fact];
57+
int count = 0;
58+
while (iter.hasNext()) {
59+
Permutation p = iter.next();
60+
int permID = p.toInteger();
61+
assertFalse(found[permID]);
62+
found[permID] = true;
63+
count++;
64+
}
65+
assertEquals(fact, count);
66+
NoSuchElementException thrown = assertThrows(
67+
NoSuchElementException.class,
68+
() -> iter.next()
69+
);
70+
}
71+
}
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* JavaPermutationTools: A Java library for computation on permutations and sequences
3+
* Copyright 2005-2022 Vincent A. Cicirello, <https://www.cicirello.org/>.
4+
*
5+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
6+
*
7+
* JavaPermutationTools is free software: you can
8+
* redistribute it and/or modify it under the terms of the GNU
9+
* General Public License as published by the Free Software
10+
* Foundation, either version 3 of the License, or (at your
11+
* option) any later version.
12+
*
13+
* JavaPermutationTools is distributed in the hope
14+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
15+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
16+
* PARTICULAR PURPOSE. See the GNU General Public License for more
17+
* details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
package org.cicirello.permutations;
23+
24+
import org.junit.jupiter.api.*;
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
/**
28+
* JUnit tests for the applyThenValidate and apply methods of the Permutation class.
29+
*/
30+
public class PermutationOperatorsTests {
31+
32+
@Test
33+
public void testIllegalPermutationStateExceptionConstructors() {
34+
IllegalPermutationStateException e1 = new IllegalPermutationStateException("test message");
35+
assertEquals("test message", e1.getMessage());
36+
assertNull(e1.getCause());
37+
e1 = new IllegalPermutationStateException("test message", new IllegalArgumentException());
38+
assertEquals("test message", e1.getMessage());
39+
assertTrue(e1.getCause() instanceof IllegalArgumentException);
40+
e1 = new IllegalPermutationStateException("test message", new IllegalPermutationStateException("testing 1 2 3"));
41+
assertEquals("test message", e1.getMessage());
42+
assertTrue(e1.getCause() instanceof IllegalPermutationStateException);
43+
}
44+
45+
@Test
46+
public void testUnaryOperator() {
47+
Permutation p = new Permutation(10);
48+
p.apply(perm -> { for (int i = 0; i < perm.length; i++) { perm[i] = i; }});
49+
for (int i = 0; i < 10; i++) {
50+
assertEquals(i, p.get(i));
51+
}
52+
p.apply(perm -> { for (int i = 0; i < perm.length; i++) { perm[perm.length-1-i] = i; }});
53+
for (int i = 0; i < 10; i++) {
54+
assertEquals(9-i, p.get(i));
55+
}
56+
}
57+
58+
@Test
59+
public void testValidatedUnaryOperator() {
60+
final Permutation p = new Permutation(10);
61+
p.applyThenValidate(perm -> { for (int i = 0; i < perm.length; i++) { perm[i] = i; }});
62+
for (int i = 0; i < 10; i++) {
63+
assertEquals(i, p.get(i));
64+
}
65+
p.applyThenValidate(perm -> { for (int i = 0; i < perm.length; i++) { perm[perm.length-1-i] = i; }});
66+
for (int i = 0; i < 10; i++) {
67+
assertEquals(9-i, p.get(i));
68+
}
69+
IllegalPermutationStateException thrown = assertThrows(
70+
IllegalPermutationStateException.class,
71+
() -> p.applyThenValidate(perm -> { for (int i = 0; i < perm.length; i++) { perm[i] = 0; }})
72+
);
73+
assertTrue(thrown.getCause() instanceof IllegalArgumentException);
74+
}
75+
76+
@Test
77+
public void testBinaryOperator() {
78+
Permutation p1 = new Permutation(10);
79+
Permutation p2 = new Permutation(10);
80+
p1.apply((perm1, perm2) -> { for (int i = 0; i < perm1.length; i++) { perm1[i] = i; perm2[perm1.length-1-i] = i; }}, p2);
81+
for (int i = 0; i < 10; i++) {
82+
assertEquals(i, p1.get(i));
83+
assertEquals(9-i, p2.get(i));
84+
}
85+
p1.apply((perm1, perm2) -> { for (int i = 0; i < perm1.length; i++) { perm2[i] = i; perm1[perm1.length-1-i] = i; }}, p2);
86+
for (int i = 0; i < 10; i++) {
87+
assertEquals(i, p2.get(i));
88+
assertEquals(9-i, p1.get(i));
89+
}
90+
}
91+
92+
@Test
93+
public void testValidatedBinaryOperator() {
94+
final Permutation p1 = new Permutation(10);
95+
final Permutation p2 = new Permutation(10);
96+
p1.applyThenValidate((perm1, perm2) -> { for (int i = 0; i < perm1.length; i++) { perm1[i] = i; perm2[perm1.length-1-i] = i; }}, p2);
97+
for (int i = 0; i < 10; i++) {
98+
assertEquals(i, p1.get(i));
99+
assertEquals(9-i, p2.get(i));
100+
}
101+
p1.applyThenValidate((perm1, perm2) -> { for (int i = 0; i < perm1.length; i++) { perm2[i] = i; perm1[perm1.length-1-i] = i; }}, p2);
102+
for (int i = 0; i < 10; i++) {
103+
assertEquals(i, p2.get(i));
104+
assertEquals(9-i, p1.get(i));
105+
}
106+
IllegalPermutationStateException thrown = assertThrows(
107+
IllegalPermutationStateException.class,
108+
() -> p1.applyThenValidate((perm1, perm2) -> { for (int i = 0; i < perm1.length; i++) { perm1[i] = 0; perm2[perm1.length-1-i] = 0; }}, p2)
109+
);
110+
assertTrue(thrown.getCause() instanceof IllegalArgumentException);
111+
}
112+
113+
@Test
114+
public void testFullUnaryOperator() {
115+
Permutation p = new Permutation(10);
116+
p.apply((perm, original) -> { for (int i = 0; i < perm.length; i++) { perm[i] = i; assertEquals(perm[i], original.get(i)); }});
117+
for (int i = 0; i < 10; i++) {
118+
assertEquals(i, p.get(i));
119+
}
120+
p.apply((perm, original) -> { for (int i = 0; i < perm.length; i++) { perm[perm.length-1-i] = i; assertEquals(perm[perm.length-1-i], original.get(perm.length-1-i)); }});
121+
for (int i = 0; i < 10; i++) {
122+
assertEquals(9-i, p.get(i));
123+
}
124+
}
125+
126+
@Test
127+
public void testValidatedFullUnaryOperator() {
128+
final Permutation p = new Permutation(10);
129+
p.applyThenValidate((perm, original) -> { for (int i = 0; i < perm.length; i++) { perm[i] = i; assertEquals(perm[i], original.get(i)); }});
130+
for (int i = 0; i < 10; i++) {
131+
assertEquals(i, p.get(i));
132+
}
133+
p.applyThenValidate((perm, original) -> { for (int i = 0; i < perm.length; i++) { perm[perm.length-1-i] = i; assertEquals(perm[perm.length-1-i], original.get(perm.length-1-i)); }});
134+
for (int i = 0; i < 10; i++) {
135+
assertEquals(9-i, p.get(i));
136+
}
137+
IllegalPermutationStateException thrown = assertThrows(
138+
IllegalPermutationStateException.class,
139+
() -> p.applyThenValidate((perm, original) -> { for (int i = 0; i < perm.length; i++) { perm[i] = 0; }})
140+
);
141+
assertTrue(thrown.getCause() instanceof IllegalArgumentException);
142+
}
143+
144+
@Test
145+
public void testFullBinaryOperator() {
146+
Permutation p1 = new Permutation(10);
147+
Permutation p2 = new Permutation(10);
148+
p1.apply((perm1, perm2, o1, o2) -> { for (int i = 0; i < perm1.length; i++) { perm1[i] = i; perm2[perm1.length-1-i] = i; assertEquals(i, o1.get(i)); assertEquals(i, o2.get(9-i)); }}, p2);
149+
for (int i = 0; i < 10; i++) {
150+
assertEquals(i, p1.get(i));
151+
assertEquals(9-i, p2.get(i));
152+
}
153+
p1.apply((perm1, perm2, o1, o2) -> { for (int i = 0; i < perm1.length; i++) { perm2[i] = i; perm1[perm1.length-1-i] = i; assertEquals(i, o2.get(i)); assertEquals(i, o1.get(9-i)); }}, p2);
154+
for (int i = 0; i < 10; i++) {
155+
assertEquals(i, p2.get(i));
156+
assertEquals(9-i, p1.get(i));
157+
}
158+
}
159+
160+
@Test
161+
public void testValidatedFullBinaryOperator() {
162+
final Permutation p1 = new Permutation(10);
163+
final Permutation p2 = new Permutation(10);
164+
p1.applyThenValidate((perm1, perm2, o1, o2) -> { for (int i = 0; i < perm1.length; i++) { perm1[i] = i; perm2[perm1.length-1-i] = i; assertEquals(i, o1.get(i)); assertEquals(i, o2.get(9-i)); }}, p2);
165+
for (int i = 0; i < 10; i++) {
166+
assertEquals(i, p1.get(i));
167+
assertEquals(9-i, p2.get(i));
168+
}
169+
p1.applyThenValidate((perm1, perm2, o1, o2) -> { for (int i = 0; i < perm1.length; i++) { perm2[i] = i; perm1[perm1.length-1-i] = i; assertEquals(i, o2.get(i)); assertEquals(i, o1.get(9-i)); }}, p2);
170+
for (int i = 0; i < 10; i++) {
171+
assertEquals(i, p2.get(i));
172+
assertEquals(9-i, p1.get(i));
173+
}
174+
IllegalPermutationStateException thrown = assertThrows(
175+
IllegalPermutationStateException.class,
176+
() -> p1.applyThenValidate((perm1, perm2, o1, o2) -> { for (int i = 0; i < perm1.length; i++) { perm1[i] = 0; perm2[perm1.length-1-i] = 0; }}, p2)
177+
);
178+
assertTrue(thrown.getCause() instanceof IllegalArgumentException);
179+
}
180+
}

0 commit comments

Comments
 (0)