2012-06-07 22:55:06 +04:00
|
|
|
// Copyright 2012, Google Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2012-02-25 11:30:03 +04:00
|
|
|
|
|
|
|
package hack
|
|
|
|
|
2015-07-31 19:10:57 +03:00
|
|
|
import "testing"
|
2012-02-25 11:30:03 +04:00
|
|
|
|
|
|
|
func TestStringArena(t *testing.T) {
|
|
|
|
sarena := NewStringArena(10)
|
|
|
|
|
2015-07-31 19:10:57 +03:00
|
|
|
s0 := sarena.NewString(nil)
|
|
|
|
checkint(t, len(sarena.buf), 0)
|
|
|
|
checkint(t, sarena.SpaceLeft(), 10)
|
|
|
|
checkstring(t, s0, "")
|
|
|
|
|
|
|
|
s1 := sarena.NewString([]byte("01234"))
|
2012-02-25 11:30:03 +04:00
|
|
|
checkint(t, len(sarena.buf), 5)
|
|
|
|
checkint(t, sarena.SpaceLeft(), 5)
|
|
|
|
checkstring(t, s1, "01234")
|
|
|
|
|
2015-07-31 19:10:57 +03:00
|
|
|
s2 := sarena.NewString([]byte("5678"))
|
2012-02-25 11:30:03 +04:00
|
|
|
checkint(t, len(sarena.buf), 9)
|
|
|
|
checkint(t, sarena.SpaceLeft(), 1)
|
|
|
|
checkstring(t, s2, "5678")
|
|
|
|
|
|
|
|
// s3 will be allocated outside of sarena
|
2015-07-31 19:10:57 +03:00
|
|
|
s3 := sarena.NewString([]byte("ab"))
|
2012-02-25 11:30:03 +04:00
|
|
|
checkint(t, len(sarena.buf), 9)
|
|
|
|
checkint(t, sarena.SpaceLeft(), 1)
|
|
|
|
checkstring(t, s3, "ab")
|
|
|
|
|
|
|
|
// s4 should still fit in sarena
|
2015-07-31 19:10:57 +03:00
|
|
|
s4 := sarena.NewString([]byte("9"))
|
2012-02-25 11:30:03 +04:00
|
|
|
checkint(t, len(sarena.buf), 10)
|
|
|
|
checkint(t, sarena.SpaceLeft(), 0)
|
|
|
|
checkstring(t, s4, "9")
|
|
|
|
|
|
|
|
sarena.buf[0] = 'A'
|
|
|
|
checkstring(t, s1, "A1234")
|
|
|
|
|
|
|
|
sarena.buf[5] = 'B'
|
|
|
|
checkstring(t, s2, "B678")
|
|
|
|
|
|
|
|
sarena.buf[9] = 'C'
|
|
|
|
// s3 will not change
|
|
|
|
checkstring(t, s3, "ab")
|
|
|
|
checkstring(t, s4, "C")
|
|
|
|
checkstring(t, sarena.str, "A1234B678C")
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkstring(t *testing.T, actual, expected string) {
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("received %s, expecting %s", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkint(t *testing.T, actual, expected int) {
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("received %d, expecting %d", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 19:10:57 +03:00
|
|
|
|
|
|
|
func TestByteToString(t *testing.T) {
|
|
|
|
v1 := []byte("1234")
|
|
|
|
if s := String(v1); s != "1234" {
|
|
|
|
t.Errorf("String(\"1234\"): %q, want 1234", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
v1 = []byte("")
|
|
|
|
if s := String(v1); s != "" {
|
|
|
|
t.Errorf("String(\"\"): %q, want empty", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
v1 = nil
|
|
|
|
if s := String(v1); s != "" {
|
|
|
|
t.Errorf("String(\"\"): %q, want empty", s)
|
|
|
|
}
|
|
|
|
}
|