package cluster

import (
	"sort"
	"testing"

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

func TestDiffInt(t *testing.T) {
	newSlice := []int{1, 2, 3, 4, 5}
	oldSlice := []int{1, 3, 5, 7, 9}

	toAdd, toDel := DiffInt(newSlice, oldSlice)
	sort.Ints(toAdd)
	sort.Ints(toDel)
	assert.Equal(t, []int{2, 4}, toAdd)
	assert.Equal(t, []int{7, 9}, toDel)
}

func TestDiffIntEmptySource(t *testing.T) {
	newSlice := []int{}
	oldSlice := []int{1, 3, 5, 7, 9}

	toAdd, toDel := DiffInt(newSlice, oldSlice)
	sort.Ints(toAdd)
	sort.Ints(toDel)
	assert.Equal(t, 0, len(toAdd))
	assert.Equal(t, []int{1, 3, 5, 7, 9}, toDel)
}

func TestDiffIntEmptyTarget(t *testing.T) {
	newSlice := []int{1, 3, 5, 7, 9}
	oldSlice := []int{}

	toAdd, toDel := DiffInt(newSlice, oldSlice)
	sort.Ints(toAdd)
	sort.Ints(toDel)
	assert.Equal(t, []int{1, 3, 5, 7, 9}, toAdd)
	assert.Equal(t, 0, len(toDel))
}

func TestDiffString(t *testing.T) {
	newSlice := []string{"a@pingcap.com", "b@pingcap.com"}
	oldSlice := []string{"c@pingcap.com"}

	toAdd, toDel := DiffString(newSlice, oldSlice)
	sort.Strings(toAdd)
	sort.Strings(toDel)
	assert.Equal(t, newSlice, toAdd)
	assert.Equal(t, oldSlice, toDel)
}

func TestDiffStringEmptySourceOrTarget(t *testing.T) {
	// empty source
	newSlice := []string{}
	oldSlice := []string{"c@pingcap.com"}

	toAdd, toDel := DiffString(newSlice, oldSlice)
	sort.Strings(toAdd)
	sort.Strings(toDel)
	assert.Equal(t, 0, len(toAdd))
	assert.Equal(t, oldSlice, toDel)

	// empty target
	newSlice = []string{"a@pingcap.com", "b@pingcap.com"}
	oldSlice = []string{}

	toAdd, toDel = DiffString(newSlice, oldSlice)
	sort.Strings(toAdd)
	sort.Strings(toDel)
	assert.Equal(t, newSlice, toAdd)
	assert.Equal(t, 0, len(toDel))
}
