f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow/*
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow * This file and its contents are supplied under the terms of the
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow * Common Development and Distribution License ("CDDL"), version 1.0.
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow * You may only use this file in accordance with the terms of version
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow * 1.0 of the CDDL.
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow *
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow * A full copy of the text of the CDDL should have accompanied this
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow * source. A copy of the CDDL is also available via the Internet at
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow * http://www.illumos.org/license/CDDL.
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow */
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow/*
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow * Copyright 2012, Joyent, Inc. All rights reserved.
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow */
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow/*
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow * General functional tests of JSON parser for json().
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow */
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow#pragma D option quiet
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow#pragma D option strsize=1k
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow#define TST(name) \
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow printf("\ntst |%s|\n", name)
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow#define IN2(vala, valb) \
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow in = strjoin(vala, valb); \
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow printf("in |%s|\n", in)
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow#define IN(val) \
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow in = val; \
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow printf("in |%s|\n", in)
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow#define SEL(ss) \
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow out = json(in, ss); \
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow printf("sel |%s|\nout |%s|\n", ss, \
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow out != NULL ? out : "<NULL>")
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. ClulowBEGIN
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow{
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("empty array");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("one-element array: integer");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[1]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("1");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("100");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("-1");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("one-element array: hex integer (not in spec, not supported)");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[0x1000]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("one-element array: float");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[1.5001]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("one-element array: float + exponent");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[16.3e10]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("one-element array: integer + whitespace");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[ \t 5\t]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("one-element array: integer + exponent + whitespace");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[ \t \t 16E10 \t ]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("one-element array: string");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[\"alpha\"]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("alternative first-element indexing");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[1,5,10,15,20]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("[0]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("[3]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("[4]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("[5]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("one-element array: object");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[ { \"first\": true, \"second\": false }]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0.first");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0.second");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0.third");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("many-element array: integers");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("10"); /* F(10) = 55 */
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("14"); /* F(14) = 377 */
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("19");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("many-element array: multiple types");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN2("[\"string\",32,true,{\"a\":9,\"b\":false},100.3e10,false,200.5,",
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow "{\"key\":\"val\"},null]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0.notobject");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("1");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("2");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("3");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("3.a");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("3.b");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("3.c");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("4");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("5");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("6");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("7");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("7.key");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("7.key.notobject");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("7.nonexist");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("8");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("9");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("many-element array: multiple types + whitespace");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN2("\n[\t\"string\" ,\t32 , true\t,\t {\"a\": 9,\t\"b\": false},\t\t",
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow "100.3e10, false, 200.5,{\"key\" \t:\n \"val\"},\t\t null ]\t\t");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0.notobject");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("1");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("2");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("3");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("3.a");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("3.b");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("3.c");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("4");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("5");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("6");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("7");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("7.key");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("7.key.notobject");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("7.nonexist");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("8");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("9");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("two-element array: various string escape codes");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN2("[\"abcd \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u0000 \\uf00F \", ",
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow "\"final\"]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("1");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("three-element array: broken escape code");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN("[\"fine here\", \"dodgey \\u00AZ\", \"wont get here\"]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("0");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("1");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("2");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow TST("nested objects");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow IN2("{ \"top\": { \"mid\" : { \"legs\": \"feet\" }, \"number\": 9, ",
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow "\"array\":[0,1,{\"a\":true,\"bb\":[1,2,false,{\"x\":\"yz\"}]}]}}");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("fargo");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.mid");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.centre");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.mid.legs");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.mid.number");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.mid.array");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.number");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[0]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[1]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2].a");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2].b");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2].bb");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2].bb[0]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2].bb[1]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2].bb[2]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2].bb[3]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2].bb[3].x");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2].bb[3].x.nofurther");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[2].bb[4]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow SEL("top.array[3]");
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow exit(0);
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow}
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. ClulowERROR
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow{
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow exit(1);
f497f9fe231e0e400f339c84a7d80c4aae2ac4d5Joshua M. Clulow}