Skip to content

Commit

Permalink
Merge pull request PwPJ#3 from emmadevjr/main
Browse files Browse the repository at this point in the history
ref: Implement Property-Based Testing for LinkedList
  • Loading branch information
pouriya committed May 24, 2024
2 parents 635a0f0 + d8bbb2a commit 2d73475
Show file tree
Hide file tree
Showing 7 changed files with 705 additions and 12 deletions.
42 changes: 35 additions & 7 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ jobs:
- name: Check out code
uses: actions/checkout@v2
- name: Build
run: go build -v ./...
run: go build -v .

fmt:
name: Format Check
runs-on: ubuntu-latest
needs: build
steps:
- name: Format Check code
- name: Check out code
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.22.2'
- name: Format
- name: Check Formatting
run: |
unformatted=$(gofmt -l .)
if [ -n "$unformatted" ]; then
Expand All @@ -40,8 +40,8 @@ jobs:
exit 1
fi
test:
name: Test
unit-test:
name: Unit Tests
runs-on: ubuntu-latest
needs: fmt
steps:
Expand All @@ -51,5 +51,33 @@ jobs:
go-version: '1.22.2'
- name: Check out code
uses: actions/checkout@v2
- name: Test
run: go test -v ./...
- name: Cache Go Modules
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run Unit Tests
run: go test -v ./... -run '.*_unit_test.go'

property-test:
name: Property Tests
runs-on: ubuntu-latest
needs: fmt
steps:
- name: Setting up Go
uses: actions/setup-go@v3
with:
go-version: '1.22.2'
- name: Check out code
uses: actions/checkout@v2
- name: Cache Go Modules
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run Property Tests
run: go test -v ./... -run '.*_property_test.go'
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 2d73475

Please sign in to comment.