arithmeticCompare.cpp 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson/Numbers/arithmeticCompare.hpp>
  5. #include <catch.hpp>
  6. using namespace ArduinoJson::detail;
  7. TEST_CASE("arithmeticCompare()") {
  8. SECTION("int vs uint8_t") {
  9. CHECK(arithmeticCompare<int, uint8_t>(256, 1) == COMPARE_RESULT_GREATER);
  10. CHECK(arithmeticCompare<int, uint8_t>(41, 42) == COMPARE_RESULT_LESS);
  11. CHECK(arithmeticCompare<int, uint8_t>(42, 42) == COMPARE_RESULT_EQUAL);
  12. CHECK(arithmeticCompare<int, uint8_t>(43, 42) == COMPARE_RESULT_GREATER);
  13. }
  14. SECTION("unsigned vs int") {
  15. CHECK(arithmeticCompare<unsigned, int>(0, -1) == COMPARE_RESULT_GREATER);
  16. CHECK(arithmeticCompare<unsigned, int>(42, 41) == COMPARE_RESULT_GREATER);
  17. CHECK(arithmeticCompare<unsigned, int>(42, 42) == COMPARE_RESULT_EQUAL);
  18. CHECK(arithmeticCompare<unsigned, int>(42, 43) == COMPARE_RESULT_LESS);
  19. }
  20. SECTION("float vs int") {
  21. CHECK(arithmeticCompare<float, int>(42, 41) == COMPARE_RESULT_GREATER);
  22. CHECK(arithmeticCompare<float, int>(42, 42) == COMPARE_RESULT_EQUAL);
  23. CHECK(arithmeticCompare<float, int>(42, 43) == COMPARE_RESULT_LESS);
  24. }
  25. SECTION("int vs unsigned") {
  26. CHECK(arithmeticCompare<int, unsigned>(-1, 0) == COMPARE_RESULT_LESS);
  27. CHECK(arithmeticCompare<int, unsigned>(0, 0) == COMPARE_RESULT_EQUAL);
  28. CHECK(arithmeticCompare<int, unsigned>(1, 0) == COMPARE_RESULT_GREATER);
  29. CHECK(arithmeticCompare<int, unsigned>(42, 41) == COMPARE_RESULT_GREATER);
  30. CHECK(arithmeticCompare<int, unsigned>(42, 42) == COMPARE_RESULT_EQUAL);
  31. CHECK(arithmeticCompare<int, unsigned>(42, 43) == COMPARE_RESULT_LESS);
  32. }
  33. SECTION("unsigned vs unsigned") {
  34. CHECK(arithmeticCompare<unsigned, unsigned>(42, 41) ==
  35. COMPARE_RESULT_GREATER);
  36. CHECK(arithmeticCompare<unsigned, unsigned>(42, 42) ==
  37. COMPARE_RESULT_EQUAL);
  38. CHECK(arithmeticCompare<unsigned, unsigned>(42, 43) == COMPARE_RESULT_LESS);
  39. }
  40. SECTION("bool vs bool") {
  41. CHECK(arithmeticCompare<bool, bool>(false, false) == COMPARE_RESULT_EQUAL);
  42. CHECK(arithmeticCompare<bool, bool>(true, true) == COMPARE_RESULT_EQUAL);
  43. CHECK(arithmeticCompare<bool, bool>(false, true) == COMPARE_RESULT_LESS);
  44. CHECK(arithmeticCompare<bool, bool>(true, false) == COMPARE_RESULT_GREATER);
  45. }
  46. SECTION("bool vs int") {
  47. CHECK(arithmeticCompare<bool, int>(false, -1) == COMPARE_RESULT_GREATER);
  48. CHECK(arithmeticCompare<bool, int>(false, 0) == COMPARE_RESULT_EQUAL);
  49. CHECK(arithmeticCompare<bool, int>(false, 1) == COMPARE_RESULT_LESS);
  50. CHECK(arithmeticCompare<bool, int>(true, 0) == COMPARE_RESULT_GREATER);
  51. CHECK(arithmeticCompare<bool, int>(true, 1) == COMPARE_RESULT_EQUAL);
  52. CHECK(arithmeticCompare<bool, int>(true, 2) == COMPARE_RESULT_LESS);
  53. }
  54. SECTION("bool vs int") {
  55. CHECK(arithmeticCompare<int, bool>(0, false) == COMPARE_RESULT_EQUAL);
  56. CHECK(arithmeticCompare<int, bool>(1, true) == COMPARE_RESULT_EQUAL);
  57. CHECK(arithmeticCompare<int, bool>(1, false) == COMPARE_RESULT_GREATER);
  58. CHECK(arithmeticCompare<int, bool>(0, true) == COMPARE_RESULT_LESS);
  59. }
  60. }
  61. TEST_CASE("arithmeticCompareNegateLeft()") {
  62. SECTION("unsigned vs int") {
  63. CHECK(arithmeticCompareNegateLeft<int>(0, 1) == COMPARE_RESULT_LESS);
  64. CHECK(arithmeticCompareNegateLeft<int>(42, -41) == COMPARE_RESULT_LESS);
  65. CHECK(arithmeticCompareNegateLeft<int>(42, -42) == COMPARE_RESULT_EQUAL);
  66. CHECK(arithmeticCompareNegateLeft<int>(42, -43) == COMPARE_RESULT_GREATER);
  67. }
  68. SECTION("unsigned vs unsigned") {
  69. CHECK(arithmeticCompareNegateLeft<unsigned>(42, 42) == COMPARE_RESULT_LESS);
  70. }
  71. }
  72. TEST_CASE("arithmeticCompareNegateRight()") {
  73. SECTION("int vs unsigned") {
  74. CHECK(arithmeticCompareNegateRight<int>(1, 0) == COMPARE_RESULT_GREATER);
  75. CHECK(arithmeticCompareNegateRight<int>(-41, 42) == COMPARE_RESULT_GREATER);
  76. CHECK(arithmeticCompareNegateRight<int>(-42, 42) == COMPARE_RESULT_EQUAL);
  77. CHECK(arithmeticCompareNegateRight<int>(-43, 42) == COMPARE_RESULT_LESS);
  78. }
  79. SECTION("unsigned vs unsigned") {
  80. CHECK(arithmeticCompareNegateRight<unsigned>(42, 42) ==
  81. COMPARE_RESULT_GREATER);
  82. }
  83. }