| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package hack |
| 18 |
|
| 19 |
import "testing" |
| 20 |
|
| 21 |
func TestStringArena(t *testing.T) { |
| 22 |
sarena := NewStringArena(10) |
| 23 |
|
| 24 |
s0 := sarena.NewString(nil) |
| 25 |
checkint(t, len(sarena.buf), 0) |
| 26 |
checkint(t, sarena.SpaceLeft(), 10) |
| 27 |
checkstring(t, s0, "") |
| 28 |
|
| 29 |
s1 := sarena.NewString([]byte("01234")) |
| 30 |
checkint(t, len(sarena.buf), 5) |
| 31 |
checkint(t, sarena.SpaceLeft(), 5) |
| 32 |
checkstring(t, s1, "01234") |
| 33 |
|
| 34 |
s2 := sarena.NewString([]byte("5678")) |
| 35 |
checkint(t, len(sarena.buf), 9) |
| 36 |
checkint(t, sarena.SpaceLeft(), 1) |
| 37 |
checkstring(t, s2, "5678") |
| 38 |
|
| 39 |
|
| 40 |
s3 := sarena.NewString([]byte("ab")) |
| 41 |
checkint(t, len(sarena.buf), 9) |
| 42 |
checkint(t, sarena.SpaceLeft(), 1) |
| 43 |
checkstring(t, s3, "ab") |
| 44 |
|
| 45 |
|
| 46 |
s4 := sarena.NewString([]byte("9")) |
| 47 |
checkint(t, len(sarena.buf), 10) |
| 48 |
checkint(t, sarena.SpaceLeft(), 0) |
| 49 |
checkstring(t, s4, "9") |
| 50 |
|
| 51 |
sarena.buf[0] = 'A' |
| 52 |
checkstring(t, s1, "A1234") |
| 53 |
|
| 54 |
sarena.buf[5] = 'B' |
| 55 |
checkstring(t, s2, "B678") |
| 56 |
|
| 57 |
sarena.buf[9] = 'C' |
| 58 |
|
| 59 |
checkstring(t, s3, "ab") |
| 60 |
checkstring(t, s4, "C") |
| 61 |
checkstring(t, sarena.str, "A1234B678C") |
| 62 |
} |
| 63 |
|
| 64 |
func checkstring(t *testing.T, actual, expected string) { |
| 65 |
if actual != expected { |
| 66 |
t.Errorf("received %s, expecting %s", actual, expected) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func checkint(t *testing.T, actual, expected int) { |
| 71 |
if actual != expected { |
| 72 |
t.Errorf("received %d, expecting %d", actual, expected) |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestByteToString(t *testing.T) { |
| 77 |
v1 := []byte("1234") |
| 78 |
if s := String(v1); s != "1234" { |
| 79 |
t.Errorf("String(\"1234\"): %q, want 1234", s) |
| 80 |
} |
| 81 |
|
| 82 |
v1 = []byte("") |
| 83 |
if s := String(v1); s != "" { |
| 84 |
t.Errorf("String(\"\"): %q, want empty", s) |
| 85 |
} |
| 86 |
|
| 87 |
v1 = nil |
| 88 |
if s := String(v1); s != "" { |
| 89 |
t.Errorf("String(\"\"): %q, want empty", s) |
| 90 |
} |
| 91 |
} |