/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.suite;

import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import net.ranides.assira.collection.BlockCollection;
import net.ranides.assira.test.TCollection;
import static net.ranides.assira.junit.NewAssert.*;
import net.ranides.assira.junit.TestContract;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public final class BlockCollectionTester<K> {

    @Resource(name = "collection!")
    private TCollection<K> $var;
    
    @TestContract
    public void basicTrim(BlockCollection<K> target) {
        $var.range(1024).into(target);
        target.clear();
        $var.range(128).into(target);
        assertTrue(target.trim());
    }
    
    @TestContract
    public void basicTrimF(BlockCollection<K> target) {
        $var.range(1024).into(target);
        assertTrue(target.trim());
    }
    
    
    @TestContract
    public void basicTrimN1(BlockCollection<K> target) {
        basicTrim(target, 5*32, 32, 1024);
    }
    
    @TestContract
    public void basicTrimN2(BlockCollection<K> target) {
        assertThrows(IllegalArgumentException.class, () -> 
            basicTrim(target, 5*32, 32, 1)
        );
    }
    
    @TestContract
    public void basicTrimN3(BlockCollection<K> target) {
        basicTrim(target, 5*32, 32, 32);
    }
    
    private void basicTrim(BlockCollection<K> target, int put1st, int put2nd, int n) {
        $var.range(put1st).into(target);
        target.clear();
        $var.range(put2nd).into(target);
        target.trim(n);
    }
    
    @TestContract
    public void testEnsureCapacity(BlockCollection<K> target) {
        $var.range(256).into(target);
        List<K> list1 = target.stream().collect(Collectors.toList());
        
        assertTrue(target.reserve(32));
        assertEquals(list1.size(), target.size());
        assertTrue(target.containsAll(list1));
        
        assertTrue(target.reserve(1024));
        assertEquals(list1.size(), target.size());
        assertTrue(target.containsAll(list1));
        
        assertTrue(target.capacity() >= 1024 );
    }
        
    
}
