Add lots of tests for pack/unpack code, fix bugs found
[jansson.git] / test / suites / api / test_pack.c
index 772afb1..add5841 100644 (file)
@@ -22,7 +22,7 @@ int main()
      */
 
     /* true */
-    value = json_pack_ex(&error, 0, "b", 1);
+    value = json_pack("b", 1);
     if(!json_is_true(value))
         fail("json_pack boolean failed");
     if(value->refcount != (ssize_t)-1)
@@ -30,7 +30,7 @@ int main()
     json_decref(value);
 
     /* false */
-    value = json_pack_ex(&error, 0, "b", 0);
+    value = json_pack("b", 0);
     if(!json_is_false(value))
         fail("json_pack boolean failed");
     if(value->refcount != (ssize_t)-1)
@@ -38,7 +38,7 @@ int main()
     json_decref(value);
 
     /* null */
-    value = json_pack_ex(&error, 0, "n");
+    value = json_pack("n");
     if(!json_is_null(value))
         fail("json_pack null failed");
     if(value->refcount != (ssize_t)-1)
@@ -46,16 +46,23 @@ int main()
     json_decref(value);
 
     /* integer */
-    value = json_pack_ex(&error, 0, "i", 1);
+    value = json_pack("i", 1);
     if(!json_is_integer(value) || json_integer_value(value) != 1)
         fail("json_pack integer failed");
     if(value->refcount != (ssize_t)1)
         fail("json_pack integer refcount failed");
     json_decref(value);
 
+    /* integer from json_int_t */
+    value = json_pack("I", (json_int_t)555555);
+    if(!json_is_integer(value) || json_integer_value(value) != 555555)
+        fail("json_pack json_int_t failed");
+    if(value->refcount != (ssize_t)1)
+        fail("json_pack integer refcount failed");
+    json_decref(value);
 
     /* real */
-    value = json_pack_ex(&error, 0, "f", 1.0);
+    value = json_pack("f", 1.0);
     if(!json_is_real(value) || json_real_value(value) != 1.0)
         fail("json_pack real failed");
     if(value->refcount != (ssize_t)1)
@@ -63,7 +70,7 @@ int main()
     json_decref(value);
 
     /* string */
-    value = json_pack_ex(&error, 0, "s", "test");
+    value = json_pack("s", "test");
     if(!json_is_string(value) || strcmp("test", json_string_value(value)))
         fail("json_pack string failed");
     if(value->refcount != (ssize_t)1)
@@ -71,7 +78,7 @@ int main()
     json_decref(value);
 
     /* empty object */
-    value = json_pack_ex(&error, 0, "{}", 1.0);
+    value = json_pack("{}", 1.0);
     if(!json_is_object(value) || json_object_size(value) != 0)
         fail("json_pack empty object failed");
     if(value->refcount != (ssize_t)1)
@@ -79,7 +86,7 @@ int main()
     json_decref(value);
 
     /* empty list */
-    value = json_pack_ex(&error, 0, "[]", 1.0);
+    value = json_pack("[]", 1.0);
     if(!json_is_array(value) || json_array_size(value) != 0)
         fail("json_pack empty list failed");
     if(value->refcount != (ssize_t)1)
@@ -87,7 +94,7 @@ int main()
     json_decref(value);
 
     /* non-incref'd object */
-    value = json_pack_ex(&error, 0, "o", json_integer(1));
+    value = json_pack("o", json_integer(1));
     if(!json_is_integer(value) || json_integer_value(value) != 1)
         fail("json_pack object failed");
     if(value->refcount != (ssize_t)1)
@@ -95,7 +102,7 @@ int main()
     json_decref(value);
 
     /* incref'd object */
-    value = json_pack_ex(&error, 0, "O", json_integer(1));
+    value = json_pack("O", json_integer(1));
     if(!json_is_integer(value) || json_integer_value(value) != 1)
         fail("json_pack object failed");
     if(value->refcount != (ssize_t)2)
@@ -104,7 +111,7 @@ int main()
     json_decref(value);
 
     /* simple object */
-    value = json_pack_ex(&error, 0, "{s:[]}", "foo");
+    value = json_pack("{s:[]}", "foo");
     if(!json_is_object(value) || json_object_size(value) != 1)
         fail("json_pack array failed");
     if(!json_is_array(json_object_get(value, "foo")))
@@ -114,7 +121,7 @@ int main()
     json_decref(value);
 
     /* simple array */
-    value = json_pack_ex(&error, 0, "[i,i,i]", 0, 1, 2);
+    value = json_pack("[i,i,i]", 0, 1, 2);
     if(!json_is_array(value) || json_array_size(value) != 3)
         fail("json_pack object failed");
     for(i=0; i<3; i++)
@@ -127,19 +134,19 @@ int main()
     json_decref(value);
 
     /* Whitespace; regular string */
