|
| 1 | +/* |
| 2 | +Copyright 2017 Google Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 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 | + // s3 will be allocated outside of sarena |
| 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 | + // s4 should still fit in sarena |
| 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 | + // s3 will not change |
| 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 | +} |
0 commit comments