summaryrefslogtreecommitdiffstats
path: root/cmd/dtrace/test/tst/common/json/tst.general.d
blob: 4600811d95b99c1dfae2945b4f1cd10ac515af41 (plain)
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
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2012, Joyent, Inc.  All rights reserved.
 */

/*
 * General functional tests of JSON parser for json().
 */

#pragma D option quiet
#pragma D option strsize=1k

#define	TST(name)				\
	printf("\ntst |%s|\n", name)
#define	IN2(vala, valb)				\
	in = strjoin(vala, valb);		\
	printf("in  |%s|\n", in)
#define	IN(val)					\
	in = val;				\
	printf("in  |%s|\n", in)
#define	SEL(ss)					\
	out = json(in, ss);			\
	printf("sel |%s|\nout |%s|\n", ss,	\
	    out != NULL ? out : "<NULL>")

BEGIN
{
	TST("empty array");
	IN("[]");
	SEL("0");

	TST("one-element array: integer");
	IN("[1]");
	SEL("0");
	SEL("1");
	SEL("100");
	SEL("-1");

	TST("one-element array: hex integer (not in spec, not supported)");
	IN("[0x1000]");
	SEL("0");

	TST("one-element array: float");
	IN("[1.5001]");
	SEL("0");

	TST("one-element array: float + exponent");
	IN("[16.3e10]");
	SEL("0");

	TST("one-element array: integer + whitespace");
	IN("[ \t   5\t]");
	SEL("0");

	TST("one-element array: integer + exponent + whitespace");
	IN("[ \t    \t 16E10  \t ]");
	SEL("0");

	TST("one-element array: string");
	IN("[\"alpha\"]");
	SEL("0");

	TST("alternative first-element indexing");
	IN("[1,5,10,15,20]");
	SEL("[0]");
	SEL("[3]");
	SEL("[4]");
	SEL("[5]");

	TST("one-element array: object");
	IN("[ { \"first\": true, \"second\": false }]");
	SEL("0.first");
	SEL("0.second");
	SEL("0.third");

	TST("many-element array: integers");
	IN("[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377]");
	SEL("10"); /* F(10) = 55 */
	SEL("14"); /* F(14) = 377 */
	SEL("19");

	TST("many-element array: multiple types");
	IN2("[\"string\",32,true,{\"a\":9,\"b\":false},100.3e10,false,200.5,",
	    "{\"key\":\"val\"},null]");
	SEL("0");
	SEL("0.notobject");
	SEL("1");
	SEL("2");
	SEL("3");
	SEL("3.a");
	SEL("3.b");
	SEL("3.c");
	SEL("4");
	SEL("5");
	SEL("6");
	SEL("7");
	SEL("7.key");
	SEL("7.key.notobject");
	SEL("7.nonexist");
	SEL("8");
	SEL("9");

	TST("many-element array: multiple types + whitespace");
	IN2("\n[\t\"string\" ,\t32 , true\t,\t {\"a\":  9,\t\"b\": false},\t\t",
	    "100.3e10, false, 200.5,{\"key\" \t:\n \"val\"},\t\t null ]\t\t");
	SEL("0");
	SEL("0.notobject");
	SEL("1");
	SEL("2");
	SEL("3");
	SEL("3.a");
	SEL("3.b");
	SEL("3.c");
	SEL("4");
	SEL("5");
	SEL("6");
	SEL("7");
	SEL("7.key");
	SEL("7.key.notobject");
	SEL("7.nonexist");
	SEL("8");
	SEL("9");

	TST("two-element array: various string escape codes");
	IN2("[\"abcd \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u0000 \\uf00F \", ",
	    "\"final\"]");
	SEL("0");
	SEL("1");

	TST("three-element array: broken escape code");
	IN("[\"fine here\", \"dodgey \\u00AZ\", \"wont get here\"]");
	SEL("0");
	SEL("1");
	SEL("2");

	TST("nested objects");
	IN2("{ \"top\": { \"mid\"  : { \"legs\": \"feet\" }, \"number\": 9, ",
	    "\"array\":[0,1,{\"a\":true,\"bb\":[1,2,false,{\"x\":\"yz\"}]}]}}");
	SEL("top");
	SEL("fargo");
	SEL("top.mid");
	SEL("top.centre");
	SEL("top.mid.legs");
	SEL("top.mid.number");
	SEL("top.mid.array");
	SEL("top.number");
	SEL("top.array");
	SEL("top.array[0]");
	SEL("top.array[1]");
	SEL("top.array[2]");
	SEL("top.array[2].a");
	SEL("top.array[2].b");
	SEL("top.array[2].bb");
	SEL("top.array[2].bb[0]");
	SEL("top.array[2].bb[1]");
	SEL("top.array[2].bb[2]");
	SEL("top.array[2].bb[3]");
	SEL("top.array[2].bb[3].x");
	SEL("top.array[2].bb[3].x.nofurther");
	SEL("top.array[2].bb[4]");
	SEL("top.array[3]");

	exit(0);
}

ERROR
{
	exit(1);
}
OpenPOWER on IntegriCloud