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

import net.ranides.assira.collection.arrays.IntArrays;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class IntFoldTest {
    
    @Test
    public void testFold() {
        // mocno tautologiczny test. nie lubimy takich
        IntFold fold = (a,b) -> 7*a+b;
        
        assertEquals(9, fold.apply(1, 2));
        assertEquals(9, fold.applyAsInt(1, 2));
        assertEquals(Integer.valueOf(9), fold.apply(Integer.valueOf(1), Integer.valueOf(2)));
    }
    
    @Test
    public void testFold_array() {
        IntFold fold = (a,b) -> 10*a + b;
        
        assertEquals(123456, fold.apply(new int[]{1,2,3,4,5,6}));
        assertEquals(123, fold.apply(new int[]{1,2,3}));
        assertEquals(12, fold.apply(new int[]{1,2}));
        assertEquals(1, fold.apply(new int[]{1}));
        
        assertThrows(RuntimeException.class, ()->{
            fold.apply(new int[]{});
        });
    }
    
    @Test
    public void testFold_iterator() {
        IntFold fold = (a,b) -> 10*a + b;

        
        assertEquals(123456, fold.apply(IntArrays.asList(1,2,3,4,5,6).iterator()));
        assertEquals(123, fold.apply(IntArrays.asList(1,2,3).iterator()));
        assertEquals(12, fold.apply(IntArrays.asList(1,2).iterator()));
        assertEquals(1, fold.apply(IntArrays.asList(1).iterator()));
        
        assertThrows(RuntimeException.class, ()->{
            fold.apply(IntArrays.asList().iterator());
        });
    }
    
}
