2021-09-23 11:45:49 +02:00
|
|
|
package pointer
|
2021-09-22 00:31:53 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-24 10:09:10 +02:00
|
|
|
"git.adyxax.org/adyxax/gofunge98/pkg/field"
|
2021-11-12 15:26:16 +01:00
|
|
|
"git.adyxax.org/adyxax/gofunge98/pkg/stack"
|
2021-09-22 00:31:53 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBegin(t *testing.T) {
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
expected := NewStackStack()
|
2021-11-12 15:26:16 +01:00
|
|
|
expected.head = stack.NewStack(0, expected.head)
|
|
|
|
expected.head.Next().Push(0)
|
|
|
|
expected.head.Next().Push(0)
|
2021-09-22 00:31:53 +02:00
|
|
|
expected.height++
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 00:31:53 +02:00
|
|
|
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
|
2021-11-12 15:26:16 +01:00
|
|
|
expected.head = stack.NewStack(0, expected.head)
|
|
|
|
expected.head.Next().Push(1)
|
|
|
|
expected.head.Next().Push(0)
|
2021-09-22 00:31:53 +02:00
|
|
|
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()
|
2021-11-12 15:26:16 +01:00
|
|
|
expected.head = stack.NewStack(5, expected.head)
|
|
|
|
expected.head.Next().Push(0)
|
|
|
|
expected.head.Next().Push(0)
|
|
|
|
expected.head.Next().Push(0)
|
|
|
|
expected.head.Next().Push(0)
|
|
|
|
expected.head.Next().Push(0)
|
|
|
|
expected.head.Next().Push(0)
|
|
|
|
expected.head.Next().Push(0)
|
2021-09-22 00:31:53 +02:00
|
|
|
expected.height++
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-22 00:31:53 +02:00
|
|
|
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)
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 00:31:53 +02:00
|
|
|
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()
|
2021-11-12 15:26:16 +01:00
|
|
|
expected.head = stack.NewStack(34, expected.head)
|
|
|
|
for i := 0; i < 33; i++ {
|
|
|
|
expected.head.Push(0)
|
|
|
|
}
|
|
|
|
expected.head.Push(18)
|
|
|
|
expected.head.Next().Push(2)
|
|
|
|
expected.head.Next().Push(3)
|
2021-09-22 00:31:53 +02:00
|
|
|
expected.height++
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-22 00:31:53 +02:00
|
|
|
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)
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 00:31:53 +02:00
|
|
|
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()
|
2021-11-12 15:26:16 +01:00
|
|
|
expected.head = stack.NewStack(4, expected.head)
|
|
|
|
expected.head.Push(12)
|
|
|
|
expected.head.Push(14)
|
|
|
|
expected.head.Push(-2)
|
|
|
|
expected.head.Push(5)
|
|
|
|
expected.head.Next().Push(7)
|
|
|
|
expected.head.Next().Push(36)
|
|
|
|
expected.head.Next().Push(42)
|
|
|
|
expected.head.Next().Push(-2)
|
|
|
|
expected.head.Next().Push(5)
|
|
|
|
expected.head.Next().Push(4)
|
|
|
|
expected.head.Next().Pop()
|
|
|
|
expected.head.Next().Pop()
|
|
|
|
expected.head.Next().Pop()
|
2021-09-22 00:31:53 +02:00
|
|
|
expected.height++
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-22 00:31:53 +02:00
|
|
|
p.SetStorageOffset(36, 42)
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 00:31:53 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
2021-09-22 23:34:59 +02:00
|
|
|
|
|
|
|
func TestEnd(t *testing.T) {
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
expected := NewStackStack()
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 23:34:59 +02:00
|
|
|
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()
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 23:34:59 +02:00
|
|
|
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()
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 23:34:59 +02:00
|
|
|
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()
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 23:34:59 +02:00
|
|
|
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.Push(7)
|
|
|
|
expected.head.Push(12)
|
|
|
|
expected.head.Push(14)
|
|
|
|
expected.head.Push(-2)
|
|
|
|
expected.head.Push(5)
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 23:34:59 +02:00
|
|
|
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()
|
2021-09-24 16:45:18 +02:00
|
|
|
p := NewPointer()
|
|
|
|
reflect := p.ss.Under()
|
2021-09-22 23:34:59 +02:00
|
|
|
require.Equal(t, true, reflect)
|
2021-09-24 16:45:18 +02:00
|
|
|
require.Equal(t, expected, p.ss)
|
2021-09-22 23:34:59 +02:00
|
|
|
})
|
|
|
|
t.Run("positive", func(t *testing.T) {
|
2021-09-23 11:45:49 +02:00
|
|
|
pe := NewPointer()
|
2021-09-22 23:34:59 +02:00
|
|
|
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)
|
2021-11-12 15:26:16 +01:00
|
|
|
expected.head.Next().Pop()
|
|
|
|
expected.head.Next().Pop()
|
|
|
|
expected.head.Next().Pop()
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 23:34:59 +02:00
|
|
|
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) {
|
2021-09-23 11:45:49 +02:00
|
|
|
pe := NewPointer()
|
2021-09-22 23:34:59 +02:00
|
|
|
expected := NewStackStack()
|
|
|
|
expected.Begin(pe)
|
2021-11-12 15:26:16 +01:00
|
|
|
expected.head.Next().Push(12)
|
|
|
|
expected.head.Next().Push(5)
|
|
|
|
expected.head.Next().Push(8)
|
2021-09-22 23:34:59 +02:00
|
|
|
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()
|
2021-09-23 11:45:49 +02:00
|
|
|
p := NewPointer()
|
2021-09-24 16:45:18 +02:00
|
|
|
ss := p.ss
|
2021-09-22 23:34:59 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|