Created
June 16, 2023 22:10
-
-
Save edwardIshaq/548010042250da8d72b01f9ff41bfc60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type itemsList struct { | |
items []int | |
mapItems map[int][]int | |
} | |
func newItemsList() *itemsList { | |
return &itemsList{ | |
mapItems: make(map[int][]int), | |
} | |
} | |
func (i *itemsList) SetItems(items []int) { | |
i.items = items | |
} | |
func (i *itemsList) Items() []int { | |
return i.items | |
} | |
func (i *itemsList) SetMapItems(idx int, items []int) { | |
i.mapItems[idx] = items | |
} | |
func (i *itemsList) ItemsAt(idx int) []int { | |
return i.mapItems[idx] | |
} | |
func TestSliceReturns(t *testing.T) { | |
orig := []int{1, 2, 3, 4, 5} | |
start := make([]int, len(orig)) | |
copy(start, orig) | |
itemsList := newItemsList() | |
itemsList.SetItems(start) | |
gotItem := itemsList.Items() | |
assert.Equal(t, orig, gotItem) | |
// change gotItem | |
gotItem[0] = 999 | |
gotItem2 := itemsList.Items() | |
assert.NotEqual(t, orig, gotItem2) | |
assert.Equal(t, gotItem, gotItem2) | |
} | |
func TestMapSliceReturns(t *testing.T) { | |
idx := 1 | |
orig := []int{1, 2, 3, 4, 5} | |
start := make([]int, len(orig)) | |
copy(start, orig) | |
itemsList := newItemsList() | |
itemsList.SetMapItems(idx, start) | |
gotItem := itemsList.ItemsAt(idx) | |
assert.Equal(t, orig, gotItem) | |
// change gotItem | |
gotItem[0] = 999 | |
gotItem2 := itemsList.ItemsAt(idx) | |
assert.Equal(t, orig, gotItem2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment