fix: adjust arraylist slice capacity after unmarshalling

pull/248/head
zenbal 4 weeks ago
parent 14f714261f
commit 9ce1f9ea18

@ -642,6 +642,31 @@ func TestListSerialization(t *testing.T) {
assert()
}
func TestListSerializationCapacity(t *testing.T) {
nums := make([]int, 100)
for i := 0; i < 100; i++ {
nums[i] = i
}
list := New(nums...)
var err error
assert := func() {
if actualCapacity, expectedCapacity := cap(list.elements), 100; actualCapacity != expectedCapacity {
t.Errorf("Got cap=%d expected cap=%d", actualCapacity, expectedCapacity)
}
if actualLength, expectedLength := len(list.elements), 100; actualLength != expectedLength {
t.Errorf("Got len=%d expected len=%d", actualLength, expectedLength)
}
}
bytes, err := list.ToJSON()
err = list.FromJSON(bytes)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}
func TestListString(t *testing.T) {
c := New[int]()
c.Add(1)

@ -24,6 +24,7 @@ func (list *List[T]) FromJSON(data []byte) error {
err := json.Unmarshal(data, &list.elements)
if err == nil {
list.size = len(list.elements)
list.elements = list.elements[:len(list.elements):len(list.elements)]
}
return err
}

Loading…
Cancel
Save