Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
awa-w6d4-junit-testproject
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Eric Arndt
awa-w6d4-junit-testproject
Commits
7c37994a
Commit
7c37994a
authored
Apr 09, 2021
by
earndt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[W6D4] (ArndtED) Adds test cases for WhoIsTheBest
parent
87363416
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
81 deletions
+51
-81
ImprovedRandomSpliterator.java
...ct/src/main/java/com/nisum/ImprovedRandomSpliterator.java
+0
-61
WhoIsTheBest.java
testproject/src/main/java/com/nisum/WhoIsTheBest.java
+6
-20
WhoIsTheBestTest.java
testproject/src/test/java/com/nisum/WhoIsTheBestTest.java
+45
-0
No files found.
testproject/src/main/java/com/nisum/ImprovedRandomSpliterator.java
deleted
100644 → 0
View file @
87363416
package
com
.
nisum
;
//all credit to https://stackoverflow.com/users/2229438/grzegorz-piwowarek
//https://stackoverflow.com/questions/45981964/how-to-get-a-random-element-from-a-list-with-stream-api/45982130
import
java.util.List
;
import
java.util.Random
;
import
java.util.Spliterator
;
import
java.util.function.Consumer
;
import
java.util.function.Supplier
;
import
java.util.stream.Stream
;
import
java.util.stream.StreamSupport
;
public
class
ImprovedRandomSpliterator
<
T
>
implements
Spliterator
<
T
>
{
private
final
Random
random
;
private
final
T
[]
source
;
private
int
size
;
ImprovedRandomSpliterator
(
List
<
T
>
source
,
Supplier
<?
extends
Random
>
random
)
{
if
(
source
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"RandomSpliterator can't be initialized with an empty collection"
);
}
this
.
source
=
(
T
[])
source
.
toArray
();
this
.
random
=
random
.
get
();
this
.
size
=
this
.
source
.
length
;
}
@Override
public
boolean
tryAdvance
(
Consumer
<?
super
T
>
action
)
{
if
(
size
>
0
)
{
int
nextIdx
=
random
.
nextInt
(
size
);
int
lastIdx
=
size
-
1
;
action
.
accept
(
source
[
nextIdx
]);
source
[
nextIdx
]
=
source
[
lastIdx
];
source
[
lastIdx
]
=
null
;
// let object be GCed
size
--;
return
true
;
}
else
{
return
false
;
}
}
@Override
public
Spliterator
<
T
>
trySplit
()
{
return
null
;
}
@Override
public
long
estimateSize
()
{
return
source
.
length
;
}
@Override
public
int
characteristics
()
{
return
SIZED
;
}
}
testproject/src/main/java/com/nisum/WhoIsTheBest.java
View file @
7c37994a
package
com
.
nisum
;
import
java.util.*
;
import
java.util.stream.Collector
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
import
java.util.stream.StreamSupport
;
import
static
java
.
util
.
stream
.
Collectors
.
toCollection
;
public
class
WhoIsTheBest
{
...
...
@@ -30,21 +22,15 @@ public class WhoIsTheBest {
return
theNotSoGreats
;
}
public
Optional
<
String
>
gimmeAGreat
(){
return
theGreats
.
stream
().
collect
(
toShuffledStream
()).
findAny
();
}
public
String
gimmeAGreat
(){
Random
random
=
new
Random
();
return
theGreats
.
get
(
random
.
nextInt
(
theGreats
.
size
()));
public
Optional
<
String
>
gimmeANotSoGreat
(){
return
theNotSoGreats
.
stream
().
findAny
();
}
public
static
<
T
>
Collector
<
T
,
?,
Stream
<
T
>>
toShuffledStream
()
{
return
Collectors
.
collectingAndThen
(
toCollection
(
ArrayList:
:
new
),
list
->
!
list
.
isEmpty
()
?
StreamSupport
.
stream
(
new
ImprovedRandomSpliterator
<>(
list
,
Random:
:
new
),
false
)
:
Stream
.
empty
());
};
public
String
gimmeANotSoGreat
(){
return
theNotSoGreats
.
get
(
0
);
}
};
testproject/src/test/java/com/nisum/WhoIsTheBestTest.java
0 → 100644
View file @
7c37994a
package
com
.
nisum
;
import
org.junit.Assert
;
import
org.junit.jupiter.api.Test
;
import
java.util.Arrays
;
import
java.util.List
;
class
WhoIsTheBestTest
{
WhoIsTheBest
theBestAround
=
new
WhoIsTheBest
();
String
realNotSoGreat
=
"Eric"
;
@Test
void
getTheGreats
()
{
List
<
String
>
theRealGreats
=
Arrays
.
asList
(
"Alex P"
,
"Alex S"
,
"Ben"
,
"Chef"
,
"Chris"
,
"Darrick"
,
"Deep"
,
"Joe"
,
"John"
,
"Julius"
,
"Kevin"
,
"Khai"
,
"Kyle"
,
"Nikitha"
,
"Philippe"
,
"Shanelle"
,
"Sumaiyya"
,
"Vishal"
);
List
<
String
>
testGreats
=
theBestAround
.
getTheGreats
();
Assert
.
assertTrue
(
"These are not the greats - they should be."
,
theRealGreats
.
equals
(
testGreats
));
}
@Test
void
getTheNotSoGreats
()
{
List
<
String
>
theRealNotSoGreats
=
Arrays
.
asList
(
"Eric"
);
List
<
String
>
testGreats
=
theBestAround
.
getTheGreats
();
Assert
.
assertFalse
(
"These are the greats - they shouldn't be."
,
theRealNotSoGreats
.
equals
(
testGreats
));
}
@Test
void
gimmeAGreat
()
{
String
testGreat
=
theBestAround
.
gimmeAGreat
();
Assert
.
assertFalse
(
"This one ain't great - they should be."
,
realNotSoGreat
.
equals
(
testGreat
));
}
@Test
void
gimmeANotSoGreat
()
{
String
testNotSoGreat
=
theBestAround
.
gimmeANotSoGreat
();
Assert
.
assertTrue
(
"This one is great - they shouldn't be."
,
realNotSoGreat
.
equals
(
testNotSoGreat
));
}
}
\ No newline at end of file
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