Skip to content

Commit

Permalink
fix delete of first item
Browse files Browse the repository at this point in the history
  • Loading branch information
cooldarkdryplace committed May 5, 2018
1 parent 75cb3ea commit 2d2ed79
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
8 changes: 8 additions & 0 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ func (l *list) add(s string) {

func (l *list) del(s string) {
item := l.First
if item.Value == s {
l.First = item.Next
if item == l.Last {
l.Last = nil
}
return
}

for {
if item == nil || item.Next == nil {
return
Expand Down
58 changes: 56 additions & 2 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ import (
"testing"
)

func contains(s string, l *list) bool {
item := l.First

for {
if item == nil {
return false
}

if item.Value == s {
return true
}

item = item.Next
}
}

func TestStoreSingleRequest(t *testing.T) {
storage := NewStorage()
r := Request{Body: "test"}
Expand All @@ -22,7 +38,7 @@ func TestStoreSingleRequest(t *testing.T) {
}
}

func TestAddSingleRecort(t *testing.T) {
func TestAddSingleRecord(t *testing.T) {
l := &list{}
expected := "yo"
l.add(expected)
Expand All @@ -38,13 +54,43 @@ func TestAddSingleRecort(t *testing.T) {
}
}

func TestDeleteRecord(t *testing.T) {
func TestDeleteTheOnlyRecord(t *testing.T) {
l := &list{}
value := "test"
l.add(value)

l.del(value)
if l.First != nil {
t.Error("Reference to first item was not updated")
}

if l.Last != nil {
t.Error("Reference to last item was not updated")
}
}

func TestDeleteMiddleRecord(t *testing.T) {
var (
l = &list{}
first = "test1"
target = "test2"
last = "test3"
)

l.add(first)
l.add(target)
l.add(last)

l.del(target)
if l.First.Value != first {
t.Error("Unexpected first item")
}

if l.Last.Value != last {
t.Error("Unexpected last item")
}

if contains(target, l) {
t.Error("Item was not deleted")
}
}
Expand Down Expand Up @@ -74,4 +120,12 @@ func TestAddMultipleRecords(t *testing.T) {
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Got: %s, expected: %s", actual, expected)
}

if l.First.Value != expected[0] {
t.Error("Unexpected first item")
}

if l.Last.Value != expected[len(expected)-1] {
t.Error("Unexpected last item")
}
}

0 comments on commit 2d2ed79

Please sign in to comment.