func trace(prog bit.Program, buf *bytes.Buffer, step func(interface{}, interface{}), ch chan<- interface{}) { ctx, err := bitdebug.RunTrace(prog, nil, bitio.NewWriter(buf), step) if err != nil { handle(err) panic("unreachable") } ch <- ctx }
func (p Program) RunByte(r io.ByteReader, w io.ByteWriter) error { in := bitio.NewReader(r) out := bitio.NewWriter(w) if err := p.Run(in, out); err != nil { return err } // flush any remaining bits for i := 0; i < 7; i++ { if err := out.WriteBit(false); err != nil { return err } } return nil }
func testOptRaw(t *testing.T, f func(w *bitgen.Writer) (int64, error)) { r, w := io.Pipe() errs := make(chan error, 2) ch := make(chan Program, 1) go func() { bw := bitgen.NewWriter(w) _, err := f(bw) if err != nil { t.Error(err) errs <- err return } err = bw.Close() if err != nil { t.Error(err) errs <- err return } err = w.Close() if err != nil { t.Error(err) errs <- err return } }() go func() { prog, err := Parse(bufio.NewReader(r)) if err != nil { t.Error(err) errs <- err return } ch <- prog }() select { case err := <-errs: panic(err) case prog := <-ch: var buf0, buf1 bytes.Buffer ctx0, err := prog.run(nil, bitio.NewWriter(&buf0), nil) if err != nil { panic(err) } prog.Optimize() trace := func(l *line, c *context) { if l.opt != nil { t.Logf("%d: %#v", l.num, l.opt) } else { t.Logf("%d: %#v", l.num, l.stmt) } } ctx1, err := prog.run(nil, bitio.NewWriter(&buf1), trace) if err != nil { panic(err) } if !reflect.DeepEqual(ctx0, ctx1) || !bytes.Equal(buf0.Bytes(), buf1.Bytes()) { t.Errorf("expected equal:\n%#v\n%q\n%#v\n%q", ctx0, buf0.Bytes(), ctx1, buf1.Bytes()) } } }