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

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.cache.Fqn;

import java.util.Iterator;

/**
 * Unit test for LFUAlgorithm.
 *
 * @author Daniel Huang - dhuang@jboss.org - 10/2005
 * @version $Revision: 1.3 $
 */
public class LFUAlgorithmTest extends TestCase
{

   RegionManager regionManager;
   LFUAlgorithm algo;

   public LFUAlgorithmTest(String s)
   {
      super(s);
   }

   public void setUp() throws Exception
   {
      super.setUp();

      algo = new LFUAlgorithm();
      LFUConfiguration config = new LFUConfiguration();
      regionManager = new RegionManager();
      regionManager.createRegion("/a/b", new DummyEvictionPolicy(), config);
   }

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

   public void testMaxNode1()
   {
      Fqn fqn1 = Fqn.fromString("/a/b/c");
      Fqn fqn2 = Fqn.fromString("/a/b/d");
      Region region = regionManager.getRegion("/a/b");
      LFUConfiguration config = (LFUConfiguration) region.getEvictionConfiguration();
      config.setMaxNodes(0);
      config.setMinNodes(20);
      region.setAddedNode(fqn1);
      region.setAddedNode(fqn2);

      try
      {
         algo.process(region);
      }
      catch (EvictionException e)
      {
         fail("testMaxNode: process failed " + e);
         e.printStackTrace();
      }
      assertEquals("Queue size should be ", 2, algo.getEvictionQueue().getNumberOfNodes());

   }

   public void testMaxNode2()
   {
      Fqn fqn1 = Fqn.fromString("/a/b/c");
      Fqn fqn2 = Fqn.fromString("/a/b/d");
      Fqn fqn3 = Fqn.fromString("/a/b/e");
      Region region = regionManager.getRegion("/a/b");
      LFUConfiguration config = (LFUConfiguration) region.getEvictionConfiguration();

      config.setMaxNodes(1);
      config.setMinNodes(20);
      region.setAddedNode(fqn1);
      region.setAddedNode(fqn2);

      try
      {
         algo.process(region);
      }
      catch (EvictionException e)
      {
         fail("testMaxNode: process failed " + e);
         e.printStackTrace();
      }
      assertEquals("Queue size should be ", 1, algo.getEvictionQueue().getNumberOfNodes());

      region.setAddedNode(fqn2);
      region.setAddedNode(fqn3);

      try
      {
         algo.process(region);
      }
      catch (EvictionException e)
      {
         fail("testMaxNode: process failed " + e);
         e.printStackTrace();
      }
      assertEquals("Queue size should be ", 1, algo.getEvictionQueue().getNumberOfNodes());
   }

   public void testMaxNode3() throws Exception
   {
      Region region = regionManager.getRegion("/a/b");
      LFUConfiguration config = (LFUConfiguration) region.getEvictionConfiguration();

      config.setMaxNodes(15000);
      config.setMinNodes(15000);
      for (int i = 0; i < 20000; i++)
      {
         Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
         region.setAddedNode(fqn);
         if ((i % 2) == 0)
         {
            region.setVisitedNode(fqn);
         }
      }

      algo.process(region);
//      LFUQueue queue = (LFUQueue) algo.evictionQueue;
//      Iterator it = queue.iterate();

   }

   public void testMinNode1() throws Exception
   {
      Fqn fqn1 = Fqn.fromString("/a/b/c");
      Fqn fqn2 = Fqn.fromString("/a/b/c/d");
      Fqn fqn3 = Fqn.fromString("/a/b/c/d/e");
      Fqn fqn4 = Fqn.fromString("/a/b/c/d/e/f");

      Region region = regionManager.getRegion("/a/b");
      LFUConfiguration config = (LFUConfiguration) region.getEvictionConfiguration();

      config.setMaxNodes(0);
      config.setMinNodes(2);

      region.setAddedNode(fqn1);
      region.setAddedNode(fqn2);
      region.setAddedNode(fqn3);
      region.setAddedNode(fqn4);

      algo.process(region);

      assertEquals("Queue size should be ", 2, algo.getEvictionQueue().getNumberOfNodes());
   }

   public void testMinNode2() throws Exception
   {
      Fqn fqn1 = Fqn.fromString("/a/b/c");
      Fqn fqn2 = Fqn.fromString("/a/b/d");

      Region region = regionManager.getRegion("/a/b");
      LFUConfiguration config = (LFUConfiguration) region.getEvictionConfiguration();

      config.setMaxNodes(0);
      config.setMinNodes(0);

      region.setAddedNode(fqn1);
      region.setAddedNode(fqn2);

      algo.process(region);

      assertEquals("Queue size should be ", 0, algo.getEvictionQueue().getNumberOfNodes());
   }

