github.com/blend/go-sdk@v1.20220411.3/collections/set_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package collections
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  func TestSetOfInt(t *testing.T) {
    17  	assert := assert.New(t)
    18  
    19  	set := SetOfInt{}
    20  	set.Add(1)
    21  	assert.True(set.Contains(1))
    22  	assert.Equal(1, set.Len())
    23  	assert.False(set.Contains(2))
    24  	set.Remove(1)
    25  	assert.False(set.Contains(1))
    26  	assert.Zero(set.Len())
    27  }
    28  
    29  func TestSetOfString(t *testing.T) {
    30  	assert := assert.New(t)
    31  
    32  	set := SetOfString{}
    33  	assert.Equal(0, set.Len())
    34  
    35  	set.Add("test")
    36  	assert.Equal(1, set.Len())
    37  	assert.True(set.Contains("test"))
    38  
    39  	set.Add("test")
    40  	assert.Equal(1, set.Len())
    41  	assert.True(set.Contains("test"))
    42  
    43  	set.Add("not test")
    44  	assert.Equal(2, set.Len())
    45  	assert.True(set.Contains("not test"))
    46  
    47  	set.Remove("test")
    48  	assert.Equal(1, set.Len())
    49  	assert.False(set.Contains("test"))
    50  	assert.True(set.Contains("not test"))
    51  
    52  	set.Remove("not test")
    53  	assert.Equal(0, set.Len())
    54  	assert.False(set.Contains("test"))
    55  	assert.False(set.Contains("not test"))
    56  }
    57  
    58  func TestSetOfIntOperations(t *testing.T) {
    59  	assert := assert.New(t)
    60  
    61  	a := NewSetOfInt(1, 2, 3, 4)
    62  	b := NewSetOfInt(1, 2)
    63  	c := NewSetOfInt(3, 4, 5, 6)
    64  
    65  	union := a.Union(c)
    66  	assert.Len(union, 6)
    67  	intersect := a.Intersect(b)
    68  	assert.Len(intersect, 2)
    69  	diff := a.Difference(c)
    70  	assert.Len(diff, 4)
    71  	diff = c.Difference(a)
    72  	assert.Len(diff, 4)
    73  	assert.True(b.IsSubsetOf(a))
    74  	assert.False(a.IsSubsetOf(b))
    75  }
    76  
    77  func TestSetOfStringOperations(t *testing.T) {
    78  	assert := assert.New(t)
    79  
    80  	a := NewSetOfString("a", "b", "c", "d")
    81  	b := NewSetOfString("a", "b")
    82  	c := NewSetOfString("c", "d", "e", "f")
    83  
    84  	union := a.Union(c)
    85  	assert.Len(union, 6)
    86  	intersect := a.Intersect(b)
    87  	assert.Len(intersect, 2)
    88  	diff := a.Difference(c)
    89  	assert.Len(diff, 4)
    90  	diff = c.Difference(a)
    91  	assert.Len(diff, 4)
    92  	assert.True(b.IsSubsetOf(a))
    93  	assert.False(a.IsSubsetOf(b))
    94  }