aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorJulien Dessaux2021-09-22 23:34:59 +0200
committerJulien Dessaux2021-09-22 23:34:59 +0200
commitea72b9dbb21ebd249b84bdb17d34cf70d14daf1b (patch)
tree81afadd0f48bae8153e3bbd73f0e4fe63cbd544d /pkg
parentContinued implementing the stack stack (diff)
downloadgofunge98-ea72b9dbb21ebd249b84bdb17d34cf70d14daf1b.tar.gz
gofunge98-ea72b9dbb21ebd249b84bdb17d34cf70d14daf1b.tar.bz2
gofunge98-ea72b9dbb21ebd249b84bdb17d34cf70d14daf1b.zip
Finished implementing the stack stack
Diffstat (limited to '')
-rw-r--r--pkg/stack/stack-stack.go46
-rw-r--r--pkg/stack/stack-stack_test.go149
2 files changed, 195 insertions, 0 deletions
diff --git a/pkg/stack/stack-stack.go b/pkg/stack/stack-stack.go
index adc41c6..9b80079 100644
--- a/pkg/stack/stack-stack.go
+++ b/pkg/stack/stack-stack.go
@@ -43,3 +43,49 @@ func (ss *StackStack) Begin(p *pointer.Pointer) {
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
index 6c0c47f..d2a7949 100644
--- a/pkg/stack/stack-stack_test.go
+++ b/pkg/stack/stack-stack_test.go
@@ -100,3 +100,152 @@ func TestBegin(t *testing.T) {
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)
+ })
+}