/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.eviction;

import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Unit tests for LFUQueue.
 *
 * @author Daniel Huang (dhuang@jboss.org)
 * @version $Revision: 1852 $
 */
public class LFUQueueTest extends TestCase
{
   private LFUQueue queue;

   public void setUp() throws Exception
   {
      super.setUp();
      queue = new LFUQueue();
   }

   public void tearDown() throws Exception
   {
      super.tearDown();
   }

   public void testQueue() throws Exception
   {
      NodeEntry ne;
      for (int i = 0; i < 500; i++)
      {
         ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
         queue.addNodeEntry(ne);
      }

      queue.resortEvictionQueue();

      assertEquals(500, queue.getNumberOfNodes());
      assertTrue(queue.containsNodeEntry(new NodeEntry("/a/b/c/250")));

      NodeEntry ne275 = queue.getNodeEntry("/a/b/c/275");
      assertEquals("/a/b/c/275", ne275.getFqn().toString());

      // now make sure the ordering is correct.
      Iterator it = queue.iterate();
      int k = 0;
      while (it.hasNext())
      {
         ne = (NodeEntry) it.next();
         assertEquals("/a/b/c/" + Integer.toString(k), ne.getFqn().toString());
         if (k % 2 == 0)
         {
            ne.setNumberOfNodeVisits(ne.getNumberOfNodeVisits() + 1);
         }
         k++;
      }

      queue.resortEvictionQueue();

      assertEquals("/a/b/c/1", queue.getFirstNodeEntry().getFqn().toString());

      // now check the sort order.
      it = queue.iterate();
      k = 0;
      while (it.hasNext())
      {
         ne = (NodeEntry) it.next();
         if (k < 250)
         {
            assertEquals(0, ne.getNumberOfNodeVisits());
         }
         else
         {
            assertEquals(1, ne.getNumberOfNodeVisits());
         }
         k++;
      }

      k = 0;
      while ((ne = queue.getFirstNodeEntry()) != null)
      {
         if (k == 250)
         {
            break;
         }
         queue.removeNodeEntry(ne);
         k++;
      }

      assertEquals(250, queue.getNumberOfNodes());

      assertFalse(queue.containsNodeEntry(new NodeEntry("/a/b/c/275")));
      assertNull(queue.getNodeEntry("/a/b/c/275"));

      for (int i = 0; i < 500; i++)
      {
         if (i % 2 == 0)
         {
            ne = queue.getNodeEntry("/a/b/c/" + Integer.toString(i));
            assertEquals(1, ne.getNumberOfNodeVisits());
            if (i > 250)
            {
               ne.setNumberOfNodeVisits(ne.getNumberOfNodeVisits() + 1);
            }
         }
      }

      queue.resortEvictionQueue();
      assertEquals(250, queue.getNumberOfNodes());

      k = 0;
      it = queue.iterate();
      while (it.hasNext())
      {
         ne = (NodeEntry) it.next();
         if (k <= 125)
         {
            assertEquals(1, ne.getNumberOfNodeVisits());
         }
         else
         {
            assertEquals(2, ne.getNumberOfNodeVisits());
         }
         k++;
      }

   }

   public void testPrune() throws Exception
   {
      for (int i = 0; i < 5000; i++)
      {
         queue.addNodeEntry(new NodeEntry("/a/b/c/" + Integer.toString(i)));
      }

      NodeEntry ne;
      Iterator it = queue.iterate();
      int i = 0;
      while (it.hasNext())
      {
         ne = (NodeEntry) it.next();
         if (i % 2 == 0)
         {
            queue.removeNodeEntry(ne);
         }
         i++;
      }

      assertEquals(2500, queue.getNumberOfNodes());

      Set removalQueue = queue.getRemovalQueue();
      List evictionList = queue.getEvictionList();

      assertEquals(2500, removalQueue.size());

      it = removalQueue.iterator();
      while (it.hasNext())
      {
         ne = (NodeEntry) it.next();
         int currentIndex = Integer.parseInt((String) ne.getFqn().get(3));
         assertEquals(0, currentIndex % 2);

         assertFalse(queue.containsNodeEntry(ne));
         assertNull(queue.getNodeEntry(ne.getFqn()));
         assertTrue(evictionList.contains(ne));
      }

      assertEquals(5000, evictionList.size());

      queue.prune();

      assertEquals(0, removalQueue.size());
      assertEquals(2500, evictionList.size());
   }

