syntax and style changes

This commit is contained in:
Trek H 2019-07-11 14:54:06 +09:30
parent d48a11794b
commit be389fca6e
3 changed files with 17 additions and 18 deletions

View File

@ -52,17 +52,16 @@ func (l *ByteLexer) Lex(dst io.Writer, src io.Reader, t time.Duration) error {
if t < 0 {
return fmt.Errorf("invalid delay: %v", t)
}
var tick <-chan time.Time
var ticker *time.Ticker
if t > 0 {
ticker := time.NewTicker(t)
ticker = time.NewTicker(t)
defer ticker.Stop()
tick = ticker.C
}
buf := make([]byte, bufSize)
for {
if t != 0 {
<-tick
<-ticker.C
}
off, err := src.Read(buf)
if err != nil {

View File

@ -36,15 +36,15 @@ var lexTests = []struct {
data []byte
t time.Duration
n int
fail bool // Whether or not this test should fail.
isValid bool // Whether or not this test should fail.
}{
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 4, false},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 3, false},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Duration(0), 2, false},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Duration(0), 1, false},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Nanosecond, 0, true},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, -1, true},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 15, false},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 4, true},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 3, true},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, 0, 2, true},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, 0, 1, true},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Nanosecond, 0, false},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, -1, false},
{[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 15, true},
}
func TestByteLexer(t *testing.T) {
@ -54,8 +54,8 @@ func TestByteLexer(t *testing.T) {
l := NewByteLexer(&tt.n)
err := l.Lex(dst, bytes.NewReader(tt.data), tt.t)
if err != nil && err != io.EOF {
if !tt.fail {
t.Errorf("unexpected error: %v", err.Error())
if tt.isValid {
t.Errorf("unexpected error: %v", err)
}
} else if !bytes.Equal(dst.Bytes(), tt.data) {
t.Errorf("data before and after lex are not equal: want %v, got %v", tt.data, dst.Bytes())

View File

@ -54,7 +54,7 @@ const (
// "paused" means the input routine is sleeping until unpaused or stopped.
// "stopped" means the input routine is stopped and the ALSA device is closed.
const (
running = iota
running = iota + 1
paused
stopped
)