// Copyright 2020 Andrew Thornton. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package levelqueue

import (
	"testing"

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

func TestSet(t *testing.T) {
	dbDir := t.TempDir()

	set, err := OpenSet(dbDir)
	require.NoError(t, err)

	// SAdd
	added, err := set.Add([]byte("test1"))
	require.NoError(t, err)
	assert.True(t, added)

	added, err = set.Add([]byte("test2"))
	require.NoError(t, err)
	assert.True(t, added)

	added, err = set.Add([]byte("test1"))
	require.NoError(t, err)
	assert.False(t, added)

	added, err = set.Add([]byte("test3"))
	require.NoError(t, err)
	assert.True(t, added)

	added, err = set.Add([]byte("test5"))
	require.NoError(t, err)
	assert.True(t, added)

	// SRem
	removed, err := set.Remove([]byte("test1"))
	require.NoError(t, err)
	assert.True(t, removed)

	removed, err = set.Remove([]byte("test4"))
	require.NoError(t, err)
	assert.False(t, removed)

	removed, err = set.Remove([]byte("test3"))
	require.NoError(t, err)
	assert.True(t, removed)

	// SIsMember
	isMember, err := set.Has([]byte("test2"))
	require.NoError(t, err)
	assert.True(t, isMember)

	isMember, err = set.Has([]byte("test1"))
	require.NoError(t, err)
	assert.False(t, isMember)

	// SMembers
	members, err := set.Members()
	require.NoError(t, err)
	assert.Contains(t, members, []byte("test2"))
	assert.Contains(t, members, []byte("test5"))
	assert.NotContains(t, members, []byte("test1"))
	assert.NotContains(t, members, []byte("test3"))
	assert.NotContains(t, members, []byte("test4"))
}
