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
cs2-20wi
project03
Commits
8b332aad
Commit
8b332aad
authored
4 years ago
by
Adam H. Abbas
Browse files
Options
Download
Plain Diff
Merge branch 'small-revisions' into 'master'
Small revisions Closes #4 See merge request
!2
parents
39cdbef5
57128a4e
master
1 merge request
!2
Small revisions
Pipeline
#43524
failed with stage
in 0 seconds
Changes
9
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java
+44
-11
tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java
tests/edu/caltech/cs2/datastructures/CircularArrayFixedSizeQueueTests.java
+2
-1
.../cs2/datastructures/CircularArrayFixedSizeQueueTests.java
tests/edu/caltech/cs2/datastructures/LinkedDequeTests.java
+10
-9
tests/edu/caltech/cs2/datastructures/LinkedDequeTests.java
tests/edu/caltech/cs2/interfaces/ICollectionTests.java
+3
-3
tests/edu/caltech/cs2/interfaces/ICollectionTests.java
tests/edu/caltech/cs2/interfaces/IDequeTests.java
+6
-8
tests/edu/caltech/cs2/interfaces/IDequeTests.java
tests/edu/caltech/cs2/interfaces/IFixedSizeQueueTests.java
+3
-4
tests/edu/caltech/cs2/interfaces/IFixedSizeQueueTests.java
tests/edu/caltech/cs2/interfaces/IQueueTests.java
+2
-2
tests/edu/caltech/cs2/interfaces/IQueueTests.java
tests/edu/caltech/cs2/interfaces/IStackTests.java
+2
-2
tests/edu/caltech/cs2/interfaces/IStackTests.java
tests/edu/caltech/cs2/project03/Project03TestOrdering.java
+7
-3
tests/edu/caltech/cs2/project03/Project03TestOrdering.java
with
79 additions
and
43 deletions
+79
-43
tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java
View file @
8b332aad
...
...
@@ -3,30 +3,25 @@ package edu.caltech.cs2.datastructures;
import
edu.caltech.cs2.helpers.Inspection
;
import
edu.caltech.cs2.helpers.Reflection
;
import
edu.caltech.cs2.helpers.RuntimeInstrumentation
;
import
edu.caltech.cs2.interfaces.ICollection
;
import
edu.caltech.cs2.interfaces.IQueue
;
import
edu.caltech.cs2.interfaces.IDeque
;
import
edu.caltech.cs2.interfaces.IStack
;
import
edu.caltech.cs2.interfaces.*
;
import
org.junit.jupiter.api.*
;
import
org.junit.jupiter.params.ParameterizedTest
;
import
org.junit.jupiter.params.provider.CsvSource
;
import
org.junit.jupiter.params.provider.ValueSource
;
import
java.lang.reflect.Constructor
;
import
java.lang.reflect.Field
;
import
java.util.*
;
import
java.util.concurrent.TimeUnit
;
import
java.util.function.Consumer
;
import
java.util.function.Function
;
import
java.util.stream.Stream
;
import
static
edu
.
caltech
.
cs2
.
project03
.
Project03TestOrdering
.*;
import
static
java
.
util
.
concurrent
.
TimeUnit
.
SECONDS
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
*
;
@TestMethodOrder
(
MethodOrderer
.
OrderAnnotation
.
class
)
@Tag
(
"C"
)
public
class
ArrayDequeTests
implements
DequeTests
,
StackTests
,
QueueTests
{
public
class
ArrayDequeTests
implements
I
DequeTests
,
I
StackTests
,
I
QueueTests
{
private
static
String
ARRAY_DEQUE_SOURCE
=
"src/edu/caltech/cs2/datastructures/ArrayDeque.java"
;
private
Constructor
arrayDequeConstructor
=
Reflection
.
getConstructor
(
ArrayDeque
.
class
);
...
...
@@ -131,6 +126,44 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests {
Inspection
.
assertConstructorHygiene
(
ARRAY_DEQUE_SOURCE
);
}
// ARRAYDEQUE TESTS ---------------------------------------------------
@Order
(
implSpecificTestLevel
)
@DisplayName
(
"The default capacity of the array is 10"
)
@Test
public
void
testArrayDequeDefaultInitialCapacity
()
throws
IllegalAccessException
{
ArrayDeque
<
Integer
>
impl
=
new
ArrayDeque
<>();
// Reflect and get the backing array
// It's actually an Object[] since that's how it (should!) be initialized internally
// Casting it doesn't change the type of the field.
// It's fine since there should only be one array.
Field
arr
=
Reflection
.
getFieldByType
(
ArrayDeque
.
class
,
Object
[].
class
);
arr
.
setAccessible
(
true
);
Integer
[]
backingArray
=
(
Integer
[])
arr
.
get
(
impl
);
assertEquals
(
10
,
backingArray
.
length
,
"Default initial capacity is not 10"
);
}
@Order
(
implSpecificTestLevel
)
@DisplayName
(
"enqueue should always succeed"
)
@Test
public
void
testThatArrayDequeEnqueueAlwaysSucceeds
()
{
ArrayDeque
<
Integer
>
impl
=
new
ArrayDeque
<>();
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
assertTrue
(
impl
.
enqueue
(
i
),
"enqueue() should always succeed for ArrayDeque"
);
}
}
@Order
(
implSpecificTestLevel
)
@DisplayName
(
"push should always succeed"
)
@Test
public
void
testThatArrayDequePushAlwaysSucceeds
()
{
ArrayDeque
<
Integer
>
impl
=
new
ArrayDeque
<>();
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
assertTrue
(
impl
.
push
(
i
),
"push() should always succeed for ArrayDeque"
);
}
}
// TOSTRING TESTS ---------------------------------------------------
...
...
@@ -179,7 +212,7 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests {
}
@Order
(
complexityTestLevel
)
@DisplayName
(
"addBack() and removeBack() take
linear
time"
)
@DisplayName
(
"addBack() and removeBack() take
constant
time"
)
@Timeout
(
value
=
20
,
unit
=
SECONDS
)
@Test
public
void
testBackDequeOperationComplexity
()
{
...
...
@@ -193,8 +226,8 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests {
Consumer
<
IDeque
<
Integer
>>
addBack
=
(
IDeque
<
Integer
>
q
)
->
q
.
addBack
(
0
);
Consumer
<
IDeque
<
Integer
>>
removeBack
=
(
IDeque
<
Integer
>
q
)
->
q
.
removeBack
();
RuntimeInstrumentation
.
assertAtMost
(
"addBack"
,
RuntimeInstrumentation
.
ComplexityType
.
LINEAR
,
provide
,
addBack
,
8
);
RuntimeInstrumentation
.
assertAtMost
(
"removeBack"
,
RuntimeInstrumentation
.
ComplexityType
.
LINEAR
,
provide
,
removeBack
,
8
);
RuntimeInstrumentation
.
assertAtMost
(
"addBack"
,
RuntimeInstrumentation
.
ComplexityType
.
CONSTANT
,
provide
,
addBack
,
8
);
RuntimeInstrumentation
.
assertAtMost
(
"removeBack"
,
RuntimeInstrumentation
.
ComplexityType
.
CONSTANT
,
provide
,
removeBack
,
8
);
}
@Order
(
complexityTestLevel
)
...
...
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/datastructures/CircularArrayFixedSizeQueueTests.java
View file @
8b332aad
...
...
@@ -3,6 +3,7 @@ package edu.caltech.cs2.datastructures;
import
edu.caltech.cs2.helpers.Inspection
;
import
edu.caltech.cs2.helpers.Reflection
;
import
edu.caltech.cs2.helpers.RuntimeInstrumentation
;
import
edu.caltech.cs2.interfaces.IFixedSizeQueueTests
;
import
edu.caltech.cs2.interfaces.IFixedSizeQueue
;
import
edu.caltech.cs2.interfaces.IQueue
;
import
org.junit.jupiter.api.*
;
...
...
@@ -21,7 +22,7 @@ import static org.junit.jupiter.api.Assertions.*;
@Tag
(
"B"
)
@TestMethodOrder
(
MethodOrderer
.
OrderAnnotation
.
class
)
public
class
CircularArrayFixedSizeQueueTests
implements
FixedSizeQueueTests
{
public
class
CircularArrayFixedSizeQueueTests
implements
I
FixedSizeQueueTests
{
private
static
String
FIXED_QUEUE_SOURCE
=
"src/edu/caltech/cs2/datastructures/CircularArrayFixedSizeQueue.java"
;
private
Constructor
circFixedSizeQueueConstructor
=
Reflection
.
getConstructor
(
CircularArrayFixedSizeQueue
.
class
,
...
...
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/datastructures/LinkedDequeTests.java
View file @
8b332aad
...
...
@@ -4,11 +4,7 @@ import edu.caltech.cs2.helpers.Inspection;
import
edu.caltech.cs2.helpers.NodeChecker
;
import
edu.caltech.cs2.helpers.Reflection
;
import
edu.caltech.cs2.helpers.RuntimeInstrumentation
;
import
edu.caltech.cs2.interfaces.ICollection
;
import
edu.caltech.cs2.interfaces.IDeque
;
import
edu.caltech.cs2.interfaces.IQueue
;
import
edu.caltech.cs2.interfaces.IStack
;
import
edu.caltech.cs2.project03.Project03TestOrdering.*
;
import
edu.caltech.cs2.interfaces.*
;
import
org.junit.jupiter.api.*
;
import
org.junit.jupiter.params.ParameterizedTest
;
import
org.junit.jupiter.params.provider.CsvSource
;
...
...
@@ -27,7 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
@Tag
(
"C"
)
@TestMethodOrder
(
MethodOrderer
.
OrderAnnotation
.
class
)
public
class
LinkedDequeTests
implements
DequeTests
,
StackTests
,
QueueTests
{
public
class
LinkedDequeTests
implements
I
DequeTests
,
I
StackTests
,
I
QueueTests
{
private
static
String
LINKED_DEQUE_SOURCE
=
"src/edu/caltech/cs2/datastructures/LinkedDeque.java"
;
private
Constructor
linkedDequeConstructor
=
Reflection
.
getConstructor
(
LinkedDeque
.
class
);
...
...
@@ -114,7 +110,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests {
}
@Order
(
classSpecificTestLevel
)
@DisplayName
(
"Check
for linked
node class"
)
@DisplayName
(
"Check
that LinkedDeque uses a
node class"
)
@Test
public
void
testLinkedNode
()
{
Class
[]
classes
=
LinkedDeque
.
class
.
getDeclaredClasses
();
...
...
@@ -283,6 +279,8 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests {
8
);
}
// "LINKED-NESS" TESTS ------------------------------------------------
@Order
(
dequeTestLevel
)
@DisplayName
(
"Cycle detection for addFront(...), addBack(...), removeFront(...), and removeBack(...)"
)
@ParameterizedTest
(
name
=
"Test cycles - {1} random numbers with seed = {0}"
)
...
...
@@ -293,7 +291,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests {
IDeque
<
Object
>
impl
=
new
LinkedDeque
<>();
// Test that first peek is null
assertNull
(
impl
.
peekFront
(),
"empty peek should return null"
);
//
Test adding values updates size and displays contained correctly
//
Randomly add / remove elements to the front / back
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
int
num
=
r
.
nextInt
();
if
(
num
%
2
==
0
)
{
...
...
@@ -312,7 +310,9 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests {
impl
.
removeBack
();
}
}
// After each operation, check whether cycles have formed
NodeChecker
.
cycleDetection
(
impl
,
true
);
// Sanity checks, though these aren't super necessary
assertEquals
(
reference
.
size
(),
impl
.
size
(),
"size()s are not equal"
);
assertEquals
(
reference
.
toString
(),
impl
.
toString
(),
"toStrings()s are not equal"
);
}
...
...
@@ -328,7 +328,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests {
IDeque
<
Object
>
impl
=
new
LinkedDeque
<>();
// Test that first peek is null
assertNull
(
impl
.
peekFront
(),
"empty peek should return null"
);
//
Test adding values updates size and displays contained correctly
//
Randomly add / remove elements to the front / back
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
int
num
=
r
.
nextInt
();
if
(
num
%
2
==
0
)
{
...
...
@@ -347,6 +347,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests {
impl
.
removeBack
();
}
}
// Check that forwards and backwards iteration are sane
NodeChecker
.
checkReverse
(
impl
);
assertEquals
(
reference
.
size
(),
impl
.
size
(),
"size()s are not equal"
);
assertEquals
(
reference
.
toString
(),
impl
.
toString
(),
"toStrings()s are not equal"
);
...
...
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/
datastructur
es/CollectionTests.java
→
tests/edu/caltech/cs2/
interfac
es/
I
CollectionTests.java
View file @
8b332aad
package
edu.caltech.cs2.
datastructur
es
;
package
edu.caltech.cs2.
interfac
es
;
import
edu.caltech.cs2.interfaces.ICollection
;
import
org.junit.jupiter.api.DisplayName
;
...
...
@@ -17,7 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertFalse
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
public
interface
CollectionTests
{
public
interface
I
CollectionTests
{
ICollection
<
Object
>
newCollection
();
@Order
(
collectionTestLevel
)
...
...
@@ -124,7 +124,7 @@ public interface CollectionTests {
// Shuffle order of nums and check that all are contained in the collection
Collections
.
shuffle
(
nums
);
for
(
int
num
:
nums
)
{
assert
Equals
(
t
rue
,
impl
.
contains
(
num
),
"value should be contained"
);
assert
T
rue
(
impl
.
contains
(
num
),
"value should be contained"
);
}
// Test that values not in collection are not contained
...
...
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/
datastructur
es/DequeTests.java
→
tests/edu/caltech/cs2/
interfac
es/
I
DequeTests.java
View file @
8b332aad
package
edu.caltech.cs2.
datastructur
es
;
package
edu.caltech.cs2.
interfac
es
;
import
edu.caltech.cs2.interfaces.ICollection
;
import
edu.caltech.cs2.interfaces.IDeque
;
import
org.hamcrest.MatcherAssert
;
import
org.hamcrest.collection.IsEmptyIterable
;
import
org.junit.jupiter.api.DisplayName
;
...
...
@@ -20,7 +18,7 @@ import java.util.Random;
import
static
edu
.
caltech
.
cs2
.
project03
.
Project03TestOrdering
.*;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
public
interface
DequeTests
extends
CollectionTests
{
public
interface
I
DequeTests
extends
I
CollectionTests
{
IDeque
<
Object
>
newDeque
();
@Order
(
dequeTestLevel
)
...
...
@@ -42,7 +40,8 @@ public interface DequeTests extends CollectionTests {
ref
.
addLast
(
value
);
MatcherAssert
.
assertThat
(
impl
,
IsIterableContainingInOrder
.
contains
(
ref
.
toArray
()));
}
for
(
Object
value
:
inputs
.
trim
().
split
(
", "
))
{
// Check that iterator is consistent while objects are removed from the back
for
(
Object
ignored
:
inputs
.
trim
().
split
(
", "
))
{
MatcherAssert
.
assertThat
(
impl
,
IsIterableContainingInOrder
.
contains
(
ref
.
toArray
()));
impl
.
removeBack
();
ref
.
removeLast
();
...
...
@@ -291,8 +290,7 @@ public interface DequeTests extends CollectionTests {
coll
.
add
(
num
);
}
impl
.
addAll
(
coll
);
for
(
Object
num
:
coll
)
{
assertTrue
(
impl
.
contains
(
num
),
"value should be contained in Deque"
);
}
MatcherAssert
.
assertThat
(
"IDeque has incorrect elements / order"
,
impl
,
IsIterableContainingInOrder
.
contains
(
coll
));
}
}
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/
datastructur
es/FixedSizeQueueTests.java
→
tests/edu/caltech/cs2/
interfac
es/
I
FixedSizeQueueTests.java
View file @
8b332aad
package
edu.caltech.cs2.
datastructur
es
;
package
edu.caltech.cs2.
interfac
es
;
import
edu.caltech.cs2.datastructures.CircularArrayFixedSizeQueue
;
import
edu.caltech.cs2.helpers.Reflection
;
import
edu.caltech.cs2.interfaces.IFixedSizeQueue
;
import
org.junit.jupiter.api.DisplayName
;
import
org.junit.jupiter.api.Order
;
import
org.junit.jupiter.api.Tag
;
import
org.junit.jupiter.params.ParameterizedTest
;
import
org.junit.jupiter.params.provider.CsvSource
;
...
...
@@ -15,7 +14,7 @@ import java.util.Random;
import
static
edu
.
caltech
.
cs2
.
project03
.
Project03TestOrdering
.*;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
public
interface
FixedSizeQueueTests
extends
QueueTests
{
public
interface
I
FixedSizeQueueTests
extends
I
QueueTests
{
IFixedSizeQueue
<
Object
>
newFixedSizeQueue
(
int
capacity
);
@Order
(
fixedSizeQueueLevel
)
...
...
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/
datastructur
es/QueueTests.java
→
tests/edu/caltech/cs2/
interfac
es/
I
QueueTests.java
View file @
8b332aad
package
edu.caltech.cs2.
datastructur
es
;
package
edu.caltech.cs2.
interfac
es
;
import
edu.caltech.cs2.interfaces.IQueue
;
import
edu.caltech.cs2.interfaces.IStack
;
...
...
@@ -14,7 +14,7 @@ import java.util.Random;
import
static
edu
.
caltech
.
cs2
.
project03
.
Project03TestOrdering
.*;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
public
interface
QueueTests
{
public
interface
I
QueueTests
{
IQueue
<
Object
>
newQueue
();
IQueue
<
Object
>
newQueue
(
int
size
);
...
...
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/
datastructur
es/StackTests.java
→
tests/edu/caltech/cs2/
interfac
es/
I
StackTests.java
View file @
8b332aad
package
edu.caltech.cs2.
datastructur
es
;
package
edu.caltech.cs2.
interfac
es
;
import
edu.caltech.cs2.interfaces.IStack
;
import
org.junit.jupiter.api.DisplayName
;
...
...
@@ -13,7 +13,7 @@ import static edu.caltech.cs2.project03.Project03TestOrdering.*;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertNull
;
public
interface
StackTests
{
public
interface
I
StackTests
{
IStack
<
Object
>
newStack
();
@Order
(
stackTestLevel
)
...
...
This diff is collapsed.
Click to expand it.
tests/edu/caltech/cs2/project03/Project03TestOrdering.java
View file @
8b332aad
...
...
@@ -5,6 +5,7 @@ public final class Project03TestOrdering {
throw
new
InstantiationError
(
"Class is only for storing constant variables"
);
}
// Tests related to class structure
public
static
final
int
classSpecificTestLevel
=
0
;
public
static
final
int
collectionTestLevel
=
1
;
...
...
@@ -14,8 +15,11 @@ public final class Project03TestOrdering {
public
static
final
int
fixedSizeQueueLevel
=
3
;
public
static
final
int
toStringTestLevel
=
4
;
public
static
final
int
co
mpl
exity
TestLevel
=
5
;
// Tests related to the particular implementation
public
static
final
int
i
mpl
Specific
TestLevel
=
4
;
public
static
final
int
guitarStringTestLevel
=
6
;
public
static
final
int
toStringTestLevel
=
5
;
public
static
final
int
complexityTestLevel
=
6
;
public
static
final
int
guitarStringTestLevel
=
7
;
}
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