mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Development for stleary#516 completed with rebased repository
- Introduced JSONObject(int) constructor. - int > Initial capacity of the underlying data structure - Test for new introduced JSONArray(int) constructor. 1. Checked with input parameter: 0 2. Checked with input parameter: positive number 3. Checked with positive number input parameter and compared length 4. If input parameter is negative number JSONException is thrown: JSONArray initial capacity cannot be negative.
This commit is contained in:
parent
19bb6fd606
commit
d088cf014e
2 changed files with 37 additions and 0 deletions
|
@ -205,6 +205,22 @@ public class JSONArray implements Iterable<Object> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a JSONArray with the specified initial capacity.
|
||||||
|
*
|
||||||
|
* @param initialCapacity
|
||||||
|
* the initial capacity of the JSONArray.
|
||||||
|
* @throws JSONException
|
||||||
|
* If the initial capacity is negative.
|
||||||
|
*/
|
||||||
|
public JSONArray(int initialCapacity) throws JSONException {
|
||||||
|
if (initialCapacity < 0) {
|
||||||
|
throw new JSONException(
|
||||||
|
"JSONArray initial capacity cannot be negative.");
|
||||||
|
}
|
||||||
|
this.myArrayList = new ArrayList<Object>(initialCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Object> iterator() {
|
public Iterator<Object> iterator() {
|
||||||
return this.myArrayList.iterator();
|
return this.myArrayList.iterator();
|
||||||
|
|
|
@ -26,6 +26,7 @@ SOFTWARE.
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
@ -1119,4 +1120,24 @@ public class JSONArrayTest {
|
||||||
assertTrue("Removing an entry should succeed", list.remove(2) != null);
|
assertTrue("Removing an entry should succeed", list.remove(2) != null);
|
||||||
assertTrue("List should have 2 elements", list.size() == 2);
|
assertTrue("List should have 2 elements", list.size() == 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a JSONArray with specified initial capacity.
|
||||||
|
* Expects an exception if the initial capacity is specified as a negative integer
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testJSONArrayInt() {
|
||||||
|
assertNotNull(new JSONArray(0));
|
||||||
|
assertNotNull(new JSONArray(5));
|
||||||
|
// Check Size -> Even though the capacity of the JSONArray can be specified using a positive
|
||||||
|
// integer but the length of JSONArray always reflects upon the items added into it.
|
||||||
|
assertEquals(0l, (long)new JSONArray(10).length());
|
||||||
|
try {
|
||||||
|
assertNotNull("Should throw an exception", new JSONArray(-1));
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expected an exception message",
|
||||||
|
"JSONArray initial capacity cannot be negative.",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue