Newer
Older
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package concpar22final03
import scala.concurrent.duration.*
import scala.concurrent.{Await, Future}
import scala.util.{Try, Success, Failure}
import scala.concurrent.ExecutionContext.Implicits.global
class Problem3Suite extends munit.FunSuite:
trait Prob3Test extends Problem3:
override val economics: EconomicsTest
class Test1 extends Prob3Test:
override val economics: EconomicsTest = new EconomicsTest:
override def sellWaitTime() = 10
override def buyWaitTime() = 20
override def depositWaitTime() = 30
override def withdrawWaitTime() = 40
override def initialBalance() = 0
class Test2 extends Prob3Test:
override val economics: EconomicsTest = new EconomicsTest:
override def sellWaitTime() = 100
override def buyWaitTime() = 5
override def depositWaitTime() = 50
override def withdrawWaitTime() = 5
override def initialBalance() = 0
class Test3 extends Prob3Test:
override val economics: EconomicsTest = new EconomicsTest:
val rgen = new scala.util.Random(666)
override def sellWaitTime() = rgen.nextInt(100)
override def buyWaitTime() = rgen.nextInt(100)
override def depositWaitTime() = rgen.nextInt(100)
override def withdrawWaitTime() = rgen.nextInt(100)
override def initialBalance() = 0
class Test4 extends Prob3Test:
var counter = 5
def next(): Int =
counter = counter + 5 % 119
counter
override val economics: EconomicsTest = new EconomicsTest:
override def sellWaitTime() = next()
override def buyWaitTime() = next()
override def depositWaitTime() = next()
override def withdrawWaitTime() = next()
override def initialBalance() = next()
def testCases = List(new Test1, new Test2)
def unevenTestCases = List(new Test3, new Test4)
def tot(cards: List[String]): Int =
cards.map[Int]((n: String) => n.length).sum
def testOk(
t: Prob3Test,
money: Int,
sold: List[String],
wanted: List[String]
): Unit =
import t.*
import economics.*
val f = orderDeck(getMoneyBag(money), sold.map(getCard), wanted)
val r = Await.ready(f, 3.seconds).value.get
assert(r.isSuccess)
r match
case Success(d) =>
assertEquals(d.map(_.name).sorted, wanted.sorted)
assertEquals(d.length, wanted.length)
assertEquals(isMine(d.head), true)
case Failure(e) => ()
def testFailure(
t: Prob3Test,
money: Int,
sold: List[String],
wanted: List[String]
): Unit =
import t.*
import economics.*
val f = orderDeck(getMoneyBag(money), sold.map(getCard), wanted)
val r = Await.ready(f, 3.seconds).value.get
assert(r.isFailure)
r match
case Failure(e: NotEnoughMoneyException) => ()
case _ => fail(
"Should have thrown a NotEnoughMoneyException exception, but did not"
)
// --- Without sold cards ---
test(
"Should work correctly when a single card is asked with enough money (no card sold) (20pts)"
) {
testCases.foreach(t => testOk(t, 7, Nil, List("Tefeiri")))
}
test(
"Should work correctly when a single card is asked with enough money (no card sold, uneven waiting time) (10pts)"
) {
unevenTestCases.foreach(t => testOk(t, 7, Nil, List("Tefeiri")))
}
test(
"Should work correctly when multiple cards are asked with enough money (no card sold) (20pts)"
) {
val cards = List("aaaa", "bbb", "ccccc", "dd", "eeee", "f", "ggggggg")
testCases.foreach(t => testOk(t, tot(cards), Nil, cards))
}
test(
"Should work correctly when multiple cards are asked with enough money (no card sold, uneven waiting time) (10pts)"
) {
val cards = List("aaaa", "bbb", "ccccc", "dd", "eeee", "f", "ggggggg")
unevenTestCases.foreach(t => testOk(t, tot(cards), Nil, cards))
}
test(
"Should work correctly when asked duplicates of cards, with enough money (no card sold) (20pts)"
) {
val cards = List("aaaa", "aaaa", "aaaa", "dd", "dd", "dd", "dd")
testCases.foreach(t => testOk(t, tot(cards), Nil, cards))
}
test(
"Should work correctly when asked duplicates of cards, with enough money (no card sold, uneven waiting time) (10pts)"
) {
val cards = List("aaaa", "aaaa", "aaaa", "dd", "dd", "dd", "dd")
unevenTestCases.foreach(t => testOk(t, tot(cards), Nil, cards))
}
// --- With sold cards ---
test(
"Should work correctly when a single card is bought and a single of the same price is sold (20pts)"
) {
testCases.foreach(t => testOk(t, 0, List("Chandra"), List("Tefeiri")))
}
test(
"Should work correctly when a single card is bought and a single of the same price is sold (uneven waiting time) (10pts)"
) {
unevenTestCases.foreach(t => testOk(t, 0, List("Chandra"), List("Tefeiri")))
}
test(
"Should work correctly when multiple cards are asked and multiple of matching values are sold (20pts)"
) {
val cards = List("aaaa", "bbb", "ccccc", "dd", "eeee", "f", "ggggggg")
val sold = List("1111111", "2", "3333", "44", "55555", "666", "7777")
testCases.foreach(t => testOk(t, 0, sold, cards))
}
test(
"Should work correctly when multiple cards are asked and multiple of matching values are sold (uneven waiting time) (10pts)"
) {
val cards = List("aaaa", "bbb", "ccccc", "dd", "eeee", "f", "ggggggg")
val sold = List("1111111", "2", "3333", "44", "55555", "666", "7777")
unevenTestCases.foreach(t => testOk(t, 0, sold, cards))
}
test(
"Should work correctly when multiple cards are asked and multiple of the same total value are sold (20pts)"
) {
val cards2 = List("aaaa", "bbb", "ccccc", "dd", "eeee", "f", "ggggggg")
val sold2 = List("111111111", "22", "3", "44", "555555", "666", "777")
assert(tot(sold2) == tot(cards2))
testCases.foreach(t => testOk(t, 0, sold2, cards2))
}
test(
"Should work correctly when multiple cards are asked and multiple of the same total value are sold (uneven waiting time) (10pts)"
) {
val cards2 = List("aaaa", "bbb", "ccccc", "dd", "eeee", "f", "ggggggg")
val sold2 = List("111111111", "22", "3", "44", "555555", "666", "777")
assert(tot(sold2) == tot(cards2))
unevenTestCases.foreach(t => testOk(t, 0, sold2, cards2))
}
test(
"Should work correctly when given money and sold cards are sufficient for the wanted cards (20pts)"
) {
val cards = List("aaaa", "bbb", "ccccc", "dd", "eeee", "f", "ggggggg")
val sold = List("11111", "2", "33", "44", "5555", "666", "777")
val bagMoney = tot(cards) - tot(sold)
testCases.foreach(t => testOk(t, bagMoney, sold, cards))
}
test(
"Should work correctly when given money and sold cards are sufficient for the wanted cards (uneven waiting time) (10pts)"
) {
val cards = List("aaaa", "bbb", "ccccc", "dd", "eeee", "f", "ggggggg")
val sold = List("11111", "2", "33", "44", "5555", "666", "777")
val bagMoney = tot(cards) - tot(sold)
unevenTestCases.foreach(t => testOk(t, bagMoney, sold, cards))
}
// --- Failures ---
test(
"Should return a failure when too little money is provided (no card sold) (20pts)"
) {
val cards = List("aaaa", "bbb", "ccccc", "dd", "eeee", "f", "ggggggg")
testCases.foreach(t => testFailure(t, tot(cards) - 1, Nil, cards))
testCases.foreach(t => testFailure(t, tot(cards) - 50, Nil, cards))
}
test(
"Should return a failure when too little money or sold cards are provided (20pts)"
) {
val cards = List("aaaa", "bbb", "ccccc", "dd", "eeee", "f", "ggggggg")
val sold = List("11111", "2", "33", "44", "5555", "666", "777")
val bagMoney = tot(cards) - tot(sold)
testCases.foreach(t => testFailure(t, bagMoney - 2, sold, cards))
}