Disallow NaN or Inf real values
[jansson.git] / test / suites / api / test_number.c
index 6a6524b..c256598 100644 (file)
@@ -1,14 +1,15 @@
 /*
- * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009-2012 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.
  */
 
+#include <math.h>
 #include <jansson.h>
 #include "util.h"
 
-int main()
+static void run_tests()
 {
     json_t *integer, *real;
     int i;
@@ -40,5 +41,33 @@ int main()
     json_decref(integer);
     json_decref(real);
 
-    return 0;
+#ifdef NAN
+    real = json_real(NAN);
+    if(real != NULL)
+        fail("could construct a real from NaN");
+
+    real = json_real(1.0);
+    if(json_real_set(real, NAN) != -1)
+        fail("could set a real to NaN");
+
+    if(json_real_value(real) != 1.0)
+        fail("real value changed unexpectedly");
+
+    json_decref(real);
+#endif
+
+#ifdef INFINITY
+    real = json_real(INFINITY);
+    if(real != NULL)
+        fail("could construct a real from Inf");
+
+    real = json_real(1.0);
+    if(json_real_set(real, INFINITY) != -1)
+        fail("could set a real to Inf");
+
+    if(json_real_value(real) != 1.0)
+        fail("real value changed unexpectedly");
+
+    json_decref(real);
+#endif
 }