   public void testGetFirstNodeEntry() throws Exception
   {
      for (int i = 0; i < 500; i++)
      {
         NodeEntry ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
         queue.addNodeEntry(ne);
         if (i % 2 == 0)
         {
            ne.setNumberOfNodeVisits(2);
         }
      }

      queue.resortEvictionQueue();

      NodeEntry ne;
      int count = 0;
      while ((ne = queue.getFirstNodeEntry()) != null)
      {
         if (count < 250)
         {
            assertEquals(0, ne.getNumberOfNodeVisits());
         }
         else
         {
            assertEquals(2, ne.getNumberOfNodeVisits());
         }
         queue.removeNodeEntry(ne);
         count++;
      }

      assertEquals(0, queue.getNumberOfNodes());
   }


   public void xtestBinarySearch() throws Exception
   {
      Comparator comp = new LFUQueue.LFUComparator();
      ArrayList l = new ArrayList();
      Map m = new HashMap();
      for (int i = 0; i < 100000; i++)
      {
         NodeEntry ne = new NodeEntry("/a/b/c/foo_" + Integer.toString(i));
         m.put(ne.getFqn(), ne);
         l.add(ne);
      }

      Collections.sort(l, comp);

//      Random rand = new Random();
      // test for stability
      assertEquals(m.size(), l.size());

      for (int i = 99999; i >= 0; i--)
      {
         NodeEntry mapNe = (NodeEntry) m.get("/a/b/c/foo_" + Integer.toString(i));
         int searchIndex = Collections.binarySearch(l, mapNe, comp);
         assertTrue(searchIndex >= 0);
         NodeEntry mapNe2 = (NodeEntry) m.remove("/a/b/c/foo_" + Integer.toString(i));
         int searchIndex2 = Collections.binarySearch(l, mapNe2, comp);
         assertTrue(searchIndex2 >= 0);

         assertEquals(searchIndex, searchIndex2);

         // now make sure the same index is continously returned.
         for (int k = 0; k < 5; k++)
         {
            NodeEntry ne = (NodeEntry) l.get(searchIndex);
            searchIndex2 = Collections.binarySearch(l, ne, comp);
            assertEquals(searchIndex, searchIndex2);
         }

         l.remove(searchIndex);
      }

      assertEquals(0, m.size());
      assertEquals(0, l.size());
   }

   public void testNumElements() throws Exception
   {
      LFUQueue queue = new LFUQueue();

      NodeEntry ne = new NodeEntry("/a/b/c");
      ne.setNumberOfElements(50);
      queue.addNodeEntry(ne);

      assertEquals(50, queue.getNumberOfElements());
      assertEquals(1, queue.getNumberOfNodes());

      queue.removeNodeEntry(ne);
      assertEquals(0, queue.getNumberOfElements());

      for (int i = 0; i < 10; i++)
      {
         ne = new NodeEntry("/a/b/c/" + Integer.toString(i));
         ne.setNumberOfElements(i);
         queue.addNodeEntry(ne);
      }

      assertEquals(45, queue.getNumberOfElements());
      assertEquals(10, queue.getNumberOfNodes());

      ne = queue.getNodeEntry("/a/b/c/0");
      assertNotNull(ne);
      assertEquals(0, ne.getNumberOfElements());
      ne.setNumberOfElements(500);

      assertEquals(545, queue.getNumberOfElements());
      ne = queue.getNodeEntry("/a/b/c/0");
      assertEquals(500, ne.getNumberOfElements());

      queue.resortEvictionQueue();

      ne = queue.getNodeEntry("/a/b/c/1");
      assertNotNull(ne);
      assertEquals(1, ne.getNumberOfElements());

      queue.resortEvictionQueue();
      ne.setNumberOfElements(2);
      queue.resortEvictionQueue();
      assertEquals(546, queue.getNumberOfElements());

      queue.removeNodeEntry(ne);

      assertEquals(544, queue.getNumberOfElements());
      assertEquals(9, queue.getNumberOfNodes());

      queue.removeNodeEntry(queue.getNodeEntry("/a/b/c/0"));

      for (int i = 2; i < 10; i++)
      {
         ne = queue.getNodeEntry("/a/b/c/" + Integer.toString(i));
         assertEquals(i, ne.getNumberOfElements());
         queue.removeNodeEntry(ne);
      }

      assertEquals(0, queue.getNumberOfNodes());
      assertEquals(0, queue.getNumberOfElements());

   }

}