test_number.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2009-2016 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. #include <math.h>
  8. #include <jansson.h>
  9. #include "util.h"
  10. #ifdef INFINITY
  11. // This test triggers "warning C4756: overflow in constant arithmetic"
  12. // in Visual Studio. This warning is triggered here by design, so disable it.
  13. // (This can only be done on function level so we keep these tests separate)
  14. #ifdef _MSC_VER
  15. #pragma warning(push)
  16. #pragma warning (disable: 4756)
  17. #endif
  18. static void test_inifity()
  19. {
  20. json_t *real = json_real(INFINITY);
  21. if (real != NULL)
  22. fail("could construct a real from Inf");
  23. real = json_real(1.0);
  24. if (json_real_set(real, INFINITY) != -1)
  25. fail("could set a real to Inf");
  26. if (json_real_value(real) != 1.0)
  27. fail("real value changed unexpectedly");
  28. json_decref(real);
  29. #ifdef _MSC_VER
  30. #pragma warning(pop)
  31. #endif
  32. }
  33. #endif // INFINITY
  34. static void run_tests()
  35. {
  36. json_t *integer, *real;
  37. json_int_t i;
  38. double d;
  39. integer = json_integer(5);
  40. real = json_real(100.1);
  41. if(!integer)
  42. fail("unable to create integer");
  43. if(!real)
  44. fail("unable to create real");
  45. i = json_integer_value(integer);
  46. if(i != 5)
  47. fail("wrong integer value");
  48. d = json_real_value(real);
  49. if(d != 100.1)
  50. fail("wrong real value");
  51. d = json_number_value(integer);
  52. if(d != 5.0)
  53. fail("wrong number value");
  54. d = json_number_value(real);
  55. if(d != 100.1)
  56. fail("wrong number value");
  57. json_decref(integer);
  58. json_decref(real);
  59. #ifdef NAN
  60. real = json_real(NAN);
  61. if(real != NULL)
  62. fail("could construct a real from NaN");
  63. real = json_real(1.0);
  64. if(json_real_set(real, NAN) != -1)
  65. fail("could set a real to NaN");
  66. if(json_real_value(real) != 1.0)
  67. fail("real value changed unexpectedly");
  68. json_decref(real);
  69. #endif
  70. #ifdef INFINITY
  71. test_inifity();
  72. #endif
  73. }