Fix reference counting on true, false and null
[jansson.git] / test / suites / api / test_simple.c
index 9d7691e..1769c65 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
  *
  * Jansson is free software; you can redistribute it and/or modify
  * it under the terms of the MIT license. See LICENSE for details.
@@ -73,6 +73,33 @@ int main()
     if(value)
         fail("json_string(<invalid utf-8>) failed");
 
+    value = json_string_nocheck("foo");
+    if(!value)
+        fail("json_string_nocheck failed");
+    if(strcmp(json_string_value(value), "foo"))
+        fail("invalid string value");
+
+    if(json_string_set_nocheck(value, "bar"))
+        fail("json_string_set_nocheck failed");
+    if(strcmp(json_string_value(value), "bar"))
+        fail("invalid string value");
+
+    json_decref(value);
+
+    /* invalid UTF-8 */
+    value = json_string_nocheck("qu\xff");
+    if(!value)
+        fail("json_string_nocheck failed");
+    if(strcmp(json_string_value(value), "qu\xff"))
+        fail("invalid string value");
+
+    if(json_string_set_nocheck(value, "\xfd\xfe\xff"))
+        fail("json_string_set_nocheck failed");
+    if(strcmp(json_string_value(value), "\xfd\xfe\xff"))
+        fail("invalid string value");
+
+    json_decref(value);
+
 
     value = json_integer(123);
     if(!value)
@@ -123,5 +150,36 @@ int main()
         fail("json_null failed");
     json_decref(value);
 
+    /* Test reference counting on singletons (true, false, null) */
+    value = json_true();
+    if(value->refcount != (unsigned int)-1)
+      fail("refcounting true works incorrectly");
+    json_decref(value);
+    if(value->refcount != (unsigned int)-1)
+      fail("refcounting true works incorrectly");
+    json_incref(value);
+    if(value->refcount != (unsigned int)-1)
+      fail("refcounting true works incorrectly");
+
+    value = json_false();
+    if(value->refcount != (unsigned int)-1)
+      fail("refcounting false works incorrectly");
+    json_decref(value);
+    if(value->refcount != (unsigned int)-1)
+      fail("refcounting false works incorrectly");
+    json_incref(value);
+    if(value->refcount != (unsigned int)-1)
+      fail("refcounting false works incorrectly");
+
+    value = json_null();
+    if(value->refcount != (unsigned int)-1)
+      fail("refcounting null works incorrectly");
+    json_decref(value);
+    if(value->refcount != (unsigned int)-1)
+      fail("refcounting null works incorrectly");
+    json_incref(value);
+    if(value->refcount != (unsigned int)-1)
+      fail("refcounting null works incorrectly");
+
     return 0;
 }