2018-07-19 21:29:26 -06:00
|
|
|
|
package gapstring
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2018-07-21 12:00:26 -06:00
|
|
|
|
func assertEqual(t *testing.T, name string, a, b interface{}) {
|
2018-07-19 21:29:26 -06:00
|
|
|
|
if a != b {
|
|
|
|
|
t.Errorf("%s: %#v != %#v", name, a, b)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestChunk(t *testing.T) {
|
|
|
|
|
var c chunk
|
|
|
|
|
|
|
|
|
|
c = chunk{gap: 2}
|
2018-07-21 12:00:26 -06:00
|
|
|
|
assertEqual(t, "gap chunk", c.length(), 2)
|
2018-07-19 21:29:26 -06:00
|
|
|
|
|
|
|
|
|
c = chunk{data: []byte("moo")}
|
2018-07-21 12:00:26 -06:00
|
|
|
|
assertEqual(t, "byte chunk", c.length(), 3)
|
|
|
|
|
assertEqual(t, "byte slice", string(c.slice(1,3).data), "oo")
|
2018-07-19 21:29:26 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGapString(t *testing.T) {
|
|
|
|
|
g := GapString{}
|
|
|
|
|
|
|
|
|
|
if 0 != bytes.Compare(g.Bytes(), []byte{}) {
|
|
|
|
|
t.Errorf("%#v.Bytes() != []byte{}", g)
|
|
|
|
|
}
|
|
|
|
|
if g.Length() != 0 {
|
|
|
|
|
t.Errorf("len(%#v) != 0", g)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g = g.Append(g)
|
|
|
|
|
if g.Length() != 0 {
|
|
|
|
|
t.Errorf("Appending two emtpy gapstrings")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g = g.AppendString("moo")
|
|
|
|
|
if 0 != bytes.Compare(g.Bytes(), []byte("moo")) {
|
|
|
|
|
t.Errorf("Simple string")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g = g.AppendString("bar")
|
|
|
|
|
if g.String("") != "moobar" {
|
|
|
|
|
t.Errorf("Append")
|
|
|
|
|
}
|
|
|
|
|
if g.Missing() != 0 {
|
|
|
|
|
t.Errorf("Missing when there shouldn't be any missing")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g = g.AppendGap(8)
|
|
|
|
|
if g.Length() != 3+3+8 {
|
|
|
|
|
t.Errorf("Length after gap append")
|
|
|
|
|
}
|
|
|
|
|
if g.Missing() != 8 {
|
|
|
|
|
t.Errorf("Gap miscounted")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g = g.AppendString("baz")
|
2018-07-21 12:00:26 -06:00
|
|
|
|
assertEqual(t, "string", g.String(""), "moobarbaz")
|
|
|
|
|
assertEqual(t, "string drop", g.String("DROP"), "moobarOPDROPDRbaz")
|
|
|
|
|
|
|
|
|
|
assertEqual(t, "xor", g.Xor(1).String(""), "lnnc`sc`{")
|
|
|
|
|
assertEqual(t, "xor drop", g.Xor(1).String("DROP"), "lnnc`sOPDROPDRc`{")
|
2018-07-19 21:29:26 -06:00
|
|
|
|
|
2018-07-21 12:00:26 -06:00
|
|
|
|
assertEqual(t, "slice", g.Slice(2, 5).String(""), "oba")
|
|
|
|
|
assertEqual(t, "slice+xor", g.Slice(2, 5).Xor(1).String(""), "nc`")
|
2018-07-19 21:29:26 -06:00
|
|
|
|
|
|
|
|
|
hexdump :=
|
|
|
|
|
"00000000 6d 6f 6f 62 61 72 -- -- -- -- -- -- -- -- 62 61 moobar<61><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ba\n" +
|
|
|
|
|
"00000010 7a z\n" +
|
|
|
|
|
"00000011\n"
|
2018-07-21 12:00:26 -06:00
|
|
|
|
assertEqual(t, "hexdump", g.Hexdump(), hexdump)
|
2018-07-19 21:29:26 -06:00
|
|
|
|
}
|