- fix comments/documentation, rigorous serialization testing

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

@ -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)

@ -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)

@ -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)

@ -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) {

@ -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)
}

@ -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) {

@ -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)

@ -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)

@ -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)

@ -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)
}

@ -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)
}

@ -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)

@ -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)
}

@ -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)

@ -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)

Loading…
Cancel
Save