File: strings_test.go

package info (click to toggle)
golang-github-influxdata-influxdb1-client 0.0~git20220302.a9ab567-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 380 kB
  • sloc: makefile: 2
file content (115 lines) | stat: -rw-r--r-- 2,134 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package escape

import (
	"testing"
)

var s string

func BenchmarkStringEscapeNoEscapes(b *testing.B) {
	for n := 0; n < b.N; n++ {
		s = String("no_escapes")
	}
}

func BenchmarkStringUnescapeNoEscapes(b *testing.B) {
	for n := 0; n < b.N; n++ {
		s = UnescapeString("no_escapes")
	}
}

func BenchmarkManyStringEscape(b *testing.B) {
	tests := []string{
		"this is my special string",
		"a field w=i th == tons of escapes",
		"some,commas,here",
	}

	for n := 0; n < b.N; n++ {
		for _, test := range tests {
			s = String(test)
		}
	}
}

func BenchmarkManyStringUnescape(b *testing.B) {
	tests := []string{
		`this\ is\ my\ special\ string`,
		`a\ field\ w\=i\ th\ \=\=\ tons\ of\ escapes`,
		`some\,commas\,here`,
	}

	for n := 0; n < b.N; n++ {
		for _, test := range tests {
			s = UnescapeString(test)
		}
	}
}

func TestStringEscape(t *testing.T) {
	tests := []struct {
		in       string
		expected string
	}{
		{
			in:       "",
			expected: "",
		},
		{
			in:       "this is my special string",
			expected: `this\ is\ my\ special\ string`,
		},
		{
			in:       "a field w=i th == tons of escapes",
			expected: `a\ field\ w\=i\ th\ \=\=\ tons\ of\ escapes`,
		},
		{
			in:       "no_escapes",
			expected: "no_escapes",
		},
		{
			in:       "some,commas,here",
			expected: `some\,commas\,here`,
		},
	}

	for _, test := range tests {
		if test.expected != String(test.in) {
			t.Errorf("Got %s, expected %s", String(test.in), test.expected)
		}
	}
}

func TestStringUnescape(t *testing.T) {
	tests := []struct {
		in       string
		expected string
	}{
		{
			in:       "",
			expected: "",
		},
		{
			in:       `this\ is\ my\ special\ string`,
			expected: "this is my special string",
		},
		{
			in:       `a\ field\ w\=i\ th\ \=\=\ tons\ of\ escapes`,
			expected: "a field w=i th == tons of escapes",
		},
		{
			in:       "no_escapes",
			expected: "no_escapes",
		},
		{
			in:       `some\,commas\,here`,
			expected: "some,commas,here",
		},
	}

	for _, test := range tests {
		if test.expected != UnescapeString(test.in) {
			t.Errorf("Got %s, expected %s", UnescapeString(test.in), test.expected)
		}
	}
}