/*
 * Copyright 2016, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */
package com.yahoo.sketches.frequencies;

import static org.testng.Assert.*;

import java.util.Random;

import org.testng.Assert;
import org.testng.annotations.Test;

import gnu.trove.function.TLongFunction;
import gnu.trove.map.hash.TLongLongHashMap;
import gnu.trove.procedure.TLongLongProcedure;

public class ReversePurgeLongHashMapTest {

  @Test(expectedExceptions = IllegalArgumentException.class)
  public void checkgetInstanceString() {
    ReversePurgeLongHashMap.getInstance("");
  }
  
  @Test
  public void checkActiveNull() {
    ReversePurgeLongHashMap map = new ReversePurgeLongHashMap(4);
    assertNull(map.getActiveKeys());
    assertNull(map.getActiveValues());
  }
  
  @Test //test only what we use
  public void testHashMap() {
    int mapLength = 128;
    ReversePurgeLongHashMap hashMap = new ReversePurgeLongHashMap(mapLength);
    testHashMapAgainstTrove(hashMap, mapLength);
    println(hashMap.toString());
  }
  
  private void testHashMapAgainstTrove(ReversePurgeLongHashMap hashMap, int mapLength){
    Random random = new Random(); 
    random.setSeed(422);
    int valueRange = 35219;
    int keyRange = 11173;

    String s = String.format("Test: %s\n", hashMap.getClass().getSimpleName());
    println(s);
    
    // Trove is a the gold standard
    TLongLongHashMap trove = new TLongLongHashMap(mapLength);
    // Insert random keys and values
    for (int i=0; i<.75*mapLength;i++) {
      long key = random.nextInt(keyRange);
      long value = random.nextInt(valueRange);
      hashMap.adjustOrPutValue(key ,value);
      trove.adjustOrPutValue(key, value, value);
    }
    
    // remove a bunch of values
    long threshold = valueRange/2;
    
    hashMap.adjustAllValuesBy(-threshold);
    hashMap.keepOnlyPositiveCounts();
    
    trove.retainEntries(new GreaterThenThreshold(threshold));
    trove.transformValues(new decreaseByThreshold(threshold));
    
    long[] keys = hashMap.getActiveKeys();
    long[] values = hashMap.getActiveValues();
    int size = hashMap.getNumActive();
    
    // map is of the correct size
    Assert.assertEquals(trove.size(), size);
    // keys and values of the same correct length
    Assert.assertEquals(size, keys.length);
    Assert.assertEquals(size, values.length);
    
    // All the keys and values are correct
    for (int i=0;i<size;i++){
      Assert.assertTrue(trove.containsKey(keys[i]));
      Assert.assertEquals(trove.get(keys[i]), values[i]);
      // Testing the get function
      Assert.assertEquals(values[i], hashMap.get(keys[i]));
    }
  }
  
  private class GreaterThenThreshold implements TLongLongProcedure {
    long threshold;
    public GreaterThenThreshold(long threshold){
      this.threshold = threshold;
    }
    
    @Override
    public boolean execute(long key, long value) {
      return (value > threshold);
    }
  }
  
  private class decreaseByThreshold implements TLongFunction {
    long threshold;
    public decreaseByThreshold(long threshold){
      this.threshold = threshold;
    }
    
    @Override
    public long execute(long value) {
      return value - threshold;
    }    
  }
  
  @Test
  public void printlnTest() {
    println("PRINTING: " + this.getClass().getName());
  }

  /**
   * @param s value to print
   */
  static void println(String s) {
    //System.out.println(s); //disable here
  }
  
}

@@ -211,231 +211,55 @@
 ert.
-*;%0A%0Aimport java.util.Random;%0A%0Aimport org.testng.Assert;%0Aimport org.testng.annotations.Test;%0A%0Aimport gnu.trove.function.TLongFunction;%0Aimport gnu.trove.map.hash.TLongLongHashMap;%0Aimport gnu.trove.procedure.TLongLongProcedure
+assertNull;%0A%0Aimport org.testng.annotations.Test
 ;%0A%0Ap
