Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix arraylist unmarshalling #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lists/arraylist/arraylist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lists/arraylist/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down