Skip to content

Commit

Permalink
updated to newer Scala 3 syntax without braces
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Läufer <[email protected]>
  • Loading branch information
klaeufer committed Sep 27, 2023
1 parent 6ab1f14 commit dccd84a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 57 deletions.
16 changes: 3 additions & 13 deletions src/main/scala/MapPerformance.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,19 @@ object MapPerformance:

val fixture = new HashMap[Int, Int]

timeThis(s"${fixture.getClass.getSimpleName} fixture creation (size = $size)") {
timeThis(s"${fixture.getClass.getSimpleName} fixture creation (size = $size)"):
for i <- 0 until size do fixture.put(i, i)
}

timeThis(s"${fixture.getClass.getSimpleName} reps = $reps fixture size = ${fixture.size} random access") {
timeThis(s"${fixture.getClass.getSimpleName} reps = $reps fixture size = ${fixture.size} random access"):
var x = 0
for r <- 0 until reps do
val randomPosition = nextInt(fixture.size)
x = fixture(randomPosition % size)
}

timeThis(s"${fixture.getClass.getSimpleName} reps = $reps fixture size = ${fixture.size} random add/remove") {
timeThis(s"${fixture.getClass.getSimpleName} reps = $reps fixture size = ${fixture.size} random add/remove"):
for r <- 0 until reps do
val randomPosition = nextInt(fixture.size)
fixture.put(randomPosition, randomPosition)
fixture.remove(randomPosition)
}

def timeThis[A](s: String)(block: => A): A =
val time0 = System.currentTimeMillis
val b = block
val time1 = System.currentTimeMillis - time0
println(s"$s = $time1 ms")
b

end MapPerformance
18 changes: 4 additions & 14 deletions src/main/scala/Performance.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,19 @@ object Performance:

val fixture = new ArrayBuffer[Int]

timeThis(s"${fixture.getClass.getSimpleName} fixture creation (size = $size)") {
timeThis(s"${fixture.getClass.getSimpleName} fixture creation (size = $size)"):
for i <- 0 until size do fixture += i
}

timeThis(s"${fixture.getClass.getSimpleName} reps = $reps fixture size = ${fixture.length} random access") {
timeThis(s"${fixture.getClass.getSimpleName} reps = $reps fixture size = ${fixture.length} random access"):
var x = 0L
for r <- 0 until reps do
val randomPosition = nextInt(fixture.length)
x = fixture(randomPosition % size)
}

timeThis(s"${fixture.getClass.getSimpleName} reps = $reps fixture size = ${fixture.length} random add/remove") {
timeThis(s"${fixture.getClass.getSimpleName} reps = $reps fixture size = ${fixture.length} random add/remove"):
for r <- 0 until reps do
val randomPosition = nextInt(fixture.length)
fixture.insert(randomPosition, 77)
fixture.remove(randomPosition)
}

def timeThis[A](s: String)(block: => A): A =
val time0 = System.currentTimeMillis
val b = block
val time1 = System.currentTimeMillis - time0
println(s"$s = $time1 ms")
b


end Performance
10 changes: 10 additions & 0 deletions src/main/scala/timeThis.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object timeThis:

def apply[A](s: String)(block: => A): A =
val time0 = System.currentTimeMillis
val b = block
val time1 = System.currentTimeMillis - time0
println(s"$s = $time1 ms")
b

end timeThis
9 changes: 3 additions & 6 deletions src/test/scala/cs271/lab/list/TestIterator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import org.scalatest.funsuite.AnyFunSuite

class TestIterator extends AnyFunSuite:

test("testEmptyList") {
test("testEmptyList"):
val list = Fixture.fixture()
val iterator = list.iterator
assert(!iterator.hasNext)
}

test("testNonEmptyList") {
test("testNonEmptyList"):
val list = Fixture.fixture()
Fixture.populate(list)
val i = list.iterator
Expand All @@ -31,9 +30,8 @@ class TestIterator extends AnyFunSuite:
assert(i.hasNext)
assert(i.next() == 0)
assert(i.hasNext)
}

