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)) + } + +}