Skip to content

Commit

Permalink
unit test for go/sets/set.go (#14973)
Browse files Browse the repository at this point in the history
Signed-off-by: Divya Pamecha <[email protected]>
Signed-off-by: Maniaco <[email protected]>
Signed-off-by: Manan Gupta <[email protected]>
Co-authored-by: Manan Gupta <[email protected]>
  • Loading branch information
Its-Maniaco and GuptaManan100 authored Jan 25, 2024
1 parent ab8ae21 commit e647a2d
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions go/sets/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2024 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package sets

import (
"testing"

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

func TestInsert(t *testing.T) {
testSet := New[int](1, 2, 3)
testSet.Insert(4, 5, 6)
compareSet := New[int](1, 2, 3, 4, 5, 6)
assert.Equal(t, testSet, compareSet)
}

func TestDelete(t *testing.T) {
testSet := New[int](1, 2, 3, 4, 5, 6)
testSet.Delete(1, 5)
compareSet := New[int](2, 3, 4, 6)
assert.Equal(t, testSet, compareSet)
testSet.Delete(2, 3, 4, 6)
assert.Empty(t, testSet)
}

func TestHas(t *testing.T) {
testSet := New[int](1, 2, 3)
assert.True(t, testSet.Has(3))
assert.False(t, testSet.Has(-1))
}

func TestHasAny(t *testing.T) {
testSet := New[int](1, 2, 3)
assert.True(t, testSet.HasAny(1, 10, 11))
assert.False(t, testSet.HasAny(-1, 10, 11))
}

func TestDifference(t *testing.T) {
testSet := New[int](1, 2, 3)
compareSet := New[int](-1, -2, 1, 2, 3)
diffSet := New[int](-1, -2)
assert.Equal(t, diffSet, compareSet.Difference(testSet))
}

func TestIntersection(t *testing.T) {
setA := New[int](1, 2, 3)
setB := New[int](1, 2, 8, 9, 10)
expectedSet := New[int](1, 2)
assert.Equal(t, expectedSet, setA.Intersection(setB))
}

func TestEqual(t *testing.T) {
testSet := New[int](1, 2, 3, 4, 5, 6)
compareSet := New[int](1, 2, 3, 4, 5, 6)
assert.True(t, testSet.Equal(compareSet))
compareSet.Insert(-1, -2)
assert.False(t, testSet.Equal(compareSet))
}

func TestLen(t *testing.T) {
testSet := New[int](1, 2, 3)
assert.Equal(t, testSet.Len(), 3)
}

func TestList(t *testing.T) {
testSet := New[string]("a string", "testing", "Capital", "34")
list := List(testSet)
require.EqualValues(t, []string{"34", "Capital", "a string", "testing"}, list)
}

0 comments on commit e647a2d

Please sign in to comment.