1
0
Fork 0
duckycode/pkg/encoder/encoder_test.go
2019-02-12 20:55:02 +01:00

96 lines
2 KiB
Go

// Copyright (C) 2019 ston1th
package encoder
import (
"bytes"
"io/ioutil"
"testing"
)
func TestComment(t *testing.T) {
var (
in = bytes.NewBufferString(`
REM Hello World
`)
out = new(bytes.Buffer)
)
err := NewEncoder(out, "de").Encode(in)
if err != nil {
t.Error(err)
}
if !bytes.Equal(out.Bytes(), nil) {
t.Error("bytes not equal")
}
}
func TestString(t *testing.T) {
var (
in = bytes.NewBufferString(`
STRING Hello World
`)
out = new(bytes.Buffer)
want = []byte{0xb, 0x2, 0x8, 0x0, 0xf, 0x0, 0xf, 0x0,
0x12, 0x0, 0x2c, 0x0, 0x1a, 0x2, 0x12, 0x0,
0x15, 0x0, 0xf, 0x0, 0x7, 0x0}
)
err := NewEncoder(out, "de").Encode(in)
if err != nil {
t.Error(err)
}
if !bytes.Equal(out.Bytes(), want) {
t.Error("bytes not equal")
}
}
func TestExample(t *testing.T) {
var (
in = bytes.NewBufferString(`
REM Type Hello World into Windows notepad. Target: Windows 95 and beyond. Author: Darren
DELAY 1000
GUI r
DELAY 100
STRING c:\windows\notepad.exe
ENTER
DELAY 1000
STRING Hello World
`)
out = new(bytes.Buffer)
want = []byte{0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xeb,
0x15, 0x8, 0x0, 0x64, 0x6, 0x0, 0x37, 0x2,
0x2d, 0x40, 0x1a, 0x0, 0xc, 0x0, 0x11, 0x0,
0x7, 0x0, 0x12, 0x0, 0x1a, 0x0, 0x16, 0x0,
0x2d, 0x40, 0x11, 0x0, 0x12, 0x0, 0x17, 0x0,
0x8, 0x0, 0x13, 0x0, 0x4, 0x0, 0x7, 0x0,
0x37, 0x0, 0x8, 0x0, 0x1b, 0x0, 0x8, 0x0,
0x28, 0x0, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff,
0x0, 0xeb, 0xb, 0x2, 0x8, 0x0, 0xf, 0x0,
0xf, 0x0, 0x12, 0x0, 0x2c, 0x0, 0x1a, 0x2,
0x12, 0x0, 0x15, 0x0, 0xf, 0x0, 0x7, 0x0}
)
err := NewEncoder(out, "de").Encode(in)
if err != nil {
t.Error(err)
}
if !bytes.Equal(out.Bytes(), want) {
t.Error("bytes not equal")
}
}
func BenchmarkExample(b *testing.B) {
var in = bytes.NewBufferString(`
REM Type Hello World into Windows notepad. Target: Windows 95 and beyond. Author: Darren
DELAY 1000
GUI r
DELAY 100
STRING c:\windows\notepad.exe
ENTER
DELAY 1000
STRING Hello World
`)
e := NewEncoder(ioutil.Discard, "de")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
e.Encode(in)
}
}