diff --git a/maps/hashbidimap/serialization.go b/maps/hashbidimap/serialization.go index 9f6247e..3db41d4 100644 --- a/maps/hashbidimap/serialization.go +++ b/maps/hashbidimap/serialization.go @@ -14,12 +14,12 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Map)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the map. func (m *Map) ToJSON() ([]byte, error) { return m.forwardMap.ToJSON() } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the map from the input JSON representation. func (m *Map) FromJSON(data []byte) error { elements := make(map[string]interface{}) err := json.Unmarshal(data, &elements) diff --git a/maps/hashmap/serialization.go b/maps/hashmap/serialization.go index b8e9026..b06eb7e 100644 --- a/maps/hashmap/serialization.go +++ b/maps/hashmap/serialization.go @@ -15,7 +15,7 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Map)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the map. func (m *Map) ToJSON() ([]byte, error) { elements := make(map[string]interface{}) for key, value := range m.m { @@ -24,7 +24,7 @@ func (m *Map) ToJSON() ([]byte, error) { return json.Marshal(&elements) } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the map from the input JSON representation. func (m *Map) FromJSON(data []byte) error { elements := make(map[string]interface{}) err := json.Unmarshal(data, &elements) diff --git a/maps/treebidimap/serialization.go b/maps/treebidimap/serialization.go index f9a7850..17204f9 100644 --- a/maps/treebidimap/serialization.go +++ b/maps/treebidimap/serialization.go @@ -15,7 +15,7 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Map)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the map. func (m *Map) ToJSON() ([]byte, error) { elements := make(map[string]interface{}) it := m.Iterator() @@ -25,7 +25,7 @@ func (m *Map) ToJSON() ([]byte, error) { return json.Marshal(&elements) } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the map from the input JSON representation. func (m *Map) FromJSON(data []byte) error { elements := make(map[string]interface{}) err := json.Unmarshal(data, &elements) diff --git a/maps/treebidimap/treebidimap_test.go b/maps/treebidimap/treebidimap_test.go index 0ef5165..bc7f4fa 100644 --- a/maps/treebidimap/treebidimap_test.go +++ b/maps/treebidimap/treebidimap_test.go @@ -473,35 +473,53 @@ func TestMapIteratorLast(t *testing.T) { } } +//noinspection GoBoolExpressions func TestMapSerialization(t *testing.T) { - m := NewWithStringComparators() - m.Put("a", "1") - m.Put("b", "2") - m.Put("c", "3") - - var err error - assert := func() { - if actualValue := m.Keys(); actualValue[0].(string) != "a" || actualValue[1].(string) != "b" || actualValue[2].(string) != "c" { - t.Errorf("Got %v expected %v", actualValue, "[a,b,c]") - } - if actualValue := m.Values(); actualValue[0].(string) != "1" || actualValue[1].(string) != "2" || actualValue[2].(string) != "3" { - t.Errorf("Got %v expected %v", actualValue, "[1,2,3]") + for i := 0; i < 10; i++ { + original := NewWith(utils.StringComparator, utils.StringComparator) + original.Put("d", "4") + original.Put("e", "5") + original.Put("c", "3") + original.Put("b", "2") + original.Put("a", "1") + + assert := func(m *Map, txt string) { + if actualValue := m.Keys(); false || + actualValue[0].(string) != "a" || + actualValue[1].(string) != "b" || + actualValue[2].(string) != "c" || + actualValue[3].(string) != "d" || + actualValue[4].(string) != "e" { + t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[a,b,c,d,e]") + } + if actualValue := m.Values(); false || + actualValue[0].(string) != "1" || + actualValue[1].(string) != "2" || + actualValue[2].(string) != "3" || + actualValue[3].(string) != "4" || + actualValue[4].(string) != "5" { + t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[1,2,3,4,5]") + } + if actualValue, expectedValue := m.Size(), 5; actualValue != expectedValue { + t.Errorf("[%s] Got %v expected %v", txt, actualValue, expectedValue) + } } - if actualValue, expectedValue := m.Size(), 3; actualValue != expectedValue { - t.Errorf("Got %v expected %v", actualValue, expectedValue) + + assert(original, "A") + + serialized, err := original.ToJSON() + if err != nil { + t.Errorf("Got error %v", err) } + assert(original, "B") + + deserialized := NewWith(utils.StringComparator, utils.StringComparator) + err = deserialized.FromJSON(serialized) if err != nil { t.Errorf("Got error %v", err) } + assert(deserialized, "C") } - - assert() - - json, err := m.ToJSON() - assert() - - err = m.FromJSON(json) - assert() } func benchmarkGet(b *testing.B, m *Map, size int) { diff --git a/maps/treemap/serialization.go b/maps/treemap/serialization.go index 25f4a6f..d856300 100644 --- a/maps/treemap/serialization.go +++ b/maps/treemap/serialization.go @@ -11,12 +11,12 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Map)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the map. func (m *Map) ToJSON() ([]byte, error) { return m.tree.ToJSON() } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the map from the input JSON representation. func (m *Map) FromJSON(data []byte) error { return m.tree.FromJSON(data) } diff --git a/maps/treemap/treemap_test.go b/maps/treemap/treemap_test.go index a73e873..d718c91 100644 --- a/maps/treemap/treemap_test.go +++ b/maps/treemap/treemap_test.go @@ -440,35 +440,53 @@ func TestMapIteratorLast(t *testing.T) { } } +//noinspection GoBoolExpressions func TestMapSerialization(t *testing.T) { - m := NewWithStringComparator() - m.Put("a", "1") - m.Put("b", "2") - m.Put("c", "3") - - var err error - assert := func() { - if actualValue := m.Keys(); actualValue[0].(string) != "a" || actualValue[1].(string) != "b" || actualValue[2].(string) != "c" { - t.Errorf("Got %v expected %v", actualValue, "[a,b,c]") - } - if actualValue := m.Values(); actualValue[0].(string) != "1" || actualValue[1].(string) != "2" || actualValue[2].(string) != "3" { - t.Errorf("Got %v expected %v", actualValue, "[1,2,3]") + for i := 0; i < 10; i++ { + original := NewWithStringComparator() + original.Put("d", "4") + original.Put("e", "5") + original.Put("c", "3") + original.Put("b", "2") + original.Put("a", "1") + + assert := func(m *Map, txt string) { + if actualValue := m.Keys(); false || + actualValue[0].(string) != "a" || + actualValue[1].(string) != "b" || + actualValue[2].(string) != "c" || + actualValue[3].(string) != "d" || + actualValue[4].(string) != "e" { + t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[a,b,c,d,e]") + } + if actualValue := m.Values(); false || + actualValue[0].(string) != "1" || + actualValue[1].(string) != "2" || + actualValue[2].(string) != "3" || + actualValue[3].(string) != "4" || + actualValue[4].(string) != "5" { + t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[1,2,3,4,5]") + } + if actualValue, expectedValue := m.Size(), 5; actualValue != expectedValue { + t.Errorf("[%s] Got %v expected %v", txt, actualValue, expectedValue) + } } - if actualValue, expectedValue := m.Size(), 3; actualValue != expectedValue { - t.Errorf("Got %v expected %v", actualValue, expectedValue) + + assert(original, "A") + + serialized, err := original.ToJSON() + if err != nil { + t.Errorf("Got error %v", err) } + assert(original, "B") + + deserialized := NewWithStringComparator() + err = deserialized.FromJSON(serialized) if err != nil { t.Errorf("Got error %v", err) } + assert(deserialized, "C") } - - assert() - - json, err := m.ToJSON() - assert() - - err = m.FromJSON(json) - assert() } func benchmarkGet(b *testing.B, m *Map, size int) { diff --git a/sets/hashset/serialization.go b/sets/hashset/serialization.go index af7bfe8..7b8506d 100644 --- a/sets/hashset/serialization.go +++ b/sets/hashset/serialization.go @@ -14,12 +14,12 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Set)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the set. func (set *Set) ToJSON() ([]byte, error) { return json.Marshal(set.Values()) } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the set from the input JSON representation. func (set *Set) FromJSON(data []byte) error { elements := []interface{}{} err := json.Unmarshal(data, &elements) diff --git a/sets/linkedhashset/serialization.go b/sets/linkedhashset/serialization.go index 806b908..7e7d291 100644 --- a/sets/linkedhashset/serialization.go +++ b/sets/linkedhashset/serialization.go @@ -14,12 +14,12 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Set)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the set. func (set *Set) ToJSON() ([]byte, error) { return json.Marshal(set.Values()) } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the set from the input JSON representation. func (set *Set) FromJSON(data []byte) error { elements := []interface{}{} err := json.Unmarshal(data, &elements) diff --git a/sets/treeset/serialization.go b/sets/treeset/serialization.go index 10b1599..a53bfcc 100644 --- a/sets/treeset/serialization.go +++ b/sets/treeset/serialization.go @@ -14,12 +14,12 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Set)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the set. func (set *Set) ToJSON() ([]byte, error) { return json.Marshal(set.Values()) } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the set from the input JSON representation. func (set *Set) FromJSON(data []byte) error { elements := []interface{}{} err := json.Unmarshal(data, &elements) diff --git a/stacks/arraystack/serialization.go b/stacks/arraystack/serialization.go index d1ad81d..c4ff549 100644 --- a/stacks/arraystack/serialization.go +++ b/stacks/arraystack/serialization.go @@ -11,12 +11,12 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Stack)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the stack. func (stack *Stack) ToJSON() ([]byte, error) { return stack.list.ToJSON() } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the stack from the input JSON representation. func (stack *Stack) FromJSON(data []byte) error { return stack.list.FromJSON(data) } diff --git a/stacks/linkedliststack/serialization.go b/stacks/linkedliststack/serialization.go index ac6a68c..63337a1 100644 --- a/stacks/linkedliststack/serialization.go +++ b/stacks/linkedliststack/serialization.go @@ -11,12 +11,12 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Stack)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the stack. func (stack *Stack) ToJSON() ([]byte, error) { return stack.list.ToJSON() } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the stack from the input JSON representation. func (stack *Stack) FromJSON(data []byte) error { return stack.list.FromJSON(data) } diff --git a/trees/avltree/serialization.go b/trees/avltree/serialization.go index 0630d87..363de7f 100644 --- a/trees/avltree/serialization.go +++ b/trees/avltree/serialization.go @@ -15,7 +15,7 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Tree)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the tree. func (tree *Tree) ToJSON() ([]byte, error) { elements := make(map[string]interface{}) it := tree.Iterator() @@ -25,7 +25,7 @@ func (tree *Tree) ToJSON() ([]byte, error) { return json.Marshal(&elements) } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the tree from the input JSON representation. func (tree *Tree) FromJSON(data []byte) error { elements := make(map[string]interface{}) err := json.Unmarshal(data, &elements) diff --git a/trees/binaryheap/serialization.go b/trees/binaryheap/serialization.go index 299319b..00d0c77 100644 --- a/trees/binaryheap/serialization.go +++ b/trees/binaryheap/serialization.go @@ -11,12 +11,12 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Heap)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the heap. func (heap *Heap) ToJSON() ([]byte, error) { return heap.list.ToJSON() } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the heap from the input JSON representation. func (heap *Heap) FromJSON(data []byte) error { return heap.list.FromJSON(data) } diff --git a/trees/btree/serialization.go b/trees/btree/serialization.go index 95c817e..4385167 100644 --- a/trees/btree/serialization.go +++ b/trees/btree/serialization.go @@ -15,7 +15,7 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Tree)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the tree. func (tree *Tree) ToJSON() ([]byte, error) { elements := make(map[string]interface{}) it := tree.Iterator() @@ -25,7 +25,7 @@ func (tree *Tree) ToJSON() ([]byte, error) { return json.Marshal(&elements) } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the tree from the input JSON representation. func (tree *Tree) FromJSON(data []byte) error { elements := make(map[string]interface{}) err := json.Unmarshal(data, &elements) diff --git a/trees/redblacktree/serialization.go b/trees/redblacktree/serialization.go index 7969fc5..a1b8a77 100644 --- a/trees/redblacktree/serialization.go +++ b/trees/redblacktree/serialization.go @@ -15,7 +15,7 @@ func assertSerializationImplementation() { var _ containers.JSONDeserializer = (*Tree)(nil) } -// ToJSON outputs the JSON representation of list's elements. +// ToJSON outputs the JSON representation of the tree. func (tree *Tree) ToJSON() ([]byte, error) { elements := make(map[string]interface{}) it := tree.Iterator() @@ -25,7 +25,7 @@ func (tree *Tree) ToJSON() ([]byte, error) { return json.Marshal(&elements) } -// FromJSON populates list's elements from the input JSON representation. +// FromJSON populates the tree from the input JSON representation. func (tree *Tree) FromJSON(data []byte) error { elements := make(map[string]interface{}) err := json.Unmarshal(data, &elements)