aboutsummaryrefslogtreecommitdiff
path: root/pkg/stack
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/stack')
-rw-r--r--pkg/stack/stack-stack.go91
-rw-r--r--pkg/stack/stack-stack_test.go251
-rw-r--r--pkg/stack/stack.go51
-rw-r--r--pkg/stack/stack_test.go74
4 files changed, 0 insertions, 467 deletions
diff --git a/pkg/stack/stack-stack.go b/pkg/stack/stack-stack.go
deleted file mode 100644
index 9b80079..0000000
--- a/pkg/stack/stack-stack.go
+++ /dev/null
@@ -1,91 +0,0 @@
-package stack
-
-import (
- "git.adyxax.org/adyxax/gofunge/pkg/pointer"
-)
-
-type StackStack struct {
- head *Stack
- height int
-}
-
-func NewStackStack() *StackStack {
- return &StackStack{
- head: NewStack(),
- height: 1,
- }
-}
-
-func (ss *StackStack) Begin(p *pointer.Pointer) {
- ss.height++
- soss := ss.head
- n := soss.Pop()
- np := n
- if np < 0 {
- np = -np
- }
- toss := &Stack{
- size: np,
- height: np,
- data: make([]int, np),
- next: soss,
- }
- ss.head = toss
- max := n - soss.height
- if max < 0 {
- max = 0
- }
- for i := n - 1; i >= max; i-- {
- toss.data[i] = soss.data[soss.height-n+i]
- }
- x, y := p.GetStorageOffset()
- soss.Push(x)
- soss.Push(y)
- p.CalculateNewStorageOffset()
-}
-
-func (ss *StackStack) End(p *pointer.Pointer) (reflect bool) {
- if ss.height == 1 {
- return true
- }
- soss := ss.head.next
- n := ss.head.Pop()
- y := soss.Pop()
- x := soss.Pop()
- p.SetStorageOffset(x, y)
- if n > 0 {
- soss.height += n
- if soss.size < soss.height {
- soss.data = append(soss.data, make([]int, soss.height-soss.size)...)
- soss.size = soss.height
- }
- for i := n; i > 0; i-- {
- soss.data[soss.height-i] = ss.head.data[ss.head.height-i]
- }
- } else {
- soss.height += n
- if soss.height < 0 {
- soss.height = 0
- }
- }
- ss.height--
- ss.head = ss.head.next
- return false
-}
-
-func (ss *StackStack) Under() (reflect bool) {
- if ss.height == 1 {
- return true
- }
- n := ss.head.Pop()
- if n > 0 {
- for i := 0; i < n; i++ {
- ss.head.Push(ss.head.next.Pop())
- }
- } else {
- for i := 0; i < -n; i++ {
- ss.head.next.Push(ss.head.Pop())
- }
- }
- return false
-}
diff --git a/pkg/stack/stack-stack_test.go b/pkg/stack/stack-stack_test.go
deleted file mode 100644
index d2a7949..0000000
--- a/pkg/stack/stack-stack_test.go
+++ /dev/null
@@ -1,251 +0,0 @@
-package stack
-
-import (
- "os"
- "testing"
-
- "git.adyxax.org/adyxax/gofunge/pkg/field"
- "git.adyxax.org/adyxax/gofunge/pkg/pointer"
- "github.com/stretchr/testify/require"
-)
-
-func TestBegin(t *testing.T) {
- t.Run("empty", func(t *testing.T) {
- expected := NewStackStack()
- expected.head = &Stack{data: make([]int, 0), next: expected.head}
- expected.head.next.Push(0)
- expected.head.next.Push(0)
- expected.height++
- ss := NewStackStack()
- p := pointer.NewPointer()
- ss.Begin(p)
- require.Equal(t, expected, ss)
- x, y := p.GetStorageOffset()
- require.Equal(t, 1, x)
- require.Equal(t, 0, y)
- // Let's push another one
- expected.head = &Stack{data: make([]int, 0), next: expected.head}
- expected.head.next.Push(1)
- expected.head.next.Push(0)
- expected.height++
- ss.Begin(p)
- require.Equal(t, expected, ss)
- x, y = p.GetStorageOffset()
- require.Equal(t, 1, x)
- require.Equal(t, 0, y)
- })
- t.Run("negative", func(t *testing.T) {
- expected := NewStackStack()
- expected.head = &Stack{size: 5, height: 5, data: make([]int, 5), next: expected.head}
- expected.head.next.Push(0)
- expected.head.next.Push(0)
- expected.height++
- p := pointer.NewPointer()
- file, err := os.Open("../field/test_data/hello.b98")
- require.NoError(t, err, "Failed to open file")
- f, err := field.Load(file)
- p.Step(*f)
- ss := NewStackStack()
- ss.head.Push(-5)
- ss.Begin(p)
- require.Equal(t, expected, ss)
- x, y := p.GetStorageOffset()
- require.Equal(t, 2, x)
- require.Equal(t, 0, y)
- })
- t.Run("ask to copy more than we have", func(t *testing.T) {
- expected := NewStackStack()
- expected.head = &Stack{size: 34, height: 34, data: make([]int, 34), next: expected.head}
- expected.head.data[33] = 18
- expected.head.next.Push(18)
- expected.head.next.Push(2)
- expected.head.next.Push(3)
- expected.height++
- p := pointer.NewPointer()
- p.SetStorageOffset(2, 3)
- file, err := os.Open("../field/test_data/hello.b98")
- require.NoError(t, err, "Failed to open file")
- f, err := field.Load(file)
- p.Step(*f)
- ss := NewStackStack()
- ss.head.Push(18)
- ss.head.Push(34)
- ss.Begin(p)
- require.Equal(t, expected, ss)
- x, y := p.GetStorageOffset()
- require.Equal(t, 2, x)
- require.Equal(t, 0, y)
- })
- t.Run("normal", func(t *testing.T) {
- expected := NewStackStack()
- expected.head = &Stack{size: 4, height: 4, data: []int{12, 14, -2, 5}, next: expected.head}
- expected.head.next.Push(7)
- expected.head.next.Push(12)
- expected.head.next.Push(14)
- expected.head.next.Push(-2)
- expected.head.next.Push(5)
- expected.head.next.Push(36)
- expected.head.next.Push(42)
- expected.height++
- p := pointer.NewPointer()
- p.SetStorageOffset(36, 42)
- ss := NewStackStack()
- ss.head.Push(7)
- ss.head.Push(12)
- ss.head.Push(14)
- ss.head.Push(-2)
- ss.head.Push(5)
- ss.head.Push(4)
- ss.Begin(p)
- require.Equal(t, expected, ss)
- })
-}
-
-func TestEnd(t *testing.T) {
- t.Run("empty", func(t *testing.T) {
- expected := NewStackStack()
- p := pointer.NewPointer()
- ss := NewStackStack()
- ss.Begin(p)
- reflect := ss.End(p)
- require.Equal(t, false, reflect)
- require.Equal(t, expected, ss)
- })
- t.Run("drop", func(t *testing.T) {
- expected := NewStackStack()
- expected.head.Push(7)
- expected.head.Push(12)
- expected.head.Push(14)
- expected.head.Push(-2)
- expected.head.Push(5)
- expected.head.Pop()
- expected.head.Pop()
- expected.head.Pop()
- p := pointer.NewPointer()
- ss := NewStackStack()
- ss.head.Push(7)
- ss.head.Push(12)
- ss.head.Push(14)
- ss.head.Push(-2)
- ss.head.Push(5)
- ss.head.Push(0)
- ss.Begin(p)
- ss.head.Push(18)
- ss.head.Push(42)
- ss.head.Push(-3)
- reflect := ss.End(p)
- require.Equal(t, false, reflect)
- require.Equal(t, expected, ss)
- })
- t.Run("drop too much", func(t *testing.T) {
- expected := NewStackStack()
- p := pointer.NewPointer()
- ss := NewStackStack()
- ss.Begin(p)
- ss.head.Push(-3)
- reflect := ss.End(p)
- require.Equal(t, false, reflect)
- require.Equal(t, expected, ss)
- })
- t.Run("reflect", func(t *testing.T) {
- expected := NewStackStack()
- p := pointer.NewPointer()
- ss := NewStackStack()
- reflect := ss.End(p)
- require.Equal(t, true, reflect)
- require.Equal(t, expected, ss)
- })
- t.Run("transfert", func(t *testing.T) {
- expected := NewStackStack()
- expected.head.size = 5
- expected.head.data = make([]int, 5)
- expected.head.Push(7)
- expected.head.Push(12)
- expected.head.Push(14)
- expected.head.Push(-2)
- expected.head.Push(5)
- p := pointer.NewPointer()
- ss := NewStackStack()
- ss.head.size = 4
- ss.head.data = make([]int, 4)
- ss.head.Push(7)
- ss.head.Push(0)
- ss.Begin(p)
- ss.head.Push(0)
- ss.head.Push(18)
- ss.head.Push(42)
- ss.head.Push(7)
- ss.head.Push(12)
- ss.head.Push(14)
- ss.head.Push(-2)
- ss.head.Push(5)
- ss.head.Push(4)
- reflect := ss.End(p)
- require.Equal(t, false, reflect)
- require.Equal(t, expected, ss)
- })
-}
-
-func TestUnder(t *testing.T) {
- t.Run("empty", func(t *testing.T) {
- expected := NewStackStack()
- ss := NewStackStack()
- reflect := ss.Under()
- require.Equal(t, true, reflect)
- require.Equal(t, expected, ss)
- })
- t.Run("positive", func(t *testing.T) {
- pe := pointer.NewPointer()
- expected := NewStackStack()
- expected.head.Push(1)
- expected.head.Push(2)
- expected.head.Push(3)
- expected.head.Push(6)
- expected.head.Push(0)
- expected.Begin(pe)
- expected.head.Push(0)
- expected.head.Push(0)
- expected.head.Push(6)
- expected.head.next.Pop()
- expected.head.next.Pop()
- expected.head.next.Pop()
- p := pointer.NewPointer()
- ss := NewStackStack()
- ss.head.Push(1)
- ss.head.Push(2)
- ss.head.Push(3)
- ss.head.Push(6)
- ss.head.Push(0)
- ss.Begin(p)
- ss.head.Push(3)
- reflect := ss.Under()
- require.Equal(t, false, reflect)
- require.Equal(t, expected, ss)
- })
- t.Run("negative", func(t *testing.T) {
- pe := pointer.NewPointer()
- expected := NewStackStack()
- expected.Begin(pe)
- expected.head.next.Push(12)
- expected.head.next.Push(5)
- expected.head.next.Push(8)
- expected.head.Push(8)
- expected.head.Push(5)
- expected.head.Push(12)
- expected.head.Push(-3)
- expected.head.Pop()
- expected.head.Pop()
- expected.head.Pop()
- expected.head.Pop()
- p := pointer.NewPointer()
- ss := NewStackStack()
- ss.Begin(p)
- ss.head.Push(8)
- ss.head.Push(5)
- ss.head.Push(12)
- ss.head.Push(-3)
- reflect := ss.Under()
- require.Equal(t, false, reflect)
- require.Equal(t, expected, ss)
- })
-}
diff --git a/pkg/stack/stack.go b/pkg/stack/stack.go
deleted file mode 100644
index 4dbb72c..0000000
--- a/pkg/stack/stack.go
+++ /dev/null
@@ -1,51 +0,0 @@
-package stack
-
-type Stack struct {
- size int
- height int
- data []int
- next *Stack // Pointer to the next element in the stack stack
-}
-
-func NewStack() *Stack {
- return &Stack{
- size: 32,
- height: 0,
- data: make([]int, 32),
- next: nil,
- }
-}
-
-func (s *Stack) Clear() {
- s.height = 0
-}
-
-func (s *Stack) Duplicate() {
- if s.height > 0 {
- s.Push(s.data[s.height-1])
- }
-}
-
-func (s *Stack) Pop() int {
- if s.height > 0 {
- s.height--
- return s.data[s.height]
- }
- return 0
-}
-
-func (s *Stack) Push(value int) {
- if s.height >= s.size {
- s.size += 32
- s.data = append(s.data, make([]int, 32)...)
- }
- s.data[s.height] = value
- s.height++
-}
-
-func (s *Stack) Swap() {
- a := s.Pop()
- b := s.Pop()
- s.Push(a)
- s.Push(b)
-}
diff --git a/pkg/stack/stack_test.go b/pkg/stack/stack_test.go
deleted file mode 100644
index e48c615..0000000
--- a/pkg/stack/stack_test.go
+++ /dev/null
@@ -1,74 +0,0 @@
-package stack
-
-import (
- "testing"
-
- "github.com/stretchr/testify/require"
-)
-
-func TestClear(t *testing.T) {
- s := NewStack()
- s.Clear()
- require.Equal(t, 0, s.height)
-}
-
-func TestDupicate(t *testing.T) {
- s := NewStack()
- s2 := NewStack()
- s.Duplicate()
- require.Equal(t, s2.height, s.height)
- s.Push(12)
- s.Duplicate()
- s2.Push(12)
- s2.Push(12)
- require.Equal(t, s2.height, s.height)
- require.Equal(t, s2.data, s.data)
-}
-
-func TestPop(t *testing.T) {
- s := NewStack()
- v := s.Pop()
- require.Equal(t, 0, v)
- s.Push(12)
- s.Push(14)
- v = s.Pop()
- require.Equal(t, 14, v)
- v = s.Pop()
- require.Equal(t, 12, v)
- v = s.Pop()
- require.Equal(t, 0, v)
-}
-
-func TestPush(t *testing.T) {
- s := NewStack()
- for i := 0; i < 32; i++ {
- s.Push(i)
- }
- require.Equal(t, 32, s.size)
- s.Push(-1)
- require.Equal(t, 64, s.size)
-}
-
-func TestSwap(t *testing.T) {
- s := NewStack()
- s2 := NewStack()
- s.Swap()
- s2.Push(0)
- s2.Push(0)
- require.Equal(t, s2, s)
- s.Clear()
- s.Push(1)
- s.Swap()
- s2.Clear()
- s2.Push(1)
- s2.Push(0)
- require.Equal(t, s2, s)
- s.Clear()
- s.Push(1)
- s.Push(2)
- s2.Clear()
- s2.Push(2)
- s2.Push(1)
- s.Swap()
- require.Equal(t, s2, s)
-}