~aleteoryx/muditaos

d724442366fc1803747e5101ac2d8423c86959ff — Jakub Pyszczak 4 years ago 2d164b9
[EGD-5171] Utils to string fix

Fixed utils to string function to work with 8 bit integers.
Also nodiscard attribute added there.
M enabled_unittests => enabled_unittests +1 -0
@@ 299,6 299,7 @@ TESTS_LIST["catch2-utils"]="
    Get value from string;
    Swap endianness;
    Floating point to string;
    Integer types to string;
    Fill leading digit in string;
    Hex to bytes;
    Bytes to hex;

M module-db/Interface/CalllogRecord.cpp => module-db/Interface/CalllogRecord.cpp +8 -1
@@ 41,6 41,13 @@ std::ostream &operator<<(std::ostream &out, const CalllogRecord &rec)
    return out;
}

[[nodiscard]] std::string CalllogRecord::str() const
{
    std::ostringstream ss;
    ss << *this;
    return ss.str();
}

CalllogRecordInterface::CalllogRecordInterface(CalllogDB *calllogDb, ContactsDB *contactsDb)
    : calllogDB(calllogDb), contactsDB(contactsDb)
{}


@@ 63,7 70,7 @@ bool CalllogRecordInterface::Add(const CalllogRecord &rec)
        if (localRec.presentation == PresentationType::PR_UNKNOWN) {
            localRec.presentation = PresentationType::PR_ALLOWED;
        }
        LOG_DEBUG("Adding call log record %s", utils::to_string(localRec).c_str());
        LOG_DEBUG("Adding call log record %s", localRec.str().c_str());
    }
    else {
        // private number so do not add contact just call log entry

M module-db/Interface/CalllogRecord.hpp => module-db/Interface/CalllogRecord.hpp +1 -0
@@ 27,6 27,7 @@ struct CalllogRecord : public Record
    bool isRead                          = true;

    friend std::ostream &operator<<(std::ostream &out, const CalllogRecord &point);
    [[nodiscard]] std::string str() const;

    CalllogRecord()  = default;
    CalllogRecord(const CalllogTableRow &tableRow);

M module-services/service-db/DBServiceAPI.cpp => module-services/service-db/DBServiceAPI.cpp +2 -2
@@ 180,7 180,7 @@ auto DBServiceAPI::CalllogAdd(sys::Service *serv, const CalllogRecord &rec) -> C
{
    std::shared_ptr<DBCalllogMessage> msg = std::make_shared<DBCalllogMessage>(MessageType::DBCalllogAdd, rec);

    LOG_DEBUG("CalllogAdd %s", utils::to_string(rec).c_str());
    LOG_DEBUG("CalllogAdd %s", rec.str().c_str());

    auto ret             = serv->bus.sendUnicastSync(msg, service::name::db, DefaultTimeoutInMs);
    auto calllogResponse = dynamic_cast<DBCalllogResponseMessage *>(ret.second.get());


@@ 219,7 219,7 @@ auto DBServiceAPI::CalllogUpdate(sys::Service *serv, const CalllogRecord &rec) -
{
    std::shared_ptr<DBCalllogMessage> msg = std::make_shared<DBCalllogMessage>(MessageType::DBCalllogUpdate, rec);

    LOG_DEBUG("CalllogUpdate %s", utils::to_string(rec).c_str());
    LOG_DEBUG("CalllogUpdate %s", rec.str().c_str());

    auto ret             = serv->bus.sendUnicastSync(msg, service::name::db, DefaultTimeoutInMs);
    auto calllogResponse = dynamic_cast<DBCalllogResponseMessage *>(ret.second.get());

M module-utils/Utils.hpp => module-utils/Utils.hpp +22 -10
@@ 22,7 22,7 @@ namespace utils

    std::string bytesToHex(const std::vector<std::uint8_t> &bytes);
    std::vector<std::uint8_t> hexToBytes(const std::string &hex);
    // template <typename T> std::string numToHex(T c);

    template <typename T> std::string numToHex(T c)
    {
        std::stringstream s;


@@ 72,14 72,12 @@ namespace utils
        return base;
    }

    template <typename T> std::string to_string(T t)
    template <typename T>[[nodiscard]] std::string to_string(T t)
    {
        std::ostringstream ss;
        ss << t;
        return ss.str();
        return std::to_string(t);
    }

    template <> inline std::string to_string<long double>(long double t)
    template <>[[nodiscard]] inline std::string to_string<long double>(long double t)
    {
        uint32_t precision = 6;
        int base           = static_cast<int>(t);


@@ 115,14 113,28 @@ namespace utils
        return baseAsStr + "." + fractionalAsStr;
    }

    template <> inline std::string to_string<float>(float t)
    template <>[[nodiscard]] inline std::string to_string<float>(float t)
    {
        return to_string((long double)(t));
        return to_string(static_cast<long double>(t));
    }

    template <> inline std::string to_string<double>(double t)
    template <>[[nodiscard]] inline std::string to_string<double>(double t)
    {
        return to_string((long double)(t));
        return to_string(static_cast<long double>(t));
    }

    template <>[[nodiscard]] inline std::string to_string<std::int64_t>(std::int64_t value)
    {
        std::ostringstream ss;
        ss << value;
        return ss.str();
    }

    template <>[[nodiscard]] inline std::string to_string<std::uint64_t>(std::uint64_t value)
    {
        std::ostringstream ss;
        ss << value;
        return ss.str();
    }

    template <typename T>[[nodiscard]] const std::string enumToString(const T &t)

M module-utils/test/unittest_utils.cpp => module-utils/test/unittest_utils.cpp +86 -10
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <cstring>


@@ 261,6 261,90 @@ TEST_CASE("Floating point to string")
    }
}

TEST_CASE("Integer types to string")
{
    GIVEN("Uint8 equals to zero")
    {
        constexpr std::uint8_t value = 0;
        REQUIRE(utils::to_string(value) == "0");
    }
    GIVEN("Uint8 equals to max")
    {
        constexpr std::uint8_t value = std::numeric_limits<std::uint8_t>::max();
        REQUIRE(utils::to_string(value) == "255");
    }
    GIVEN("Int8 equals to zero")
    {
        constexpr std::int8_t value = 0;
        REQUIRE(utils::to_string(value) == "0");
    }
    GIVEN("Int8 equals to max")
    {
        constexpr std::int8_t value = std::numeric_limits<std::int8_t>::max();
        REQUIRE(utils::to_string(value) == "127");
    }
    GIVEN("Uint16t equals to zero")
    {
        constexpr std::uint16_t value = 0;
        REQUIRE(utils::to_string(value) == "0");
    }
    GIVEN("Uint16t equals to max")
    {
        constexpr std::uint16_t value = std::numeric_limits<std::uint16_t>::max();
        REQUIRE(utils::to_string(value) == "65535");
    }
    GIVEN("Int16t equals to zero")
    {
        constexpr std::int16_t value = 0;
        REQUIRE(utils::to_string(value) == "0");
    }
    GIVEN("Int16t equals to max")
    {
        constexpr std::int16_t value = std::numeric_limits<std::int16_t>::max();
        REQUIRE(utils::to_string(value) == "32767");
    }
    GIVEN("Uint32t equals to zero")
    {
        constexpr std::uint32_t value = 0;
        REQUIRE(utils::to_string(value) == "0");
    }
    GIVEN("Uint32 equals to max")
    {
        constexpr std::uint32_t value = std::numeric_limits<std::uint32_t>::max();
        REQUIRE(utils::to_string(value) == "4294967295");
    }
    GIVEN("Int32t equals to zero")
    {
        constexpr std::int32_t value = 0;
        REQUIRE(utils::to_string(value) == "0");
    }
    GIVEN("Int32 equals to max")
    {
        constexpr std::int32_t value = std::numeric_limits<std::int32_t>::max();
        REQUIRE(utils::to_string(value) == "2147483647");
    }
    GIVEN("Int64 equals to zero")
    {
        constexpr std::int64_t value = 0;
        REQUIRE(utils::to_string(value) == "0");
    }
    GIVEN("Int64 equals to max")
    {
        constexpr std::int64_t value = std::numeric_limits<std::int64_t>::max();
        REQUIRE(utils::to_string(value) == "9223372036854775807");
    }
    GIVEN("Uint64 equals to zero")
    {
        constexpr std::uint64_t value = 0;
        REQUIRE(utils::to_string(value) == "0");
    }
    GIVEN("Uint64 equals to max")
    {
        constexpr std::uint64_t value = std::numeric_limits<std::uint64_t>::max();
        REQUIRE(utils::to_string(value) == "18446744073709551615");
    }
}

TEST_CASE("Fill leading digit in string")
{
    std::string test = "45";


@@ 328,15 412,7 @@ TEST_CASE("Hex to bytes")

    SECTION("Out of hex")
    {

        bool isthrow = false;
        try {
            auto b = utils::hexToBytes("deAdbEZZ");
        }
        catch (const std::invalid_argument &ia) {
            isthrow = true;
        }
        REQUIRE(isthrow);
        REQUIRE_THROWS_AS(utils::hexToBytes("deAdbEZZ"), std::invalid_argument);
    }
}