<commit_before>/*******************************************************************************
 * This file is part of TERMINAL RECALL
 * Copyright (c) 2012-2015 <NAME>
 * Part of the jTRFP.org project
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     chuck - initial API and implementation
 ******************************************************************************/

package org.jtrfp.trcl.pool;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Queue;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.jtrfp.trcl.pool.IndexPool.GrowthBehavior;
import org.jtrfp.trcl.pool.IndexPool.OutOfIndicesException;

public class IndexPoolTest extends TestCase {

    public void testPop() {
	final IndexPool subject = new IndexPool();
	for(int i=0; i<500; i++)
	    assertEquals(i, subject.pop());
    }

    public void testPopOrException() {
	final IndexPool subject = new IndexPool();
	subject.setHardLimit(500);
	for(int i=0; i<500; i++)
	    assertEquals(i, subject.pop());
	try {
	    subject.popOrException();
	    fail("Failed to throw expected OutOfIndicesException.");
	} catch (OutOfIndicesException e) {}//Good.
    }

    public void testPopCollectionOfIntegerInt() {
	final IndexPool subject = new IndexPool();
	final ArrayList<Integer>dest = new ArrayList<Integer>();
	subject.pop(dest, 512);
	for(int i=0; i<512; i++)
	    assertEquals(Integer.valueOf(i), dest.get(i));
	assertEquals(512,dest.size());
    }

    public void testFree() {
	final IndexPool subject = new IndexPool();
	for(int i=0; i<500; i++)
	    assertEquals(i, subject.pop());
	for(int i=0; i<250; i++)
	    subject.free(i);
	for(int i=0; i<250; i++)
	    assertEquals(i, subject.pop());
	assertEquals(500,subject.pop());
    }

    public void testSetGrowthBehavior() {
	final IndexPool subject = new IndexPool();
	assertEquals(1, subject.getMaxCapacity());
	subject.setGrowthBehavior(new GrowthBehavior(){
	    @Override
	    public int grow(int previousMaxCapacity) {
		return previousMaxCapacity+1;
	    }
	    public int shrink(int minDesiredCapacity){
		return minDesiredCapacity;
	    }});
	subject.pop();
	subject.pop();
	assertEquals(2, subject.getMaxCapacity());
	subject.pop();
	assertEquals(3, subject.getMaxCapacity());
    }

    public void testPopConsecutive() {
	final IndexPool subject = new IndexPool();
	assertEquals(0, subject.popConsecutive(4));
	assertEquals(4, subject.popConsecutive(4));
    }

    public void testGetMaxCapacity() {
	final IndexPool subject =  new IndexPool();
	for(int i=0; i<60; i++)
	    subject.pop();
	//With default doubling growth, power-of-two behavior expected.
	assertEquals(64, subject.getMaxCapacity());
    }

    public void testGetHardLimit() {
	final IndexPool subject=  new IndexPool();
	subject.setHardLimit(5);
	assertEquals(5,subject.getHardLimit());
    }
    
    public void testFreeCollectionOfIntegersInt(){
	final IndexPool subject = new IndexPool();
	final ArrayList<Integer>dest = new ArrayList<Integer>();
	subject.pop(dest, 512);
	subject.free(dest);
	assertEquals(0, subject.pop());
    }
    
    public void testGetUsedIndices(){
	final IndexPool subject = new IndexPool();
	Queue<Integer> indices = subject.getUsedIndices();
	subject.pop();
	final ArrayList<Integer> dest = new ArrayList<Integer>();
	subject.pop(dest, 2);
	assertNotNull(indices);
	assertEquals(3,indices.size());
	Iterator<Integer> it = indices.iterator();
	assertEquals(0, (int)it.next());
	assertEquals(1, (int)it.next());
	assertEquals(2, (int)it.next());
	
	assertEquals(1, (int)dest.get(0));
	assertEquals(2, (int)dest.get(1));
    }//end testGetUsedIndices()
    
