refactor: zbylé tři nálezy z review — rozpočet, rename, duplicity
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
871f75d246
commit
e7fdec8404
5 files changed
+104
-31
No files matched your search
@@ -3,6 +3,7 @@
|
||||
#include <helpers/BatteryIcon.h>
|
||||
#include <helpers/AdvertDataHelpers.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
|
||||
// The whole budget rests on these being 3 bytes, not 4. Block elements and
|
||||
// U+2600/U+26A1 are BMP; battery emoji are not, which is why they lost.
|
||||
@@ -14,22 +15,53 @@ TEST(BatteryIcon, GlyphsAreThreeBytesAndThereAreEightLevels) {
|
||||
EXPECT_EQ(6u, mesh::battIconWidth()); // marker + one glyph
|
||||
}
|
||||
|
||||
TEST(BatteryIcon, BudgetMatchesAdvertLayout) {
|
||||
EXPECT_EQ(23u, mesh::advertNameBudget(true)); // 32 - 1 flags - 8 lat/lon
|
||||
EXPECT_EQ(31u, mesh::advertNameBudget(false));
|
||||
// Rozpočet teď hlásí BUILDER, ne kopie v BatteryIcon.h -- a to je celý smysl
|
||||
// té změny: kopie nevěděla o FEAT1/FEAT2 a tvrdila by o 2-4 bajty víc.
|
||||
TEST(BatteryIcon, BuilderReportsTheNameBudget) {
|
||||
EXPECT_EQ(31u, AdvertDataBuilder(ADV_TYPE_REPEATER).nameBudget()); // jen flagy
|
||||
EXPECT_EQ(23u, AdvertDataBuilder(ADV_TYPE_REPEATER, NULL, 50.4, 12.8).nameBudget()); // + lat/lon
|
||||
}
|
||||
|
||||
TEST(BatteryIcon, BudgetShrinksWithFeatureWords) {
|
||||
AdvertDataBuilder b(ADV_TYPE_REPEATER, NULL, 50.4, 12.8);
|
||||
ASSERT_EQ(23u, b.nameBudget());
|
||||
b.setFeat1(0x1234);
|
||||
EXPECT_EQ(21u, b.nameBudget());
|
||||
b.setFeat2(0x5678);
|
||||
EXPECT_EQ(19u, b.nameBudget());
|
||||
// A tohle je ta chyba, kterou by stará kopie udělala: s FEAT1+FEAT2 by
|
||||
// 17bajtové jméno pořád hlásila jako vejde-se, a UTF-8 clamp by uřízl
|
||||
// kus glyfu.
|
||||
EXPECT_FALSE(mesh::battIconFits(strlen("mast-repeater-one"), b.nameBudget()));
|
||||
}
|
||||
|
||||
// Rozpočet z builderu musí souhlasit s tím, co encodeTo() opravdu nechá.
|
||||
TEST(BatteryIcon, BudgetAgreesWithEncoder) {
|
||||
for (int with_loc = 0; with_loc < 2; with_loc++) {
|
||||
AdvertDataBuilder b = with_loc ? AdvertDataBuilder(ADV_TYPE_REPEATER, NULL, 50.4, 12.8)
|
||||
: AdvertDataBuilder(ADV_TYPE_REPEATER);
|
||||
size_t budget = b.nameBudget();
|
||||
std::string name(budget, 'a'); // přesně na doraz
|
||||
b.setName(name.c_str());
|
||||
uint8_t app_data[MAX_ADVERT_DATA_SIZE];
|
||||
uint8_t len = b.encodeTo(app_data);
|
||||
AdvertDataParser p(app_data, len);
|
||||
ASSERT_TRUE(p.isValid());
|
||||
EXPECT_STREQ(name.c_str(), p.getName()) << "with_loc=" << with_loc;
|
||||
}
|
||||
}
|
||||
|
||||
// The three deployed names, at 6 bytes of indicator.
|
||||
TEST(BatteryIcon, FitsTheDeployedNames) {
|
||||
EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-two"), true)); // 12 + 6 = 18
|
||||
EXPECT_TRUE(mesh::battIconFits(strlen("mast-repeater-one"), true)); // 17 + 6 = 23, exactly
|
||||
EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-three.cz"), true)); // 17 + 6 = 23, exactly
|
||||
EXPECT_FALSE(mesh::battIconFits(18, true)); // one byte over
|
||||
EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-two"), 23)); // 12 + 6 = 18
|
||||
EXPECT_TRUE(mesh::battIconFits(strlen("mast-repeater-one"), 23)); // 17 + 6 = 23, exactly
|
||||
EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-three.cz"), 23)); // 17 + 6 = 23, exactly
|
||||
EXPECT_FALSE(mesh::battIconFits(18, 23)); // one byte over
|
||||
}
|
||||
|
||||
TEST(BatteryIcon, DroppingPositionBuysEightBytes) {
|
||||
EXPECT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true)); // 21 + 6 = 27
|
||||
EXPECT_TRUE(mesh::battIconFits(strlen("tth-mast-repeater-one"), false)); // 31 B budget
|
||||
EXPECT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), 23)); // 21 + 6 = 27
|
||||
EXPECT_TRUE(mesh::battIconFits(strlen("tth-mast-repeater-one"), 31)); // 31 B budget
|
||||
}
|
||||
|
||||
// Every level must be the same width, or the name length would change with
|
||||
@@ -137,7 +169,7 @@ TEST(BatteryIcon, EncoderSilentlyDropsAnIndicatorThatDoesNotFit) {
|
||||
char name[48];
|
||||
snprintf(name, sizeof(name), "tth-mast-repeater-one%s%s", BATT_ICON_MARKER, glyph);
|
||||
ASSERT_EQ(27u, strlen(name));
|
||||
ASSERT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true));
|
||||
ASSERT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), 23));
|
||||
|
||||
uint8_t app_data[MAX_ADVERT_DATA_SIZE];
|
||||
AdvertDataBuilder builder(ADV_TYPE_REPEATER, name, 50.38, 12.82);
|
||||
|
||||
Reference in new issue
Block a user