mirror of
https://github.com/liquidraver/ZephCore.git
synced 2026-12-06 15:04:19 +00:00
118 lines
5.9 KiB
C++
118 lines
5.9 KiB
C++
#include "test.h"
|
|
#include <TxtDataHelpers.h>
|
|
#include <battery_curve.h>
|
|
#include <mesh/MeshCore.h>
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
|
|
TEST(string_capacity, "UNIT-STRING-001", "String copies terminate and respect zero exact and short capacities") {
|
|
for (size_t size = 0; size <= 10; ++size) {
|
|
std::vector<char> out(size + 2, '#');
|
|
StrHelper::strncpy(out.data() + 1, "abcd", size);
|
|
CHECK(out.front() == '#' && out.back() == '#');
|
|
if (size) CHECK(std::string(out.data() + 1) == std::string("abcd").substr(0, size - 1));
|
|
std::fill(out.begin(), out.end(), '#');
|
|
StrHelper::strzcpy(out.data() + 1, "abcd", size);
|
|
CHECK(out.front() == '#' && out.back() == '#');
|
|
if (size) {
|
|
auto copied = std::min(size_t(4), size - 1);
|
|
CHECK(std::string(out.data() + 1) == std::string("abcd").substr(0, copied));
|
|
for (size_t i = copied; i < size; ++i) CHECK(out[i + 1] == 0);
|
|
}
|
|
}
|
|
}
|
|
TEST(string_normalization, "UNIT-STRING-002", "Decimal trimming and blank classification") {
|
|
const char *input[] = {"250.000", "62.500", "0.0010", "-10.000", "1000", "", "1.25"};
|
|
const char *expected[] = {"250", "62.5", "0.001", "-10", "1000", "", "1.25"};
|
|
for (unsigned i = 0; i < 7; ++i) {
|
|
char buffer[32]; std::strcpy(buffer, input[i]); StrHelper::stripTrailingZeros(buffer);
|
|
CHECK(std::strcmp(buffer, expected[i]) == 0);
|
|
}
|
|
CHECK(StrHelper::isBlank(nullptr) && StrHelper::isBlank("") && StrHelper::isBlank(" \t\r\n"));
|
|
CHECK(!StrHelper::isBlank(" a ") && !StrHelper::isBlank("0"));
|
|
}
|
|
TEST(text_then_padding, "UNIT-STRING-003", "A received text is printable characters followed only by zero padding") {
|
|
auto ok = [](std::vector<uint8_t> bytes, size_t pad = 0) {
|
|
bytes.insert(bytes.end(), pad, 0);
|
|
return StrHelper::isTextThenPadding(bytes.data(), bytes.size());
|
|
};
|
|
auto text = [](const char *s) { return std::vector<uint8_t>(s, s + std::strlen(s)); };
|
|
CHECK(ok(text("get name"), 3));
|
|
CHECK(ok(text("exactly-11!"))); // fills the block: no terminator, no padding
|
|
CHECK(ok(text("set name K\xC3\xA9kes \xF0\x9F\x93\xA1"), 5)); // UTF-8
|
|
CHECK(ok(text("two\nlines\r\n\tindented"), 2));
|
|
CHECK(ok({}, 11) && ok({})); // empty text
|
|
CHECK(!ok(text("get name\x01"), 2)); // control character inside the text
|
|
CHECK(!ok(text("get name\x1B[0m"), 2));
|
|
CHECK(!ok(text("get name\xFF"), 2));
|
|
std::vector<uint8_t> tail = text("get name"); // bytes after the terminator
|
|
tail.insert(tail.end(), {0, 0x01, 0x11, 0x22});
|
|
CHECK(!ok(tail, 4));
|
|
tail = text("get name");
|
|
tail.insert(tail.end(), {0, 0, 0, 0xFF});
|
|
CHECK(!ok(tail));
|
|
CHECK(!ok({0, 0x01}, 9)); // empty text, then data
|
|
// A plain text may carry its attempt number (4 and up) right after the terminator.
|
|
auto plain = [](std::vector<uint8_t> bytes, size_t pad = 0) {
|
|
bytes.insert(bytes.end(), pad, 0);
|
|
return StrHelper::isTextThenPadding(bytes.data(), bytes.size(), true);
|
|
};
|
|
std::vector<uint8_t> retry = text("hello");
|
|
retry.insert(retry.end(), {0, 4});
|
|
CHECK(plain(retry, 9) && plain(retry) && !ok(retry, 9));
|
|
retry.back() = 254;
|
|
CHECK(plain(retry, 9));
|
|
for (uint8_t bad : {uint8_t(1), uint8_t(3), uint8_t(0xFF)}) { // not attempt numbers
|
|
retry.back() = bad;
|
|
CHECK(!plain(retry, 9));
|
|
}
|
|
retry.back() = 7;
|
|
retry.push_back(0x55); // anything after the attempt number
|
|
CHECK(!plain(retry, 8));
|
|
CHECK(plain(text("hello"), 11) && !plain(text("hello\x01"), 2));
|
|
// The padding hint of zcpfs builds: NUL, attempt number or zero, 0x5A, then zeros.
|
|
std::vector<uint8_t> hint = text("hello");
|
|
hint.insert(hint.end(), {0, 0, 0x5A});
|
|
CHECK(plain(hint) && plain(hint, 8) && !ok(hint, 8));
|
|
hint[hint.size() - 2] = 9; // a late retry with the hint
|
|
CHECK(plain(hint, 8));
|
|
for (uint8_t bad : {uint8_t(1), uint8_t(3), uint8_t(0xFF)}) {
|
|
hint[hint.size() - 2] = bad;
|
|
CHECK(!plain(hint, 8));
|
|
}
|
|
hint[hint.size() - 2] = 0;
|
|
hint.push_back(0x01); // anything after the hint
|
|
CHECK(!plain(hint, 7));
|
|
std::vector<uint8_t> not_hint = text("hello");
|
|
not_hint.insert(not_hint.end(), {0, 0, 0x5B});
|
|
CHECK(!plain(not_hint, 8));
|
|
not_hint = text("hello");
|
|
not_hint.insert(not_hint.end(), {0, 0x5A}); // the hint byte where the attempt number goes: an attempt number
|
|
CHECK(plain(not_hint, 8));
|
|
}
|
|
TEST(rtc_unique, "UNIT-RTC-001", "Unique timestamps survive repeated and backward wall time") {
|
|
struct RTC : mesh::RTCClock {
|
|
uint32_t now = 100;
|
|
uint32_t getCurrentTime() override { return now; }
|
|
void setCurrentTime(uint32_t value) override { now = value; }
|
|
} rtc;
|
|
CHECK(rtc.getCurrentTimeUnique() == 100); CHECK(rtc.getCurrentTimeUnique() == 101);
|
|
rtc.now = 50; CHECK(rtc.getCurrentTimeUnique() == 102);
|
|
rtc.now = 200; CHECK(rtc.getCurrentTimeUnique() == 200);
|
|
}
|
|
TEST(battery_curve, "UNIT-BATTERY-001", "Battery endpoints interpolation multicell scaling and monotonicity") {
|
|
CHECK(battery_curve_lookup(&battery_curve_default, 0) == 0);
|
|
CHECK(battery_curve_lookup(&battery_curve_default, 3100) == 0);
|
|
CHECK(battery_curve_lookup(&battery_curve_default, 4190) == 100);
|
|
CHECK(battery_curve_lookup(&battery_curve_default, 3720) == 50);
|
|
const uint16_t points[] = {4200, 3600, 3000};
|
|
battery_curve_t twoCell{points, 3, 2};
|
|
CHECK(battery_curve_lookup(&twoCell, 7800) == 75);
|
|
CHECK(battery_curve_lookup(&twoCell, 6600) == 25);
|
|
unsigned previous = 0;
|
|
for (unsigned mv = 0; mv <= 65535; ++mv) {
|
|
auto percent = battery_curve_lookup(&battery_curve_default, mv);
|
|
CHECK(percent >= previous && percent <= 100); previous = percent;
|
|
}
|
|
}
|