"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.3.1/test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/util/AbstractListTest.scala" (14 Nov 2020, 2064 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.javalib.util
   14 
   15 import java.{util => ju}
   16 
   17 import scala.reflect.ClassTag
   18 
   19 class AbstractListTest extends AbstractCollectionTest with ListTest {
   20   override def factory: AbstractListFactory = new AbstractListFactory
   21 }
   22 
   23 class AbstractListFactory extends AbstractCollectionFactory with ListFactory {
   24 
   25   override def implementationName: String =
   26     "java.util.AbstractList"
   27 
   28   override def empty[E: ClassTag]: ju.AbstractList[E] = {
   29     // inefficient but simple for debugging implementation of AbstractList
   30     new ju.AbstractList[E] {
   31 
   32       private var inner = scala.collection.immutable.List.empty[E]
   33 
   34       override def get(index: Int): E = {
   35         checkIndexInBounds(index)
   36         inner(index)
   37       }
   38 
   39       override def size(): Int =
   40         inner.size
   41 
   42       override def add(index: Int, element: E): Unit = {
   43         checkIndexOnBounds(index)
   44         val (left, right) = inner.splitAt(index)
   45         inner = left ::: element :: right
   46       }
   47 
   48       override def set(index: Int, element: E): E = {
   49         checkIndexInBounds(index)
   50         val (left, right) = inner.splitAt(index)
   51         inner = left ::: element :: right.tail
   52         right.head
   53       }
   54 
   55       override def remove(index: Int): E = {
   56         checkIndexInBounds(index)
   57         val (left, right) = inner.splitAt(index)
   58         inner = left ::: right.tail
   59         right.head
   60       }
   61 
   62       override def clear(): Unit =
   63         inner = Nil
   64 
   65       private def checkIndexInBounds(index: Int): Unit = {
   66         if (index < 0 || index >= size)
   67           throw new IndexOutOfBoundsException(index.toString)
   68       }
   69 
   70       private def checkIndexOnBounds(index: Int): Unit = {
   71         if (index < 0 || index > size)
   72           throw new IndexOutOfBoundsException(index.toString)
   73       }
   74     }
   75   }
   76 
   77 }