   public void testEvictionQueueSortOrder1() throws Exception
   {
      Fqn fqn1 = Fqn.fromString("/a/b/c");
      Fqn fqn2 = Fqn.fromString("/a/b/c/d");
      Fqn fqn3 = Fqn.fromString("/a/b/c/d/e");
      Fqn fqn4 = Fqn.fromString("/a/b/c/d/e/f");
      Fqn fqn5 = Fqn.fromString("/a/b/c/d/e/f/g/h");
      Fqn fqn6 = Fqn.fromString("/a/b/c/d/e/f/g/h/i");
      Fqn fqn7 = Fqn.fromString("/a/b/c/d/e/f/g/h/i/j");
      Fqn fqn8 = Fqn.fromString("/a/b/c/d/e/f/g/h/i/j/k");
      Fqn fqn9 = Fqn.fromString("/a/b/c/d/e/f/g/h/i/j/k/l");
      Fqn fqn10 = Fqn.fromString("/a/b/c/d/e/f/g/h/i/j/k/l/m");


      Region region = regionManager.getRegion("/a/b");
      LFUConfiguration config = (LFUConfiguration) region.getEvictionConfiguration();
      config.setMaxNodes(0);
      config.setMinNodes(100);

      region.setAddedNode(fqn1);
      region.setAddedNode(fqn2);
      region.setAddedNode(fqn3);
      region.setAddedNode(fqn4);
      region.setAddedNode(fqn5);
      region.setAddedNode(fqn6);
      region.setAddedNode(fqn7);
      region.setAddedNode(fqn8);
      region.setAddedNode(fqn9);
      region.setAddedNode(fqn10);

      algo.process(region);
      LFUQueue queue = (LFUQueue) algo.evictionQueue;
      assertEquals(10, algo.getEvictionQueue().getNumberOfNodes());
      Iterator it = queue.iterate();

      while (it.hasNext())
      {
         NodeEntry ne = (NodeEntry) it.next();
         System.out.println("Fqn: " + ne.getFqn() + " NodeVisits: " + ne.getNumberOfNodeVisits());
         assertEquals(1, ne.getNumberOfNodeVisits());
      }

      // fqn1 visited 4 additional times.
      region.setVisitedNode(fqn1);
      region.setVisitedNode(fqn1);
      region.setVisitedNode(fqn1);
      region.setVisitedNode(fqn1);

      // fqn2 visited 3 additional times.
      region.setVisitedNode(fqn2);
      region.setVisitedNode(fqn2);
      region.setVisitedNode(fqn2);

      // fqn3 visited 1 additional time.
      region.setVisitedNode(fqn3);

      // fqn4 visited 2 additional times.
      region.setVisitedNode(fqn4);
      region.setVisitedNode(fqn4);

      // fqn9 visited 1 additional time.
      region.setVisitedNode(fqn9);

      // fqn10 visited 2 additional times.
      region.setVisitedNode(fqn10);
      region.setVisitedNode(fqn10);

      algo.process(region);
      System.out.println();
      System.out.println();

      it = queue.iterate();
      int count = 0;
      while (it.hasNext())
      {
         count++;
         NodeEntry ne = (NodeEntry) it.next();
         System.out.println("Fqn: " + ne.getFqn() + " NodeVisits: " + ne.getNumberOfNodeVisits());
         if (count == 5 || count == 6)
         {
            assertEquals(2, ne.getNumberOfNodeVisits());
         }
         else if (count == 7 || count == 8)
         {
            assertEquals(3, ne.getNumberOfNodeVisits());
         }
         else if (count == 9)
         {
            assertEquals(4, ne.getNumberOfNodeVisits());
         }
         else if (count == 10)
         {
            assertEquals(5, ne.getNumberOfNodeVisits());
         }
         else
         {
            assertEquals(1, ne.getNumberOfNodeVisits());
         }
      }


      assertEquals(10, algo.getEvictionQueue().getNumberOfNodes());

      Fqn fqn11 = Fqn.fromString("/a");
      Fqn fqn12 = Fqn.fromString("/a/b");

      region.setAddedNode(fqn11);
      region.setAddedNode(fqn12);

      algo.process(region);
      System.out.println();
      System.out.println();

      it = queue.iterate();
      count = 0;
      while (it.hasNext())
      {
         count++;
         NodeEntry ne = (NodeEntry) it.next();
         System.out.println("Fqn: " + ne.getFqn() + " NodeVisits: " + ne.getNumberOfNodeVisits());
         if (count == 7 || count == 8)
         {
            assertEquals(2, ne.getNumberOfNodeVisits());
         }
         else if (count == 9 || count == 10)
         {
            assertEquals(3, ne.getNumberOfNodeVisits());
         }
         else if (count == 11)
         {
            assertEquals(4, ne.getNumberOfNodeVisits());
         }
         else if (count == 12)
         {
            assertEquals(5, ne.getNumberOfNodeVisits());
         }
         else
         {
            assertEquals(1, ne.getNumberOfNodeVisits());
         }
      }

      assertEquals(12, algo.getEvictionQueue().getNumberOfNodes());

      region.setRemovedNode(fqn1);
      region.setRemovedNode(fqn11);
      region.setRemovedNode(fqn12);
      region.setRemovedNode(fqn10);

      algo.process(region);

      System.out.println();
      System.out.println();
      it = queue.iterate();
      count = 0;
      while (it.hasNext())
      {
         count++;
         NodeEntry ne = (NodeEntry) it.next();
         System.out.println("Fqn: " + ne.getFqn() + " NodeVisits: " + ne.getNumberOfNodeVisits());
         if (count == 5 || count == 6)
         {
            assertEquals(2, ne.getNumberOfNodeVisits());
         }
         else if (count == 7)
         {
            assertEquals(3, ne.getNumberOfNodeVisits());
         }
         else if (count == 8)
         {
            assertEquals(4, ne.getNumberOfNodeVisits());
         }
         else
         {
            assertEquals(1, ne.getNumberOfNodeVisits());
         }
      }

      assertEquals(8, algo.getEvictionQueue().getNumberOfNodes());

      //test add/visit/remove combination
      region.setAddedNode(fqn11);
      region.setVisitedNode(fqn11);
      region.setVisitedNode(fqn11);
      region.setVisitedNode(fqn11);
      region.setVisitedNode(fqn11);
      region.setVisitedNode(fqn11);
      region.setVisitedNode(fqn4);

      // purposefully revisit a node that has been removed. assert that it is readded.
      region.setVisitedNode(fqn1);
      region.setVisitedNode(fqn1);

      region.setRemovedNode(fqn3);

      algo.process(region);

      System.out.println();
      System.out.println();
      it = queue.iterate();
      count = 0;
      while (it.hasNext())
      {
         count++;
         NodeEntry ne = (NodeEntry) it.next();
         System.out.println("Fqn: " + ne.getFqn() + " NodeVisits: " + ne.getNumberOfNodeVisits());
         if (count == 5 || count == 6)
         {
            assertEquals(2, ne.getNumberOfNodeVisits());
         }
         else if (count == 7 || count == 8)
         {
            assertEquals(4, ne.getNumberOfNodeVisits());
         }
         else if (count == 9)
         {
            assertEquals(6, ne.getNumberOfNodeVisits());
         }
         else
         {
            assertEquals(1, ne.getNumberOfNodeVisits());
         }
      }
      assertEquals(9, algo.getEvictionQueue().getNumberOfNodes());
   }

