"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.3.1/test-suite/js/src/test/scala-new-collections/org/scalajs/testsuite/library/ArrayOpsCollectionEraDependentTest.scala" (14 Nov 2020, 10872 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.

    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.library
   14 
   15 import scala.scalajs.js
   16 
   17 import org.junit.Assert._
   18 import org.junit.Test
   19 
   20 class ArrayOpsCollectionEraDependentTest {
   21   import ArrayOpsTest._
   22 
   23   @Test def knownSize(): Unit = {
   24     assertEquals(4, js.Array(1, 2, 5, 65).knownSize)
   25   }
   26 
   27   @Test def mapInPlace(): Unit = {
   28     val array = js.Array(1, 5, 7, 2, 54, 2, 78, 0, 3)
   29     assertJSArrayEqualsSame(array, js.Array(3, 15, 21, 6, 162, 6, 234, 0, 9), array.mapInPlace(_ * 3))
   30   }
   31 
   32   @Test def flatten(): Unit = {
   33     val array = js.Array(js.Array(6, 2), js.Array(56), js.Array(), js.Array(-1, 4, 2))
   34     assertJSArrayEqualsNotSame(array, js.Array(6, 2, 56, -1, 4, 2), array.flatten)
   35   }
   36 
   37   @Test def appended(): Unit = {
   38     val array = js.Array(1, 5, 7, 2)
   39     assertJSArrayEqualsNotSame(array, js.Array(1, 5, 7, 2, 10), array.appended(10))
   40   }
   41 
   42   @Test def prepended(): Unit = {
   43     val array = js.Array(1, 5, 7, 2)
   44     assertJSArrayEqualsNotSame(array, js.Array(10, 1, 5, 7, 2), array.prepended(10))
   45   }
   46 
   47   @Test def prependedAll(): Unit = {
   48     val array = js.Array(1, 5, 7, 2)
   49     assertJSArrayEqualsNotSame(array, js.Array(10, 11, 1, 5, 7, 2), array.prependedAll(js.Array(10, 11)))
   50     assertJSArrayEqualsNotSame(array, js.Array(10, 11, 1, 5, 7, 2), array.prependedAll(List(10, 11)))
   51   }
   52 
   53   @Test def :++(): Unit = {
   54     val array = js.Array(1, 5, 7, 2)
   55     assertJSArrayEqualsNotSame(array, js.Array(1, 5, 7, 2, 10, 11), array :++ js.Array(10, 11))
   56     assertJSArrayEqualsNotSame(array, js.Array(1, 5, 7, 2, 10, 11), array :++ List(10, 11))
   57   }
   58 
   59   @Test def appendedAll(): Unit = {
   60     val array = js.Array(1, 5, 7, 2)
   61     assertJSArrayEqualsNotSame(array, js.Array(1, 5, 7, 2, 10, 11), array.appendedAll(js.Array(10, 11)))
   62     assertJSArrayEqualsNotSame(array, js.Array(1, 5, 7, 2, 10, 11), array.appendedAll(List(10, 11)))
   63   }
   64 
   65   @Test def unzip(): Unit = {
   66     val array = js.Array(1 -> "foo", 5 -> "bar", 7 -> "baz")
   67     val unzipped = array.unzip // do not influence type inference
   68     val typed: (js.Array[Int], js.Array[String]) = unzipped
   69     assertJSArrayPairEquals((js.Array(1, 5, 7), js.Array("foo", "bar", "baz")), typed)
   70   }
   71 
   72   @Test def unzip3(): Unit = {
   73     val array = js.Array((1, "foo", Nil), (5, "bar", List(54)), (7, "baz", List(32, 12)))
   74     val unzipped = array.unzip3 // do not influence type inference
   75     val typed: (js.Array[Int], js.Array[String], js.Array[List[Int]]) = unzipped
   76     assertJSArrayEquals(js.Array(1, 5, 7), typed._1)
   77     assertJSArrayEquals(js.Array("foo", "bar", "baz"), typed._2)
   78     assertJSArrayEquals(js.Array(Nil, List(54), List(32, 12)), typed._3)
   79   }
   80 
   81   @Test def transpose(): Unit = {
   82     val array = js.Array(
   83         js.Array(1, 2, 3, 4),
   84         js.Array(5, 6, 7, 8),
   85         js.Array(9, 10, 11, 12))
   86     val transposed = array.transpose // do not influence type inference
   87     assertNotSame(array, transposed)
   88     val typed: js.Array[js.Array[Int]] = transposed
   89 
   90     assertEquals(4, typed.length)
   91     assertJSArrayEquals(js.Array(1, 5, 9), typed(0))
   92     assertJSArrayEquals(js.Array(2, 6, 10), typed(1))
   93     assertJSArrayEquals(js.Array(3, 7, 11), typed(2))
   94     assertJSArrayEquals(js.Array(4, 8, 12), typed(3))
   95 
   96     val emptyArray = js.Array[js.Array[Int]]()
   97     assertJSArrayEqualsNotSame(emptyArray, emptyArray, emptyArray.transpose)
   98   }
   99 
  100   @Test def distinctBy(): Unit = {
  101     val array = js.Array(5, 7, 1, 34, 7, 3, 5, 9, 9)
  102     assertJSArrayEqualsNotSame(array, js.Array(5, 7, 34), array.distinctBy(_ % 4))
  103   }
  104 
  105   @Test def groupMap(): Unit = {
  106     val array = js.Array("foo" -> 1, "bar" -> 5, "baz" -> 1, "foobar" -> 3,
  107         "hello" -> 7, "bonjour" -> 3)
  108     val groups = array.groupMap(_._2)(_._1)
  109     assertEquals(Set(1, 3, 5, 7), groups.keySet)
  110     assertJSArrayEquals(js.Array("foo", "baz"), groups(1))
  111     assertJSArrayEquals(js.Array("foobar", "bonjour"), groups(3))
  112     assertJSArrayEquals(js.Array("bar"), groups(5))
  113     assertJSArrayEquals(js.Array("hello"), groups(7))
  114   }
  115 
  116   @Test def addOne(): Unit = {
  117     val array = js.Array(2, 3, 5)
  118     assertJSArrayEqualsSame(array, js.Array(2, 3, 5, 13), array.addOne(13))
  119   }
  120 
  121   @Test def addAll(): Unit = {
  122     val array = js.Array(2, 3, 5)
  123     array.addAll(List(13, 21, 36))
  124     assertJSArrayEquals(js.Array(2, 3, 5, 13, 21, 36), array)
  125     array.addAll(js.Array(51, 0, 2))
  126     assertJSArrayEquals(js.Array(2, 3, 5, 13, 21, 36, 51, 0, 2), array)
  127   }
  128 
  129   @Test def subtractOne(): Unit = {
  130     val array = js.Array(2, 3, 5, 3, 24, 2)
  131     array.subtractOne(3)
  132     assertJSArrayEquals(js.Array(2, 5, 3, 24, 2), array)
  133     array.subtractOne(42)
  134     assertJSArrayEquals(js.Array(2, 5, 3, 24, 2), array)
  135   }
  136 
  137   @Test def subtractAll(): Unit = {
  138     val array = js.Array(2, 3, 5, 3, 24, 2)
  139     array.subtractAll(List(3, 24))
  140     assertJSArrayEquals(js.Array(2, 5, 3, 2), array)
  141     array.subtractAll(js.Array(5, 2, 2))
  142     assertJSArrayEquals(js.Array(3), array)
  143   }
  144 
  145   @Test def prepend(): Unit = {
  146     val array = js.Array(2, 3, 5)
  147     assertJSArrayEqualsSame(array, js.Array(13, 2, 3, 5), array.prepend(13))
  148   }
  149 
  150   @Test def patchInPlace(): Unit = {
  151     val arrayBase = js.Array(1, 5, 7, 2, 54, 2, 78, 0, 3)
  152     var array = arrayBase.jsSlice()
  153     assertJSArrayEqualsSame(array,
  154         js.Array(1, 5, 7, 0, 3),
  155         array.patchInPlace(3, Nil, 4))
  156     array = arrayBase.jsSlice()
  157     assertJSArrayEqualsSame(array,
  158         js.Array(1, 5, 7, 42, 34, 0, 3),
  159         array.patchInPlace(3, js.Array(42, 34), 4))
  160     array = arrayBase.jsSlice()
  161     assertJSArrayEqualsSame(array,
  162         js.Array(42, 34, 54, 2, 78, 0, 3),
  163         array.patchInPlace(-3, js.Array(42, 34), 4))
  164     array = arrayBase.jsSlice()
  165     assertJSArrayEqualsSame(array,
  166         js.Array(1, 5, 7, 2, 54, 2, 78, 0, 3, 42, 34),
  167         array.patchInPlace(17, js.Array(42, 34), 6))
  168     array = arrayBase.jsSlice()
  169     assertJSArrayEqualsSame(array,
  170         js.Array(1, 5, 7, 2, 42, 34, 54, 2, 78, 0, 3),
  171         array.patchInPlace(4, js.Array(42, 34), -6))
  172 
  173     /* Unclear what the semantics of the following one should be.
  174      * See https://github.com/scala/bug/issues/11114
  175      */
  176     /*array = arrayBase.jsSlice()
  177     assertJSArrayEqualsSame(array,
  178         js.Array(1, 5, 7, 2, 54, 2, 78, 42),
  179         array.patchInPlace(7, js.Array(42), 6))*/
  180   }
  181 
  182   @Test def takeInPlace(): Unit = {
  183     val array = js.Array(1, 5, 7, 2, 42, 34, 54, 2, 78, 0, 3)
  184     assertJSArrayEqualsSame(array, js.Array(1, 5, 7, 2, 42, 34, 54), array.takeInPlace(7))
  185     assertJSArrayEqualsSame(array, js.Array(1, 5, 7, 2), array.takeInPlace(4))
  186     assertJSArrayEqualsSame(array, js.Array(1, 5, 7, 2), array.takeInPlace(10))
  187     assertJSArrayEqualsSame(array, js.Array(), array.takeInPlace(-3))
  188   }
  189 
  190   @Test def dropInPlace(): Unit = {
  191     val array = js.Array(1, 5, 7, 2, 42, 34, 54, 2, 78, 0, 3)
  192     assertJSArrayEqualsSame(array, js.Array(5, 7, 2, 42, 34, 54, 2, 78, 0, 3), array.dropInPlace(1))
  193     assertJSArrayEqualsSame(array, js.Array(34, 54, 2, 78, 0, 3), array.dropInPlace(4))
  194     assertJSArrayEqualsSame(array, js.Array(34, 54, 2, 78, 0, 3), array.dropInPlace(-3))
  195     assertJSArrayEqualsSame(array, js.Array(), array.dropInPlace(10))
  196   }
  197 
  198   @Test def takeRightInPlace(): Unit = {
  199     val array = js.Array(1, 5, 7, 2, 42, 34, 54, 2, 78, 0, 3)
  200     assertJSArrayEqualsSame(array, js.Array(42, 34, 54, 2, 78, 0, 3), array.takeRightInPlace(7))
  201     assertJSArrayEqualsSame(array, js.Array(2, 78, 0, 3), array.takeRightInPlace(4))
  202     assertJSArrayEqualsSame(array, js.Array(2, 78, 0, 3), array.takeRightInPlace(10))
  203     assertJSArrayEqualsSame(array, js.Array(), array.takeRightInPlace(-3))
  204   }
  205 
  206   @Test def dropRightInPlace(): Unit = {
  207     val array = js.Array(1, 5, 7, 2, 42, 34, 54, 2, 78, 0, 3)
  208     assertJSArrayEqualsSame(array, js.Array(1, 5, 7, 2, 42, 34, 54, 2, 78, 0), array.dropRightInPlace(1))
  209     assertJSArrayEqualsSame(array, js.Array(1, 5, 7, 2, 42, 34), array.dropRightInPlace(4))
  210     assertJSArrayEqualsSame(array, js.Array(1, 5, 7, 2, 42, 34), array.dropRightInPlace(-3))
  211     assertJSArrayEqualsSame(array, js.Array(), array.dropRightInPlace(10))
  212   }
  213 
  214   @Test def sliceInPlace(): Unit = {
  215     val array = js.Array(1, 5, 7, 2, 42, 34, 54, 2, 78, 0, 3)
  216     assertJSArrayEqualsSame(array, js.Array(2, 42, 34, 54, 2, 78), array.sliceInPlace(3, 9))
  217     assertJSArrayEqualsSame(array, js.Array(2, 42, 34, 54, 2), array.sliceInPlace(-1, 5))
  218     assertJSArrayEqualsSame(array, js.Array(34, 54, 2), array.sliceInPlace(2, 10))
  219     assertJSArrayEqualsSame(array, js.Array(), array.sliceInPlace(2, 1))
  220   }
  221 
  222   @Test def dropWhileInPlace(): Unit = {
  223     val array = js.Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
  224     assertJSArrayEqualsSame(array, js.Array(4, 5, 6, 7, 8, 9, 10, 11, 12), array.dropWhileInPlace(_ < 4))
  225     assertJSArrayEqualsSame(array, js.Array(8, 9, 10, 11, 12), array.dropWhileInPlace(_ < 8))
  226     assertJSArrayEqualsSame(array, js.Array(8, 9, 10, 11, 12), array.dropWhileInPlace(_ < 5))
  227     assertJSArrayEqualsSame(array, js.Array(), array.dropWhileInPlace(_ < 20))
  228   }
  229 
  230   @Test def takeWhileInPlace(): Unit = {
  231     val array = js.Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
  232     assertJSArrayEqualsSame(array, js.Array(1, 2, 3, 4, 5, 6, 7, 8, 9), array.takeWhileInPlace(_ < 10))
  233     assertJSArrayEqualsSame(array, js.Array(1, 2, 3, 4), array.takeWhileInPlace(_ < 5))
  234     assertJSArrayEqualsSame(array, js.Array(1, 2, 3, 4), array.takeWhileInPlace(_ < 8))
  235     assertJSArrayEqualsSame(array, js.Array(), array.takeWhileInPlace(_ < 0))
  236   }
  237 
  238   @Test def padToInPlace(): Unit = {
  239     val array = js.Array(1, 5, 7, 2, 42)
  240     assertJSArrayEqualsSame(array, js.Array(1, 5, 7, 2, 42, 5, 5), array.padToInPlace(7, 5))
  241     assertJSArrayEqualsSame(array, js.Array(1, 5, 7, 2, 42, 5, 5), array.padToInPlace(3, 55))
  242     assertJSArrayEqualsSame(array, js.Array(1, 5, 7, 2, 42, 5, 5), array.padToInPlace(-2, 555))
  243   }
  244 
  245   @Test def flatMapInPlace(): Unit = {
  246     val array = js.Array(6, 2, 56, -1)
  247     assertJSArrayEqualsSame(array,
  248         js.Array(6, 7, 8, 2, 3, 4, 56, 57, 58, -1, 0, 1),
  249         array.flatMapInPlace(x => js.Array(x, x + 1, x + 2)))
  250   }
  251 
  252   @Test def filterInPlace(): Unit = {
  253     val array = js.Array(1, 5, 7, 2, 54, 2, 78, 0, 3)
  254     assertJSArrayEqualsSame(array,
  255         js.Array(1, 5, 7, 2, 54, 2, 78, 0, 3), array.filterInPlace(_ < 100))
  256     assertJSArrayEqualsSame(array,
  257         js.Array(1, 5, 7, 2, 2, 0, 3), array.filterInPlace(_ < 10))
  258     assertJSArrayEqualsSame(array,
  259         js.Array(), array.filterInPlace(_ < 0))
  260   }
  261 
  262   @Test def to_T_issue_843(): Unit = {
  263     val array = js.Array(1, 2, 1, 3, 1, 10, 9)
  264     val list = array.to(List)
  265     assertArrayEquals(array.toArray, list.toArray)
  266   }
  267 
  268 }