File: sortedset_test.go

package info (click to toggle)
golang-github-rs-cors 1.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 260 kB
  • sloc: makefile: 3
file content (218 lines) | stat: -rw-r--r-- 5,806 bytes parent folder | download
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package internal

import (
	"strings"
	"testing"
)

func TestSortedSet(t *testing.T) {
	cases := []struct {
		desc  string
		elems []string
		// expectations
		size     int
		combined string
		slice    []string
		accepted [][]string
		rejected [][]string
	}{
		{
			desc:     "empty set",
			size:     0,
			combined: "",
			accepted: [][]string{
				// some empty elements, possibly with OWS
				{""},
				{","},
				{"\t, , "},
				// multiple field lines, some empty elements
				make([]string, maxEmptyElements),
			},
			rejected: [][]string{
				{"x-bar"},
				{"x-bar,x-foo"},
				// too many empty elements
				{strings.Repeat(",", maxEmptyElements+1)},
				// multiple field lines, too many empty elements
				make([]string, maxEmptyElements+1),
			},
		}, {
			desc:     "singleton set",
			elems:    []string{"x-foo"},
			size:     1,
			combined: "x-foo",
			slice:    []string{"X-Foo"},
			accepted: [][]string{
				{"x-foo"},
				// some empty elements, possibly with OWS
				{""},
				{","},
				{"\t, , "},
				{"\tx-foo ,"},
				{" x-foo\t,"},
				{strings.Repeat(",", maxEmptyElements) + "x-foo"},
				// multiple field lines, some empty elements
				append(make([]string, maxEmptyElements), "x-foo"),
				make([]string, maxEmptyElements),
			},
			rejected: [][]string{
				{"x-bar"},
				{"x-bar,x-foo"},
				// too much OWS
				{"x-foo  "},
				{" x-foo  "},
				{"  x-foo  "},
				{"x-foo\t\t"},
				{"\tx-foo\t\t"},
				{"\t\tx-foo\t\t"},
				// too many empty elements
				{strings.Repeat(",", maxEmptyElements+1) + "x-foo"},
				// multiple field lines, too many empty elements
				append(make([]string, maxEmptyElements+1), "x-foo"),
				make([]string, maxEmptyElements+1),
			},
		}, {
			desc:     "no dupes",
			elems:    []string{"x-foo", "x-bar", "x-baz"},
			size:     3,
			combined: "x-bar,x-baz,x-foo",
			slice:    []string{"X-Bar", "X-Baz", "X-Foo"},
			accepted: [][]string{
				{"x-bar"},
				{"x-baz"},
				{"x-foo"},
				{"x-bar,x-baz"},
				{"x-bar,x-foo"},
				{"x-baz,x-foo"},
				{"x-bar,x-baz,x-foo"},
				// some empty elements, possibly with OWS
				{""},
				{","},
				{"\t, , "},
				{"\tx-bar ,"},
				{" x-baz\t,"},
				{"x-foo,"},
				{"\tx-bar ,\tx-baz ,"},
				{" x-bar\t, x-foo\t,"},
				{"x-baz,x-foo,"},
				{" x-bar , x-baz , x-foo ,"},
				{"x-bar" + strings.Repeat(",", maxEmptyElements+1) + "x-foo"},
				// multiple field lines
				{"x-bar", "x-foo"},
				{"x-bar", "x-baz,x-foo"},
				// multiple field lines, some empty elements
				append(make([]string, maxEmptyElements), "x-bar", "x-foo"),
				make([]string, maxEmptyElements),
			},
			rejected: [][]string{
				{"x-qux"},
				{"x-bar,x-baz,x-baz"},
				{"x-qux,x-baz"},
				{"x-qux,x-foo"},
				{"x-quxbaz,x-foo"},
				// too much OWS
				{"x-bar  "},
				{" x-baz  "},
				{"  x-foo  "},
				{"x-bar\t\t,x-baz"},
				{"x-bar,\tx-foo\t\t"},
				{"\t\tx-baz,x-foo\t\t"},
				{" x-bar\t,\tx-baz\t ,x-foo"},
				// too many empty elements
				{"x-bar" + strings.Repeat(",", maxEmptyElements+2) + "x-foo"},
				// multiple field lines, elements in the wrong order
				{"x-foo", "x-bar"},
				// multiple field lines, too many empty elements
				append(make([]string, maxEmptyElements+1), "x-bar", "x-foo"),
				make([]string, maxEmptyElements+1),
			},
		}, {
			desc:     "some dupes",
			elems:    []string{"x-foo", "x-bar", "x-foo"},
			size:     2,
			combined: "x-bar,x-foo",
			slice:    []string{"X-Bar", "X-Foo"},
			accepted: [][]string{
				{"x-bar"},
				{"x-foo"},
				{"x-bar,x-foo"},
				// some empty elements, possibly with OWS
				{""},
				{","},
				{"\t, , "},
				{"\tx-bar ,"},
				{" x-foo\t,"},
				{"x-foo,"},
				{"\tx-bar ,\tx-foo ,"},
				{" x-bar\t, x-foo\t,"},
				{"x-bar,x-foo,"},
				{" x-bar , x-foo ,"},
				{"x-bar" + strings.Repeat(",", maxEmptyElements+1) + "x-foo"},
				// multiple field lines
				{"x-bar", "x-foo"},
				// multiple field lines, some empty elements
				append(make([]string, maxEmptyElements), "x-bar", "x-foo"),
				make([]string, maxEmptyElements),
			},
			rejected: [][]string{
				{"x-qux"},
				{"x-qux,x-bar"},
				{"x-qux,x-foo"},
				{"x-qux,x-baz,x-foo"},
				// too much OWS
				{"x-qux  "},
				{"x-qux,\t\tx-bar"},
				{"x-qux,x-foo\t\t"},
				{"\tx-qux , x-baz\t\t,x-foo"},
				// too many empty elements
				{"x-bar" + strings.Repeat(",", maxEmptyElements+2) + "x-foo"},
				// multiple field lines, elements in the wrong order
				{"x-foo", "x-bar"},
				// multiple field lines, too much whitespace
				{"x-qux", "\t\tx-bar"},
				{"x-qux", "x-foo\t\t"},
				{"\tx-qux ", " x-baz\t\t,x-foo"},
				// multiple field lines, too many empty elements
				append(make([]string, maxEmptyElements+1), "x-bar", "x-foo"),
				make([]string, maxEmptyElements+1),
			},
		},
	}
	for _, tc := range cases {
		f := func(t *testing.T) {
			elems := clone(tc.elems)
			set := NewSortedSet(tc.elems...)
			size := set.Size()
			if set.Size() != tc.size {
				const tmpl = "NewSortedSet(%#v...).Size(): got %d; want %d"
				t.Errorf(tmpl, elems, size, tc.size)
			}
			combined := set.String()
			if combined != tc.combined {
				const tmpl = "NewSortedSet(%#v...).String(): got %q; want %q"
				t.Errorf(tmpl, elems, combined, tc.combined)
			}
			for _, a := range tc.accepted {
				if !set.Accepts(a) {
					const tmpl = "%q rejects %q, but should accept it"
					t.Errorf(tmpl, set, a)
				}
			}
			for _, r := range tc.rejected {
				if set.Accepts(r) {
					const tmpl = "%q accepts %q, but should reject it"
					t.Errorf(tmpl, set, r)
				}
			}
		}
		t.Run(tc.desc, f)
	}
}

// adapted from https://pkg.go.dev/slices#Clone
// TODO: when updating go directive to 1.21 or later,
// use slices.Clone instead.
func clone(s []string) []string {
	// The s[:0:0] preserves nil in case it matters.
	return append(s[:0:0], s...)
}