   public void testEvictionQueueSortOrder2() throws Exception
   {
      Region region = regionManager.getRegion("/a/b");
      LFUConfiguration config = (LFUConfiguration) region.getEvictionConfiguration();

      config.setMaxNodes(0);
      config.setMinNodes(10000);
      for (int i = 0; i < 10000; i++)
      {
         Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
         region.setAddedNode(fqn);
      }

      algo.process(region);
      LFUQueue queue = (LFUQueue) algo.evictionQueue;
      Iterator it = queue.iterate();

      long lastModifiedTimestamp = 0;
      while (it.hasNext())
      {
         NodeEntry ne = (NodeEntry) it.next();
         assertTrue(lastModifiedTimestamp <= ne.getModifiedTimeStamp());
         lastModifiedTimestamp = ne.getModifiedTimeStamp();
      }

      for (int i = 0; i < 10000; i++)
      {
         if ((i % 2) == 0)
         {
            Fqn fqn = Fqn.fromString("/a/b/" + Integer.toString(i));
            region.setVisitedNode(fqn);
         }
      }

      algo.process(region);

      it = queue.iterate();
      int count = 0;
      lastModifiedTimestamp = 0;
      while (it.hasNext())
      {
         NodeEntry ne = (NodeEntry) it.next();
         assertTrue(lastModifiedTimestamp <= ne.getModifiedTimeStamp());
         lastModifiedTimestamp = ne.getModifiedTimeStamp();

         if (count < 5000)
         {
            assertEquals(1, ne.getNumberOfNodeVisits());
         }
         else
         {
            assertEquals(2, ne.getNumberOfNodeVisits());
         }
         count++;

      }

      assertEquals(10000, algo.getEvictionQueue().getNumberOfNodes());
   }

   public static Test suite()
   {
      return new TestSuite(LFUAlgorithmTest.class);
   }

   public static void main(String[] args)
   {
      junit.textui.TestRunner.run(suite());
   }

}
