FoodDelivery/ArduinoJson/extras/tests/Misc/arithmeticCompare.cpp
tk b708b47c4b 【类 型】:fix
【主	题】:添加库文件
【描	述】:
	[原因]:
	[过程]:
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
2024-06-14 17:16:36 +08:00

104 lines
4.3 KiB
C++

// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2022, Benoit BLANCHON
// MIT License
#include <ArduinoJson/Numbers/arithmeticCompare.hpp>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("arithmeticCompare()") {
SECTION("int vs uint8_t") {
CHECK((arithmeticCompare<int, uint8_t>(256, 1) == COMPARE_RESULT_GREATER));
CHECK((arithmeticCompare<int, uint8_t>(41, 42) == COMPARE_RESULT_LESS));
CHECK((arithmeticCompare<int, uint8_t>(42, 42) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<int, uint8_t>(43, 42) == COMPARE_RESULT_GREATER));
}
SECTION("unsigned vs int") {
CHECK((arithmeticCompare<unsigned, int>(0, -1) == COMPARE_RESULT_GREATER));
CHECK((arithmeticCompare<unsigned, int>(42, 41) == COMPARE_RESULT_GREATER));
CHECK((arithmeticCompare<unsigned, int>(42, 42) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<unsigned, int>(42, 43) == COMPARE_RESULT_LESS));
}
SECTION("float vs int") {
CHECK((arithmeticCompare<float, int>(42, 41) == COMPARE_RESULT_GREATER));
CHECK((arithmeticCompare<float, int>(42, 42) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<float, int>(42, 43) == COMPARE_RESULT_LESS));
}
SECTION("int vs unsigned") {
CHECK((arithmeticCompare<int, unsigned>(-1, 0) == COMPARE_RESULT_LESS));
CHECK((arithmeticCompare<int, unsigned>(0, 0) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<int, unsigned>(1, 0) == COMPARE_RESULT_GREATER));
CHECK((arithmeticCompare<int, unsigned>(42, 41) == COMPARE_RESULT_GREATER));
CHECK((arithmeticCompare<int, unsigned>(42, 42) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<int, unsigned>(42, 43) == COMPARE_RESULT_LESS));
}
SECTION("unsigned vs unsigned") {
CHECK((arithmeticCompare<unsigned, unsigned>(42, 41) ==
COMPARE_RESULT_GREATER));
CHECK((arithmeticCompare<unsigned, unsigned>(42, 42) ==
COMPARE_RESULT_EQUAL));
CHECK(
(arithmeticCompare<unsigned, unsigned>(42, 43) == COMPARE_RESULT_LESS));
}
SECTION("bool vs bool") {
CHECK(
(arithmeticCompare<bool, bool>(false, false) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<bool, bool>(true, true) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<bool, bool>(false, true) == COMPARE_RESULT_LESS));
CHECK(
(arithmeticCompare<bool, bool>(true, false) == COMPARE_RESULT_GREATER));
}
SECTION("bool vs int") {
CHECK((arithmeticCompare<bool, int>(false, -1) == COMPARE_RESULT_GREATER));
CHECK((arithmeticCompare<bool, int>(false, 0) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<bool, int>(false, 1) == COMPARE_RESULT_LESS));
CHECK((arithmeticCompare<bool, int>(true, 0) == COMPARE_RESULT_GREATER));
CHECK((arithmeticCompare<bool, int>(true, 1) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<bool, int>(true, 2) == COMPARE_RESULT_LESS));
}
SECTION("bool vs int") {
CHECK((arithmeticCompare<int, bool>(0, false) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<int, bool>(1, true) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompare<int, bool>(1, false) == COMPARE_RESULT_GREATER));
CHECK((arithmeticCompare<int, bool>(0, true) == COMPARE_RESULT_LESS));
}
}
TEST_CASE("arithmeticCompareNegateLeft()") {
SECTION("unsigned vs int") {
CHECK((arithmeticCompareNegateLeft<int>(0, 1) == COMPARE_RESULT_LESS));
CHECK((arithmeticCompareNegateLeft<int>(42, -41) == COMPARE_RESULT_LESS));
CHECK((arithmeticCompareNegateLeft<int>(42, -42) == COMPARE_RESULT_EQUAL));
CHECK(
(arithmeticCompareNegateLeft<int>(42, -43) == COMPARE_RESULT_GREATER));
}
SECTION("unsigned vs unsigned") {
CHECK(
(arithmeticCompareNegateLeft<unsigned>(42, 42) == COMPARE_RESULT_LESS));
}
}
TEST_CASE("arithmeticCompareNegateRight()") {
SECTION("int vs unsigned") {
CHECK((arithmeticCompareNegateRight<int>(1, 0) == COMPARE_RESULT_GREATER));
CHECK(
(arithmeticCompareNegateRight<int>(-41, 42) == COMPARE_RESULT_GREATER));
CHECK((arithmeticCompareNegateRight<int>(-42, 42) == COMPARE_RESULT_EQUAL));
CHECK((arithmeticCompareNegateRight<int>(-43, 42) == COMPARE_RESULT_LESS));
}
SECTION("unsigned vs unsigned") {
CHECK((arithmeticCompareNegateRight<unsigned>(42, 42) ==
COMPARE_RESULT_GREATER));
}
}