jansson/test/suites/api/test_number.c

124 lines
3.2 KiB
C
Raw Normal View History

2009-08-25 01:54:40 +08:00
/*
2016-09-18 19:17:03 +08:00
* Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
2009-08-25 01:54:40 +08:00
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include "util.h"
2019-10-17 14:08:51 +08:00
#include <jansson.h>
#include <math.h>
2009-08-25 01:54:40 +08:00
#ifdef INFINITY
// This test triggers "warning C4756: overflow in constant arithmetic"
// in Visual Studio. This warning is triggered here by design, so disable it.
// (This can only be done on function level so we keep these tests separate)
#ifdef _MSC_VER
#pragma warning(push)
2019-10-17 14:08:51 +08:00
#pragma warning(disable : 4756)
#endif
2019-10-17 14:08:51 +08:00
static void test_inifity() {
json_t *real = json_real(INFINITY);
if (real != NULL)
2019-10-17 14:08:51 +08:00
fail("could construct a real from Inf");
real = json_real(1.0);
if (json_real_set(real, INFINITY) != -1)
2019-10-17 14:08:51 +08:00
fail("could set a real to Inf");
if (json_real_value(real) != 1.0)
2019-10-17 14:08:51 +08:00
fail("real value changed unexpectedly");
json_decref(real);
#ifdef _MSC_VER
#pragma warning(pop)
#endif
}
#endif // INFINITY
2019-10-17 14:08:51 +08:00
static void test_bad_args(void) {
json_t *txt = json_string("test");
2019-10-17 14:08:51 +08:00
if (json_integer_value(NULL) != 0)
fail("json_integer_value did not return 0 for non-integer");
2019-10-17 14:08:51 +08:00
if (json_integer_value(txt) != 0)
fail("json_integer_value did not return 0 for non-integer");
2019-10-17 14:08:51 +08:00
if (!json_integer_set(NULL, 0))
fail("json_integer_set did not return error for non-integer");
2019-10-17 14:08:51 +08:00
if (!json_integer_set(txt, 0))
fail("json_integer_set did not return error for non-integer");
2019-10-17 14:08:51 +08:00
if (json_real_value(NULL) != 0.0)
fail("json_real_value did not return 0.0 for non-real");
2019-10-17 14:08:51 +08:00
if (json_real_value(txt) != 0.0)
fail("json_real_value did not return 0.0 for non-real");
2019-10-17 14:08:51 +08:00
if (!json_real_set(NULL, 0.0))
fail("json_real_set did not return error for non-real");
2019-10-17 14:08:51 +08:00
if (!json_real_set(txt, 0.0))
fail("json_real_set did not return error for non-real");
2019-10-17 14:08:51 +08:00
if (json_number_value(NULL) != 0.0)
fail("json_number_value did not return 0.0 for non-numeric");
2019-10-17 14:08:51 +08:00
if (json_number_value(txt) != 0.0)
fail("json_number_value did not return 0.0 for non-numeric");
if (txt->refcount != 1)
fail("unexpected reference count for txt");
json_decref(txt);
}
2019-10-17 14:08:51 +08:00
static void run_tests() {
2009-08-25 01:54:40 +08:00
json_t *integer, *real;
json_int_t i;
2009-08-25 01:54:40 +08:00
double d;
integer = json_integer(5);
real = json_real(100.1);
2019-10-17 14:08:51 +08:00
if (!integer)
2009-08-25 01:54:40 +08:00
fail("unable to create integer");
2019-10-17 14:08:51 +08:00
if (!real)
2009-08-25 01:54:40 +08:00
fail("unable to create real");
i = json_integer_value(integer);
2019-10-17 14:08:51 +08:00
if (i != 5)
2009-08-25 01:54:40 +08:00
fail("wrong integer value");
d = json_real_value(real);
2019-10-17 14:08:51 +08:00
if (d != 100.1)
2009-08-25 01:54:40 +08:00
fail("wrong real value");
d = json_number_value(integer);
2019-10-17 14:08:51 +08:00
if (d != 5.0)
2009-08-25 01:54:40 +08:00
fail("wrong number value");
d = json_number_value(real);
2019-10-17 14:08:51 +08:00
if (d != 100.1)
2009-08-25 01:54:40 +08:00
fail("wrong number value");
json_decref(integer);
json_decref(real);
2012-09-14 02:30:19 +08:00
#ifdef NAN
real = json_real(NAN);
2019-10-17 14:08:51 +08:00
if (real != NULL)
2012-09-14 02:30:19 +08:00
fail("could construct a real from NaN");
real = json_real(1.0);
2019-10-17 14:08:51 +08:00
if (json_real_set(real, NAN) != -1)
2012-09-14 02:30:19 +08:00
fail("could set a real to NaN");
2019-10-17 14:08:51 +08:00
if (json_real_value(real) != 1.0)
2012-09-14 02:30:19 +08:00
fail("real value changed unexpectedly");
json_decref(real);
#endif
#ifdef INFINITY
test_inifity();
2012-09-14 02:30:19 +08:00
#endif
test_bad_args();
2009-08-25 01:54:40 +08:00
}