Skip to content

Commit

Permalink
Update simple_linked_list_test.go with an idempotency test
Browse files Browse the repository at this point in the history
If the submitter changes the actual list while iterating through its elements, the existing tests won't catch that.
Example of a String() method that changes the underlying list:
func (l *List) String() string {
	var listString string
	for l.head != nil {
		listString += fmt.Sprintf("%d ", l.head.value)
		l.head = l.head.next
	}
	return listString
}
  • Loading branch information
curiousz-peel authored May 21, 2024
1 parent fea5f9a commit c91be00
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions exercises/practice/simple-linked-list/simple_linked_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ func TestReverseNonEmptyList(t *testing.T) {
}
}

func TestArrayListIdempotency(t *testing.T) {
expected := []int{1, 2, 3}
list := New(expected)
_ := list.Array()
actual := list.Array()
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Calling Array() changes list contents: %v, want %v", actual, expected)
}
}

func BenchmarkNewList(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
Expand Down

0 comments on commit c91be00

Please sign in to comment.