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