Skip to content

Commit

Permalink
Fix a few broken tests around serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
PapaCharlie committed Oct 18, 2023
1 parent f190ab9 commit 6708ddd
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 37 deletions.
2 changes: 1 addition & 1 deletion maps/treebidimap/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _ containers.JSONDeserializer = (*Map[string, int])(nil)

// ToJSON outputs the JSON representation of the map.
func (m *Map[K, V]) ToJSON() ([]byte, error) {
return json.Marshal(m.forwardMap)
return m.forwardMap.ToJSON()
}

// FromJSON populates the map from the input JSON representation.
Expand Down
37 changes: 10 additions & 27 deletions maps/treebidimap/treebidimap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,20 +578,26 @@ func TestMapSerialization(t *testing.T) {
original.Put("b", "2")
original.Put("a", "1")

assertSerialization(original, "A", t)

serialized, err := original.ToJSON()
if err != nil {
t.Errorf("Got error %v", err)
}
assertSerialization(original, "B", t)

deserialized := New[string, string]()
err = deserialized.FromJSON(serialized)
if err != nil {
t.Errorf("Got error %v", err)
}
assertSerialization(deserialized, "C", t)

if original.Size() != deserialized.Size() {
t.Errorf("Got map of size %d, expected %d", original.Size(), deserialized.Size())
}
original.Each(func(key string, expected string) {
actual, ok := deserialized.Get(key)
if !ok || actual != expected {
t.Errorf("Did not find expected value %v for key %v in deserialied map (got %q)", expected, key, actual)
}
})
}

m := New[string, float64]()
Expand All @@ -618,29 +624,6 @@ func TestMapString(t *testing.T) {
}
}

// noinspection GoBoolExpressions
func assertSerialization(m *Map[string, string], txt string, t *testing.T) {
if actualValue := m.Keys(); false ||
actualValue[0] != "a" ||
actualValue[1] != "b" ||
actualValue[2] != "c" ||
actualValue[3] != "d" ||
actualValue[4] != "e" {
t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[a,b,c,d,e]")
}
if actualValue := m.Values(); false ||
actualValue[0] != "1" ||
actualValue[1] != "2" ||
actualValue[2] != "3" ||
actualValue[3] != "4" ||
actualValue[4] != "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)
}
}

func benchmarkGet(b *testing.B, m *Map[int, int], size int) {
for i := 0; i < b.N; i++ {
for n := 0; n < size; n++ {
Expand Down
3 changes: 2 additions & 1 deletion queues/arrayqueue/arrayqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,11 @@ func TestQueueSerialization(t *testing.T) {
t.Errorf("Got error %v", err)
}

err = json.Unmarshal([]byte(`[1,2,3]`), &queue)
err = json.Unmarshal([]byte(`["a","b","c"]`), &queue)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}

func TestQueueString(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion queues/circularbuffer/circularbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,11 @@ func TestQueueSerialization(t *testing.T) {
t.Errorf("Got error %v", err)
}

err = json.Unmarshal([]byte(`[1,2,3]`), &queue)
err = json.Unmarshal([]byte(`["a","b","c"]`), &queue)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}

func TestQueueString(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion queues/linkedlistqueue/linkedlistqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,11 @@ func TestQueueSerialization(t *testing.T) {
t.Errorf("Got error %v", err)
}

err = json.Unmarshal([]byte(`[1,2,3]`), &queue)
err = json.Unmarshal([]byte(`["a","b","c"]`), &queue)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}

func TestQueueString(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion queues/priorityqueue/priorityqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,11 @@ func TestBinaryQueueSerialization(t *testing.T) {
t.Errorf("Got error %v", err)
}

err = json.Unmarshal([]byte(`[1,2,3]`), &queue)
err = json.Unmarshal([]byte(`["a","b","c"]`), &queue)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}

func TestBTreeString(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion sets/hashset/hashset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ func TestSetSerialization(t *testing.T) {
t.Errorf("Got error %v", err)
}

err = json.Unmarshal([]byte(`[1,2,3]`), &set)
err = json.Unmarshal([]byte(`["a","b","c"]`), &set)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}

func TestSetString(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion sets/linkedhashset/linkedhashset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,11 @@ func TestSetSerialization(t *testing.T) {
t.Errorf("Got error %v", err)
}

err = json.Unmarshal([]byte(`[1,2,3]`), &set)
err = json.Unmarshal([]byte(`["a","b","c"]`), &set)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}

func TestSetString(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion stacks/arraystack/arraystack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,11 @@ func TestStackSerialization(t *testing.T) {
t.Errorf("Got error %v", err)
}

err = json.Unmarshal([]byte(`[1,2,3]`), &stack)
err = json.Unmarshal([]byte(`["a","b","c"]`), &stack)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}

func TestStackString(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions stacks/linkedliststack/linkedliststack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,17 @@ func TestStackSerialization(t *testing.T) {
t.Errorf("Got error %v", err)
}

err = json.Unmarshal([]byte(`[1,2,3]`), &stack)
err = json.Unmarshal([]byte(`["a","b","c"]`), &stack)
if err != nil {
t.Errorf("Got error %v", err)
}
assert()
}

func TestStackString(t *testing.T) {
c := New[int]()
c.Push(1)
if !strings.HasPrefix(c.String(), "ArrayStack") {
if !strings.HasPrefix(c.String(), "LinkedListStack") {
t.Errorf("String should start with container name")
}
}
Expand Down

0 comments on commit 6708ddd

Please sign in to comment.