test("testAverageValue") {
test("testAverageValue"):
val list = Fixture.fixture()
Fixture.populate(list)

Expand All @@ -43,6 +41,5 @@ class TestIterator extends AnyFunSuite:
// (defined as the sum of the items divided by the number of items)
assert(n == 7)
assert((sum.toDouble / n).round == 61)
}

end TestIterator
36 changes: 12 additions & 24 deletions src/test/scala/cs271/lab/list/TestList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,29 @@ import org.scalatest.funsuite.AnyFunSuite

class TestList extends AnyFunSuite:

test("testSizeEmpty") {
test("testSizeEmpty"):
val list = Fixture.fixture()
assert(list.isEmpty)
assert(list.size == 0)
intercept[IndexOutOfBoundsException] {
intercept[IndexOutOfBoundsException]:
list(0)
}
}

test("testSizeNonEmpty") {
test("testSizeNonEmpty"):
// TODO fix the expected values in the assertions below
val list = Fixture.fixture()
assert(list.isEmpty)
list += 77
assert(list.isEmpty)
assert(list.size == 0)
assert(list(0) == 0)
}

test("testContains") {
test("testContains"):
val list = Fixture.fixture()
// TODO write assertions using list.contains(77)
// that hold before and after adding 77 to the list
fail("Not yet implemented") // remove this line when done
}

test("testAddMultiple") {
test("testAddMultiple"):
val list = Fixture.fixture()
list += 77
list += 77
Expand All @@ -40,9 +36,8 @@ class TestList extends AnyFunSuite:
assert(list.indexOf(77) == 0)
assert(list(1) == 0)
assert(list.lastIndexOf(77) == 0)
}

test("testAddMultiple2") {
test("testAddMultiple2"):
val list = Fixture.fixture()
Fixture.populate(list)
// TODO fix the expected values in the assertions below
Expand All @@ -52,9 +47,8 @@ class TestList extends AnyFunSuite:
assert(list(2) == 0)
assert(list(3) == 0)
assert(list == List(33, 77, 44))
}

test("testRemoveObject") {
test("testRemoveObject"):
val list = Fixture.fixture()
Fixture.populate(list)
// TODO fix the expected values in the assertions below
Expand All @@ -69,17 +63,15 @@ class TestList extends AnyFunSuite:
assert(list.lastIndexOf(77) == 0)
assert(list(2) == 0)
assert(list(3) == 0)
}

test("testContainsAll") {
test("testContainsAll"):
val list = Fixture.fixture()
Fixture.populate(list)
// TODO 1) assert that list contains all five different numbers added
// TODO 2) assert that list does not contain all of 11, 22, and 33 (together)
fail("Not yet implemented") // remove this line when done
}

test("testAddAll") {
test("testAddAll"):
val list = Fixture.fixture()
// TODO in a single statement add items to the list to make the following assertions pass
// (without touching the assertions themselves)
Expand All @@ -91,18 +83,16 @@ class TestList extends AnyFunSuite:
assert(list(4) == 55)
assert(list(5) == 77)
assert(list(6) == 66)
}

test("testRemoveAll") {
test("testRemoveAll"):
val list = Fixture.fixture()
Fixture.populate(list)
// TODO in a single statement, remove items from the list to make the following assertions pass
// (without touching the assertions themselves)
assert(list.size == 3)
assert(List(77, 77, 77) == list)
}

test("testSet") {
test("testSet"):
val list = Fixture.fixture()
Fixture.populate(list)
// TODO use the update method to change specific elements in the list
Expand All @@ -116,14 +106,12 @@ class TestList extends AnyFunSuite:
assert(list(4) == 55)
assert(list(5) == 99)
assert(list(6) == 66)
}

test("testSubList") {
test("testSubList"):
val list = Fixture.fixture()
Fixture.populate(list)
// TODO fix the arguments in the slice method so that the assertion
// passes
assert(List(44, 77, 55) == list.slice(0, 0))
}

end TestList

0 comments on commit dccd84a

Please sign in to comment.