package protocol

import "bytes"
import "testing"

func makeTestBuffer (length int) (buffer []byte) {
	buffer = make([]byte, length)
	for index := range buffer {
		buffer[index] = byte(index)
	}
	return
}

func sliceEqual[T comparable] (left, right []T) bool {
	if len(left) != len(right) { return false }
	
	for index, item := range left {
		if item != right[index] { return false }
	}
	
	return true
}

func TestBuf8 (test *testing.T) {
	var conduit bytes.Buffer
	input := makeTestBuffer(97)
	test.Log("generated buffer")
	
	err := WriteBuf8(&conduit, input)
	if err != nil {
		test.Fatal(err)
		return
	}
	test.Log("wrote buffer")
	
	output, err := ReadBuf8(&conduit)
	if err != nil {
		test.Fatal(err)
		return
	}
	test.Log("read buffer")
	
	if len(input) != len(output) {
		test.Fatalf("len(input) %d != len(output) %d", len(input), len(output))
		return
	}

	if !sliceEqual(input, output) {
		test.Fatal("input != output")
		return
	}
}

func TestBuf16 (test *testing.T) {
	var conduit bytes.Buffer
	input := makeTestBuffer(1731)
	test.Log("generated buffer")
	
	err := WriteBuf16(&conduit, input)
	if err != nil {
		test.Fatal(err)
		return
	}
	test.Log("wrote buffer")
	
	output, err := ReadBuf16(&conduit)
	if err != nil {
		test.Fatal(err)
		return
	}
	test.Log("read buffer")
	
	if len(input) != len(output) {
		test.Fatalf("len(input) %d != len(output) %d", len(input), len(output))
		return
	}

	if !sliceEqual(input, output) {
		test.Fatal("input != output")
		return
	}
}

func TestString8 (test *testing.T) {
	var conduit bytes.Buffer
	input := "The quick brown fox jumped over the lazy dog."
	test.Log("generated string")
	
	err := WriteString8(&conduit, input)
	if err != nil {
		test.Fatal(err)
		return
	}
	test.Log("wrote buffer")
	
	output, err := ReadString8(&conduit)
	if err != nil {
		test.Fatal(err)
		return
	}
	test.Log("read buffer")
	
	if len(input) != len(output) {
		test.Fatalf("len(input) %d != len(output) %d", len(input), len(output))
		return
	}

	if input != output {
		test.Fatal("input != output")
		return
	}
}

func TestString16 (test *testing.T) {
	var conduit bytes.Buffer
	input := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Blandit aliquam etiam erat velit scelerisque. Euismod elementum nisi quis eleifend. Lorem ipsum dolor sit amet. Pellentesque diam volutpat commodo sed egestas egestas fringilla phasellus. Tristique sollicitudin nibh sit amet commodo nulla facilisi nullam vehicula. Lacus vel facilisis volutpat est velit egestas dui id. Mi quis hendrerit dolor magna. Sed ullamcorper morbi tincidunt ornare massa eget egestas purus viverra. Purus gravida quis blandit turpis cursus in hac. Enim nulla aliquet porttitor lacus luctus.
	
	Cras adipiscing enim eu turpis egestas pretium aenean pharetra. Mauris vitae ultricies leo integer malesuada nunc vel risus. Quis enim lobortis scelerisque fermentum dui faucibus in ornare. Amet dictum sit amet justo donec enim diam. Vulputate ut pharetra sit amet. Tellus integer feugiat scelerisque varius morbi enim. Pulvinar etiam non quam lacus suspendisse faucibus interdum posuere. Mauris in aliquam sem fringilla ut. Sagittis id consectetur purus ut faucibus pulvinar elementum integer enim. Fermentum odio eu feugiat pretium nibh. Arcu risus quis varius quam quisque.`
	test.Log("generated buffer")
	
	err := WriteString16(&conduit, input)
	if err != nil {
		test.Fatal(err)
		return
	}
	test.Log("wrote buffer")
	
	output, err := ReadString16(&conduit)
	if err != nil {
		test.Fatal(err)
		return
	}
	test.Log("read buffer")
	
	if len(input) != len(output) {
		test.Fatalf("len(input) %d != len(output) %d", len(input), len(output))
		return
	}

	if input != output {
		test.Fatal("input != output")
		return
	}
}
