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

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.SeekableByteChannel;
import java.util.function.Function;
import java.util.regex.Pattern;
import net.ranides.assira.ContractTesters;
import net.ranides.assira.test.TAdapter;
import net.ranides.assira.text.Charsets;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class ChannelAdapterTest {
    
    @Test
    public void testWrap_Contract_ByteBuffer() {
        TAdapter<ByteChannel, ByteBuffer> map = new TAdapter<>();
        
        Function<ByteChannel, byte[]> mr = (channel) -> {
            return map.get(channel).array();
        };
        ContractTesters.runner()
			.ignore("ChannelTester.basicRead_LE")
			.ignore("ChannelTester.basicWrite_LE")
            .param("channel.bytes!", mr)
			.function(new byte[0], map.map(ByteBuffer::wrap, ChannelAdapter::wrap))
			.run();
        assertTrue(true);
        
    }
    
    @Test
    public void testWrap_Contract_ByteArray() {
        TAdapter<SeekableByteChannel, byte[]> map = new TAdapter<>();
        
        Function<SeekableByteChannel, byte[]> mr = (channel) -> {
			return map.get(channel);
        };
        ContractTesters.runner()
			.ignore("ChannelTester.basicRead_LE")
			.ignore("ChannelTester.basicWrite_LE")
            .param("channel.bytes!", mr)
            .function(new byte[0], map.map(v->v, ChannelAdapter::wrap))
			.run();
        assertTrue(true);
    }
    
    @Test
    public void testWrap_Contract_CharBuffer() throws IOException {
        TAdapter<SeekableByteChannel, CharSequence> map = new TAdapter<>();
        
        Function<SeekableByteChannel, byte[]> mr = (channel) -> {
			return map.get(channel).toString().getBytes(Charsets.UTF16BE);
        };
        ContractTesters.runner()
            .ignore(Pattern.compile("ChannelTester.basicWrite.*"))
            .param("channel.bytes!", mr)
            .function(new byte[0], map.map(v->new String(v, Charsets.UTF16BE), ChannelAdapter::wrap))
			.run();
        assertTrue(true);
    }
    
    @Test
    public void testWrap_Contract_CharArray() throws IOException {
        TAdapter<SeekableByteChannel, char[]> map = new TAdapter<>();
        
        Function<SeekableByteChannel, byte[]> mr = (channel) -> {
			return new String(map.get(channel)).getBytes(Charsets.UTF16BE);
        };
        ContractTesters.runner()
            .param("channel.bytes!", mr)
            .function(new byte[0], map.map((array) -> new String(array, Charsets.UTF16BE).toCharArray(), ChannelAdapter::wrap))
			.run();
        assertTrue(true);
    }

}
