/*
 * @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 net.ranides.assira.text.Charsets;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class DataBufferTest {
    
    @Test
    public void testWrite() throws IOException {
        String text = "Quick brown\nfox jumps\nover lazy\ndog\n";
        
        DataBuffer db1 = new DataBuffer();
        
        db1.write(text.getBytes(Charsets.US_ASCII));
        db1.write(text.getBytes(Charsets.US_ASCII));
        db1.write(text.getBytes(Charsets.US_ASCII));
        
        byte[] exp = (text+text+text).getBytes(Charsets.US_ASCII);
        byte[] buf = new byte[exp.length];
        db1.seek(0);
        db1.readFully(buf);
        assertArrayEquals(exp, buf);
    }
    
    @Test
    public void testReadLine() throws IOException {
        DataBuffer db1 = new DataBuffer();
        db1.write("Quick brown\nfox jumps\nover lazy\ndog\n".getBytes(Charsets.US_ASCII));
        db1.seek(0);

        assertEquals("Quick brown", db1.readLine());
        assertEquals("fox jumps", db1.readLine());
        assertEquals("over lazy", db1.readLine());
        assertEquals("dog", db1.readLine());
        assertEquals(null, db1.readLine());
        
        DataBuffer db2 = new DataBuffer();
        db2.write("Quick brown\nfox jumps\nover lazy\ndog".getBytes(Charsets.US_ASCII));
        db2.seek(0);
        assertEquals("Quick brown", db2.readLine());
        assertEquals("fox jumps", db2.readLine());
        assertEquals("over lazy", db2.readLine());
        assertEquals("dog", db2.readLine());
        assertEquals(null, db2.readLine());
        
        DataBuffer db3 = new DataBuffer();
        db3.write("Quick brown\rfox jumps\r\nover lazy\n\rdog\r!".getBytes(Charsets.US_ASCII));
        db3.seek(0);
        assertEquals("Quick brown", db3.readLine());
        assertEquals("fox jumps", db3.readLine());
        assertEquals("over lazy", db3.readLine());
        assertEquals("", db3.readLine());
        assertEquals("dog", db3.readLine());
        assertEquals("!", db3.readLine());
        assertEquals(null, db3.readLine());
    }

    @Test
    public void testReadLineCS() throws IOException {
        DataBuffer db1 = new DataBuffer();
        db1.write("Pchnąć w tę\nłódź jeża\nlub ośm\nskrzyń fig\n".getBytes(Charsets.UTF8));
        db1.seek(0);

        assertEquals("Pchnąć w tę", db1.readLine(Charsets.UTF8));
        assertEquals("łódź jeża", db1.readLine(Charsets.UTF8));
        assertEquals("lub ośm", db1.readLine(Charsets.UTF8));
        assertEquals("skrzyń fig", db1.readLine(Charsets.UTF8));
        assertEquals(null, db1.readLine(Charsets.UTF8));
        
        DataBuffer db2 = new DataBuffer();
        db2.write("Pchnąć w tę\nłódź jeża\nlub ośm\nskrzyń fig".getBytes(Charsets.UTF8));
        db2.seek(0);
        assertEquals("Pchnąć w tę", db2.readLine(Charsets.UTF8));
        assertEquals("łódź jeża", db2.readLine(Charsets.UTF8));
        assertEquals("lub ośm", db2.readLine(Charsets.UTF8));
        assertEquals("skrzyń fig", db2.readLine(Charsets.UTF8));
        assertEquals(null, db2.readLine(Charsets.UTF8));
        
        DataBuffer db3 = new DataBuffer();
        db3.write("Pchnąć w tę\rłódź jeża\r\nlub ośm\n\rskrzyń fig\r!".getBytes(Charsets.UTF8));
        db3.seek(0);
        assertEquals("Pchnąć w tę", db3.readLine(Charsets.UTF8));
        assertEquals("łódź jeża", db3.readLine(Charsets.UTF8));
        assertEquals("lub ośm", db3.readLine(Charsets.UTF8));
        assertEquals("", db3.readLine(Charsets.UTF8));
        assertEquals("skrzyń fig", db3.readLine(Charsets.UTF8));
        assertEquals("!", db3.readLine(Charsets.UTF8));
        assertEquals(null, db3.readLine(Charsets.UTF8));
    }
    
}
