forked from igorwojda/kotlin-coding-challenges
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchallenge.kt
116 lines (94 loc) · 2.38 KB
/
challenge.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.igorwojda.queue.basic
import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Test
private class Queue<E> {
var size = 0
private set
fun add(element: E) {
TODO("not implemented")
}
fun remove(): E = TODO("not implemented")
fun peek(): E = TODO("not implemented")
fun isEmpty(): Boolean {
TODO("not implemented")
}
}
private class Test {
@Test
fun `can add elements to a queue`() {
Queue<Int>().apply { add(1) }
}
@Test
fun `can remove elements from empty queue`() {
Queue<Int>().apply { remove() shouldBeEqualTo null }
}
@Test
fun `can remove elements from a queue`() {
Queue<String>().apply {
add("ABC")
remove()
}
}
@Test
fun `order of elements is maintained`() {
Queue<Char>().apply {
add('A')
add('B')
add('C')
remove() shouldBeEqualTo 'A'
remove() shouldBeEqualTo 'B'
remove() shouldBeEqualTo 'C'
remove() shouldBeEqualTo null
}
}
@Test
fun `peek returns, but does not remove element`() {
Queue<Int>().apply {
add(1)
add(2)
peek() shouldBeEqualTo 1
peek() shouldBeEqualTo 1
remove() shouldBeEqualTo 1
peek() shouldBeEqualTo 2
remove() shouldBeEqualTo 2
peek() shouldBeEqualTo null
remove() shouldBeEqualTo null
}
}
@Test
fun `isEmpty returns true`() {
Queue<Int>().isEmpty() shouldBeEqualTo true
}
@Test
fun `isEmpty returns false`() {
Queue<Int>().apply {
add(1)
isEmpty() shouldBeEqualTo false
}
}
@Test
fun `queue has correct size`() {
Queue<Char>().apply {
size shouldBeEqualTo 0
add('A')
size shouldBeEqualTo 1
add('B')
size shouldBeEqualTo 2
add('C')
size shouldBeEqualTo 3
remove()
size shouldBeEqualTo 2
remove()
size shouldBeEqualTo 1
remove()
size shouldBeEqualTo 0
}
}
@Test
fun `remove item from empty queue`() {
Queue<Int>().apply {
remove()
size shouldBeEqualTo 0
}
}
}