- fix comments/documentation, rigorous serialization testing

pull/91/head
emirpasic 7 years ago
parent 73e1b206f9
commit 5123d6be01

@ -14,12 +14,12 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Map)(nil) 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) { func (m *Map) ToJSON() ([]byte, error) {
return m.forwardMap.ToJSON() 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 { func (m *Map) FromJSON(data []byte) error {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
err := json.Unmarshal(data, &elements) err := json.Unmarshal(data, &elements)

@ -15,7 +15,7 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Map)(nil) 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) { func (m *Map) ToJSON() ([]byte, error) {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
for key, value := range m.m { for key, value := range m.m {
@ -24,7 +24,7 @@ func (m *Map) ToJSON() ([]byte, error) {
return json.Marshal(&elements) 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 { func (m *Map) FromJSON(data []byte) error {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
err := json.Unmarshal(data, &elements) err := json.Unmarshal(data, &elements)

@ -15,7 +15,7 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Map)(nil) 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) { func (m *Map) ToJSON() ([]byte, error) {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
it := m.Iterator() it := m.Iterator()
@ -25,7 +25,7 @@ func (m *Map) ToJSON() ([]byte, error) {
return json.Marshal(&elements) 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 { func (m *Map) FromJSON(data []byte) error {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
err := json.Unmarshal(data, &elements) err := json.Unmarshal(data, &elements)

@ -473,35 +473,53 @@ func TestMapIteratorLast(t *testing.T) {
} }
} }
//noinspection GoBoolExpressions
func TestMapSerialization(t *testing.T) { func TestMapSerialization(t *testing.T) {
m := NewWithStringComparators() for i := 0; i < 10; i++ {
m.Put("a", "1") original := NewWith(utils.StringComparator, utils.StringComparator)
m.Put("b", "2") original.Put("d", "4")
m.Put("c", "3") original.Put("e", "5")
original.Put("c", "3")
var err error original.Put("b", "2")
assert := func() { original.Put("a", "1")
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]") assert := func(m *Map, txt string) {
} if actualValue := m.Keys(); false ||
if actualValue := m.Values(); actualValue[0].(string) != "1" || actualValue[1].(string) != "2" || actualValue[2].(string) != "3" { actualValue[0].(string) != "a" ||
t.Errorf("Got %v expected %v", actualValue, "[1,2,3]") 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 { if err != nil {
t.Errorf("Got error %v", err) 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) { func benchmarkGet(b *testing.B, m *Map, size int) {

@ -11,12 +11,12 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Map)(nil) 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) { func (m *Map) ToJSON() ([]byte, error) {
return m.tree.ToJSON() 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 { func (m *Map) FromJSON(data []byte) error {
return m.tree.FromJSON(data) return m.tree.FromJSON(data)
} }

@ -440,35 +440,53 @@ func TestMapIteratorLast(t *testing.T) {
} }
} }
//noinspection GoBoolExpressions
func TestMapSerialization(t *testing.T) { func TestMapSerialization(t *testing.T) {
m := NewWithStringComparator() for i := 0; i < 10; i++ {
m.Put("a", "1") original := NewWithStringComparator()
m.Put("b", "2") original.Put("d", "4")
m.Put("c", "3") original.Put("e", "5")
original.Put("c", "3")
var err error original.Put("b", "2")
assert := func() { original.Put("a", "1")
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]") assert := func(m *Map, txt string) {
} if actualValue := m.Keys(); false ||
if actualValue := m.Values(); actualValue[0].(string) != "1" || actualValue[1].(string) != "2" || actualValue[2].(string) != "3" { actualValue[0].(string) != "a" ||
t.Errorf("Got %v expected %v", actualValue, "[1,2,3]") 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 { if err != nil {
t.Errorf("Got error %v", err) 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) { func benchmarkGet(b *testing.B, m *Map, size int) {

@ -14,12 +14,12 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Set)(nil) 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) { func (set *Set) ToJSON() ([]byte, error) {
return json.Marshal(set.Values()) 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 { func (set *Set) FromJSON(data []byte) error {
elements := []interface{}{} elements := []interface{}{}
err := json.Unmarshal(data, &elements) err := json.Unmarshal(data, &elements)

@ -14,12 +14,12 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Set)(nil) 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) { func (set *Set) ToJSON() ([]byte, error) {
return json.Marshal(set.Values()) 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 { func (set *Set) FromJSON(data []byte) error {
elements := []interface{}{} elements := []interface{}{}
err := json.Unmarshal(data, &elements) err := json.Unmarshal(data, &elements)

@ -14,12 +14,12 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Set)(nil) 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) { func (set *Set) ToJSON() ([]byte, error) {
return json.Marshal(set.Values()) 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 { func (set *Set) FromJSON(data []byte) error {
elements := []interface{}{} elements := []interface{}{}
err := json.Unmarshal(data, &elements) err := json.Unmarshal(data, &elements)

@ -11,12 +11,12 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Stack)(nil) 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) { func (stack *Stack) ToJSON() ([]byte, error) {
return stack.list.ToJSON() 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 { func (stack *Stack) FromJSON(data []byte) error {
return stack.list.FromJSON(data) return stack.list.FromJSON(data)
} }

@ -11,12 +11,12 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Stack)(nil) 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) { func (stack *Stack) ToJSON() ([]byte, error) {
return stack.list.ToJSON() 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 { func (stack *Stack) FromJSON(data []byte) error {
return stack.list.FromJSON(data) return stack.list.FromJSON(data)
} }

@ -15,7 +15,7 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Tree)(nil) 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) { func (tree *Tree) ToJSON() ([]byte, error) {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
it := tree.Iterator() it := tree.Iterator()
@ -25,7 +25,7 @@ func (tree *Tree) ToJSON() ([]byte, error) {
return json.Marshal(&elements) 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 { func (tree *Tree) FromJSON(data []byte) error {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
err := json.Unmarshal(data, &elements) err := json.Unmarshal(data, &elements)

@ -11,12 +11,12 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Heap)(nil) 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) { func (heap *Heap) ToJSON() ([]byte, error) {
return heap.list.ToJSON() 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 { func (heap *Heap) FromJSON(data []byte) error {
return heap.list.FromJSON(data) return heap.list.FromJSON(data)
} }

@ -15,7 +15,7 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Tree)(nil) 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) { func (tree *Tree) ToJSON() ([]byte, error) {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
it := tree.Iterator() it := tree.Iterator()
@ -25,7 +25,7 @@ func (tree *Tree) ToJSON() ([]byte, error) {
return json.Marshal(&elements) 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 { func (tree *Tree) FromJSON(data []byte) error {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
err := json.Unmarshal(data, &elements) err := json.Unmarshal(data, &elements)

@ -15,7 +15,7 @@ func assertSerializationImplementation() {
var _ containers.JSONDeserializer = (*Tree)(nil) 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) { func (tree *Tree) ToJSON() ([]byte, error) {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
it := tree.Iterator() it := tree.Iterator()
@ -25,7 +25,7 @@ func (tree *Tree) ToJSON() ([]byte, error) {
return json.Marshal(&elements) 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 { func (tree *Tree) FromJSON(data []byte) error {
elements := make(map[string]interface{}) elements := make(map[string]interface{})
err := json.Unmarshal(data, &elements) err := json.Unmarshal(data, &elements)

Loading…
Cancel
Save