1//  Copyright (c) 2013 Couchbase, Inc.
2
3package collatejson
4
5import (
6	"bytes"
7	"testing"
8)
9
10var testcasesdesc = []struct {
11	text     string
12	desc     []bool
13	encoded  []byte
14	reversed []byte
15}{
16
17	{`[10]`,
18		[]bool{true},
19		[]byte{8, 5, 62, 62, 50, 49, 45, 0, 0},
20		[]byte{8, 250, 193, 193, 205, 206, 210, 255, 0},
21	},
22
23	{`[99.9876514]`,
24		[]bool{true},
25		[]byte{8, 5, 62, 62, 50, 57, 57, 57, 56, 55, 54, 53, 49, 52, 45, 0, 0},
26		[]byte{8, 250, 193, 193, 205, 198, 198, 198, 199, 200, 201, 202, 206, 203, 210, 255, 0},
27	},
28
29	{`[true]`,
30		[]bool{true},
31		[]byte{8, 4, 0, 0},
32		[]byte{8, 251, 255, 0},
33	},
34
35	{`[false]`,
36		[]bool{true},
37		[]byte{8, 3, 0, 0},
38		[]byte{8, 252, 255, 0},
39	},
40
41	{`[null]`,
42		[]bool{true},
43		[]byte{8, 2, 0, 0},
44		[]byte{8, 253, 255, 0},
45	},
46
47	{`[""]`,
48		[]bool{true},
49		[]byte{8, 6, 0, 0, 0},
50		[]byte{8, 249, 255, 255, 0},
51	},
52
53	{`["abcde"]`,
54		[]bool{true},
55		[]byte{8, 6, 97, 98, 99, 100, 101, 0, 0, 0},
56		[]byte{8, 249, 158, 157, 156, 155, 154, 255, 255, 0},
57	},
58
59	{`["ab","bcd","de"]`,
60		[]bool{true, false, false},
61		[]byte{8, 6, 97, 98, 0, 0, 6, 98, 99, 100, 0, 0, 6, 100, 101, 0, 0, 0},
62		[]byte{8, 249, 158, 157, 255, 255, 6, 98, 99, 100, 0, 0, 6, 100, 101, 0, 0, 0},
63	},
64
65	{`["ab","bcd","de"]`,
66		[]bool{false, false, true},
67		[]byte{8, 6, 97, 98, 0, 0, 6, 98, 99, 100, 0, 0, 6, 100, 101, 0, 0, 0},
68		[]byte{8, 6, 97, 98, 0, 0, 6, 98, 99, 100, 0, 0, 249, 155, 154, 255, 255, 0},
69	},
70
71	{`["ab","bcd","de"]`,
72		[]bool{true, false, true},
73		[]byte{8, 6, 97, 98, 0, 0, 6, 98, 99, 100, 0, 0, 6, 100, 101, 0, 0, 0},
74		[]byte{8, 249, 158, 157, 255, 255, 6, 98, 99, 100, 0, 0, 249, 155, 154, 255, 255, 0},
75	},
76
77	{`["ab","bcd","de"]`,
78		[]bool{true, true, true},
79		[]byte{8, 6, 97, 98, 0, 0, 6, 98, 99, 100, 0, 0, 6, 100, 101, 0, 0, 0},
80		[]byte{8, 249, 158, 157, 255, 255, 249, 157, 156, 155, 255, 255, 249, 155, 154, 255, 255, 0},
81	},
82
83	{`["ab",null,10]`,
84		[]bool{false, true, true},
85		[]byte{8, 6, 97, 98, 0, 0, 2, 0, 5, 62, 62, 50, 49, 45, 0, 0},
86		[]byte{8, 6, 97, 98, 0, 0, 253, 255, 250, 193, 193, 205, 206, 210, 255, 0},
87	},
88
89	{`["ab",null,10]`,
90		[]bool{true, false, true},
91		[]byte{8, 6, 97, 98, 0, 0, 2, 0, 5, 62, 62, 50, 49, 45, 0, 0},
92		[]byte{8, 249, 158, 157, 255, 255, 2, 0, 250, 193, 193, 205, 206, 210, 255, 0},
93	},
94
95	{`[[1,2],[2],[3]]`,
96		[]bool{true, false, true},
97		[]byte{8, 8, 5, 62, 62, 49, 49, 45, 0, 5, 62, 62, 49, 50, 45, 0, 0, 8, 5, 62, 62, 49, 50, 45,
98			0, 0, 8, 5, 62, 62, 49, 51, 45, 0, 0, 0},
99		[]byte{8, 247, 250, 193, 193, 206, 206, 210, 255, 250, 193, 193, 206, 205, 210, 255, 255,
100			8, 5, 62, 62, 49, 50, 45, 0, 0, 247, 250, 193, 193, 206, 204, 210, 255, 255, 0},
101	},
102
103	{`[[1,2,3],["ab",null,10],["ab","bcd","de"]]`,
104		[]bool{true, false, true},
105		[]byte{8, 8, 5, 62, 62, 49, 49, 45, 0, 5, 62, 62, 49, 50, 45, 0, 5, 62, 62, 49, 51, 45, 0, 0,
106			8, 6, 97, 98, 0, 0, 2, 0, 5, 62, 62, 50, 49, 45, 0, 0, 8, 6, 97, 98, 0, 0, 6, 98, 99, 100, 0,
107			0, 6, 100, 101, 0, 0, 0, 0},
108		[]byte{8, 247, 250, 193, 193, 206, 206, 210, 255, 250, 193, 193, 206, 205, 210, 255, 250, 193,
109			193, 206, 204, 210, 255, 255, 8, 6, 97, 98, 0, 0, 2, 0, 5, 62, 62, 50, 49, 45, 0, 0, 247, 249,
110			158, 157, 255, 255, 249, 157, 156, 155, 255, 255, 249, 155, 154, 255, 255, 255, 0},
111	},
112
113	{`[{ "intf" : 10 }]`,
114		[]bool{true},
115		[]byte{8, 9, 7, 62, 49, 0, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 0, 0},
116		[]byte{8, 246, 248, 193, 206, 255, 249, 150, 145, 139, 153, 255, 255, 250, 193, 193,
117			205, 206, 210, 255, 255, 0},
118	},
119
120	{`[{ "intf" : 10, "boolf" : true, "strf" : "abc"}]`,
121		[]bool{true},
122		[]byte{8, 9, 7, 62, 51, 0, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 6, 105, 110, 116,
123			102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 6, 115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0,
124			0, 0},
125		[]byte{8, 246, 248, 193, 204, 255, 249, 157, 144, 144, 147, 153, 255, 255, 251, 255,
126			249, 150, 145, 139, 153, 255, 255, 250, 193, 193, 205, 206, 210, 255, 249, 140, 139,
127			141, 153, 255, 255, 249, 158, 157, 156, 255, 255, 255, 0},
128	},
129
130	{`[{ "arrf" : ["ab","bcd","de"], "intf" : 10, "boolf" : true, "strf" : "abc"}]`,
131		[]bool{true},
132		[]byte{8, 9, 7, 62, 52, 0, 6, 97, 114, 114, 102, 0, 0, 8, 6, 97, 98, 0, 0, 6, 98,
133			99, 100, 0, 0, 6, 100, 101, 0, 0, 0, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 6, 105,
134			110, 116, 102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 6, 115, 116, 114, 102, 0, 0, 6, 97,
135			98, 99, 0, 0, 0, 0},
136		[]byte{8, 246, 248, 193, 203, 255, 249, 158, 141, 141, 153, 255, 255, 247, 249, 158,
137			157, 255, 255, 249, 157, 156, 155, 255, 255, 249, 155, 154, 255, 255, 255, 249, 157,
138			144, 144, 147, 153, 255, 255, 251, 255, 249, 150, 145, 139, 153, 255, 255, 250, 193,
139			193, 205, 206, 210, 255, 249, 140, 139, 141, 153, 255, 255, 249, 158, 157, 156, 255,
140			255, 255, 0},
141	},
142
143	{`[{ "intf" : 10},  {"boolf" : true}, {"strf" : "abc"}]`,
144		[]bool{true, true, false},
145		[]byte{8, 9, 7, 62, 49, 0, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 0,
146			9, 7, 62, 49, 0, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 0, 9, 7, 62, 49, 0, 6,
147			115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0, 0, 0},
148		[]byte{8, 246, 248, 193, 206, 255, 249, 150, 145, 139, 153, 255, 255, 250, 193, 193, 205,
149			206, 210, 255, 255, 246, 248, 193, 206, 255, 249, 157, 144, 144, 147, 153, 255, 255, 251, 255,
150			255, 9, 7, 62, 49, 0, 6, 115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0, 0, 0},
151	},
152
153	{`[{ "intf" : 10},  {"boolf" : true}, {"strf" : "abc"}]`,
154		[]bool{true, true, true},
155		[]byte{8, 9, 7, 62, 49, 0, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 0,
156			9, 7, 62, 49, 0, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 0, 9, 7, 62, 49, 0, 6,
157			115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0, 0, 0},
158		[]byte{8, 246, 248, 193, 206, 255, 249, 150, 145, 139, 153, 255, 255, 250, 193, 193, 205,
159			206, 210, 255, 255, 246, 248, 193, 206, 255, 249, 157, 144, 144, 147, 153, 255, 255, 251,
160			255, 255, 246, 248, 193, 206, 255, 249, 140, 139, 141, 153, 255, 255, 249, 158, 157, 156,
161			255, 255, 255, 0},
162	},
163
164	{`[{ "intf" : 10},  {"boolf" : true}, {"strf" : "abc"}]`,
165		[]bool{true, false, true},
166		[]byte{8, 9, 7, 62, 49, 0, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 0,
167			9, 7, 62, 49, 0, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 0, 9, 7, 62, 49, 0, 6,
168			115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0, 0, 0},
169		[]byte{8, 246, 248, 193, 206, 255, 249, 150, 145, 139, 153, 255, 255, 250, 193, 193, 205,
170			206, 210, 255, 255, 9, 7, 62, 49, 0, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 0, 246, 248, 193,
171			206, 255, 249, 140, 139, 141, 153, 255, 255, 249, 158, 157, 156, 255, 255, 255, 0},
172	},
173
174	{`[{ "intf" : 10},  {"boolf" : true}, {"strf" : "abc"}]`,
175		[]bool{false, true, false},
176		[]byte{8, 9, 7, 62, 49, 0, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 0,
177			9, 7, 62, 49, 0, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 0, 9, 7, 62, 49, 0, 6,
178			115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0, 0, 0},
179		[]byte{8, 9, 7, 62, 49, 0, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 0,
180			246, 248, 193, 206, 255, 249, 157, 144, 144, 147, 153, 255, 255, 251, 255, 255,
181			9, 7, 62, 49, 0, 6, 115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0, 0, 0},
182	},
183
184	{`[{ "intf" : 10},  {"boolf" : true}, {"strf" : "abc"}]`,
185		[]bool{false, false, false},
186		[]byte{8, 9, 7, 62, 49, 0, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 0,
187			9, 7, 62, 49, 0, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 0, 9, 7, 62, 49, 0, 6,
188			115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0, 0, 0},
189		[]byte{8, 9, 7, 62, 49, 0, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 0,
190			9, 7, 62, 49, 0, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 0, 9, 7, 62, 49, 0, 6,
191			115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0, 0, 0},
192	},
193
194	{`[[1,2],[5,""],[null]]`,
195		[]bool{false, true, true},
196		[]byte{8, 8, 5, 62, 62, 49, 49, 45, 0, 5, 62, 62, 49, 50, 45, 0, 0,
197			8, 5, 62, 62, 49, 53, 45, 0, 6, 0, 0, 0, 8, 2, 0, 0, 0},
198		[]byte{8, 8, 5, 62, 62, 49, 49, 45, 0, 5, 62, 62, 49, 50, 45, 0, 0,
199			247, 250, 193, 193, 206, 202, 210, 255, 249, 255, 255, 255, 247, 253, 255, 255, 0},
200	},
201
202	{`["[1,2]",[5,""],[null]]`,
203		[]bool{true, true, false},
204		[]byte{8, 6, 91, 49, 44, 50, 93, 0, 0, 8, 5, 62, 62, 49, 53, 45, 0, 6, 0, 0, 0,
205			8, 2, 0, 0, 0},
206		[]byte{8, 249, 164, 206, 211, 205, 162, 255, 255, 247, 250, 193, 193, 206, 202, 210,
207			255, 249, 255, 255, 255, 8, 2, 0, 0, 0},
208	},
209
210	{`["ab","~[]{}falsenilNA~",10]`,
211		[]bool{true, true, false},
212		[]byte{8, 6, 97, 98, 0, 0, 1, 0, 5, 62, 62, 50, 49, 45, 0, 0},
213		[]byte{8, 249, 158, 157, 255, 255, 254, 255, 5, 62, 62, 50, 49, 45, 0, 0},
214	},
215
216	{`["ab","~[]{}falsenilNA~",null]`,
217		[]bool{true, true, true},
218		[]byte{8, 6, 97, 98, 0, 0, 1, 0, 2, 0, 0},
219		[]byte{8, 249, 158, 157, 255, 255, 254, 255, 253, 255, 0},
220	},
221}
222
223func TestCodecDesc(t *testing.T) {
224	codec := NewCodec(16)
225	for _, tcase := range testcasesdesc {
226
227		code, err := codec.Encode([]byte(tcase.text), code[:0])
228		if err != nil {
229			t.Error(err)
230		}
231
232		if !bytes.Equal(code, tcase.encoded) {
233			t.Errorf("Enocded bytes mismatch %v %v", code, tcase.encoded)
234		}
235
236		codec.ReverseCollate(code, tcase.desc)
237
238		if !bytes.Equal(code, tcase.reversed) {
239			t.Errorf("Reversed bytes mismatch %v %v", code, tcase.reversed)
240		}
241
242		codec.ReverseCollate(code, tcase.desc)
243
244		if !bytes.Equal(code, tcase.encoded) {
245			t.Errorf("Re-reversed bytes mismatch %v %v", code, tcase.encoded)
246		}
247
248	}
249}
250
251var testcasesproplen = []struct {
252	text     string
253	desc     []bool
254	encoded  []byte
255	reversed []byte
256}{
257
258	{`[{ "intf" : 10 }]`,
259		[]bool{true},
260		[]byte{8, 9, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62, 50, 49, 45, 0, 0, 0},
261		[]byte{8, 246, 249, 150, 145, 139, 153, 255, 255, 250, 193, 193, 205, 206, 210, 255, 255, 0},
262	},
263
264	{`[{ "intf" : 10, "boolf" : true, "strf" : "abc"}]`,
265		[]bool{true},
266		[]byte{8, 9, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62,
267			50, 49, 45, 0, 6, 115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0, 0, 0},
268		[]byte{8, 246, 249, 157, 144, 144, 147, 153, 255, 255, 251, 255, 249, 150, 145, 139, 153,
269			255, 255, 250, 193, 193, 205, 206, 210, 255, 249, 140, 139, 141, 153, 255, 255, 249, 158,
270			157, 156, 255, 255, 255, 0},
271	},
272
273	{`[{ "arrf" : ["ab","bcd","de"], "intf" : 10, "boolf" : true, "strf" : "abc"}]`,
274		[]bool{true},
275		[]byte{8, 9, 6, 97, 114, 114, 102, 0, 0, 8, 6, 97, 98, 0, 0, 6, 98, 99, 100, 0, 0, 6, 100,
276			101, 0, 0, 0, 6, 98, 111, 111, 108, 102, 0, 0, 4, 0, 6, 105, 110, 116, 102, 0, 0, 5, 62, 62,
277			50, 49, 45, 0, 6, 115, 116, 114, 102, 0, 0, 6, 97, 98, 99, 0, 0, 0, 0},
278		[]byte{8, 246, 249, 158, 141, 141, 153, 255, 255, 247, 249, 158, 157, 255, 255, 249, 157,
279			156, 155, 255, 255, 249, 155, 154, 255, 255, 255, 249, 157, 144, 144, 147, 153, 255, 255, 251,
280			255, 249, 150, 145, 139, 153, 255, 255, 250, 193, 193, 205, 206, 210, 255, 249, 140, 139, 141,
281			153, 255, 255, 249, 158, 157, 156, 255, 255, 255, 0},
282	},
283}
284
285func TestCodecDescPropLen(t *testing.T) {
286	codec := NewCodec(16)
287	codec.SortbyPropertyLen(false)
288	for _, tcase := range testcasesproplen {
289
290		code, err := codec.Encode([]byte(tcase.text), code[:0])
291		if err != nil {
292			t.Error(err)
293		}
294		if !bytes.Equal(code, tcase.encoded) {
295			t.Errorf("Enocded bytes mismatch %v %v", code, tcase.encoded)
296		}
297
298		codec.ReverseCollate(code, tcase.desc)
299		if !bytes.Equal(code, tcase.reversed) {
300			t.Errorf("Reversed bytes mismatch %v %v", code, tcase.reversed)
301		}
302
303		codec.ReverseCollate(code, tcase.desc)
304		if !bytes.Equal(code, tcase.encoded) {
305			t.Errorf("Re-reversed bytes mismatch %v %v", code, tcase.encoded)
306		}
307
308	}
309}
310