From dccd84a72b215b496e3302dbd166399eacc083ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20L=C3=A4ufer?= Date: Tue, 26 Sep 2023 20:21:26 -0500 Subject: [PATCH] updated to newer Scala 3 syntax without braces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Konstantin Läufer --- src/main/scala/MapPerformance.scala | 16 ++------- src/main/scala/Performance.scala | 18 +++------- src/main/scala/timeThis.scala | 10 ++++++ .../scala/cs271/lab/list/TestIterator.scala | 9 ++--- src/test/scala/cs271/lab/list/TestList.scala | 36 +++++++------------ 5 files changed, 32 insertions(+), 57 deletions(-) create mode 100644 src/main/scala/timeThis.scala diff --git a/src/main/scala/MapPerformance.scala b/src/main/scala/MapPerformance.scala index 680a9e5..2a3980b 100644 --- a/src/main/scala/MapPerformance.scala +++ b/src/main/scala/MapPerformance.scala @@ -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 diff --git a/src/main/scala/Performance.scala b/src/main/scala/Performance.scala index 635f189..f636d29 100644 --- a/src/main/scala/Performance.scala +++ b/src/main/scala/Performance.scala @@ -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 diff --git a/src/main/scala/timeThis.scala b/src/main/scala/timeThis.scala new file mode 100644 index 0000000..f058e00 --- /dev/null +++ b/src/main/scala/timeThis.scala @@ -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 diff --git a/src/test/scala/cs271/lab/list/TestIterator.scala b/src/test/scala/cs271/lab/list/TestIterator.scala index 4eaf450..cbdfa32 100644 --- a/src/test/scala/cs271/lab/list/TestIterator.scala +++ b/src/test/scala/cs271/lab/list/TestIterator.scala @@ -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 @@ -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) @@ -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 diff --git a/src/test/scala/cs271/lab/list/TestList.scala b/src/test/scala/cs271/lab/list/TestList.scala index b8bb712..b5ba9f7 100644 --- a/src/test/scala/cs271/lab/list/TestList.scala +++ b/src/test/scala/cs271/lab/list/TestList.scala @@ -4,16 +4,14 @@ 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) @@ -21,16 +19,14 @@ class TestList extends AnyFunSuite: 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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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