"Fossies" - the Fresh Open Source Software Archive

Member "scala-js-1.3.1/test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/util/ArrayDequeTest.scala" (14 Nov 2020, 4658 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
   16 
   17 import org.junit.Test
   18 import org.junit.Assert._
   19 
   20 import java.{util => ju}
   21 
   22 import scala.reflect.ClassTag
   23 
   24 class ArrayDequeTest extends AbstractCollectionTest with DequeTest {
   25 
   26   override def factory: ArrayDequeFactory = new ArrayDequeFactory
   27 
   28   @Test def should_add_and_remove_head_and_last(): Unit = {
   29     val ad = factory.empty[Int]
   30 
   31     ad.addLast(1)
   32     ad.removeFirst()
   33     ad.addLast(2)
   34     assertEquals(ad.peekFirst(), 2)
   35 
   36     ad.clear()
   37 
   38     ad.addFirst(1)
   39     ad.removeLast()
   40     ad.addFirst(2)
   41     assertEquals(ad.peekLast(), 2)
   42   }
   43 
   44   @Test def could_be_instantiated_with_a_prepopulated_Collection(): Unit = {
   45     val l = TrivialImmutableCollection(1, 5, 2, 3, 4)
   46     val ad = factory.from[Int](l)
   47 
   48     assertEquals(ad.size(), 5)
   49 
   50     for (i <- 0 until l.size())
   51       assertEquals(ad.poll(), l(i))
   52 
   53     assertTrue(ad.isEmpty)
   54   }
   55 
   56   @Test def should_add_multiple_element_in_one_operation(): Unit = {
   57     val ad = factory.empty[Int]
   58 
   59     assertEquals(ad.size(), 0)
   60     ad.addAll(TrivialImmutableCollection(1, 5, 2, 3, 4))
   61     assertEquals(ad.size(), 5)
   62     ad.add(6)
   63     assertEquals(ad.size(), 6)
   64   }
   65 
   66   @Test def should_retrieve_last_element(): Unit = {
   67     val adInt = factory.empty[Int]
   68 
   69     assertTrue(adInt.add(1000))
   70     assertTrue(adInt.add(10))
   71     assertEquals(adInt.pollLast(), 10)
   72 
   73     val adString = factory.empty[String]
   74 
   75     assertTrue(adString.add("pluto"))
   76     assertTrue(adString.add("pippo"))
   77     assertEquals(adString.pollLast(), "pippo")
   78 
   79     val adDouble = factory.empty[Double]
   80 
   81     assertTrue(adDouble.add(+10000.987))
   82     assertTrue(adDouble.add(-0.987))
   83     assertEquals(adDouble.pollLast(), -0.987, 0.0)
   84   }
   85 
   86   @Test def should_perform_as_a_stack_with_push_and_pop(): Unit = {
   87     val adInt = factory.empty[Int]
   88 
   89     adInt.push(1000)
   90     adInt.push(10)
   91     assertEquals(adInt.pop(), 10)
   92     assertEquals(adInt.pop(), 1000)
   93     assertTrue(adInt.isEmpty())
   94 
   95     val adString = factory.empty[String]
   96 
   97     adString.push("pluto")
   98     adString.push("pippo")
   99     assertEquals(adString.pop(), "pippo")
  100     assertEquals(adString.pop(), "pluto")
  101     assertTrue(adString.isEmpty())
  102 
  103     val adDouble = factory.empty[Double]
  104 
  105     adDouble.push(+10000.987)
  106     adDouble.push(-0.987)
  107     assertEquals(adDouble.pop(), -0.987, 0.0)
  108     assertEquals(adDouble.pop(), +10000.987, 0.0)
  109     assertTrue(adString.isEmpty())
  110   }
  111 
  112   @Test def should_poll_and_peek_elements(): Unit = {
  113     val pq = factory.empty[String]
  114 
  115     assertTrue(pq.add("one"))
  116     assertTrue(pq.add("two"))
  117     assertTrue(pq.add("three"))
  118 
  119     assertTrue(pq.peek.equals("one"))
  120     assertTrue(pq.poll.equals("one"))
  121 
  122     assertTrue(pq.peekFirst.equals("two"))
  123     assertTrue(pq.pollFirst.equals("two"))
  124 
  125     assertTrue(pq.peekLast.equals("three"))
  126     assertTrue(pq.pollLast.equals("three"))
  127 
  128     assertNull(pq.peekFirst)
  129     assertNull(pq.pollFirst)
  130 
  131     assertNull(pq.peekLast)
  132     assertNull(pq.pollLast)
  133   }
  134 
  135   @Test def should_remove_occurrences_of_provided_elements(): Unit = {
  136     val ad = factory.from[String](
  137         TrivialImmutableCollection("one", "two", "three", "two", "one"))
  138 
  139     assertTrue(ad.removeFirstOccurrence("one"))
  140     assertTrue(ad.removeLastOccurrence("two"))
  141     assertTrue(ad.removeFirstOccurrence("one"))
  142     assertTrue(ad.removeLastOccurrence("two"))
  143     assertTrue(ad.removeFirstOccurrence("three"))
  144     assertFalse(ad.removeLastOccurrence("three"))
  145     assertTrue(ad.isEmpty)
  146   }
  147 
  148   @Test def should_iterate_over_elements_in_both_directions(): Unit = {
  149     val l = TrivialImmutableCollection("one", "two", "three")
  150     val ad = factory.from[String](l)
  151 
  152     val iter = ad.iterator()
  153     for (i <- 0 until l.size()) {
  154       assertTrue(iter.hasNext())
  155       assertEquals(iter.next(), l(i))
  156     }
  157     assertFalse(iter.hasNext())
  158 
  159     val diter = ad.descendingIterator()
  160     for (i <- (0 until l.size()).reverse) {
  161       assertTrue(diter.hasNext())
  162       assertEquals(diter.next(), l(i))
  163     }
  164     assertFalse(diter.hasNext())
  165   }
  166 }
  167 
  168 class ArrayDequeFactory extends AbstractCollectionFactory with DequeFactory {
  169   override def implementationName: String =
  170     "java.util.ArrayDeque"
  171 
  172   override def empty[E: ClassTag]: ju.ArrayDeque[E] =
  173     new ju.ArrayDeque[E]
  174 
  175   def from[E](coll: ju.Collection[E]): ju.ArrayDeque[E] =
  176     new ju.ArrayDeque[E](coll)
  177 
  178   override def allowsNullElement: Boolean = false
  179 }