arithmeticCompare.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson/Numbers/arithmeticCompare.hpp>
  5. #include <catch.hpp>
  6. using namespace ARDUINOJSON_NAMESPACE;
  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(
  39. (arithmeticCompare<unsigned, unsigned>(42, 43) == COMPARE_RESULT_LESS));
  40. }
  41. SECTION("bool vs bool") {
  42. CHECK(
  43. (arithmeticCompare<bool, bool>(false, false) == COMPARE_RESULT_EQUAL));
  44. CHECK((arithmeticCompare<bool, bool>(true, true) == COMPARE_RESULT_EQUAL));
  45. CHECK((arithmeticCompare<bool, bool>(false, true) == COMPARE_RESULT_LESS));
  46. CHECK(
  47. (arithmeticCompare<bool, bool>(true, false) == COMPARE_RESULT_GREATER));
  48. }
  49. SECTION("bool vs int") {
  50. CHECK((arithmeticCompare<bool, int>(false, -1) == COMPARE_RESULT_GREATER));
  51. CHECK((arithmeticCompare<bool, int>(false, 0) == COMPARE_RESULT_EQUAL));
  52. CHECK((arithmeticCompare<bool, int>(false, 1) == COMPARE_RESULT_LESS));
  53. CHECK((arithmeticCompare<bool, int>(true, 0) == COMPARE_RESULT_GREATER));
  54. CHECK((arithmeticCompare<bool, int>(true, 1) == COMPARE_RESULT_EQUAL));
  55. CHECK((arithmeticCompare<bool, int>(true, 2) == COMPARE_RESULT_LESS));
  56. }
  57. SECTION("bool vs int") {
  58. CHECK((arithmeticCompare<int, bool>(0, false) == COMPARE_RESULT_EQUAL));
  59. CHECK((arithmeticCompare<int, bool>(1, true) == COMPARE_RESULT_EQUAL));
  60. CHECK((arithmeticCompare<int, bool>(1, false) == COMPARE_RESULT_GREATER));
  61. CHECK((arithmeticCompare<int, bool>(0, true) == COMPARE_RESULT_LESS));
  62. }
  63. }
  64. TEST_CASE("arithmeticCompareNegateLeft()") {
  65. SECTION("unsigned vs int") {
  66. CHECK((arithmeticCompareNegateLeft<int>(0, 1) == COMPARE_RESULT_LESS));
  67. CHECK((arithmeticCompareNegateLeft<int>(42, -41) == COMPARE_RESULT_LESS));
  68. CHECK((arithmeticCompareNegateLeft<int>(42, -42) == COMPARE_RESULT_EQUAL));
  69. CHECK(
  70. (arithmeticCompareNegateLeft<int>(42, -43) == COMPARE_RESULT_GREATER));
  71. }
  72. SECTION("unsigned vs unsigned") {
  73. CHECK(
  74. (arithmeticCompareNegateLeft<unsigned>(42, 42) == COMPARE_RESULT_LESS));
  75. }
  76. }
  77. TEST_CASE("arithmeticCompareNegateRight()") {
  78. SECTION("int vs unsigned") {
  79. CHECK((arithmeticCompareNegateRight<int>(1, 0) == COMPARE_RESULT_GREATER));
  80. CHECK(
  81. (arithmeticCompareNegateRight<int>(-41, 42) == COMPARE_RESULT_GREATER));
  82. CHECK((arithmeticCompareNegateRight<int>(-42, 42) == COMPARE_RESULT_EQUAL));
  83. CHECK((arithmeticCompareNegateRight<int>(-43, 42) == COMPARE_RESULT_LESS));
  84. }
  85. SECTION("unsigned vs unsigned") {
  86. CHECK((arithmeticCompareNegateRight<unsigned>(42, 42) ==
  87. COMPARE_RESULT_GREATER));
  88. }
  89. }