test_number.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2009-2012 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. static void run_tests()
  11. {
  12. json_t *integer, *real;
  13. int i;
  14. double d;
  15. integer = json_integer(5);
  16. real = json_real(100.1);
  17. if(!integer)
  18. fail("unable to create integer");
  19. if(!real)
  20. fail("unable to create real");
  21. i = json_integer_value(integer);
  22. if(i != 5)
  23. fail("wrong integer value");
  24. d = json_real_value(real);
  25. if(d != 100.1)
  26. fail("wrong real value");
  27. d = json_number_value(integer);
  28. if(d != 5.0)
  29. fail("wrong number value");
  30. d = json_number_value(real);
  31. if(d != 100.1)
  32. fail("wrong number value");
  33. json_decref(integer);
  34. json_decref(real);
  35. #ifdef NAN
  36. real = json_real(NAN);
  37. if(real != NULL)
  38. fail("could construct a real from NaN");
  39. real = json_real(1.0);
  40. if(json_real_set(real, NAN) != -1)
  41. fail("could set a real to NaN");
  42. if(json_real_value(real) != 1.0)
  43. fail("real value changed unexpectedly");
  44. json_decref(real);
  45. #endif
  46. #ifdef INFINITY
  47. real = json_real(INFINITY);
  48. if(real != NULL)
  49. fail("could construct a real from Inf");
  50. real = json_real(1.0);
  51. if(json_real_set(real, INFINITY) != -1)
  52. fail("could set a real to Inf");
  53. if(json_real_value(real) != 1.0)
  54. fail("real value changed unexpectedly");
  55. json_decref(real);
  56. #endif
  57. }
粤ICP备19079148号