...

Source file src/github.com/rs/zerolog/internal/cbor/string_test.go

Documentation: github.com/rs/zerolog/internal/cbor

     1  package cbor
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  var encodeStringTests = []struct {
     9  	plain  string
    10  	binary string
    11  	json   string //begin and end quotes are implied
    12  }{
    13  	{"", "\x60", ""},
    14  	{"\\", "\x61\x5c", "\\\\"},
    15  	{"\x00", "\x61\x00", "\\u0000"},
    16  	{"\x01", "\x61\x01", "\\u0001"},
    17  	{"\x02", "\x61\x02", "\\u0002"},
    18  	{"\x03", "\x61\x03", "\\u0003"},
    19  	{"\x04", "\x61\x04", "\\u0004"},
    20  	{"*", "\x61*", "*"},
    21  	{"a", "\x61a", "a"},
    22  	{"IETF", "\x64IETF", "IETF"},
    23  	{"abcdefghijklmnopqrstuvwxyzABCD", "\x78\x1eabcdefghijklmnopqrstuvwxyzABCD", "abcdefghijklmnopqrstuvwxyzABCD"},
    24  	{"<------------------------------------  This is a 100 character string ----------------------------->" +
    25  		"<------------------------------------  This is a 100 character string ----------------------------->" +
    26  		"<------------------------------------  This is a 100 character string ----------------------------->",
    27  		"\x79\x01\x2c<------------------------------------  This is a 100 character string ----------------------------->" +
    28  			"<------------------------------------  This is a 100 character string ----------------------------->" +
    29  			"<------------------------------------  This is a 100 character string ----------------------------->",
    30  		"<------------------------------------  This is a 100 character string ----------------------------->" +
    31  			"<------------------------------------  This is a 100 character string ----------------------------->" +
    32  			"<------------------------------------  This is a 100 character string ----------------------------->"},
    33  	{"emoji \u2764\ufe0f!", "\x6demoji ❤️!", "emoji \u2764\ufe0f!"},
    34  }
    35  
    36  var encodeByteTests = []struct {
    37  	plain  []byte
    38  	binary string
    39  }{
    40  	{[]byte{}, "\x40"},
    41  	{[]byte("\\"), "\x41\x5c"},
    42  	{[]byte("\x00"), "\x41\x00"},
    43  	{[]byte("\x01"), "\x41\x01"},
    44  	{[]byte("\x02"), "\x41\x02"},
    45  	{[]byte("\x03"), "\x41\x03"},
    46  	{[]byte("\x04"), "\x41\x04"},
    47  	{[]byte("*"), "\x41*"},
    48  	{[]byte("a"), "\x41a"},
    49  	{[]byte("IETF"), "\x44IETF"},
    50  	{[]byte("abcdefghijklmnopqrstuvwxyzABCD"), "\x58\x1eabcdefghijklmnopqrstuvwxyzABCD"},
    51  	{[]byte("<------------------------------------  This is a 100 character string ----------------------------->" +
    52  		"<------------------------------------  This is a 100 character string ----------------------------->" +
    53  		"<------------------------------------  This is a 100 character string ----------------------------->"),
    54  		"\x59\x01\x2c<------------------------------------  This is a 100 character string ----------------------------->" +
    55  			"<------------------------------------  This is a 100 character string ----------------------------->" +
    56  			"<------------------------------------  This is a 100 character string ----------------------------->"},
    57  	{[]byte("emoji \u2764\ufe0f!"), "\x4demoji ❤️!"},
    58  }
    59  
    60  func TestAppendString(t *testing.T) {
    61  	for _, tt := range encodeStringTests {
    62  		b := enc.AppendString([]byte{}, tt.plain)
    63  		if got, want := string(b), tt.binary; got != want {
    64  			t.Errorf("appendString(%q) = %#q, want %#q", tt.plain, got, want)
    65  		}
    66  	}
    67  	//Test a large string > 65535 length
    68  
    69  	var buffer bytes.Buffer
    70  	for i := 0; i < 0x00011170; i++ { //70,000 character string
    71  		buffer.WriteString("a")
    72  	}
    73  	inp := buffer.String()
    74  	want := "\x7a\x00\x01\x11\x70" + inp
    75  	b := enc.AppendString([]byte{}, inp)
    76  	if got := string(b); got != want {
    77  		t.Errorf("appendString(%q) = %#q, want %#q", inp, got, want)
    78  	}
    79  }
    80  
    81  func TestAppendBytes(t *testing.T) {
    82  	for _, tt := range encodeByteTests {
    83  		b := enc.AppendBytes([]byte{}, tt.plain)
    84  		if got, want := string(b), tt.binary; got != want {
    85  			t.Errorf("appendString(%q) = %#q, want %#q", tt.plain, got, want)
    86  		}
    87  	}
    88  	//Test a large string > 65535 length
    89  
    90  	inp := []byte{}
    91  	for i := 0; i < 0x00011170; i++ { //70,000 character string
    92  		inp = append(inp, byte('a'))
    93  	}
    94  	want := "\x5a\x00\x01\x11\x70" + string(inp)
    95  	b := enc.AppendBytes([]byte{}, inp)
    96  	if got := string(b); got != want {
    97  		t.Errorf("appendString(%q) = %#q, want %#q", inp, got, want)
    98  	}
    99  }
   100  func BenchmarkAppendString(b *testing.B) {
   101  	tests := map[string]string{
   102  		"NoEncoding":       `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
   103  		"EncodingFirst":    `"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
   104  		"EncodingMiddle":   `aaaaaaaaaaaaaaaaaaaaaaaaa"aaaaaaaaaaaaaaaaaaaaaaaa`,
   105  		"EncodingLast":     `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"`,
   106  		"MultiBytesFirst":  `❤️aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
   107  		"MultiBytesMiddle": `aaaaaaaaaaaaaaaaaaaaaaaaa❤️aaaaaaaaaaaaaaaaaaaaaaaa`,
   108  		"MultiBytesLast":   `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa❤️`,
   109  	}
   110  	for name, str := range tests {
   111  		b.Run(name, func(b *testing.B) {
   112  			buf := make([]byte, 0, 120)
   113  			for i := 0; i < b.N; i++ {
   114  				_ = enc.AppendString(buf, str)
   115  			}
   116  		})
   117  	}
   118  }
   119  

View as plain text