package go_alac

import (
	"io/ioutil"
	"os"
	"testing"
)

func TestEncode(t *testing.T) {
	t.Parallel()

	f, err := os.Open("sample/test_s16.raw")
	if err != nil {
		t.Error(err)
		return
	}
	defer f.Close()

	data, err := ioutil.ReadAll(f)
	if err != nil {
		t.Error(err)
		return
	}

	o, err := os.Create("sample/test_s16_output.alac")
	if err != nil {
		t.Error(err)
		return
	}
	defer o.Close()

	encoder := NewFrameEncoder(44100, 2, 16, false)
	packetSize := encoder.GetInputSize()
	for len(data) > packetSize {
		resultPacket := encoder.WritePacket(data[:packetSize])
		o.Write(resultPacket)
		data = data[packetSize:]
	}

	if len(data) > 0 {
		resultPacket := encoder.WritePacket(data)
		o.Write(resultPacket)
	}

	o.Sync()
	o.Close()
}
