brainuser5705

testing heap junit

Jun 17th, 2022 (edited)
27
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None
  1. package Heaps;
  2.  
  3. import org.junit.jupiter.api.Test;
  4. import static org.junit.jupiter.api.Assertions.*;
  5.  
  6. import java.util.Arrays;
  7.  
  8. /**
  9.  * JUnit Tests for {@see HeapArray}
  10.  * Testing is done by checking if heap removals will create a sorted array.
  11.  * @author Ashley Liew
  12.  */
  13. public class TestHeapArray {
  14.  
  15.     private final int NUM_ELEMENTS = 100;
  16.     private final int BOUND = 100;
  17.  
  18.     @Test
  19.     public void testMinHeap(){
  20.         HeapArray<Integer> minHeap = new HeapArray<>(true);
  21.         int[] values = new int[NUM_ELEMENTS];
  22.  
  23.         for (int i = 0; i < NUM_ELEMENTS; i++){
  24.             int randomValue = (int) (Math.random() * BOUND) + 0;
  25.             values[i] = randomValue;
  26.             minHeap.insert(randomValue);
  27.         }
  28.  
  29.         Arrays.sort(values);
  30.        
  31.         int[] result = new int[NUM_ELEMENTS];
  32.         for (int i = 0; i < NUM_ELEMENTS; i++){
  33.             result[i] = minHeap.remove();
  34.         }
  35.  
  36.         assertTrue(Arrays.compare(values, result) == 0);
  37.     }
  38.  
  39.     @Test
  40.     public void testMaxHeap(){
  41.         HeapArray<Integer> maxHeap = new HeapArray<>(false);
  42.         int[] values = new int[NUM_ELEMENTS];
  43.  
  44.         for (int i = 0; i < NUM_ELEMENTS; i++){
  45.             int randomValue = (int) (Math.random() * BOUND) + 0;
  46.             values[i] = randomValue;
  47.             maxHeap.insert(randomValue);
  48.         }
  49.  
  50.         Arrays.sort(values);
  51.        
  52.         int[] result = new int[NUM_ELEMENTS];
  53.         for (int i = 0; i < NUM_ELEMENTS; i++){
  54.             result[(NUM_ELEMENTS - 1) - i] = maxHeap.remove();
  55.         }
  56.  
  57.         assertTrue(Arrays.compare(values, result) == 0);
  58.     }
  59.  
  60. }
RAW Paste Data Copied