convertNumber.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <stdint.h>
  5. #include <ArduinoJson.hpp>
  6. #include <catch.hpp>
  7. using namespace ARDUINOJSON_NAMESPACE;
  8. TEST_CASE("canConvertNumber<TOut, TIn>()") {
  9. SECTION("int8_t -> int8_t") {
  10. CHECK((canConvertNumber<int8_t, int8_t>(0)) == true);
  11. CHECK((canConvertNumber<int8_t, int8_t>(127)) == true);
  12. CHECK((canConvertNumber<int8_t, int8_t>(-128)) == true);
  13. }
  14. SECTION("int8_t -> int16_t") {
  15. CHECK((canConvertNumber<int16_t, int8_t>(0)) == true);
  16. CHECK((canConvertNumber<int16_t, int8_t>(127)) == true);
  17. CHECK((canConvertNumber<int16_t, int8_t>(-128)) == true);
  18. }
  19. SECTION("int8_t -> uint8_t") {
  20. CHECK((canConvertNumber<uint8_t, int8_t>(0)) == true);
  21. CHECK((canConvertNumber<uint8_t, int8_t>(127)) == true);
  22. CHECK((canConvertNumber<uint8_t, int8_t>(-128)) == false);
  23. }
  24. SECTION("int8_t -> uint16_t") {
  25. CHECK((canConvertNumber<uint16_t, int8_t>(0)) == true);
  26. CHECK((canConvertNumber<uint16_t, int8_t>(127)) == true);
  27. CHECK((canConvertNumber<uint16_t, int8_t>(-128)) == false);
  28. }
  29. SECTION("int16_t -> int8_t") {
  30. CHECK((canConvertNumber<int8_t, int16_t>(0)) == true);
  31. CHECK((canConvertNumber<int8_t, int16_t>(127)) == true);
  32. CHECK((canConvertNumber<int8_t, int16_t>(128)) == false);
  33. CHECK((canConvertNumber<int8_t, int16_t>(-128)) == true);
  34. CHECK((canConvertNumber<int8_t, int16_t>(-129)) == false);
  35. }
  36. SECTION("int16_t -> uint8_t") {
  37. CHECK((canConvertNumber<uint8_t, int16_t>(0)) == true);
  38. CHECK((canConvertNumber<uint8_t, int16_t>(255)) == true);
  39. CHECK((canConvertNumber<uint8_t, int16_t>(256)) == false);
  40. CHECK((canConvertNumber<uint8_t, int16_t>(-1)) == false);
  41. }
  42. SECTION("uint8_t -> int8_t") {
  43. CHECK((canConvertNumber<int8_t, uint8_t>(0)) == true);
  44. CHECK((canConvertNumber<int8_t, uint8_t>(127)) == true);
  45. CHECK((canConvertNumber<int8_t, uint8_t>(128)) == false);
  46. CHECK((canConvertNumber<int8_t, uint8_t>(255)) == false);
  47. }
  48. SECTION("uint8_t -> int16_t") {
  49. CHECK((canConvertNumber<int16_t, uint8_t>(0)) == true);
  50. CHECK((canConvertNumber<int16_t, uint8_t>(127)) == true);
  51. CHECK((canConvertNumber<int16_t, uint8_t>(128)) == true);
  52. CHECK((canConvertNumber<int16_t, uint8_t>(255)) == true);
  53. }
  54. SECTION("uint8_t -> uint8_t") {
  55. CHECK((canConvertNumber<uint8_t, uint8_t>(0)) == true);
  56. CHECK((canConvertNumber<uint8_t, uint8_t>(127)) == true);
  57. CHECK((canConvertNumber<uint8_t, uint8_t>(128)) == true);
  58. CHECK((canConvertNumber<uint8_t, uint8_t>(255)) == true);
  59. }
  60. SECTION("uint8_t -> uint16_t") {
  61. CHECK((canConvertNumber<uint16_t, uint8_t>(0)) == true);
  62. CHECK((canConvertNumber<uint16_t, uint8_t>(127)) == true);
  63. CHECK((canConvertNumber<uint16_t, uint8_t>(128)) == true);
  64. CHECK((canConvertNumber<uint16_t, uint8_t>(255)) == true);
  65. }
  66. SECTION("float -> int32_t") {
  67. CHECK((canConvertNumber<int32_t, float>(0)) == true);
  68. CHECK((canConvertNumber<int32_t, float>(-2.147483904e9f)) == false);
  69. CHECK((canConvertNumber<int32_t, float>(-2.147483648e+9f)) == true);
  70. CHECK((canConvertNumber<int32_t, float>(2.14748352e+9f)) == true);
  71. CHECK((canConvertNumber<int32_t, float>(2.14748365e+9f)) == false);
  72. }
  73. SECTION("double -> int32_t") {
  74. CHECK((canConvertNumber<int32_t, double>(0)) == true);
  75. CHECK((canConvertNumber<int32_t, double>(-2.147483649e+9)) == false);
  76. CHECK((canConvertNumber<int32_t, double>(-2.147483648e+9)) == true);
  77. CHECK((canConvertNumber<int32_t, double>(2.147483647e+9)) == true);
  78. CHECK((canConvertNumber<int32_t, double>(2.147483648e+9)) == false);
  79. }
  80. SECTION("float -> uint32_t") {
  81. CHECK((canConvertNumber<uint32_t, float>(0)) == true);
  82. CHECK((canConvertNumber<uint32_t, float>(-1.401298e-45f)) == false);
  83. CHECK((canConvertNumber<uint32_t, float>(4.29496704e+9f)) == true);
  84. CHECK((canConvertNumber<uint32_t, float>(4.294967296e+9f)) == false);
  85. }
  86. #if ARDUINOJSON_HAS_LONG_LONG
  87. SECTION("float -> int64_t") {
  88. CHECK((canConvertNumber<int64_t, float>(0)) == true);
  89. CHECK((canConvertNumber<int64_t, float>(-9.22337204e+18f)) == true);
  90. CHECK((canConvertNumber<int64_t, float>(9.22337149e+18f)) == true);
  91. CHECK((canConvertNumber<int64_t, float>(9.22337204e+18f)) == false);
  92. }
  93. SECTION("double -> int64_t") {
  94. CHECK((canConvertNumber<int64_t, double>(0)) == true);
  95. CHECK((canConvertNumber<int64_t, double>(-9.2233720368547758e+18)) == true);
  96. CHECK((canConvertNumber<int64_t, double>(9.2233720368547748e+18)) == true);
  97. CHECK((canConvertNumber<int64_t, double>(9.2233720368547758e+18)) == false);
  98. }
  99. SECTION("float -> uint64_t") {
  100. CHECK((canConvertNumber<uint64_t, float>(0)) == true);
  101. CHECK((canConvertNumber<uint64_t, float>(-1.401298e-45f)) == false);
  102. CHECK((canConvertNumber<uint64_t, float>(1.844674297419792384e+19f)) ==
  103. true);
  104. CHECK((canConvertNumber<uint64_t, float>(1.8446744073709551616e+19f)) ==
  105. false);
  106. }
  107. SECTION("double -> uint64_t") {
  108. CHECK((canConvertNumber<uint64_t, double>(0)) == true);
  109. CHECK((canConvertNumber<uint64_t, double>(-4.94065645841247e-324)) ==
  110. false);
  111. CHECK((canConvertNumber<uint64_t, double>(1.8446744073709549568e+19)) ==
  112. true);
  113. CHECK((canConvertNumber<uint64_t, double>(1.8446744073709551616e+19)) ==
  114. false);
  115. }
  116. #endif
  117. }