Format with clang

This commit is contained in:
wingphil
2025-03-07 17:08:05 +00:00
parent 9874ecde82
commit a6f77250b5
3 changed files with 30 additions and 28 deletions

View File

@@ -627,15 +627,18 @@ std::string calculate(const std::string & expr) {
} }
void skipBrackets(const std::string & expr, size_t & pos, const int direction) { void skipBrackets(const std::string & expr, size_t & pos, const int direction) {
int i = pos; int i = pos;
char open = '(', close = ')'; char open = '(', close = ')';
if (direction == -1) { if (direction == -1) {
open = ')'; close = '('; open = ')';
close = '(';
} }
size_t depth = 1; size_t depth = 1;
while ((i >= 0) && (i < expr.size()) && depth > 0) { while ((i >= 0) && (i < expr.size()) && depth > 0) {
if (expr[i] == open) depth++; if (expr[i] == open)
if (expr[i] == close) depth--; depth++;
if (expr[i] == close)
depth--;
i += direction; i += direction;
} }
if (depth > 0) { if (depth > 0) {
@@ -644,7 +647,6 @@ void skipBrackets(const std::string & expr, size_t & pos, const int direction) {
} else { } else {
pos = i - direction * 2; pos = i - direction * 2;
} }
} }
// check for multiple instances of <cond> ? <expr1> : <expr2> // check for multiple instances of <cond> ? <expr1> : <expr2>
@@ -726,7 +728,7 @@ std::string compute(const std::string & expr) {
if (cond.length() == 0) { if (cond.length() == 0) {
return ""; return "";
} else if (cond[0] == '1') { } else if (cond[0] == '1') {
expr_new.erase(c, e + 1 - c); // remove second expression after colon expr_new.erase(c, e + 1 - c); // remove second expression after colon
expr_new.erase(s, q + 1 - s); // remove condition before questionmark expr_new.erase(s, q + 1 - s); // remove condition before questionmark
} else if (cond[0] == '0') { } else if (cond[0] == '0') {
expr_new.erase(s, c + 1 - s); // remove condition and first expression expr_new.erase(s, c + 1 - s); // remove condition and first expression

View File

@@ -453,9 +453,9 @@ int main() {
// //
UNITY_BEGIN(); UNITY_BEGIN();
run_tests(); // execute the generated tests run_tests(); // execute the generated tests
run_manual_tests(); // execute some other manual tests from this file run_manual_tests(); // execute some other manual tests from this file
run_console_tests(); // execute some console tests run_console_tests(); // execute some console tests
run_shuntingYard_tests(); // execute the shuntingYard tests run_shuntingYard_tests(); // execute the shuntingYard tests
return UNITY_END(); return UNITY_END();

View File

@@ -6,117 +6,117 @@
void shuntingYard_test1() { void shuntingYard_test1() {
std::string expected_result = "locale is en"; std::string expected_result = "locale is en";
std::string test_value = "\"locale is \"system/settings/locale"; std::string test_value = "\"locale is \"system/settings/locale";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test2() { void shuntingYard_test2() {
// test with negative value // test with negative value
std::string expected_result = "rssi is -23"; std::string expected_result = "rssi is -23";
std::string test_value = "\"rssi is \"0+system/network/rssi"; std::string test_value = "\"rssi is \"0+system/network/rssi";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test3() { void shuntingYard_test3() {
std::string expected_result = "rssi is -23 dbm"; std::string expected_result = "rssi is -23 dbm";
std::string test_value = "\"rssi is \"(system/network/rssi)\" dBm\""; std::string test_value = "\"rssi is \"(system/network/rssi)\" dBm\"";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test4() { void shuntingYard_test4() {
std::string expected_result = "14"; std::string expected_result = "14";
std::string test_value = "(custom/seltemp/value)"; std::string test_value = "(custom/seltemp/value)";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test5() { void shuntingYard_test5() {
std::string expected_result = "seltemp=14"; std::string expected_result = "seltemp=14";
std::string test_value = "\"seltemp=\"(custom/seltemp/value)"; std::string test_value = "\"seltemp=\"(custom/seltemp/value)";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test6() { void shuntingYard_test6() {
std::string expected_result = "14"; std::string expected_result = "14";
std::string test_value = "(custom/seltemp)"; std::string test_value = "(custom/seltemp)";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test7() { void shuntingYard_test7() {
std::string expected_result = "40"; std::string expected_result = "40";
std::string test_value = "boiler/flowtempoffset"; std::string test_value = "boiler/flowtempoffset";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test8() { void shuntingYard_test8() {
std::string expected_result = "40"; std::string expected_result = "40";
std::string test_value = "(boiler/flowtempoffset/value)"; std::string test_value = "(boiler/flowtempoffset/value)";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test9() { void shuntingYard_test9() {
std::string expected_result = "53.8"; std::string expected_result = "53.8";
std::string test_value = "(boiler/storagetemp1/value)"; std::string test_value = "(boiler/storagetemp1/value)";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test10() { void shuntingYard_test10() {
// (14 - 40) * 2.8 + 5 = -67.8 // (14 - 40) * 2.8 + 5 = -67.8
std::string expected_result = "-67.8"; std::string expected_result = "-67.8";
std::string test_value = "(custom/seltemp - boiler/flowtempoffset) * 2.8 + 5"; std::string test_value = "(custom/seltemp - boiler/flowtempoffset) * 2.8 + 5";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test11() { void shuntingYard_test11() {
std::string expected_result = "4"; std::string expected_result = "4";
std::string test_value = "1 > 2 ? 3 : 4"; std::string test_value = "1 > 2 ? 3 : 4";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test12() { void shuntingYard_test12() {
std::string expected_result = "3"; std::string expected_result = "3";
std::string test_value = "1 < 2 ? 3 : 4"; std::string test_value = "1 < 2 ? 3 : 4";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test13() { void shuntingYard_test13() {
std::string expected_result = "5"; std::string expected_result = "5";
std::string test_value = "1<2?(3<4?5:6):7"; std::string test_value = "1<2?(3<4?5:6):7";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test14() { void shuntingYard_test14() {
std::string expected_result = "7"; std::string expected_result = "7";
std::string test_value = "1>2?(3<4?5:6):7"; std::string test_value = "1>2?(3<4?5:6):7";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test15() { void shuntingYard_test15() {
std::string expected_result = "6"; std::string expected_result = "6";
std::string test_value = "1<2?(3>4?5:6):7"; std::string test_value = "1<2?(3>4?5:6):7";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test16() { void shuntingYard_test16() {
std::string expected_result = "3"; std::string expected_result = "3";
std::string test_value = "1<2?3:(4<5?6:7)"; std::string test_value = "1<2?3:(4<5?6:7)";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test17() { void shuntingYard_test17() {
std::string expected_result = "6"; std::string expected_result = "6";
std::string test_value = "1>2?3:(4<5?6:7)"; std::string test_value = "1>2?3:(4<5?6:7)";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test18() { void shuntingYard_test18() {
std::string expected_result = "7"; std::string expected_result = "7";
std::string test_value = "1>2?3:(4>5?6:7)"; std::string test_value = "1>2?3:(4>5?6:7)";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }
void shuntingYard_test19() { void shuntingYard_test19() {
std::string expected_result = "44"; std::string expected_result = "44";
std::string test_value = "(1>2?3:4)+(10>20?30:40)"; std::string test_value = "(1>2?3:4)+(10>20?30:40)";
TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str()); TEST_ASSERT_EQUAL_STRING(expected_result.c_str(), compute(test_value).c_str());
} }