Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d433e197 authored by Yu Shan's avatar Yu Shan
Browse files

Add toString and formatter for PropIdAreaId.

This helps printing out debug string for PropIdAreaId struct.

Flag: EXEMPT hal
Bug: 382563296
Test: atest VehicleHalVehicleUtilsTest
Change-Id: I54613982506a804cf7b05e03f9f100595c1a874a
parent 8cddcd9f
Loading
Loading
Loading
Loading
+28 −6
Original line number Diff line number Diff line
@@ -310,6 +310,12 @@ ndk::ScopedAStatus toScopedAStatus(const VhalResult<T>& result,
    return toScopedAStatus(result, getErrorCode(result), additionalErrorMsg);
}

// This is for debug purpose only.
inline std::string propIdToString(int32_t propId) {
    return toString(
            static_cast<aidl::android::hardware::automotive::vehicle::VehicleProperty>(propId));
}

struct PropIdAreaId {
    int32_t propId;
    int32_t areaId;
@@ -317,6 +323,11 @@ struct PropIdAreaId {
    inline bool operator==(const PropIdAreaId& other) const {
        return areaId == other.areaId && propId == other.propId;
    }

    // This is for debug purpose only.
    inline std::string toString() const {
        return fmt::format("{{propId: {}, areaId: {}}}", propIdToString(propId), areaId);
    }
};

struct PropIdAreaIdHash {
@@ -328,12 +339,6 @@ struct PropIdAreaIdHash {
    }
};

// This is for debug purpose only.
inline std::string propIdToString(int32_t propId) {
    return toString(
            static_cast<aidl::android::hardware::automotive::vehicle::VehicleProperty>(propId));
}

// This is for debug purpose only.
android::base::Result<int32_t> stringToPropId(const std::string& propName);

@@ -362,4 +367,21 @@ inline void sanitizeByResolution(aidl::android::hardware::automotive::vehicle::R
}  // namespace hardware
}  // namespace android

// Formatter must not be defined inside our namespace.
template <>
struct fmt::formatter<android::hardware::automotive::vehicle::PropIdAreaId> {
    template <typename ParseContext>
    constexpr auto parse(ParseContext& ctx) {
        return ctx.begin();
    }

    template <typename FormatContext>
    auto format(const android::hardware::automotive::vehicle::PropIdAreaId& p,
                FormatContext& ctx) const {
        return fmt::format_to(ctx.out(), "{{propId: {}, areaId: {}}}",
                              android::hardware::automotive::vehicle::propIdToString(p.propId),
                              p.areaId);
    }
};

#endif  // android_hardware_automotive_vehicle_aidl_impl_utils_common_include_VehicleUtils_H_
+23 −0
Original line number Diff line number Diff line
@@ -787,6 +787,29 @@ TEST(VehicleUtilsTest, testStringToPropId_InvalidName) {
    ASSERT_FALSE(result.ok());
}

TEST(VehicleUtilsTest, testPropIdAreaIdToString) {
    PropIdAreaId propIdAreaId = {
            .propId = toInt(VehicleProperty::PERF_VEHICLE_SPEED),
            .areaId = 0,
    };

    ASSERT_EQ(propIdAreaId.toString(), "{propId: PERF_VEHICLE_SPEED, areaId: 0}");
}

TEST(VehicleUtilsTest, testPropIdAreaIdFormatter) {
    PropIdAreaId propIdAreaId1 = {
            .propId = toInt(VehicleProperty::PERF_VEHICLE_SPEED),
            .areaId = 0,
    };
    PropIdAreaId propIdAreaId2 = {
            .propId = toInt(VehicleProperty::HVAC_FAN_SPEED),
            .areaId = 1,
    };

    ASSERT_EQ(fmt::format("{}", std::vector<PropIdAreaId>{propIdAreaId1, propIdAreaId2}),
              "[{propId: PERF_VEHICLE_SPEED, areaId: 0}, {propId: HVAC_FAN_SPEED, areaId: 1}]");
}

class InvalidPropValueTest : public testing::TestWithParam<InvalidPropValueTestCase> {};

INSTANTIATE_TEST_SUITE_P(InvalidPropValueTests, InvalidPropValueTest,