@@ -448,26 +448,24 @@
 ce(%22%22);%0A  %7D%0A
-  
 %0A  @Test%0A  p
@@ -645,2670 +645,7 @@
   %7D%0A
-  %0A  @Test //test only what we use%0A  public void testHashMap() %7B%0A    int mapLength = 128;%0A    ReversePurgeLongHashMap hashMap = new ReversePurgeLongHashMap(mapLength);%0A    testHashMapAgainstTrove(hashMap, mapLength);%0A    println(hashMap.toString());%0A  %7D%0A  %0A  private void testHashMapAgainstTrove(ReversePurgeLongHashMap hashMap, int mapLength)%7B%0A    Random random = new Random(); %0A    random.setSeed(422);%0A    int valueRange = 35219;%0A    int keyRange = 11173;%0A%0A    String s = String.format(%22Test: %25s%5Cn%22, hashMap.getClass().getSimpleName());%0A    println(s);%0A    %0A    // Trove is a the gold standard%0A    TLongLongHashMap trove = new TLongLongHashMap(mapLength);%0A    // Insert random keys and values%0A    for (int i=0; i%3C.75*mapLength;i++) %7B%0A      long key = random.nextInt(keyRange);%0A      long value = random.nextInt(valueRange);%0A      hashMap.adjustOrPutValue(key ,value);%0A      trove.adjustOrPutValue(key, value, value);%0A    %7D%0A    %0A    // remove a bunch of values%0A    long threshold = valueRange/2;%0A    %0A    hashMap.adjustAllValuesBy(-threshold);%0A    hashMap.keepOnlyPositiveCounts();%0A    %0A    trove.retainEntries(new GreaterThenThreshold(threshold));%0A    trove.transformValues(new decreaseByThreshold(threshold));%0A    %0A    long%5B%5D keys = hashMap.getActiveKeys();%0A    long%5B%5D values = hashMap.getActiveValues();%0A    int size = hashMap.getNumActive();%0A    %0A    // map is of the correct size%0A    Assert.assertEquals(trove.size(), size);%0A    // keys and values of the same correct length%0A    Assert.assertEquals(size, keys.length);%0A    Assert.assertEquals(size, values.length);%0A    %0A    // All the keys and values are correct%0A    for (int i=0;i%3Csize;i++)%7B%0A      Assert.assertTrue(trove.containsKey(keys%5Bi%5D));%0A      Assert.assertEquals(trove.get(keys%5Bi%5D), values%5Bi%5D);%0A      // Testing the get function%0A      Assert.assertEquals(values%5Bi%5D, hashMap.get(keys%5Bi%5D));%0A    %7D%0A  %7D%0A  %0A  private class GreaterThenThreshold implements TLongLongProcedure %7B%0A    long threshold;%0A    public GreaterThenThreshold(long threshold)%7B%0A      this.threshold = threshold;%0A    %7D%0A    %0A    @Override%0A    public boolean execute(long key, long value) %7B%0A      return (value %3E threshold);%0A    %7D%0A  %7D%0A  %0A  private class decreaseByThreshold implements TLongFunction %7B%0A    long threshold;%0A    public decreaseByThreshold(long threshold)%7B%0A      this.threshold = threshold;%0A    %7D%0A    %0A    @Override%0A    public long execute(long value) %7B%0A      return value - threshold;%0A    %7D    %0A  %7D%0A  %0A  @Test%0A  public void printlnTest() %7B%0A    println(%22PRINTING: %22 + this.getClass().getName());%0A  %7D%0A%0A  /**%0A   * @param s value to print%0A   */%0A  static void println(String s) %7B%0A    //System.out.println(s); //disable here%0A  %7D%0A  
 %0A%7D%0A

Remove Trove