Skip to content

Commit

Permalink
Add property-based tests for linked list operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Iajrdev committed May 11, 2024
1 parent 635a0f0 commit ac9add4
Show file tree
Hide file tree
Showing 6 changed files with 670 additions and 5 deletions.
Binary file modified README.md
Binary file not shown.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module linkedlist

go 1.22.2

require github.com/leanovate/gopter v0.2.11
603 changes: 603 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion linkedList.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type L struct {
}

// new creates and returns a new instance of a linked list
func new() *L {
func New() *L {
return &L{}
}

Expand Down
8 changes: 4 additions & 4 deletions linkedList_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// TestLinkedListInsert tests various scenarios of the Insert method
func TestLinkedListInsert(t *testing.T) {
l := new()
l := New()
// Test inserting into an empty list
if !l.Insert(0, 10) {
t.Error("Insert failed to insert the first element")
Expand Down Expand Up @@ -39,7 +39,7 @@ func TestLinkedListInsert(t *testing.T) {

// TestLinkedListRemove tests various scenarios of the Remove method
func TestLinkedListRemove(t *testing.T) {
l := new()
l := New()
l.Insert(0, 10)
l.Insert(1, 20)
l.Insert(2, 30)
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestLinkedListRemove(t *testing.T) {

// TestLinkedListGet tests various scenarios of the Get method
func TestLinkedListGet(t *testing.T) {
l := new()
l := New()
l.Insert(0, 10)
l.Insert(1, 20)
l.Insert(2, 30)
Expand All @@ -101,7 +101,7 @@ func TestLinkedListGet(t *testing.T) {

// TestLinkedListFind tests various scenarios of the Find method
func TestLinkedListFind(t *testing.T) {
l := new()
l := New()
l.Insert(0, 10)
l.Insert(1, 20)
l.Insert(2, 30)
Expand Down
60 changes: 60 additions & 0 deletions linkedlist_property_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"testing"

"github.com/leanovate/gopter"
"github.com/leanovate/gopter/gen"
"github.com/leanovate/gopter/prop"
)

func TestLinkedListProperties(t *testing.T) {
parameters := gopter.DefaultTestParameters()
parameters.MinSuccessfulTests = 320 //successful test cases run per property
parameters.MaxSize = 80 //maxsize for generated data

properties := gopter.NewProperties(parameters)

// test the insert and get property
properties.Property("test insert and get", prop.ForAll(
func(a int, b int) bool {
l := New()
l.Insert(0, a)
l.Insert(1, b)
va, _ := l.Get(0)
vb, _ := l.Get(1)
return va == a && vb == b
},
gen.Int(),
gen.Int(),
))

properties.Property("length after insert and Remove", prop.ForAll(
func(a int) bool {
l := New()
l.Insert(0, a)
success := l.Remove(0)
return success && l.length == 0
},
gen.Int(),
))

properties.Property("keeping the order the same after adding many items", prop.ForAll(
func(a []int) bool {
l := New()
for i, v := range a {
l.Insert(uint(i), v)
}
for i, v := range a {
value, _ := l.Get(uint(i))
if value != v {
return false
}
}
return true
},
gen.SliceOf(gen.Int()),
))

properties.TestingRun(t)
}

0 comments on commit ac9add4

Please sign in to comment.