package io.marmot.laboratory.algorithm;

import com.google.common.collect.MinMaxPriorityQueue;
import org.junit.Before;
import org.junit.Test;

import java.util.Comparator;
import java.util.Random;

/**
 * Test MaxMinHeap, meanwhile a sample show how to use MinMaxPriorityQueue
 * as a Min Or Max heap.
 */

public class MaxMinHeapTest {
  MinMaxPriorityQueue<Integer> heap;
  @Before
  public void setUp() {
    heap = MinMaxPriorityQueue.orderedBy(new Comparator<Integer>() {
      @Override
      public int compare(Integer i1, Integer i2) {
        return i1.compareTo(i2);
      }
    }).maximumSize(12 + 1).create();
    /*
    heap.add(4);
    heap.add(8);
    heap.add(2);
    heap.add(13);
    heap.add(82);
    heap.add(12);
    heap.add(45);
    heap.add(63);
    heap.add(33);
    heap.add(46);
    heap.add(46);
    */
    Random r = new Random();
    for (int i = 0; i < 12; ++i) {
      int x = Math.abs(r.nextInt());
      System.out.println("Data coming: " + x);
      heap.add(x);
    }

    for (int i = 0; i < 512; ++i) {
      int x = Math.abs(r.nextInt());
      System.out.println("Data coming: " + x);
      int y = heap.peekFirst();
      if (x > y) {
        heap.add(x);
        System.out.println("Data popping: " + heap.removeFirst());
      }

    }
    boolean b = r.nextBoolean();
    System.out.println(b);
  }

  @Test
  public void test() {
    /*
    Assert.assertEquals(heap.removeFirst(), (Integer) 82);
    Assert.assertEquals(heap.removeFirst(), (Integer) 63);
    Assert.assertEquals(heap.removeFirst(), (Integer) 46);
    Assert.assertEquals(heap.removeFirst(), (Integer) 46);
    */
  }

}

@@ -88,16 +88,41 @@
 yQueue;%0A
+import org.junit.Assert;%0A
 import o
@@ -162,16 +162,74 @@
 .Test;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0A
 import j
@@ -249,16 +249,39 @@
 arator;%0A
+import java.util.List;%0A
 import j
@@ -437,16 +437,29 @@
 pTest %7B%0A
+  // Top 12.%0A
   MinMax
@@ -723,12 +723,8 @@
 e(12
- + 1
 ).cr
@@ -735,216 +735,145 @@
 ();%0A
+%0A
   
-  /*%0A    heap.add(4);%0A    heap.add(8);%0A    heap.add(2);%0A    heap.add(13);%0A    heap.add(82);%0A    heap.add(12);%0A    heap.add(45);%0A    heap.add(63);%0A    heap.add(33);%0A    heap.add(46);%0A    heap.add(46);%0A    */
+%7D%0A%0A  @Test%0A  @SuppressWarnings(%22unchecked%22)%0A  public void test() %7B%0A    // fix random seed.%0A    List list = new ArrayList%3C%3E(12 + 1024);
 %0A   
@@ -895,16 +895,17 @@
  Random(
+1
 );%0A    f
@@ -936,33 +936,35 @@
 +i) %7B%0A      int 
-x
+ele
  = Math.abs(r.ne
@@ -967,32 +967,38 @@
 r.nextInt())
+ %25 512
 ;%0A      
 System.out.p
@@ -989,46 +989,20 @@
     
-System.out.println(%22Data coming: %22 + x
+heap.add(ele
 );%0A 
@@ -1002,34 +1002,39 @@
 ele);%0A      
-heap
+list
 .add(
-x
+i, ele
 );%0A    %7D%0A%0A  
@@ -1055,19 +1055,20 @@
  0; i %3C 
-512
+1024
 ; ++i) %7B
@@ -1078,17 +1078,19 @@
     int 
-x
+ele
  = Math.
@@ -1109,55 +1109,14 @@
 t())
-;%0A      System.out.println(%22Data coming: %22 + x)
+ %25 512
 ;%0A  
@@ -1124,14 +1124,16 @@
    i
-nt y =
+f (ele %3E
  hea
@@ -1149,28 +1149,39 @@
 st()
-;
+) %7B
 %0A      
-if (x %3E y) %7B
+  heap.removeFirst();
 %0A   
@@ -1194,17 +1194,19 @@
 eap.add(
-x
+ele
 );%0A     
@@ -1210,364 +1210,239 @@
     
-  System.out.println(%22Data popping: %22 + heap.removeFirst()
+%7D%0A      list.add(i + 12, ele
 );%0A    
-  
 %7D%0A%0A    
-%7D%0A    boolean b = r.nextBoolean();%0A    System.out.println(b);%0A  %7D%0A%0A  @Test%0A  public void test() %7B%0A    /*%0A    Assert.assertEquals(heap.removeFirst(), (Integer) 82);%0A    Assert.assertEquals(heap.removeFirst(), (Integer) 63);%0A    Assert.assertEquals(heap.removeFirst(), (Integer) 46);%0A
+Collections.sort(list);%0A    // The max value not in the heap.%0A    Integer max = (Integer) list.get(1024 - 1);%0A    Assert.assertEquals(heap.size(), 12);%0A    for (int i = 0; i %3C 12; ++i) %7B%0A  
     
@@ -1450,31 +1450,36 @@
 ssert.assert
-Equals(
+True(max %3C= 
 heap.removeF
@@ -1488,35 +1488,19 @@
 st()
-, (Integer) 46
 );%0A    
-*/
+%7D
 %0A  %7D%0A
-%0A
 %7D%0A

Rewrite Max min heap test.