package net.heartshield.sensors;

import junit.framework.TestCase;

import net.heartshield.data.WavData;
import net.heartshield.signal.DVec;
import net.heartshield.signal.IVec;

import java.nio.ByteBuffer;
import java.util.List;

public class SensorDataTest extends TestCase {
    public void testOne() {
        SensorData data = new SensorData(1);
        data.addSample(new double[]{0.0, 1.0});
        if(data.length() != 1)
            throw new RuntimeException("data.length() != 1");
        List<double[]> samples = data.getSamples();
        if(samples.size() != 2)
            throw new RuntimeException("samples.size() != 2");
        if(!IVec.all(DVec.eq(samples.get(0), new double[]{0.0})))
            throw new RuntimeException("time vector mismatch");
        if(!IVec.all(DVec.eq(samples.get(1), new double[]{1.0})))
            throw new RuntimeException("sample vector mismatch");
    }

    public void testExtend() {
        SensorData data = new SensorData(1);
        data.addSample(new double[]{0.0, 1.0});
        data.addSample(new double[]{1.0, 2.0});
        if(data.length() != 2)
            throw new RuntimeException("data.length() != 2");
        List<double[]> samples = data.getSamples();
        if(samples.size() != 2)
            throw new RuntimeException("samples.size() != 2");
        if(!IVec.all(DVec.eq(samples.get(0), new double[]{0.0, 1.0})))
            throw new RuntimeException("time vector mismatch");
        if(!IVec.all(DVec.eq(samples.get(1), new double[]{1.0, 2.0})))
            throw new RuntimeException("sample vector mismatch");
    }

    public void testExtendPCM() {
        SensorDataPCM data = new SensorDataPCM();
        data.addData(ByteBuffer.wrap(new byte[]{0, 1}), 2);
        data.addData(ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6, 7}), 7);

        //byte[] samples = data.getData();
        byte[] samples = new byte[data.bodyLength()];
        ByteBuffer buf = data.getData();
        buf.position(WavData.HEADER_SIZE);
        buf.get(samples, 0, samples.length);
        byte[] expected = new byte[]{0, 1, 1, 2, 3, 4, 5, 6, 7};
        if(samples.length != expected.length)
            throw new RuntimeException("samples.length != expected.length");
        if(data.bodyLength() != expected.length)
            throw new RuntimeException("data.length() != expected.length");
        if(data.bodyLength() + WavData.HEADER_SIZE != data.length())
            throw new RuntimeException("data.bodyLength() + WavData.HEADER_SIZE != data.length()");
        for(int i = 0; i < expected.length; i++)
            if(samples[i] != expected[i])
                throw new RuntimeException("vector mismatch at i=" + i);
    }

    public void testClear() {
        SensorData data = new SensorData(1);
        data.addSample(new double[]{0.0, 1.0});
        if(data.length() != 1)
            throw new RuntimeException("data.length() != 1");

        data.clear();

        if(data.length() != 0)
            throw new RuntimeException("data.length() != 0 after data.clear()");

        List<double[]> samples = data.getSamples();
        if(samples.size() != 2)
            throw new RuntimeException("samples.size() != 2");
    }
}
