mirror of
https://github.com/emirpasic/gods.git
synced 2026-06-14 16:06:42 +03:00
- all stacks (de)serialization
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
package arraystack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -231,6 +232,34 @@ func TestStackIteratorLast(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStackSerialization(t *testing.T) {
|
||||
stack := New()
|
||||
stack.Push("a")
|
||||
stack.Push("b")
|
||||
stack.Push("c")
|
||||
|
||||
var err error
|
||||
assert := func() {
|
||||
if actualValue, expectedValue := fmt.Sprintf("%s%s%s", stack.Values()...), "cba"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
if actualValue, expectedValue := stack.Size(), 3; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Got error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
assert()
|
||||
|
||||
json, err := stack.ToJSON()
|
||||
assert()
|
||||
|
||||
err = stack.FromJSON(json)
|
||||
assert()
|
||||
}
|
||||
|
||||
func benchmarkPush(b *testing.B, stack *Stack, size int) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for n := 0; n < size; n++ {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package arraystack
|
||||
|
||||
import "github.com/emirpasic/gods/containers"
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Stack)(nil)
|
||||
var _ containers.JSONDeserializer = (*Stack)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (stack *Stack) ToJSON() ([]byte, error) {
|
||||
return stack.list.ToJSON()
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (stack *Stack) FromJSON(data []byte) error {
|
||||
return stack.list.FromJSON(data)
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
package linkedliststack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -147,6 +148,34 @@ func TestStackIteratorFirst(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStackSerialization(t *testing.T) {
|
||||
stack := New()
|
||||
stack.Push("a")
|
||||
stack.Push("b")
|
||||
stack.Push("c")
|
||||
|
||||
var err error
|
||||
assert := func() {
|
||||
if actualValue, expectedValue := fmt.Sprintf("%s%s%s", stack.Values()...), "cba"; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
if actualValue, expectedValue := stack.Size(), 3; actualValue != expectedValue {
|
||||
t.Errorf("Got %v expected %v", actualValue, expectedValue)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("Got error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
assert()
|
||||
|
||||
json, err := stack.ToJSON()
|
||||
assert()
|
||||
|
||||
err = stack.FromJSON(json)
|
||||
assert()
|
||||
}
|
||||
|
||||
func benchmarkPush(b *testing.B, stack *Stack, size int) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for n := 0; n < size; n++ {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2015, Emir Pasic. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package linkedliststack
|
||||
|
||||
import "github.com/emirpasic/gods/containers"
|
||||
|
||||
func assertSerializationImplementation() {
|
||||
var _ containers.JSONSerializer = (*Stack)(nil)
|
||||
var _ containers.JSONDeserializer = (*Stack)(nil)
|
||||
}
|
||||
|
||||
// ToJSON outputs the JSON representation of list's elements.
|
||||
func (stack *Stack) ToJSON() ([]byte, error) {
|
||||
return stack.list.ToJSON()
|
||||
}
|
||||
|
||||
// FromJSON populates list's elements from the input JSON representation.
|
||||
func (stack *Stack) FromJSON(data []byte) error {
|
||||
return stack.list.FromJSON(data)
|
||||
}
|
||||
Reference in New Issue
Block a user