Skip to content

Commit

Permalink
test(vm): add test for Backtrack of Death scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Sep 23, 2024
1 parent f3de7f0 commit 237ae8f
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"context"
"errors"
"fmt"
"github.com/ichiban/prolog/engine"
"github.com/stretchr/testify/assert"
"io"
"os"
"regexp"
"testing"
"time"

"github.com/ichiban/prolog/engine"
"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -1184,6 +1185,42 @@ next(N) :- retract(count(X)), N is X + 1, asserta(count(N)).
})
}

func TestInterpreter_Bombing(t *testing.T) {
const callLimit = 25
limitHooker := func(nbCall *int) engine.HookFunc {
return func(opcode engine.Opcode, operand engine.Term, env *engine.Env) error {
if opcode == engine.OpCall {
*nbCall++
if *nbCall > callLimit {
return engine.ResourceError(engine.NewAtom("calls"), env)
}
}
return nil
}
}

t.Run("💣 recursion of death", func(t *testing.T) {
nbCalls := 0
t.Run("create vm", func(t *testing.T) {
i := New(nil, nil)
assert.NotNil(t, i)
i.InstallHook(limitHooker(&nbCalls))

t.Run("execute program", func(t *testing.T) {
assert.NoError(t, i.Exec("recursionOfDeath :- recursionOfDeath."))

t.Run("💥", func(t *testing.T) {
sol := i.QuerySolutionContext(context.Background(), `recursionOfDeath.`)

assert.Nil(t, sol.sols)
assert.EqualError(t, sol.Err(), "error(resource_error(calls),recursionOfDeath/0)")
})
})
})
})

}

func TestInterpreter_QuerySolution(t *testing.T) {
var i Interpreter
assert.NoError(t, i.Exec(`
Expand Down

0 comments on commit 237ae8f

Please sign in to comment.