package gnet

import (
	"testing"
)

func TestBigEndian_BitSplit(t *testing.T) {
	u := String("0x30 0x10 0x20 0x10 0x10 0x10 0x00 0x10").Hex()
	if u == nil {
		t.Error()
		return
	}
	t.Log(u.HexTo())
	b := BigEndian.BitSplit(u)
	t.Log(b)
}

func TestBigEndian_BitSplit_Single(t *testing.T) {
	n := uint8(36)
	bs := BigEndian.BitSplit([]byte{n})
	t.Log(bs)
	t.Log(bs.Is1(2))
	t.Log(bs.Is0(1))
	t.Log(bs.Is0(7))
	t.Log(bs.Is1(7))
}

func TestLittleEndian_BitSplit(t *testing.T) {
	u := String("0x0d 0x13").Hex()
	if u == nil {
		t.Error()
		return
	}
	t.Log(u.HexTo())
	b := LittleEndian.BitSplit(u)
	t.Log(b)
}

func TestBigEndian_Int16(t *testing.T) {
	raw := []byte{0xFF, 0xFF}
	covert := BigEndian.Int16(raw)
	t.Log(covert)
}

func TestLittleEndian_Float32(t *testing.T) {
	raw := []byte{0x00, 0x00, 0xca, 0x41}
	covert := LittleEndian.Float32(raw)
	t.Log(covert)
}

func combineBig(numbers [8]int) uint8 {
	// 检查输入是否合法
	for _, n := range numbers {
		if n != 0 && n != 1 {
			panic("number must be 0 or 1")
		}
	}

	// 生成结果
	var result uint8
	for i := len(numbers) - 1; i >= 0; i-- {
		result |= uint8(numbers[i]) << (7 - i)
	}

	return result
}

func combineLittle(numbers [8]int) uint8 {
	// 检查输入是否合法
	for _, n := range numbers {
		if n != 0 && n != 1 {
			panic("number must be 0 or 1")
		}
	}

	// 生成结果
	var result uint8
	for i := 0; i < len(numbers); i++ {
		result |= uint8(numbers[i]) << i
	}

	return result
}

func TestBitMerge(t *testing.T) {
	// 10
	l := LittleEndian.BitMerge([8]byte{0, 1, 0, 1, 0, 0, 0, 0})
	b := BigEndian.BigMerge([8]byte{0, 0, 0, 0, 1, 0, 1, 0})
	if l != b {
		t.Errorf("little: %d, big: %d", l, b)
		return
	}
}
