Better argument validation
[jansson.git] / test / testprogs / test_array.c
1 /*
2  * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
3  *
4  * Jansson is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7
8 #include <jansson.h>
9 #include "util.h"
10
11 int main()
12 {
13     json_t *array, *five, *seven, *value;
14     int i;
15
16     array = json_array();
17     five = json_integer(5);
18     seven = json_integer(7);
19
20     if(!array)
21         fail("unable to create array");
22     if(!five)
23         fail("unable to create integer");
24     if(!seven)
25         fail("unable to create integer");
26
27     if(json_array_size(array) != 0)
28         fail("empty array has nonzero size");
29
30     if(!json_array_append(array, NULL))
31         fail("able to append NULL");
32
33     if(json_array_append(array, five))
34         fail("unable to append");
35
36     if(json_array_size(array) != 1)
37         fail("wrong array size");
38
39     value = json_array_get(array, 0);
40     if(!value)
41         fail("unable to get item");
42     if(value != five)
43         fail("got wrong value");
44
45     if(json_array_append(array, seven))
46         fail("unable to append value");
47
48     if(json_array_size(array) != 2)
49         fail("wrong array size");
50
51     value = json_array_get(array, 1);
52     if(!value)
53         fail("unable to get item");
54     if(value != seven)
55         fail("got wrong value");
56
57     if(json_array_set(array, 0, seven))
58         fail("unable to set value");
59
60     if(!json_array_set(array, 0, NULL))
61         fail("able to set NULL");
62
63     if(json_array_size(array) != 2)
64         fail("wrong array size");
65
66     value = json_array_get(array, 0);
67     if(!value)
68         fail("unable to get item");
69     if(value != seven)
70         fail("got wrong value");
71
72     if(json_array_get(array, 2) != NULL)
73         fail("able to get value out of bounds");
74
75     if(!json_array_set(array, 2, seven))
76         fail("able to set value out of bounds");
77
78     for(i = 2; i < 30; i++) {
79         if(json_array_append(array, seven))
80             fail("unable to append value");
81
82         if(json_array_size(array) != i + 1)
83             fail("wrong array size");
84     }
85
86     for(i = 0; i < 30; i++) {
87         value = json_array_get(array, i);
88         if(!value)
89             fail("unable to get item");
90         if(value != seven)
91             fail("got wrong value");
92     }
93
94     if(json_array_set_new(array, 15, json_integer(123)))
95         fail("unable to set new value");
96
97     value = json_array_get(array, 15);
98     if(!json_is_integer(value) || json_integer_value(value) != 123)
99       fail("json_array_set_new works incorrectly");
100
101     if(!json_array_set_new(array, 15, NULL))
102         fail("able to set_new NULL value");
103
104     if(json_array_append_new(array, json_integer(321)))
105         fail("unable to append new value");
106
107     value = json_array_get(array, json_array_size(array) - 1);
108     if(!json_is_integer(value) || json_integer_value(value) != 321)
109       fail("json_array_append_new works incorrectly");
110
111     if(!json_array_append_new(array, NULL))
112         fail("able to append_new NULL value");
113
114     json_decref(five);
115     json_decref(seven);
116     json_decref(array);
117
118     return 0;
119 }