-    value = json_pack_ex(&error, 0, " s ", "test");
+    value = json_pack(" s ", "test");
     if(!json_is_string(value) || strcmp("test", json_string_value(value)))
         fail("json_pack string (with whitespace) failed");
     json_decref(value);
 
     /* Whitespace; empty array */
-    value = json_pack_ex(&error, 0, "[ ]");
+    value = json_pack("[ ]");
     if(!json_is_array(value) || json_array_size(value) != 0)
         fail("json_pack empty array (with whitespace) failed");
     json_decref(value);
 
     /* Whitespace; array */
-    value = json_pack_ex(&error, 0, "[ i , i,  i ] ", 1, 2, 3);
+    value = json_pack("[ i , i,  i ] ", 1, 2, 3);
     if(!json_is_array(value) || json_array_size(value) != 3)
         fail("json_pack array (with whitespace) failed");
     json_decref(value);
@@ -148,56 +155,67 @@ int main()
      * Invalid cases
      */
 
+    /* newline in format string */
+    if(json_pack_ex(&error, 0, "{\n\n1"))
+        fail("json_pack failed to catch invalid format '1'");
+    check_error("Expected format 's', got '1'", "<format>", 3, 1, 4);
+
     /* mismatched open/close array/object */
     if(json_pack_ex(&error, 0, "[}"))
         fail("json_pack failed to catch mismatched '}'");
-    if(error.line != 1 || error.column != 2)
-        fail("json_pack didn't get the error coordinates right!");
+    check_error("Unexpected format character '}'", "<format>", 1, 2, 2);
 
     if(json_pack_ex(&error, 0, "{]"))
         fail("json_pack failed to catch mismatched ']'");
-    if(error.line != 1 || error.column != 2)
-        fail("json_pack didn't get the error coordinates right!");
+    check_error("Expected format 's', got ']'", "<format>", 1, 2, 2);
 
     /* missing close array */
     if(json_pack_ex(&error, 0, "["))
         fail("json_pack failed to catch missing ']'");
-    if(error.line != 1 || error.column != 2)
-        fail("json_pack didn't get the error coordinates right!");
+    check_error("Unexpected end of format string", "<format>", 1, 2, 2);
 
     /* missing close object */
     if(json_pack_ex(&error, 0, "{"))
         fail("json_pack failed to catch missing '}'");
-    if(error.line != 1 || error.column != 2)
-        fail("json_pack didn't get the error coordinates right!");
+    check_error("Unexpected end of format string", "<format>", 1, 2, 2);
+
+    /* garbage after format string */
+    if(json_pack_ex(&error, 0, "[i]a", 42))
+        fail("json_pack failed to catch garbage after format string");
+    check_error("Garbage after format string", "<format>", 1, 4, 4);
+
+    if(json_pack_ex(&error, 0, "ia", 42))
+        fail("json_pack failed to catch garbage after format string");
+    check_error("Garbage after format string", "<format>", 1, 2, 2);
 
     /* NULL string */
     if(json_pack_ex(&error, 0, "s", NULL))
         fail("json_pack failed to catch null argument string");
-    if(error.line != 1 || error.column != 1)
-        fail("json_pack didn't get the error coordinates right!");
+    check_error("NULL string argument", "<args>", 1, 1, 1);
 
     /* NULL format */
     if(json_pack_ex(&error, 0, NULL))
         fail("json_pack failed to catch NULL format string");
-    if(error.line != -1 || error.column != -1)
-        fail("json_pack didn't get the error coordinates right!");
+    check_error("NULL or empty format string", "<format>", -1, -1, 0);
+
+    /* NULL key */
+    if(json_pack_ex(&error, 0, "{s:i}", NULL, 1))
+        fail("json_pack failed to catch NULL key");
+    check_error("NULL object key", "<args>", 1, 2, 2);
 
     /* More complicated checks for row/columns */
     if(json_pack_ex(&error, 0, "{ {}: s }", "foo"))
         fail("json_pack failed to catch object as key");
-    if(error.line != 1 || error.column != 3)
-        fail("json_pack didn't get the error coordinates right!");
+    check_error("Expected format 's', got '{'", "<format>", 1, 3, 3);
+
 
     if(json_pack_ex(&error, 0, "{ s: {},  s:[ii{} }", "foo", "bar", 12, 13))
         fail("json_pack failed to catch missing ]");
-    if(error.line != 1 || error.column != 19)
-        fail("json_pack didn't get the error coordinates right!");
+    check_error("Unexpected format character '}'", "<format>", 1, 19, 19);
 
     if(json_pack_ex(&error, 0, "[[[[[   [[[[[  [[[[ }]]]] ]]]] ]]]]]"))
         fail("json_pack failed to catch extra }");
-    if(error.line != 1 || error.column != 21)
-        fail("json_pack didn't get the error coordinates right!");
+    check_error("Unexpected format character '}'", "<format>", 1, 21, 21);
 
     return 0;
 }