go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/types/maps_test.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package types
     5  
     6  import (
     7  	"testing"
     8  
     9  	uuid "github.com/gofrs/uuid"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  const max = 1000
    14  
    15  func RunStringSet(t *testing.T, s *StringSet) {
    16  	id := uuid.Must(uuid.NewV4()).String()
    17  	assert.False(t, s.Exist(id), "key that wasn't yet added doesn't exist")
    18  	s.Store(id)
    19  	assert.True(t, s.Exist(id), "key that was added exists")
    20  	s.Delete(id)
    21  	assert.False(t, s.Exist(id), "key that was deleted doesn't exist")
    22  }
    23  
    24  func TestMaps_StringSet(t *testing.T) {
    25  	t.Run("all functions", func(t *testing.T) {
    26  		s := StringSet{}
    27  		id := uuid.Must(uuid.NewV4()).String()
    28  		assert.False(t, s.Exist(id), "key that wasn't yet added doesn't exist")
    29  		s.Store(id)
    30  		assert.True(t, s.Exist(id), "key that was added exists")
    31  		assert.Equal(t, []string{id}, s.List())
    32  		s.Delete(id)
    33  		assert.False(t, s.Exist(id), "key that was deleted doesn't exist")
    34  		assert.Equal(t, []string{}, s.List())
    35  	})
    36  
    37  	t.Run("concurrent runs", func(t *testing.T) {
    38  		s := StringSet{}
    39  		for i := 0; i < max; i++ {
    40  			go RunStringSet(t, &s)
    41  		}
    42  	})
    43  }
    44  
    45  func RunStringToStrings(t *testing.T, s *StringToStrings) {
    46  	a := uuid.Must(uuid.NewV4()).String()
    47  	b := uuid.Must(uuid.NewV4()).String()
    48  	assert.False(t, s.Exist(a, b), "key that wasn't yet added doesn't exist")
    49  	s.Store(a, b)
    50  	assert.True(t, s.Exist(a, b), "key that was added exists")
    51  	s.Delete(a, b)
    52  	assert.False(t, s.Exist(a, b), "key that was deleted doesn't exist")
    53  }
    54  
    55  func TestMaps_StringToStrings(t *testing.T) {
    56  	t.Run("all functions", func(t *testing.T) {
    57  		s := StringToStrings{}
    58  		a := uuid.Must(uuid.NewV4()).String()
    59  		b := uuid.Must(uuid.NewV4()).String()
    60  		assert.False(t, s.Exist(a, b), "key that wasn't yet added doesn't exist")
    61  		s.Store(a, b)
    62  		assert.True(t, s.Exist(a, b), "key that was added exists")
    63  		assert.Equal(t, map[string][]string{
    64  			a: []string{b},
    65  		}, s.List())
    66  		s.Delete(a, b)
    67  		assert.False(t, s.Exist(a, b), "key that was deleted doesn't exist")
    68  		assert.Equal(t, map[string][]string{}, s.List())
    69  	})
    70  
    71  	t.Run("concurrent runs", func(t *testing.T) {
    72  		s := StringToStrings{}
    73  		for i := 0; i < max; i++ {
    74  			go RunStringToStrings(t, &s)
    75  		}
    76  	})
    77  }