    public void testCompact(){
	final IndexPool subject = new IndexPool();
	subject.pop();
	subject.pop();
	subject.pop();
	subject.free(0);
	subject.free(1);
	// f f 2
	assertEquals(2, subject.getFreeIndices().size());
	assertEquals(1, subject.getUsedIndices().size());
	subject.compact();
	// f f 2
	assertEquals(2, subject.getFreeIndices().size());
	assertEquals(1, subject.getUsedIndices().size());
	assertEquals(0,subject.pop());
	subject.free(2);
	// 0 f f
	assertEquals(2, subject.getFreeIndices().size());
	assertEquals(1, subject.getUsedIndices().size());
	subject.compact();
	// 0
	assertEquals(0, subject.getFreeIndices().size());
	assertEquals(1, subject.getUsedIndices().size());
    }

}//end IndexPoolTest<commit_msg>Add property change listening to IndexPoolTest.<commit_after>/*******************************************************************************
 * This file is part of TERMINAL RECALL
 * Copyright (c) 2012-2015 <NAME>
 * Part of the jTRFP.org project
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     chuck - initial API and implementation
 ******************************************************************************/

package org.jtrfp.trcl.pool;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Queue;

import junit.framework.TestCase;

import org.jtrfp.trcl.dbg.PropertyChangeQueue;
import org.jtrfp.trcl.pool.IndexPool.GrowthBehavior;
import org.jtrfp.trcl.pool.IndexPool.OutOfIndicesException;

public class IndexPoolTest extends TestCase {

    public void testPop() {
	final IndexPool subject = new IndexPool();
	for(int i=0; i<500; i++)
	    assertEquals(i, subject.pop());
    }

    public void testPopOrException() {
	final IndexPool subject = new IndexPool();
	subject.setHardLimit(500);
	for(int i=0; i<500; i++)
	    assertEquals(i, subject.pop());
	try {
	    subject.popOrException();
	    fail("Failed to throw expected OutOfIndicesException.");
	} catch (OutOfIndicesException e) {}//Good.
    }

    public void testPopCollectionOfIntegerInt() {
	final IndexPool subject = new IndexPool();
	final ArrayList<Integer>dest = new ArrayList<Integer>();
	subject.pop(dest, 512);
	for(int i=0; i<512; i++)
	    assertEquals(Integer.valueOf(i), dest.get(i));
	assertEquals(512,dest.size());
    }

    public void testFree() {
	final IndexPool subject = new IndexPool();
	for(int i=0; i<500; i++)
	    assertEquals(i, subject.pop());
	for(int i=0; i<250; i++)
	    subject.free(i);
	for(int i=0; i<250; i++)
	    assertEquals(i, subject.pop());
	assertEquals(500,subject.pop());
    }

    public void testSetGrowthBehavior() {
	final IndexPool subject = new IndexPool();
	assertEquals(1, subject.getMaxCapacity());
	subject.setGrowthBehavior(new GrowthBehavior(){
	    @Override
	    public int grow(int previousMaxCapacity) {
		return previousMaxCapacity+1;
	    }
	    public int shrink(int minDesiredCapacity){
		return minDesiredCapacity;
	    }});
	subject.pop();
	subject.pop();
	assertEquals(2, subject.getMaxCapacity());
	subject.pop();
	assertEquals(3, subject.getMaxCapacity());
    }

    public void testPopConsecutive() {
	final IndexPool subject = new IndexPool();
	assertEquals(0, subject.popConsecutive(4));
	assertEquals(4, subject.popConsecutive(4));
    }

    public void testGetMaxCapacity() {
	final IndexPool subject =  new IndexPool();
	for(int i=0; i<60; i++)
	    subject.pop();
	//With default doubling growth, power-of-two behavior expected.
	assertEquals(64, subject.getMaxCapacity());
    }

    public void testGetHardLimit() {
	final IndexPool subject=  new IndexPool();
	subject.setHardLimit(5);
	assertEquals(5,subject.getHardLimit());
    }
    
