tst.general.d revision 266987
1266987Smarkj/* 2266987Smarkj * This file and its contents are supplied under the terms of the 3266987Smarkj * Common Development and Distribution License ("CDDL"), version 1.0. 4266987Smarkj * You may only use this file in accordance with the terms of version 5266987Smarkj * 1.0 of the CDDL. 6266987Smarkj * 7266987Smarkj * A full copy of the text of the CDDL should have accompanied this 8266987Smarkj * source. A copy of the CDDL is also available via the Internet at 9266987Smarkj * http://www.illumos.org/license/CDDL. 10266987Smarkj */ 11266987Smarkj 12266987Smarkj/* 13266987Smarkj * Copyright 2012, Joyent, Inc. All rights reserved. 14266987Smarkj */ 15266987Smarkj 16266987Smarkj/* 17266987Smarkj * General functional tests of JSON parser for json(). 18266987Smarkj */ 19266987Smarkj 20266987Smarkj#pragma D option quiet 21266987Smarkj#pragma D option strsize=1k 22266987Smarkj 23266987Smarkj#define TST(name) \ 24266987Smarkj printf("\ntst |%s|\n", name) 25266987Smarkj#define IN2(vala, valb) \ 26266987Smarkj in = strjoin(vala, valb); \ 27266987Smarkj printf("in |%s|\n", in) 28266987Smarkj#define IN(val) \ 29266987Smarkj in = val; \ 30266987Smarkj printf("in |%s|\n", in) 31266987Smarkj#define SEL(ss) \ 32266987Smarkj out = json(in, ss); \ 33266987Smarkj printf("sel |%s|\nout |%s|\n", ss, \ 34266987Smarkj out != NULL ? out : "<NULL>") 35266987Smarkj 36266987SmarkjBEGIN 37266987Smarkj{ 38266987Smarkj TST("empty array"); 39266987Smarkj IN("[]"); 40266987Smarkj SEL("0"); 41266987Smarkj 42266987Smarkj TST("one-element array: integer"); 43266987Smarkj IN("[1]"); 44266987Smarkj SEL("0"); 45266987Smarkj SEL("1"); 46266987Smarkj SEL("100"); 47266987Smarkj SEL("-1"); 48266987Smarkj 49266987Smarkj TST("one-element array: hex integer (not in spec, not supported)"); 50266987Smarkj IN("[0x1000]"); 51266987Smarkj SEL("0"); 52266987Smarkj 53266987Smarkj TST("one-element array: float"); 54266987Smarkj IN("[1.5001]"); 55266987Smarkj SEL("0"); 56266987Smarkj 57266987Smarkj TST("one-element array: float + exponent"); 58266987Smarkj IN("[16.3e10]"); 59266987Smarkj SEL("0"); 60266987Smarkj 61266987Smarkj TST("one-element array: integer + whitespace"); 62266987Smarkj IN("[ \t 5\t]"); 63266987Smarkj SEL("0"); 64266987Smarkj 65266987Smarkj TST("one-element array: integer + exponent + whitespace"); 66266987Smarkj IN("[ \t \t 16E10 \t ]"); 67266987Smarkj SEL("0"); 68266987Smarkj 69266987Smarkj TST("one-element array: string"); 70266987Smarkj IN("[\"alpha\"]"); 71266987Smarkj SEL("0"); 72266987Smarkj 73266987Smarkj TST("alternative first-element indexing"); 74266987Smarkj IN("[1,5,10,15,20]"); 75266987Smarkj SEL("[0]"); 76266987Smarkj SEL("[3]"); 77266987Smarkj SEL("[4]"); 78266987Smarkj SEL("[5]"); 79266987Smarkj 80266987Smarkj TST("one-element array: object"); 81266987Smarkj IN("[ { \"first\": true, \"second\": false }]"); 82266987Smarkj SEL("0.first"); 83266987Smarkj SEL("0.second"); 84266987Smarkj SEL("0.third"); 85266987Smarkj 86266987Smarkj TST("many-element array: integers"); 87266987Smarkj IN("[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377]"); 88266987Smarkj SEL("10"); /* F(10) = 55 */ 89266987Smarkj SEL("14"); /* F(14) = 377 */ 90266987Smarkj SEL("19"); 91266987Smarkj 92266987Smarkj TST("many-element array: multiple types"); 93266987Smarkj IN2("[\"string\",32,true,{\"a\":9,\"b\":false},100.3e10,false,200.5,", 94266987Smarkj "{\"key\":\"val\"},null]"); 95266987Smarkj SEL("0"); 96266987Smarkj SEL("0.notobject"); 97266987Smarkj SEL("1"); 98266987Smarkj SEL("2"); 99266987Smarkj SEL("3"); 100266987Smarkj SEL("3.a"); 101266987Smarkj SEL("3.b"); 102266987Smarkj SEL("3.c"); 103266987Smarkj SEL("4"); 104266987Smarkj SEL("5"); 105266987Smarkj SEL("6"); 106266987Smarkj SEL("7"); 107266987Smarkj SEL("7.key"); 108266987Smarkj SEL("7.key.notobject"); 109266987Smarkj SEL("7.nonexist"); 110266987Smarkj SEL("8"); 111266987Smarkj SEL("9"); 112266987Smarkj 113266987Smarkj TST("many-element array: multiple types + whitespace"); 114266987Smarkj IN2("\n[\t\"string\" ,\t32 , true\t,\t {\"a\": 9,\t\"b\": false},\t\t", 115266987Smarkj "100.3e10, false, 200.5,{\"key\" \t:\n \"val\"},\t\t null ]\t\t"); 116266987Smarkj SEL("0"); 117266987Smarkj SEL("0.notobject"); 118266987Smarkj SEL("1"); 119266987Smarkj SEL("2"); 120266987Smarkj SEL("3"); 121266987Smarkj SEL("3.a"); 122266987Smarkj SEL("3.b"); 123266987Smarkj SEL("3.c"); 124266987Smarkj SEL("4"); 125266987Smarkj SEL("5"); 126266987Smarkj SEL("6"); 127266987Smarkj SEL("7"); 128266987Smarkj SEL("7.key"); 129266987Smarkj SEL("7.key.notobject"); 130266987Smarkj SEL("7.nonexist"); 131266987Smarkj SEL("8"); 132266987Smarkj SEL("9"); 133266987Smarkj 134266987Smarkj TST("two-element array: various string escape codes"); 135266987Smarkj IN2("[\"abcd \\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u0000 \\uf00F \", ", 136266987Smarkj "\"final\"]"); 137266987Smarkj SEL("0"); 138266987Smarkj SEL("1"); 139266987Smarkj 140266987Smarkj TST("three-element array: broken escape code"); 141266987Smarkj IN("[\"fine here\", \"dodgey \\u00AZ\", \"wont get here\"]"); 142266987Smarkj SEL("0"); 143266987Smarkj SEL("1"); 144266987Smarkj SEL("2"); 145266987Smarkj 146266987Smarkj TST("nested objects"); 147266987Smarkj IN2("{ \"top\": { \"mid\" : { \"legs\": \"feet\" }, \"number\": 9, ", 148266987Smarkj "\"array\":[0,1,{\"a\":true,\"bb\":[1,2,false,{\"x\":\"yz\"}]}]}}"); 149266987Smarkj SEL("top"); 150266987Smarkj SEL("fargo"); 151266987Smarkj SEL("top.mid"); 152266987Smarkj SEL("top.centre"); 153266987Smarkj SEL("top.mid.legs"); 154266987Smarkj SEL("top.mid.number"); 155266987Smarkj SEL("top.mid.array"); 156266987Smarkj SEL("top.number"); 157266987Smarkj SEL("top.array"); 158266987Smarkj SEL("top.array[0]"); 159266987Smarkj SEL("top.array[1]"); 160266987Smarkj SEL("top.array[2]"); 161266987Smarkj SEL("top.array[2].a"); 162266987Smarkj SEL("top.array[2].b"); 163266987Smarkj SEL("top.array[2].bb"); 164266987Smarkj SEL("top.array[2].bb[0]"); 165266987Smarkj SEL("top.array[2].bb[1]"); 166266987Smarkj SEL("top.array[2].bb[2]"); 167266987Smarkj SEL("top.array[2].bb[3]"); 168266987Smarkj SEL("top.array[2].bb[3].x"); 169266987Smarkj SEL("top.array[2].bb[3].x.nofurther"); 170266987Smarkj SEL("top.array[2].bb[4]"); 171266987Smarkj SEL("top.array[3]"); 172266987Smarkj 173266987Smarkj exit(0); 174266987Smarkj} 175266987Smarkj 176266987SmarkjERROR 177266987Smarkj{ 178266987Smarkj exit(1); 179266987Smarkj} 180