Пример #1
0
  @Test
  public void shouldBehaveExactlyLikeAnotherPriorityQueue() {
    for (int i = 0; i < 10; i++) {
      final Random random = getRandom(-1);

      final java.util.PriorityQueue<Integer> mutablePriorityQueue = new java.util.PriorityQueue<>();
      javaslang.collection.PriorityQueue<Integer> functionalPriorityQueue =
          javaslang.collection.PriorityQueue.empty();

      final int size = 100_000;
      for (int j = 0; j < size; j++) {
        /* Insert */
        if (random.nextInt() % 3 == 0) {
          assertMinimumsAreEqual(mutablePriorityQueue, functionalPriorityQueue);

          final int value = random.nextInt(size) - (size / 2);
          mutablePriorityQueue.add(value);
          functionalPriorityQueue = functionalPriorityQueue.enqueue(value);
        }

        assertMinimumsAreEqual(mutablePriorityQueue, functionalPriorityQueue);

        /* Delete */
        if (random.nextInt() % 5 == 0) {
          if (!mutablePriorityQueue.isEmpty()) {
            mutablePriorityQueue.poll();
          }
          if (!functionalPriorityQueue.isEmpty()) {
            functionalPriorityQueue = functionalPriorityQueue.tail();
          }

          assertMinimumsAreEqual(mutablePriorityQueue, functionalPriorityQueue);
        }
      }

      final Collection<Integer> oldValues =
          mutablePriorityQueue.stream().sorted().collect(toList());
      final Collection<Integer> newValues = functionalPriorityQueue.toJavaList();
      assertThat(oldValues).isEqualTo(newValues);
    }
  }