package container

import (
	"testing"
)

func isIDArrayEqual(a1 []uint64, a2 []uint64) bool {
	if len(a1) != len(a2) {
		return false
	}

	val := map[uint64]int{}
	for _, id := range a1 {
		val[id] += 1
	}

	for _, id := range a2 {
		val[id] -= 1
	}

	for _, i := range val {
		if i != 0 {
			return false
		}
	}

	return true
}

func TestIDSetAddElements(t *testing.T) {
	idList := []uint64{1, 2, 3, 5, 7}

	s := NewIDSet()
	s.AddList(idList)

	got := s.MakeList()
	if !isIDArrayEqual(got, idList) {
		t.Errorf("wrong result: %v", got)
	}
}

func TestIDSetAddDuplicatedElements(t *testing.T) {
	idList := []uint64{1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5}

	s := NewIDSet()
	s.AddList(idList)

	got := s.MakeList()
	if !isIDArrayEqual(got, idList) {
		t.Errorf("wrong result: %v", got)
	}
}

func TestIDSetRemoveElements(t *testing.T) {
	idList := []uint64{1, 2, 3, 5, 7}

	s := NewIDSet()
	s.AddList(idList)

	removeList := []uint64{2, 5}
	s.RemoveList(removeList)

	exp := []uint64{1, 3, 7}
	got := s.MakeList()
	if !isIDArrayEqual(got, exp) {
		t.Errorf("wrong result: %v", got)
	}
}

func TestIDSetRemoveDuplicatedElements(t *testing.T) {
	idList := []uint64{1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5}

	s := NewIDSet()
	s.AddList(idList)

	removeList := []uint64{1, 2, 3, 4, 5}
	s.RemoveList(removeList)

	exp := []uint64{2, 3, 3, 4, 4, 4, 5, 5, 5, 5}
	got := s.MakeList()
	if !isIDArrayEqual(got, exp) {
		t.Errorf("wrong result: %v", got)
	}
}

func TestIDSetRemoveNotExistedElements(t *testing.T) {
	idList := []uint64{1, 2, 3, 4, 5}

	s := NewIDSet()
	s.AddList(idList)

	s.Remove(233)
	got := s.MakeList()
	if !isIDArrayEqual(got, idList) {
		t.Errorf("wrong result: %v", got)
	}
}

func TestIDSetRemoveAll(t *testing.T) {
	idList := []uint64{1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5}

	s := NewIDSet()
	s.AddList(idList)

	s.RemoveAll(5)

	exp := []uint64{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}
	got := s.MakeList()
	if !isIDArrayEqual(got, exp) {
		t.Errorf("wrong result: %v", got)
	}
}
