From c0c0b4d5e86f5c37bf1d0609eab06863f73c5abd Mon Sep 17 00:00:00 2001 From: Julien Richard-Foy Date: Thu, 25 Jan 2018 15:43:25 +0100 Subject: [PATCH] Fix clear operation in ListBuffer --- .../collection/mutable/ListBuffer.scala | 3 ++ .../collection/mutable/ListBufferTest.scala | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 test/junit/src/test/scala/strawman/collection/mutable/ListBufferTest.scala diff --git a/collections/src/main/scala/strawman/collection/mutable/ListBuffer.scala b/collections/src/main/scala/strawman/collection/mutable/ListBuffer.scala index 39e5fe7d39..b6d83c4bd4 100644 --- a/collections/src/main/scala/strawman/collection/mutable/ListBuffer.scala +++ b/collections/src/main/scala/strawman/collection/mutable/ListBuffer.scala @@ -94,6 +94,9 @@ class ListBuffer[A] def clear(): Unit = { first = Nil + len = 0 + last0 = null + aliased = false } def addOne(elem: A): this.type = { diff --git a/test/junit/src/test/scala/strawman/collection/mutable/ListBufferTest.scala b/test/junit/src/test/scala/strawman/collection/mutable/ListBufferTest.scala new file mode 100644 index 0000000000..408dfdba8c --- /dev/null +++ b/test/junit/src/test/scala/strawman/collection/mutable/ListBufferTest.scala @@ -0,0 +1,30 @@ +package strawman.collection.mutable + +import strawman.collection.immutable.Nil + +import org.junit.{Assert, Test} +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class ListBufferTest { + + @Test + def hasCorrectClear(): Unit = { + val b = ListBuffer.empty[String] + b += "a" + Assert.assertTrue(b.sameElements("a" :: Nil)) + b.clear() + Assert.assertEquals(ListBuffer.empty[String], b) + b += "b" + Assert.assertTrue(b.sameElements("b" :: Nil)) + + val b2 = ListBuffer.empty[String] + b2 += "a" + val _ = b2.toList + b2.clear() + b2 += "b" + Assert.assertTrue(b2.sameElements("b" :: Nil)) + } + +}