go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

loop_test.go (2501B)


      1 package engine
      2 
      3 import (
      4 	"bytes"
      5 	"context"
      6 	"fmt"
      7 	"log"
      8 	"strings"
      9 	"testing"
     10 
     11 	"git.defalsify.org/vise.git/cache"
     12 	"git.defalsify.org/vise.git/resource"
     13 	"git.defalsify.org/vise.git/state"
     14 )
     15 
     16 func TestLoopTop(t *testing.T) {
     17 	generateTestData(t)
     18 	ctx := context.TODO()
     19 	st := state.NewState(0)
     20 	rs := resource.NewFsResource(dataDir)
     21 	ca := cache.NewCache().WithCacheSize(1024)
     22 
     23 	cfg := Config{
     24 		Root: "root",
     25 	}
     26 	en := NewEngine(ctx, cfg, &st, rs, ca)
     27 	var err error
     28 	_, err = en.Init(ctx)
     29 	if err != nil {
     30 		t.Fatal(err)
     31 	}
     32 
     33 	input := []string{
     34 		"2",
     35 		"j",
     36 		"1",
     37 		}		
     38 	inputStr := strings.Join(input, "\n")
     39 	inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
     40 	outputBuf := bytes.NewBuffer(nil)
     41 	log.Printf("running with input: %s", inputBuf.Bytes())
     42 
     43 	err = Loop(ctx, &en, inputBuf, outputBuf)
     44 	if err != nil {
     45 		t.Fatal(err)
     46 	}
     47 	location, _ := st.Where()
     48 	if location != "foo" {
     49 		fmt.Errorf("expected location 'foo', got %s", location)
     50 	}
     51 }
     52 
     53 func TestLoopBackForth(t *testing.T) {
     54 	generateTestData(t)
     55 	ctx := context.TODO()
     56 	st := state.NewState(0)
     57 	rs := resource.NewFsResource(dataDir)
     58 	ca := cache.NewCache().WithCacheSize(1024)
     59 	
     60 	cfg := Config{
     61 		Root: "root",
     62 	}
     63 	en := NewEngine(ctx, cfg, &st, rs, ca)
     64 	var err error
     65 	_, err = en.Init(ctx)
     66 	if err != nil {
     67 		t.Fatal(err)
     68 	}
     69 
     70 	input := []string{
     71 		"1",
     72 		"0",
     73 		"1",
     74 		"0",
     75 		}		
     76 	inputStr := strings.Join(input, "\n")
     77 	inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
     78 	outputBuf := bytes.NewBuffer(nil)
     79 	log.Printf("running with input: %s", inputBuf.Bytes())
     80 
     81 	err = Loop(ctx, &en, inputBuf, outputBuf)
     82 	if err != nil {
     83 		t.Fatal(err)
     84 	}
     85 }
     86 
     87 func TestLoopBrowse(t *testing.T) {
     88 	generateTestData(t)
     89 	ctx := context.TODO()
     90 	st := state.NewState(0)
     91 	rs := resource.NewFsResource(dataDir)
     92 	ca := cache.NewCache().WithCacheSize(1024)
     93 
     94 	cfg := Config{
     95 		OutputSize: 68,
     96 		Root: "root",
     97 	}
     98 	en := NewEngine(ctx, cfg, &st, rs, ca)
     99 	var err error
    100 	_, err = en.Init(ctx)
    101 	if err != nil {
    102 		t.Fatal(err)
    103 	}
    104 
    105 	input := []string{
    106 		"1",
    107 		"2",
    108 		"00",
    109 		"11",
    110 		"00",
    111 		}
    112 	inputStr := strings.Join(input, "\n")
    113 	inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
    114 	outputBuf := bytes.NewBuffer(nil)
    115 	log.Printf("running with input: %s", inputBuf.Bytes())
    116 
    117 	err = Loop(ctx, &en, inputBuf, outputBuf)
    118 	if err != nil {
    119 		t.Fatal(err)
    120 	}
    121 
    122 	location, idx := st.Where()
    123 	if location != "long" {
    124 		fmt.Errorf("expected location 'long', got %s", location)
    125 	}
    126 	if idx != 1 {
    127 		fmt.Errorf("expected idx 1, got %v", idx)
    128 	}
    129 }