2023-04-10 09:07:08 +02:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2023-04-12 08:56:02 +02:00
|
|
|
"fmt"
|
2023-04-10 09:07:08 +02:00
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-04-14 10:59:37 +02:00
|
|
|
"git.defalsify.org/vise/cache"
|
|
|
|
"git.defalsify.org/vise/resource"
|
|
|
|
"git.defalsify.org/vise/state"
|
2023-04-10 09:07:08 +02:00
|
|
|
)
|
|
|
|
|
2023-04-12 15:20:09 +02:00
|
|
|
func TestLoopTop(t *testing.T) {
|
|
|
|
generateTestData(t)
|
|
|
|
ctx := context.TODO()
|
|
|
|
st := state.NewState(0)
|
|
|
|
rs := resource.NewFsResource(dataDir)
|
|
|
|
ca := cache.NewCache().WithCacheSize(1024)
|
2023-04-13 01:38:33 +02:00
|
|
|
|
|
|
|
cfg := Config{
|
|
|
|
Root: "root",
|
|
|
|
}
|
|
|
|
en := NewEngine(cfg, &st, &rs, ca, ctx)
|
2023-04-12 15:20:09 +02:00
|
|
|
err := en.Init("root", ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
input := []string{
|
|
|
|
"2",
|
|
|
|
"j",
|
|
|
|
"1",
|
|
|
|
}
|
|
|
|
inputStr := strings.Join(input, "\n")
|
|
|
|
inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
|
|
|
|
outputBuf := bytes.NewBuffer(nil)
|
|
|
|
log.Printf("running with input: %s", inputBuf.Bytes())
|
|
|
|
|
2023-04-13 01:38:33 +02:00
|
|
|
err = Loop(&en, inputBuf, outputBuf, ctx)
|
2023-04-12 15:20:09 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
location, _ := st.Where()
|
|
|
|
if location != "foo" {
|
|
|
|
fmt.Errorf("expected location 'foo', got %s", location)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 08:56:02 +02:00
|
|
|
func TestLoopBackForth(t *testing.T) {
|
2023-04-10 09:07:08 +02:00
|
|
|
generateTestData(t)
|
|
|
|
ctx := context.TODO()
|
|
|
|
st := state.NewState(0)
|
|
|
|
rs := resource.NewFsResource(dataDir)
|
|
|
|
ca := cache.NewCache().WithCacheSize(1024)
|
|
|
|
|
2023-04-13 01:38:33 +02:00
|
|
|
cfg := Config{
|
|
|
|
Root: "root",
|
|
|
|
}
|
|
|
|
en := NewEngine(cfg, &st, &rs, ca, ctx)
|
2023-04-10 09:07:08 +02:00
|
|
|
err := en.Init("root", ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
input := []string{
|
|
|
|
"1",
|
|
|
|
"0",
|
|
|
|
"1",
|
|
|
|
"0",
|
|
|
|
}
|
|
|
|
inputStr := strings.Join(input, "\n")
|
|
|
|
inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
|
|
|
|
outputBuf := bytes.NewBuffer(nil)
|
|
|
|
log.Printf("running with input: %s", inputBuf.Bytes())
|
|
|
|
|
2023-04-13 01:38:33 +02:00
|
|
|
err = Loop(&en, inputBuf, outputBuf, ctx)
|
2023-04-10 09:07:08 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 08:56:02 +02:00
|
|
|
|
|
|
|
func TestLoopBrowse(t *testing.T) {
|
|
|
|
generateTestData(t)
|
|
|
|
ctx := context.TODO()
|
|
|
|
st := state.NewState(0)
|
|
|
|
rs := resource.NewFsResource(dataDir)
|
|
|
|
ca := cache.NewCache().WithCacheSize(1024)
|
|
|
|
|
|
|
|
cfg := Config{
|
|
|
|
OutputSize: 68,
|
2023-04-13 01:38:33 +02:00
|
|
|
Root: "root",
|
2023-04-12 08:56:02 +02:00
|
|
|
}
|
2023-04-13 01:38:33 +02:00
|
|
|
en := NewEngine(cfg, &st, &rs, ca, ctx)
|
2023-04-12 08:56:02 +02:00
|
|
|
err := en.Init("root", ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
input := []string{
|
|
|
|
"1",
|
|
|
|
"2",
|
|
|
|
"00",
|
|
|
|
"11",
|
|
|
|
"00",
|
|
|
|
}
|
|
|
|
inputStr := strings.Join(input, "\n")
|
|
|
|
inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
|
|
|
|
outputBuf := bytes.NewBuffer(nil)
|
|
|
|
log.Printf("running with input: %s", inputBuf.Bytes())
|
|
|
|
|
2023-04-13 01:38:33 +02:00
|
|
|
err = Loop(&en, inputBuf, outputBuf, ctx)
|
2023-04-12 08:56:02 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
location, idx := st.Where()
|
|
|
|
if location != "long" {
|
|
|
|
fmt.Errorf("expected location 'long', got %s", location)
|
|
|
|
}
|
|
|
|
if idx != 1 {
|
|
|
|
fmt.Errorf("expected idx 1, got %v", idx)
|
|
|
|
}
|
|
|
|
}
|