File tree 4 files changed +43
-2
lines changed
src/library/scala/collection/generic
test/junit/scala/collection 4 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -39,7 +39,10 @@ abstract class GenericCompanion[+CC[X] <: GenTraversable[X]] {
39
39
/** An empty collection of type `$Coll[A]`
40
40
* @tparam A the type of the ${coll}'s elements
41
41
*/
42
- def empty [A ]: CC [A ] = newBuilder[A ].result()
42
+ def empty [A ]: CC [A ] = {
43
+ if ((this eq immutable.Seq ) || (this eq collection.Seq )) Nil .asInstanceOf [CC [A ]]
44
+ else newBuilder[A ].result()
45
+ }
43
46
44
47
/** Creates a $coll with the specified elements.
45
48
* @tparam A the type of the ${coll}'s elements
Original file line number Diff line number Diff line change
1
+ package scala .collection
2
+
3
+ import org .junit .Test
4
+ import org .junit .runner .RunWith
5
+ import org .junit .runners .JUnit4
6
+
7
+ import scala .tools .testing .AllocationTest
8
+
9
+ @ RunWith (classOf [JUnit4 ])
10
+ class SeqTest extends AllocationTest {
11
+
12
+ @ Test def emptyNonAllocating (): Unit = {
13
+ nonAllocating(Seq .empty)
14
+ nonAllocating(Seq ())
15
+ }
16
+ }
Original file line number Diff line number Diff line change @@ -5,9 +5,10 @@ import org.junit.runner.RunWith
5
5
import org .junit .runners .JUnit4
6
6
7
7
import scala .ref .WeakReference
8
+ import scala .tools .testing .AllocationTest
8
9
9
10
@ RunWith (classOf [JUnit4 ])
10
- class ListTest {
11
+ class ListTest extends AllocationTest {
11
12
/**
12
13
* Test that empty iterator does not hold reference
13
14
* to complete List
@@ -46,4 +47,9 @@ class ListTest {
46
47
// real assertion
47
48
Assert .assertTrue(emptyIterators.exists(_._2.get.isEmpty))
48
49
}
50
+
51
+ @ Test def emptyNonAllocating (): Unit = {
52
+ nonAllocating(List .empty)
53
+ nonAllocating(List ())
54
+ }
49
55
}
Original file line number Diff line number Diff line change
1
+ package scala .collection .immutable
2
+
3
+ import org .junit .runner .RunWith
4
+ import org .junit .runners .JUnit4
5
+ import org .junit .{Assert , Test }
6
+
7
+ import scala .tools .testing .AllocationTest
8
+
9
+ @ RunWith (classOf [JUnit4 ])
10
+ class SeqTest extends AllocationTest {
11
+
12
+ @ Test def emptyNonAllocating (): Unit = {
13
+ nonAllocating(Seq .empty)
14
+ nonAllocating(Seq ())
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments