MCPcopy
hub / github.com/rs/zerolog / TestStringBytes

Function TestStringBytes

internal/json/bytes_test.go:30–65  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

28}
29
30func TestStringBytes(t *testing.T) {
31 t.Parallel()
32 // Test that encodeState.stringBytes and encodeState.string use the same encoding.
33 var r []rune
34 for i := '\u0000'; i <= unicode.MaxRune; i++ {
35 r = append(r, i)
36 }
37 s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
38
39 encStr := string(enc.AppendString([]byte{}, s))
40 encBytes := string(enc.AppendBytes([]byte{}, []byte(s)))
41
42 if encStr != encBytes {
43 i := 0
44 for i < len(encStr) && i < len(encBytes) && encStr[i] == encBytes[i] {
45 i++
46 }
47 encStr = encStr[i:]
48 encBytes = encBytes[i:]
49 i = 0
50 for i < len(encStr) && i < len(encBytes) && encStr[len(encStr)-i-1] == encBytes[len(encBytes)-i-1] {
51 i++
52 }
53 encStr = encStr[:len(encStr)-i]
54 encBytes = encBytes[:len(encBytes)-i]
55
56 if len(encStr) > 20 {
57 encStr = encStr[:20] + "..."
58 }
59 if len(encBytes) > 20 {
60 encBytes = encBytes[:20] + "..."
61 }
62
63 t.Errorf("encodings differ at %#q vs %#q", encStr, encBytes)
64 }
65}
66
67func BenchmarkAppendBytes(b *testing.B) {
68 tests := map[string]string{

Callers

nothing calls this directly

Calls 2

AppendStringMethod · 0.65
AppendBytesMethod · 0.65

Tested by

no test coverage detected