    public void testFreeCollectionOfIntegersInt(){
	final IndexPool subject = new IndexPool();
	final ArrayList<Integer>dest = new ArrayList<Integer>();
	subject.pop(dest, 512);
	subject.free(dest);
	assertEquals(0, subject.pop());
    }
    
    public void testGetUsedIndices(){
	final IndexPool subject = new IndexPool();
	Queue<Integer> indices = subject.getUsedIndices();
	subject.pop();
	final ArrayList<Integer> dest = new ArrayList<Integer>();
	subject.pop(dest, 2);
	assertNotNull(indices);
	assertEquals(3,indices.size());
	Iterator<Integer> it = indices.iterator();
	assertEquals(0, (int)it.next());
	assertEquals(1, (int)it.next());
	assertEquals(2, (int)it.next());
	
	assertEquals(1, (int)dest.get(0));
	assertEquals(2, (int)dest.get(1));
    }//end testGetUsedIndices()
    
    public void testCompact(){
	final IndexPool subject = new IndexPool();
	subject.pop();
	subject.pop();
	subject.pop();
	subject.free(0);
	subject.free(1);
	// f f 2
	assertEquals(2, subject.getFreeIndices().size());
	assertEquals(1, subject.getUsedIndices().size());
	subject.compact();
	// f f 2
	assertEquals(2, subject.getFreeIndices().size());
	assertEquals(1, subject.getUsedIndices().size());
	assertEquals(0,subject.pop());
	subject.free(2);
	// 0 f f
	assertEquals(2, subject.getFreeIndices().size());
	assertEquals(1, subject.getUsedIndices().size());
	subject.compact();
	// 0
	assertEquals(0, subject.getFreeIndices().size());
	assertEquals(1, subject.getUsedIndices().size());
    }
    
    public void testGetNumUnusedIndices(){
	final IndexPool subject = new IndexPool();
	assertEquals(0,subject.getNumUnusedIndices());
	subject.pop();
	assertEquals(0,subject.getNumUnusedIndices());
	subject.free(0);
	assertEquals(1,subject.getNumUnusedIndices());
	subject.pop();
	assertEquals(0,subject.getNumUnusedIndices());
    }
    
    public void testGetNumUsedIndices(){
	final IndexPool subject = new IndexPool();
	assertEquals(0,subject.getNumUsedIndices());
	subject.pop();
	assertEquals(1,subject.getNumUsedIndices());
	subject.free(0);
	assertEquals(0,subject.getNumUsedIndices());
    }
    
    public void testNumUsedIndicesPropertyChange(){
	final IndexPool         subject = new IndexPool();
	final PropertyChangeQueue queue = new PropertyChangeQueue();
	subject.addPropertyChangeListener(IndexPool.NUM_USED_INDICES,queue);
	assertEquals(0,queue.size());
	subject.pop();
	assertEquals(1,queue.size());
	assertEquals(1,queue.pop().getNewValue());
	subject.pop();
	assertEquals(1,queue.size());
	assertEquals(2,queue.pop().getNewValue());
    }
    
    public void testNumUnusedIndicesPropertyChange(){
	final IndexPool         subject = new IndexPool();
	final PropertyChangeQueue queue = new PropertyChangeQueue();
	subject.addPropertyChangeListener(IndexPool.NUM_UNUSED_INDICES,queue);
	subject.pop();
	subject.pop();
	subject.pop();
	assertEquals(0,queue.size());
	subject.free(0);
	assertEquals(1,queue.size());
	assertEquals(1,queue.pop().getNewValue());
	subject.free(1);
	assertEquals(1,queue.size());
	assertEquals(2,queue.pop().getNewValue());
    }

}//end IndexPoolTest
{'fraction_non_alphanumeric': 0.16913123844731978, 'fraction_numerical': 0.01857670979667283, 'mean_word_length': 4.926067907995619, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5039597', 'n_tokens_mistral': 3389, 'n_tokens_neox': 3264, 'n_words': 747}