"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.3.1/test-suite/shared/src/test/scala/org/scalajs/testsuite/scalalib/ArrayBuilderTest.scala" (14 Nov 2020, 7941 Bytes) of package /linux/www/scala-js-1.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Scala source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "ArrayBuilderTest.scala": 1.1.0_vs_1.1.1.

    1 /*
    2  * Scala.js (https://www.scala-js.org/)
    3  *
    4  * Copyright EPFL.
    5  *
    6  * Licensed under Apache License 2.0
    7  * (https://www.apache.org/licenses/LICENSE-2.0).
    8  *
    9  * See the NOTICE file distributed with this work for
   10  * additional information regarding copyright ownership.
   11  */
   12 
   13 package org.scalajs.testsuite.scalalib
   14 
   15 import scala.language.implicitConversions
   16 
   17 import scala.reflect._
   18 import scala.collection.mutable.ArrayBuilder
   19 
   20 import org.junit.Test
   21 import org.junit.Assert._
   22 import org.junit.Assume._
   23 
   24 import org.scalajs.testsuite.utils.Platform._
   25 
   26 class ArrayBuilderTest {
   27   import ArrayBuilderTest._
   28 
   29   @noinline
   30   def erase(x: Any): Any = x
   31 
   32   @inline
   33   def makeNoInline[T](implicit ct: ClassTag[T]): ArrayBuilder[T] = {
   34     /* The dance in this method is to be source compatible with the old and
   35      * new collections. In the new collections, ArrayBuilder.make[T] doesn't
   36      * take an explicit () parameter list, but it does in the old collections.
   37      */
   38 
   39     @noinline def ctNoInline = ct
   40 
   41     {
   42       implicit val ct = ctNoInline
   43       ArrayBuilder.make[T]
   44     }
   45   }
   46 
   47   @inline
   48   def zerosInline[T: ClassTag](length: Int): Array[T] = {
   49     val builder = ArrayBuilder.make[T]
   50     builder.sizeHint(length)
   51     var i = 0
   52     while (i < length) {
   53       builder += null.asInstanceOf[T]
   54       i += 1
   55     }
   56     builder.result()
   57   }
   58 
   59   @noinline
   60   def zerosNoInline[T: ClassTag](length: Int): Array[T] =
   61     zerosInline[T](length)
   62 
   63   @noinline def someInt: Int = 53
   64   @noinline def someChar: Char = 'S'
   65   @noinline def someBoolean: Boolean = false
   66   @noinline def someString: String = "world"
   67 
   68   @Test def Int_normal_case_inline(): Unit = {
   69     val b = ArrayBuilder.make[Int]
   70     b += 42
   71     b += someInt
   72     val a = b.result()
   73 
   74     assertSame(classOf[Array[Int]], a.getClass)
   75     assertEquals(2, a.length)
   76     assertTrue(erase(a(0)).isInstanceOf[Int])
   77     assertEquals(42, erase(a(0)))
   78     assertEquals(53, erase(a(1)))
   79   }
   80 
   81   @Test def Int_normal_case_noinline(): Unit = {
   82     val b = makeNoInline[Int]
   83     b += 42
   84     b += someInt
   85     val a = b.result()
   86 
   87     assertSame(classOf[Array[Int]], a.getClass)
   88     assertEquals(2, a.length)
   89     assertTrue(erase(a(0)).isInstanceOf[Int])
   90     assertEquals(42, erase(a(0)))
   91     assertEquals(53, erase(a(1)))
   92   }
   93 
   94   @Test def Int_zeros_inline(): Unit = {
   95     val a = zerosInline[Int](3)
   96     assertSame(classOf[Array[Int]], a.getClass)
   97     assertEquals(3, a.length)
   98     assertTrue(erase(a(0)).isInstanceOf[Int])
   99     assertEquals(0, erase(a(0)))
  100   }
  101 
  102   @Test def Int_zeros_noinline(): Unit = {
  103     val a = zerosNoInline[Int](3)
  104     assertSame(classOf[Array[Int]], a.getClass)
  105     assertEquals(3, a.length)
  106     assertTrue(erase(a(0)).isInstanceOf[Int])
  107     assertEquals(0, erase(a(0)))
  108   }
  109 
  110   @Test def Char_normal_case_inline(): Unit = {
  111     val b = ArrayBuilder.make[Char]
  112     b += 'A'
  113     b += someChar
  114     val a = b.result()
  115 
  116     assertSame(classOf[Array[Char]], a.getClass)
  117     assertEquals(2, a.length)
  118     assertTrue(erase(a(0)).isInstanceOf[Char])
  119     assertEquals('A', erase(a(0)))
  120     assertEquals('S', erase(a(1)))
  121   }
  122 
  123   @Test def Char_normal_case_noinline(): Unit = {
  124     val b = makeNoInline[Char]
  125     b += 'A'
  126     b += someChar
  127     val a = b.result()
  128 
  129     assertSame(classOf[Array[Char]], a.getClass)
  130     assertEquals(2, a.length)
  131     assertTrue(erase(a(0)).isInstanceOf[Char])
  132     assertEquals('A', erase(a(0)))
  133     assertEquals('S', erase(a(1)))
  134   }
  135 
  136   @Test def Char_zeros_inline(): Unit = {
  137     val a = zerosInline[Char](3)
  138     assertSame(classOf[Array[Char]], a.getClass)
  139     assertEquals(3, a.length)
  140     assertTrue(erase(a(0)).isInstanceOf[Char])
  141     assertEquals('\u0000', erase(a(0)))
  142   }
  143 
  144   @Test def Char_zeros_noinline(): Unit = {
  145     val a = zerosNoInline[Char](3)
  146     assertSame(classOf[Array[Char]], a.getClass)
  147     assertEquals(3, a.length)
  148     assertTrue(erase(a(0)).isInstanceOf[Char])
  149     assertEquals('\u0000', erase(a(0)))
  150   }
  151 
  152   @Test def Boolean_normal_case_inline(): Unit = {
  153     val b = ArrayBuilder.make[Boolean]
  154     b += true
  155     b += someBoolean
  156     val a = b.result()
  157 
  158     assertSame(classOf[Array[Boolean]], a.getClass)
  159     assertEquals(2, a.length)
  160     assertTrue(erase(a(0)).isInstanceOf[Boolean])
  161     assertEquals(true, erase(a(0)))
  162     assertEquals(false, erase(a(1)))
  163   }
  164 
  165   @Test def Boolean_normal_case_noinline(): Unit = {
  166     val b = makeNoInline[Boolean]
  167     b += true
  168     b += someBoolean
  169     val a = b.result()
  170 
  171     assertSame(classOf[Array[Boolean]], a.getClass)
  172     assertEquals(2, a.length)
  173     assertTrue(erase(a(0)).isInstanceOf[Boolean])
  174     assertEquals(true, erase(a(0)))
  175     assertEquals(false, erase(a(1)))
  176   }
  177 
  178   @Test def Boolean_zeros_inline(): Unit = {
  179     val a = zerosInline[Boolean](3)
  180     assertSame(classOf[Array[Boolean]], a.getClass)
  181     assertEquals(3, a.length)
  182     assertTrue(erase(a(0)).isInstanceOf[Boolean])
  183     assertEquals(false, erase(a(0)))
  184   }
  185 
  186   @Test def Boolean_zeros_noinline(): Unit = {
  187     val a = zerosNoInline[Boolean](3)
  188     assertSame(classOf[Array[Boolean]], a.getClass)
  189     assertEquals(3, a.length)
  190     assertTrue(erase(a(0)).isInstanceOf[Boolean])
  191     assertEquals(false, erase(a(0)))
  192   }
  193 
  194   @Test def Unit_normal_case_inline(): Unit = {
  195     val b = ArrayBuilder.make[Unit]
  196     b += ()
  197     val a = b.result()
  198 
  199     assertSame(classOf[Array[Unit]], a.getClass)
  200     assertEquals(1, a.length)
  201     assertTrue(erase(a(0)).isInstanceOf[Unit])
  202     assertEquals((), erase(a(0)))
  203   }
  204 
  205   @Test def Unit_normal_case_noinline(): Unit = {
  206     val b = makeNoInline[Unit]
  207     b += ()
  208     val a = b.result()
  209 
  210     assertSame(classOf[Array[Unit]], a.getClass)
  211     assertEquals(1, a.length)
  212     assertTrue(erase(a(0)).isInstanceOf[Unit])
  213     assertEquals((), erase(a(0)))
  214   }
  215 
  216   @Test def Unit_zeros_inline(): Unit = {
  217     val a = zerosInline[Unit](3)
  218     assertSame(classOf[Array[Unit]], a.getClass)
  219     assertEquals(3, a.length)
  220     if (!executingInJVM) {
  221       assertTrue(erase(a(0)).isInstanceOf[Unit])
  222       assertTrue(() == erase(a(0)))
  223     }
  224   }
  225 
  226   @Test def Unit_zeros_noinline(): Unit = {
  227     val a = zerosNoInline[Unit](3)
  228     assertSame(classOf[Array[Unit]], a.getClass)
  229     assertEquals(3, a.length)
  230     if (!executingInJVM) {
  231       assertTrue(erase(a(0)).isInstanceOf[Unit])
  232       assertTrue(() == erase(a(0)))
  233     }
  234   }
  235 
  236   @Test def String_normal_case_inline(): Unit = {
  237     val b = ArrayBuilder.make[String]
  238     b += "hello"
  239     b += someString
  240     val a = b.result()
  241 
  242     assertSame(classOf[Array[String]], a.getClass)
  243     assertEquals(2, a.length)
  244     assertTrue(erase(a(0)).isInstanceOf[String])
  245     assertEquals("hello", erase(a(0)))
  246     assertEquals("world", erase(a(1)))
  247   }
  248 
  249   @Test def String_normal_case_noinline(): Unit = {
  250     val b = makeNoInline[String]
  251     b += "hello"
  252     b += someString
  253     val a = b.result()
  254 
  255     assertSame(classOf[Array[String]], a.getClass)
  256     assertEquals(2, a.length)
  257     assertTrue(erase(a(0)).isInstanceOf[String])
  258     assertEquals("hello", erase(a(0)))
  259     assertEquals("world", erase(a(1)))
  260   }
  261 
  262   @Test def String_zeros_inline(): Unit = {
  263     val a = zerosInline[String](3)
  264     assertSame(classOf[Array[String]], a.getClass)
  265     assertEquals(3, a.length)
  266     assertEquals(null, erase(a(0)))
  267   }
  268 
  269   @Test def String_zeros_noinline(): Unit = {
  270     val a = zerosNoInline[String](3)
  271     assertSame(classOf[Array[String]], a.getClass)
  272     assertEquals(3, a.length)
  273     assertEquals(null, erase(a(0)))
  274   }
  275 
  276   @Test def addAll(): Unit = {
  277     assumeFalse("Needs at least Scala 2.13",
  278         scalaVersion.startsWith("2.11.") ||
  279         scalaVersion.startsWith("2.12."))
  280 
  281     val b = ArrayBuilder.make[Int]
  282     val arr = Array[Int](1, 2, 3, 4, 5)
  283     b.addAll(arr, 3, 2)
  284     assertArrayEquals(Array[Int](4, 5), b.result())
  285   }
  286 }
  287 
  288 object ArrayBuilderTest {
  289   implicit class ArrayBuilderCompat[A](val __sef: ArrayBuilder[A]) extends AnyVal {
  290     def addAll(xs: Array[_ <: A], offset: Int, length: Int): Unit =
  291       throw new AssertionError("unreachable code")
  292   }
  293 }