151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson/*
267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * Copyright (C) 2010 The Android Open Source Project
367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson *
467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * you may not use this file except in compliance with the License.
667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * You may obtain a copy of the License at
767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson *
851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson *      http://www.apache.org/licenses/LICENSE-2.0
967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson *
1067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * Unless required by applicable law or agreed to in writing, software
1167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
1267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * See the License for the specific language governing permissions and
1467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * limitations under the License.
1567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson */
1667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
17d430cc782689a7f6a256ef6b0ebfc7210b0c31d9Tobias Thiererpackage libcore.org.json;
1867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
1967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilsonimport java.util.Arrays;
20609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughesimport java.util.Collection;
2151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilsonimport java.util.Collections;
227365de1056414750d0a7d1fdd26025fd247f0d04Jesse Wilsonimport java.util.List;
237365de1056414750d0a7d1fdd26025fd247f0d04Jesse Wilsonimport junit.framework.TestCase;
2467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
25d430cc782689a7f6a256ef6b0ebfc7210b0c31d9Tobias Thiererimport org.json.JSONArray;
26d430cc782689a7f6a256ef6b0ebfc7210b0c31d9Tobias Thiererimport org.json.JSONException;
27d430cc782689a7f6a256ef6b0ebfc7210b0c31d9Tobias Thiererimport org.json.JSONObject;
28d430cc782689a7f6a256ef6b0ebfc7210b0c31d9Tobias Thiererimport org.json.JSONTokener;
29d430cc782689a7f6a256ef6b0ebfc7210b0c31d9Tobias Thierer
3067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson/**
3167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson * This black box test was written without inspecting the non-free org.json sourcecode.
3267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson */
3367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilsonpublic class JSONArrayTest extends TestCase {
3467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
3567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testEmptyArray() throws JSONException {
3667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray array = new JSONArray();
3767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(0, array.length());
3867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("", array.join(" AND "));
3967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        try {
4067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            array.get(0);
4167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            fail();
4267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        } catch (JSONException e) {
4367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        }
4467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        try {
4567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            array.getBoolean(0);
4667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            fail();
4767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        } catch (JSONException e) {
4867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        }
4967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
5067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("[]", array.toString());
5167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("[]", array.toString(4));
5267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
5367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        // out of bounds is co-opted with defaulting
5467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(array.isNull(0));
5567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertNull(array.opt(0));
5667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.optBoolean(0));
5767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(array.optBoolean(0, true));
5867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
5951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        // bogus (but documented) behaviour: returns null rather than an empty object!
6067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertNull(array.toJSONObject(new JSONArray()));
6167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
6267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
6367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testEqualsAndHashCode() throws JSONException {
6467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray a = new JSONArray();
6567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray b = new JSONArray();
6667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(a.equals(b));
6751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals("equals() not consistent with hashCode()", a.hashCode(), b.hashCode());
6867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
6967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        a.put(true);
7067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        a.put(false);
7167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        b.put(true);
7267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        b.put(false);
7367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(a.equals(b));
7467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(a.hashCode(), b.hashCode());
7567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
7667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        b.put(true);
7767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(a.equals(b));
7867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(a.hashCode() != b.hashCode());
7967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
8067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
8167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testBooleans() throws JSONException {
8267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray array = new JSONArray();
8367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put(true);
8467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put(false);
8567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put(2, false);
8667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put(3, false);
8767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put(2, true);
8867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("[true,false,true,false]", array.toString());
8967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(4, array.length());
9067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Boolean.TRUE, array.get(0));
9167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Boolean.FALSE, array.get(1));
9267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Boolean.TRUE, array.get(2));
9367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Boolean.FALSE, array.get(3));
9467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.isNull(0));
9567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.isNull(1));
9667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.isNull(2));
9767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.isNull(3));
9867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(true, array.optBoolean(0));
9967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(false, array.optBoolean(1, true));
10067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(true, array.optBoolean(2, false));
10167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(false, array.optBoolean(3));
10267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("true", array.getString(0));
10367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("false", array.getString(1));
10467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("true", array.optString(2));
10567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("false", array.optString(3, "x"));
10667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("[\n     true,\n     false,\n     true,\n     false\n]", array.toString(5));
10767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
10867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray other = new JSONArray();
10967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put(true);
11067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put(false);
11167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put(true);
11267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put(false);
11367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(array.equals(other));
11467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put(true);
11567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.equals(other));
11667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
11767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other = new JSONArray();
11867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put("true");
11967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put("false");
12067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put("truE");
12167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put("FALSE");
12267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.equals(other));
12367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(other.equals(array));
12467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(true, other.getBoolean(0));
12567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(false, other.optBoolean(1, true));
12667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(true, other.optBoolean(2));
12767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(false, other.getBoolean(3));
12867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
12967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
130661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson    // http://code.google.com/p/android/issues/detail?id=16411
131661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson    public void testCoerceStringToBoolean() throws JSONException {
132661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson        JSONArray array = new JSONArray();
133661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson        array.put("maybe");
134661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson        try {
135661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson            array.getBoolean(0);
136661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson            fail();
137661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson        } catch (JSONException expected) {
138661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson        }
139661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson        assertEquals(false, array.optBoolean(0));
140661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson        assertEquals(true, array.optBoolean(0, true));
141661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson    }
142661054f5a2f7f8f5f3ceffb97e803211b546e7fcJesse Wilson
14367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testNulls() throws JSONException {
14467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray array = new JSONArray();
145609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        array.put(3, (Collection) null);
14667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put(0, JSONObject.NULL);
14767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(4, array.length());
14867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("[null,null,null,null]", array.toString());
14967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
15051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        // there's 2 ways to represent null; each behaves differently!
15167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(JSONObject.NULL, array.get(0));
15267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        try {
15353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            array.get(1);
15467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            fail();
15567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        } catch (JSONException e) {
15667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        }
15767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        try {
15853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            array.get(2);
15967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            fail();
16067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        } catch (JSONException e) {
16167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        }
16267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        try {
16353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            array.get(3);
16467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            fail();
16567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        } catch (JSONException e) {
16667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        }
16767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(JSONObject.NULL, array.opt(0));
16867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(null, array.opt(1));
16967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(null, array.opt(2));
17067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(null, array.opt(3));
17167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(array.isNull(0));
17267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(array.isNull(1));
17367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(array.isNull(2));
17467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(array.isNull(3));
17567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("null", array.optString(0));
17667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("", array.optString(1));
17767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("", array.optString(2));
17867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("", array.optString(3));
17967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
18067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
181adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson    /**
182adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson     * Our behaviour is questioned by this bug:
183adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson     * http://code.google.com/p/android/issues/detail?id=7257
184adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson     */
185adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson    public void testParseNullYieldsJSONObjectNull() throws JSONException {
186adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        JSONArray array = new JSONArray("[\"null\",null]");
187609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        array.put((Collection) null);
188adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        assertEquals("null", array.get(0));
189adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        assertEquals(JSONObject.NULL, array.get(1));
190adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        try {
191adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson            array.get(2);
192adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson            fail();
193adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        } catch (JSONException e) {
194adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        }
195adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        assertEquals("null", array.getString(0));
196adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        assertEquals("null", array.getString(1));
197adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        try {
198adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson            array.getString(2);
199adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson            fail();
200adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        } catch (JSONException e) {
201adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson        }
202adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson    }
203adfb1bcaae256f9bd591b90a5b05d30e777e27c6Jesse Wilson
20467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testNumbers() throws JSONException {
20567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray array = new JSONArray();
20667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put(Double.MIN_VALUE);
20767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put(9223372036854775806L);
20867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put(Double.MAX_VALUE);
20967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put(-0d);
21067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(4, array.length());
21167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
21251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        // toString() and getString(int) return different values for -0d
21367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("[4.9E-324,9223372036854775806,1.7976931348623157E308,-0]", array.toString());
21467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
21567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Double.MIN_VALUE, array.get(0));
21667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(9223372036854775806L, array.get(1));
21767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Double.MAX_VALUE, array.get(2));
21867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(-0d, array.get(3));
21967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Double.MIN_VALUE, array.getDouble(0));
22067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(9.223372036854776E18, array.getDouble(1));
22167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Double.MAX_VALUE, array.getDouble(2));
22267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(-0d, array.getDouble(3));
22367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(0, array.getLong(0));
22467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(9223372036854775806L, array.getLong(1));
22567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Long.MAX_VALUE, array.getLong(2));
22667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(0, array.getLong(3));
22767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(0, array.getInt(0));
22867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(-2, array.getInt(1));
22967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Integer.MAX_VALUE, array.getInt(2));
23067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(0, array.getInt(3));
23167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Double.MIN_VALUE, array.opt(0));
23267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Double.MIN_VALUE, array.optDouble(0));
23367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(0, array.optLong(0, 1L));
23467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(0, array.optInt(0, 1));
23567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("4.9E-324", array.getString(0));
23667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("9223372036854775806", array.getString(1));
23767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("1.7976931348623157E308", array.getString(2));
23867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("-0.0", array.getString(3));
23967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
24067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray other = new JSONArray();
24167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put(Double.MIN_VALUE);
24267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put(9223372036854775806L);
24367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put(Double.MAX_VALUE);
24467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put(-0d);
24567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertTrue(array.equals(other));
24667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        other.put(0, 0L);
24767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.equals(other));
24867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
24967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
25067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testStrings() throws JSONException {
25167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray array = new JSONArray();
25267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put("true");
25367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put("5.5");
25467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put("9223372036854775806");
25567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put("null");
25667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        array.put("5\"8' tall");
25767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(5, array.length());
25867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("[\"true\",\"5.5\",\"9223372036854775806\",\"null\",\"5\\\"8' tall\"]",
25967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson                array.toString());
26067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
26167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        // although the documentation doesn't mention it, join() escapes text and wraps
26267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        // strings in quotes
26367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("\"true\" \"5.5\" \"9223372036854775806\" \"null\" \"5\\\"8' tall\"",
26467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson                array.join(" "));
26567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
26667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("true", array.get(0));
26767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("null", array.getString(3));
26867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("5\"8' tall", array.getString(4));
26967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("true", array.opt(0));
27067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("5.5", array.optString(1));
27167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("9223372036854775806", array.optString(2, null));
27267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("null", array.optString(3, "-1"));
27367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.isNull(0));
27467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.isNull(3));
27567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
27667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(true, array.getBoolean(0));
27767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(true, array.optBoolean(0));
27867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(true, array.optBoolean(0, false));
27967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(0, array.optInt(0));
28067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(-2, array.optInt(0, -2));
28167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
28267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(5.5d, array.getDouble(1));
28353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals(5L, array.getLong(1));
28467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(5, array.getInt(1));
28567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(5, array.optInt(1, 3));
28667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
28767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        // The last digit of the string is a 6 but getLong returns a 7. It's probably parsing as a
28867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        // double and then converting that to a long. This is consistent with JavaScript.
28967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(9223372036854775807L, array.getLong(2));
29067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(9.223372036854776E18, array.getDouble(2));
29167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Integer.MAX_VALUE, array.getInt(2));
29267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
29367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(array.isNull(3));
29467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        try {
29567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            array.getDouble(3);
29667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            fail();
29767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        } catch (JSONException e) {
29867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        }
29967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Double.NaN, array.optDouble(3));
30067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(-1.0d, array.optDouble(3, -1.0d));
30167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
30267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
30351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testJoin() throws JSONException {
30451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        JSONArray array = new JSONArray();
305609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        array.put((Collection) null);
30651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals("null", array.join(" & "));
30751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        array.put("\"");
30851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals("null & \"\\\"\"", array.join(" & "));
30951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        array.put(5);
31051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals("null & \"\\\"\" & 5", array.join(" & "));
31151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        array.put(true);
31251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals("null & \"\\\"\" & 5 & true", array.join(" & "));
31351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        array.put(new JSONArray(Arrays.asList(true, false)));
31451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals("null & \"\\\"\" & 5 & true & [true,false]", array.join(" & "));
31551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        array.put(new JSONObject(Collections.singletonMap("x", 6)));
31651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals("null & \"\\\"\" & 5 & true & [true,false] & {\"x\":6}", array.join(" & "));
31751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
31851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
31951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testJoinWithNull() throws JSONException {
32051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        JSONArray array = new JSONArray(Arrays.asList(5, 6));
32151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals("5null6", array.join(null));
32251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
32351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
32451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testJoinWithSpecialCharacters() throws JSONException {
32551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        JSONArray array = new JSONArray(Arrays.asList(5, 6));
32651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals("5\"6", array.join("\""));
32751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
32851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
32967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testToJSONObject() throws JSONException {
33067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray keys = new JSONArray();
33167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        keys.put("a");
33267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        keys.put("b");
33367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
33467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray values = new JSONArray();
33567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        values.put(5.5d);
33667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        values.put(false);
33767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
33867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONObject object = values.toJSONObject(keys);
33967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(5.5d, object.get("a"));
34067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(false, object.get("b"));
34167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
34267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        keys.put(0, "a");
34367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        values.put(0, 11.0d);
34467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(5.5d, object.get("a"));
34567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
34667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
34767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testToJSONObjectWithNulls() throws JSONException {
34867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray keys = new JSONArray();
34967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        keys.put("a");
35067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        keys.put("b");
35167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
35267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray values = new JSONArray();
35367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        values.put(5.5d);
354609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        values.put((Collection) null);
35567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
35651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        // null values are stripped!
35767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONObject object = values.toJSONObject(keys);
35867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(1, object.length());
35967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertFalse(object.has("b"));
36067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("{\"a\":5.5}", object.toString());
36167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
36267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
36353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    public void testToJSONObjectMoreNamesThanValues() throws JSONException {
36453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONArray keys = new JSONArray();
36553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        keys.put("a");
36653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        keys.put("b");
36753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONArray values = new JSONArray();
36853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        values.put(5.5d);
36953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONObject object = values.toJSONObject(keys);
37053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals(1, object.length());
37153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals(5.5d, object.get("a"));
37253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    }
37353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson
37453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    public void testToJSONObjectMoreValuesThanNames() throws JSONException {
37553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONArray keys = new JSONArray();
37653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        keys.put("a");
37753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONArray values = new JSONArray();
37853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        values.put(5.5d);
37953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        values.put(11.0d);
38053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONObject object = values.toJSONObject(keys);
38153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals(1, object.length());
38253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals(5.5d, object.get("a"));
38353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    }
38453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson
38553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    public void testToJSONObjectNullKey() throws JSONException {
38653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONArray keys = new JSONArray();
38753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        keys.put(JSONObject.NULL);
38853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONArray values = new JSONArray();
38953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        values.put(5.5d);
39053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONObject object = values.toJSONObject(keys);
39153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals(1, object.length());
39253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals(5.5d, object.get("null"));
39353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    }
39453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson
39567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testPutUnsupportedNumbers() throws JSONException {
39667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray array = new JSONArray();
39767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
39867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        try {
39967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            array.put(Double.NaN);
40067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            fail();
40167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        } catch (JSONException e) {
40267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        }
40367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        try {
40467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            array.put(0, Double.NEGATIVE_INFINITY);
40567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            fail();
40667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        } catch (JSONException e) {
40767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        }
40867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        try {
40967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            array.put(0, Double.POSITIVE_INFINITY);
41067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson            fail();
41167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        } catch (JSONException e) {
41267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        }
41367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
41467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
41551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testPutUnsupportedNumbersAsObject() throws JSONException {
41651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        JSONArray array = new JSONArray();
41751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        array.put(Double.valueOf(Double.NaN));
41851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        array.put(Double.valueOf(Double.NEGATIVE_INFINITY));
41951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        array.put(Double.valueOf(Double.POSITIVE_INFINITY));
42051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals(null, array.toString());
42151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
42251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
42353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    /**
42453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson     * Although JSONArray is usually defensive about which numbers it accepts,
42553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson     * it doesn't check inputs in its constructor.
42653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson     */
42767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testCreateWithUnsupportedNumbers() throws JSONException {
42867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray array = new JSONArray(Arrays.asList(5.5, Double.NaN));
42967f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(2, array.length());
43067f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(5.5, array.getDouble(0));
43167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(Double.NaN, array.getDouble(1));
43267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
43367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
43467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testToStringWithUnsupportedNumbers() throws JSONException {
43551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        // when the array contains an unsupported number, toString returns null!
43667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray array = new JSONArray(Arrays.asList(5.5, Double.NaN));
43767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertNull(array.toString());
43867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
439f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes
44053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    public void testListConstructorCopiesContents() throws JSONException {
44153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        List<Object> contents = Arrays.<Object>asList(5);
44253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONArray array = new JSONArray(contents);
44353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        contents.set(0, 10);
44453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals(5, array.get(0));
44553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    }
44667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
44751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testTokenerConstructor() throws JSONException {
44851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        JSONArray object = new JSONArray(new JSONTokener("[false]"));
44951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals(1, object.length());
45051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals(false, object.get(0));
45151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
45251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
45351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testTokenerConstructorWrongType() throws JSONException {
45451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        try {
45551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            new JSONArray(new JSONTokener("{\"foo\": false}"));
45651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            fail();
45751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        } catch (JSONException e) {
45851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        }
45951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
46051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
46151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testTokenerConstructorNull() throws JSONException {
46251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        try {
46351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            new JSONArray((JSONTokener) null);
46451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            fail();
46551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        } catch (NullPointerException e) {
46651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        }
46751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
46851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
46951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testTokenerConstructorParseFail() {
47051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        try {
47151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            new JSONArray(new JSONTokener("["));
47251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            fail();
47351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        } catch (JSONException e) {
47451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        } catch (StackOverflowError e) {
47551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            fail("Stack overflowed on input: \"[\"");
47651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        }
47751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
47851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
47951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testStringConstructor() throws JSONException {
48051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        JSONArray object = new JSONArray("[false]");
48151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals(1, object.length());
48251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        assertEquals(false, object.get(0));
48351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
48451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
48551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testStringConstructorWrongType() throws JSONException {
48651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        try {
48751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            new JSONArray("{\"foo\": false}");
48851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            fail();
48951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        } catch (JSONException e) {
49051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        }
49151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
49251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
49351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testStringConstructorNull() throws JSONException {
49451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        try {
49551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            new JSONArray((String) null);
49651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            fail();
49751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        } catch (NullPointerException e) {
49851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        }
49951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
50051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
50151a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    public void testStringConstructorParseFail() {
50251a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        try {
50351a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            new JSONArray("[");
50451a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            fail();
50551a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        } catch (JSONException e) {
50651a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        } catch (StackOverflowError e) {
50751a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson            fail("Stack overflowed on input: \"[\"");
50851a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson        }
50951a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson    }
51051a095f0bc7aadfcc7e6b3873b97c050c523d102Jesse Wilson
51167f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    public void testCreate() throws JSONException {
51267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        JSONArray array = new JSONArray(Arrays.asList(5.5, true));
51367f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(2, array.length());
51467f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(5.5, array.getDouble(0));
51567f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals(true, array.get(1));
51667f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson        assertEquals("[5.5,true]", array.toString());
51767f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson    }
51867f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson
51953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    public void testAccessOutOfBounds() throws JSONException {
52053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        JSONArray array = new JSONArray();
52153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        array.put("foo");
52253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals(null, array.opt(3));
52353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals(null, array.opt(-3));
52453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals("", array.optString(3));
52553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        assertEquals("", array.optString(-3));
52653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        try {
52753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            array.get(3);
52853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            fail();
52953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        } catch (JSONException e) {
53053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        }
53153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        try {
53253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            array.get(-3);
53353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            fail();
53453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        } catch (JSONException e) {
53553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        }
53653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        try {
53753c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            array.getString(3);
53853c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            fail();
53953c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        } catch (JSONException e) {
54053c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        }
54153c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        try {
54253c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            array.getString(-3);
54353c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson            fail();
54453c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        } catch (JSONException e) {
54553c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson        }
54653c13031e4b68645b0ca7c7a335aae41d0b84276Jesse Wilson    }
547609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes
548609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes    public void test_remove() throws Exception {
549609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        JSONArray a = new JSONArray();
550609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        assertEquals(null, a.remove(-1));
551609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        assertEquals(null, a.remove(0));
552609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes
553609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        a.put("hello");
554609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        assertEquals(null, a.remove(-1));
555609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        assertEquals(null, a.remove(1));
556609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        assertEquals("hello", a.remove(0));
557609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes        assertEquals(null, a.remove(0));
558609601075ec0934cb23783c0b1194ecde041b6f5Elliott Hughes    }
559c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes
560c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes    enum MyEnum { A, B, C; }
561c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes
562c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes    // https://code.google.com/p/android/issues/detail?id=62539
563c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes    public void testEnums() throws Exception {
564c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes        // This works because it's in java.* and any class in there falls back to toString.
565c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes        JSONArray a1 = new JSONArray(java.lang.annotation.RetentionPolicy.values());
566c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes        assertEquals("[\"SOURCE\",\"CLASS\",\"RUNTIME\"]", a1.toString());
567c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes
568c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes        // This doesn't because it's not.
569c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes        JSONArray a2 = new JSONArray(MyEnum.values());
570d62e296abe060ff7557e731fe70f240dbaa142e8Narayan Kamath        assertEquals("[null,null,null]", a2.toString());
571c06da90b0b7d26e68805d4944fbc5446d48fe360Elliott Hughes    }
57267f4459a2fcc50cb3378c302b6c663af48f60ff0Jesse Wilson}
573