Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs0a-24su
war
Commits
11aab9b1
Commit
11aab9b1
authored
1 year ago
by
Vansh V. Tibrewal
Browse files
Options
Download
Email Patches
Plain Diff
Extensive tests for faroShuffle added
parent
eaca888d
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/edu/caltech/cs0/war/DeckTests.java
+81
-22
tests/edu/caltech/cs0/war/DeckTests.java
with
81 additions
and
22 deletions
+81
-22
tests/edu/caltech/cs0/war/DeckTests.java
View file @
11aab9b1
...
...
@@ -9,10 +9,7 @@ import org.junit.jupiter.params.provider.CsvSource;
import
java.lang.reflect.Field
;
import
java.lang.reflect.InvocationTargetException
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Random
;
import
java.util.*
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
...
...
@@ -93,6 +90,10 @@ public class DeckTests {
return
inner
;
}
private
static
List
<
Card
>
cloneList
(
List
<
Card
>
list
)
{
return
(
ArrayList
<
Card
>)
((
ArrayList
<
Card
>)
list
).
clone
();
}
@Test
@DisplayName
(
"hasNoCards returns true on an empty deck"
)
public
void
testHasNoCardsEmpty
()
{
...
...
@@ -174,7 +175,7 @@ public class DeckTests {
List
<
Card
>
ref
=
new
ArrayList
<>();
for
(
int
i
=
1
;
i
<
10
;
i
++)
{
final
int
drawCount
=
i
;
// should throw an exception for numCards drawn (deck has numCards - 1 cards)
final
List
<
Card
>
inp
=
(
ArrayList
<
Card
>)
((
ArrayList
<
Card
>)
ref
).
clone
(
);
final
List
<
Card
>
inp
=
cloneList
(
ref
);
assertThrowsExactly
(
EmptyDeckException
.
class
,
()
->
newDeck
(
inp
).
draw
(
drawCount
));
ref
.
add
(
new
Card
(
null
,
null
));
}
...
...
@@ -213,25 +214,83 @@ public class DeckTests {
assertEquals
(
List
.
of
(),
getList
(
deck
));
}
//TODO: write more tests for faroShuffle?
//public void faroShuffle() {
@Test
@DisplayName
(
"faroShuffle of decks with 0-3 cards work as expected (no effect on 0-2, swap last 2 cards for 3)"
)
public
void
testFaroShuffleSmall
()
{
List
<
Card
>
ref
=
new
ArrayList
<>();
Deck
deck
=
newDeck
(
cloneList
(
ref
));
for
(
int
i
=
0
;
i
<
2
;
i
++)
{
deck
.
faroShuffle
();
assertEquals
(
ref
,
getList
(
deck
));
}
ref
.
add
(
new
Card
(
Rank
.
ACE
,
null
));
deck
=
newDeck
(
cloneList
(
ref
));
for
(
int
i
=
0
;
i
<
2
;
i
++)
{
deck
.
faroShuffle
();
assertEquals
(
ref
,
getList
(
deck
));
}
ref
.
add
(
new
Card
(
Rank
.
TWO
,
null
));
deck
=
newDeck
(
cloneList
(
ref
));
for
(
int
i
=
0
;
i
<
5
;
i
++)
{
deck
.
faroShuffle
();
assertEquals
(
ref
,
getList
(
deck
));
}
ref
.
add
(
new
Card
(
Rank
.
THREE
,
null
));
deck
=
newDeck
(
cloneList
(
ref
));
List
<
Card
>
swapped
=
List
.
of
(
new
Card
(
Rank
.
ACE
,
null
),
new
Card
(
Rank
.
THREE
,
null
),
new
Card
(
Rank
.
TWO
,
null
));
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
deck
.
faroShuffle
();
if
(
i
%
2
==
0
)
assertEquals
(
swapped
,
getList
(
deck
));
else
assertEquals
(
ref
,
getList
(
deck
));
}
}
public
class
CardIterator
implements
Iterator
<
Card
>
{
private
Rank
[]
ranks
=
Rank
.
values
();
private
Suit
[]
suits
=
Suit
.
values
();
private
int
rankIdx
=
0
;
private
int
suitIdx
=
0
;
@Override
public
boolean
hasNext
()
{
return
rankIdx
<
ranks
.
length
&&
suitIdx
<
suits
.
length
;
}
@Override
public
Card
next
()
{
Card
card
=
new
Card
(
ranks
[
rankIdx
],
suits
[
suitIdx
]);
suitIdx
++;
if
(
suitIdx
>=
suits
.
length
)
{
suitIdx
=
0
;
rankIdx
++;
}
return
card
;
}
}
@Test
public
void
testPerfectFaroShuffle
()
{
Deck
d
=
new
Deck
();
Field
dl
=
Reflection
.
getFieldByType
(
Deck
.
class
,
List
.
class
);
assert
dl
!=
null
;
List
<
Card
>
curr
=
Reflection
.
getFieldValue
(
Deck
.
class
,
dl
.
getName
(),
d
);
assert
curr
!=
null
;
List
<
Card
>
initCopy
=
curr
.
stream
().
toList
();
for
(
int
i
=
0
;
i
<
7
;
i
++)
{
d
.
faroShuffle
();
curr
=
Reflection
.
getFieldValue
(
Deck
.
class
,
dl
.
getName
(),
d
);
assertNotEquals
(
initCopy
,
curr
);
@DisplayName
(
"faroShuffle of decks with 1-52 cards works as expected via perfect shuffle properties"
)
public
void
testFaroShuffleBig
()
{
Iterator
<
Card
>
cardGen
=
new
CardIterator
();
List
<
Card
>
ref
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
52
;
i
++)
{
ref
.
add
(
cardGen
.
next
());
Deck
deck
=
newDeck
(
cloneList
(
ref
));
int
size
=
ref
.
size
();
int
modBy
=
size
;
if
(
size
%
2
==
0
)
modBy
-=
1
;
long
pow
=
1
;
boolean
firstMatch
=
1
%
modBy
!=
1
;
for
(
int
shuffles
=
1
;
shuffles
<
63
;
shuffles
++)
{
pow
*=
2
;
if
(
pow
%
modBy
==
1
)
{
for
(
int
j
=
0
;
j
<
shuffles
;
j
++)
{
deck
.
faroShuffle
();
if
(
firstMatch
)
assertNotEquals
(
ref
,
getList
(
deck
));
}
firstMatch
=
false
;
assertEquals
(
ref
,
getList
(
deck
));
}
}
}
d
.
faroShuffle
();
curr
=
Reflection
.
getFieldValue
(
Deck
.
class
,
dl
.
getName
(),
d
);
assertEquals
(
initCopy
,
curr
);
}
}
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help