package fragmentation

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestEncode(t *testing.T) {
	data := make([]byte, 100)
	for i := range data {
		data[i] = byte(i)
	}

	tests := []struct {
		Name              string
		Data              []byte
		FragmentSize      int
		Redundancy        int
		ExpectedFragments [][]byte
		ExpectedError     error
	}{
		{
			Name:          "invalid fragment size",
			Data:          make([]byte, 5),
			FragmentSize:  10,
			ExpectedError: errors.New("length of data must be a multiple of the given fragment-size"),
		},
		{
			Name:         "fragment size 10, redundancy 10",
			Data:         data,
			FragmentSize: 10,
			Redundancy:   10,
			ExpectedFragments: [][]byte{
				[]byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9},
				[]byte{0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13},
				[]byte{0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d},
				[]byte{0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27},
				[]byte{0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31},
				[]byte{0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b},
				[]byte{0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45},
				[]byte{0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f},
				[]byte{0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59},
				[]byte{0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63},
				[]byte{0x26, 0x26, 0x22, 0x22, 0x2e, 0x2e, 0x22, 0x22, 0x26, 0x26},
				[]byte{0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x6a, 0x6b, 0x7c, 0x7d},
				[]byte{0x5c, 0x5d, 0x6e, 0x6f, 0x10, 0x11, 0x2, 0x3, 0x4, 0x5},
				[]byte{0x36, 0x36, 0x32, 0x32, 0x3e, 0x3e, 0x22, 0x22, 0x36, 0x36},
				[]byte{0x3a, 0x3a, 0xe, 0xe, 0xa, 0xa, 0x6, 0x6, 0xa, 0xa},
				[]byte{0xe, 0xe, 0x32, 0x32, 0x36, 0x36, 0x22, 0x22, 0x3e, 0x3e},
				[]byte{0x2, 0x2, 0xe, 0xe, 0x72, 0x72, 0x76, 0x76, 0x62, 0x62},
				[]byte{0x1e, 0x1e, 0x1a, 0x1a, 0x66, 0x66, 0x5a, 0x5a, 0x4e, 0x4e},
				[]byte{0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x30, 0x31, 0x22, 0x23},
				[]byte{0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x18, 0x19, 0xa, 0xb}},
		},
		{
			Name:         "fragment size 10, redundancy 5",
			Data:         data,
			FragmentSize: 10,
			Redundancy:   5,
			ExpectedFragments: [][]byte{
				[]byte{0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9},
				[]byte{0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13},
				[]byte{0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d},
				[]byte{0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27},
				[]byte{0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31},
				[]byte{0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b},
				[]byte{0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45},
				[]byte{0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f},
				[]byte{0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59},
				[]byte{0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63},
				[]byte{0x26, 0x26, 0x22, 0x22, 0x2e, 0x2e, 0x22, 0x22, 0x26, 0x26},
				[]byte{0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x6a, 0x6b, 0x7c, 0x7d},
				[]byte{0x5c, 0x5d, 0x6e, 0x6f, 0x10, 0x11, 0x2, 0x3, 0x4, 0x5},
				[]byte{0x36, 0x36, 0x32, 0x32, 0x3e, 0x3e, 0x22, 0x22, 0x36, 0x36},
				[]byte{0x3a, 0x3a, 0xe, 0xe, 0xa, 0xa, 0x6, 0x6, 0xa, 0xa}},
		},
	}

	for _, tst := range tests {
		t.Run(tst.Name, func(t *testing.T) {
			assert := require.New(t)

			fragments, err := Encode(tst.Data, tst.FragmentSize, tst.Redundancy)
			if tst.ExpectedError != nil {
				assert.Equal(tst.ExpectedError, err)
				return
			}

			assert.NoError(err)
			assert.Equal(tst.ExpectedFragments, fragments)
		})
	}
}
