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

import java.util.Locale;
import java.util.Random;
import net.ranides.assira.text.FormatHex;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class LFSRTest {
    
    @Test
    public void testTimeSeed() throws InterruptedException {
        LFSR s1 = new LFSR(0x44);
        LFSR s2 = new LFSR(0x44);
        
        LFSR time1 = new LFSR();
        Thread.sleep(50);
        LFSR time2 = new LFSR();
        
        assertTrue(s1.nextInt() == s2.nextInt());
        assertTrue(time1.nextInt() != time2.nextInt());
    }
    
    @Test
    public void testSession() {
        Random generator = new LFSR(0x1);
        byte[] bytes = new byte[16];
        
        String[] expected = new String[]{
            "01 00 00 00 01 00 00 80 01 00 00 c0 00 00 00 60",
            "01 00 00 b0 01 00 00 d8 00 00 00 6c 01 00 00 b6",
            "01 00 00 db 00 00 80 6d 01 00 c0 b6 01 00 60 db",
            "00 00 b0 6d 01 00 d8 b6 01 00 6c db 00 00 b6 6d",
            "01 00 db b6 01 80 6d db 00 c0 b6 6d 01 60 db b6",
            "01 b0 6d db 00 d8 b6 6d 01 6c db b6 00 b6 6d 5b",
            "00 db b6 2d 80 6d db 16 c1 b6 6d 8b 60 db b6 45",
            "b1 6d db a2 d8 b6 6d 51 6c db b6 28 b6 6d 5b 14",
            "dc b6 2d 8a 6e db 16 c5 b7 6d 8b e2 dc b6 45 f1",
            "6d db a2 78 b6 6d 51 3c dc b6 28 9e 6e 5b 14 cf",
            "b7 2d 8a e7 dc 16 c5 f3 6d 8b e2 79 b6 45 f1 3c",
            "dc a2 78 9e 6d 51 3c 4f b6 28 9e 27 5b 14 cf 13",
            "2d 8a e7 09 17 c5 f3 84 8b e2 79 42 45 f1 3c 21",
            "a3 78 9e 90 52 3c 4f c8 28 9e 27 64 14 cf 13 32",
            "8b e7 09 99 c5 f3 84 4c e2 79 42 26 f1 3c 21 13",
            "78 9e 90 09 3d 4f c8 84 9e 27 64 42 cf 13 32 21"
        };
        
        for(int i=0; i<16; i++) {
            generator.nextBytes(bytes);
            assertEquals(expected[i], FormatHex.format(bytes).toLowerCase(Locale.ROOT));
        }
    }
    
}
