Neale Pickett
·
2018-07-24
gapstring_test.go
1package gapstring
2
3import (
4 "bytes"
5 "testing"
6)
7
8func assertEqual(t *testing.T, name string, a, b interface{}) {
9 if a != b {
10 t.Errorf("%s: %#v != %#v", name, a, b)
11 }
12}
13
14func TestChunk(t *testing.T) {
15 var c chunk
16
17 c = chunk{gap: 2}
18 assertEqual(t, "gap chunk", c.length(), 2)
19
20 c = chunk{data: []byte("moo")}
21 assertEqual(t, "byte chunk", c.length(), 3)
22 assertEqual(t, "byte slice", string(c.slice(1, 3).data), "oo")
23}
24
25func TestGapString(t *testing.T) {
26 g := GapString{}
27
28 if 0 != bytes.Compare(g.Bytes(), []byte{}) {
29 t.Errorf("%#v.Bytes() != []byte{}", g)
30 }
31 if g.Length() != 0 {
32 t.Errorf("len(%#v) != 0", g)
33 }
34
35 g = g.Append(g)
36 if g.Length() != 0 {
37 t.Errorf("Appending two emtpy gapstrings")
38 }
39
40 g = g.AppendString("moo")
41 if 0 != bytes.Compare(g.Bytes(), []byte("moo")) {
42 t.Errorf("Simple string")
43 }
44
45 g = g.AppendString("bar")
46 if g.String("") != "moobar" {
47 t.Errorf("Append")
48 }
49 if g.Missing() != 0 {
50 t.Errorf("Missing when there shouldn't be any missing")
51 }
52
53 g = g.AppendGap(8)
54 if g.Length() != 3+3+8 {
55 t.Errorf("Length after gap append")
56 }
57 if g.Missing() != 8 {
58 t.Errorf("Gap miscounted")
59 }
60
61 g = g.AppendString("baz")
62 assertEqual(t, "string", g.String(""), "moobarbaz")
63 assertEqual(t, "string drop", g.String("DROP"), "moobarOPDROPDRbaz")
64
65 assertEqual(t, "xor", g.Xor(1).String(""), "lnnc`sc`{")
66 assertEqual(t, "xor drop", g.Xor(1).String("DROP"), "lnnc`sOPDROPDRc`{")
67
68 assertEqual(t, "slice", g.Slice(2, 5).String(""), "oba")
69 assertEqual(t, "slice+xor", g.Slice(2, 5).Xor(1).String(""), "nc`")
70
71 hexdump :=
72 "00000000 6d 6f 6f 62 61 72 -- -- -- -- -- -- -- -- 62 61 moobar��������ba\n" +
73 "00000010 7a z\n" +
74 "00000011\n"
75 assertEqual(t, "hexdump", g.Hexdump(), hexdump)
76}