[ox/std] Add UUID str conv test and fix bugs found

This commit is contained in:
Gary Talent 2023-02-12 20:38:29 -06:00
parent 1b7b6e306e
commit 54eebf81da
4 changed files with 36 additions and 15 deletions

View File

@ -80,7 +80,7 @@ constexpr void expect(const char *file, int line, const auto &actual, const auto
if (!std::is_constant_evaluated()) {
#if defined(OX_USE_STDLIB)
oxErrf("\n\033[31;1;1mASSERT FAILURE:\033[0m [{}:{}]: {}\n", file, line, "Value incorrect");
oxErrf("expected: {}, actual: {}\n", detail::toStringView<true>(expected), detail::toStringView<true>(actual));
oxErrf("expected: {}\nactual: {}\n", detail::toStringView<true>(expected), detail::toStringView<true>(actual));
printStackTrace(2);
oxTracef("assert::expect", "Failed assert: {} == {} [{}:{}]", detail::toStringView<true>(actual), detail::toStringView<true>(expected), file, line);
std::abort();

View File

@ -21,3 +21,4 @@ add_test("[ox/std] BufferWriter" StdTest "BufferWriter")
add_test("[ox/std] StringSplit" StdTest "StringSplit")
add_test("[ox/std] FromHex" StdTest "FromHex")
add_test("[ox/std] ToHex" StdTest "ToHex")
add_test("[ox/std] UUID" StdTest "UUID")

View File

@ -197,6 +197,16 @@ static std::map<ox::String, ox::Error(*)()> tests = {
oxExpect(ox::detail::toHex(0x0f), "0f");
oxExpect(ox::detail::toHex(0x93), "93");
oxExpect(ox::detail::toHex(0x40), "40");
oxExpect(ox::detail::toHex(0xf0), "f0");
return OxError(0);
}
},
{
"UUID",
[] {
constexpr ox::StringView uuidStr = "8d814442-f46e-4cc3-8edc-ca3c01cc86db";
oxRequire(uuid, ox::UUID::fromString(uuidStr));
oxExpect(ox::StringView(uuid.toString()), uuidStr);
return OxError(0);
}
},

View File

@ -81,7 +81,7 @@ constexpr ox::BString<2> toHex(uint8_t v) noexcept {
'f',
};
ox::Array<char, 3> out;
out[0] = valMap[static_cast<unsigned>((v & 0xf0) / 0xf)];
out[0] = valMap[static_cast<unsigned>((v & 0xf0) / 16)];
out[1] = valMap[static_cast<unsigned>(v & 0x0f)];
out[2] = 0;
return out.data();
@ -100,10 +100,16 @@ class UUID {
static ox::Result<UUID> generate() noexcept;
constexpr ox::Error fromString(ox::CRStringView s) noexcept {
[[nodiscard]]
constexpr auto value() const noexcept {
return m_value;
}
static constexpr ox::Result<ox::UUID> fromString(ox::CRStringView s) noexcept {
if (s.len() < 36) {
return OxError(1, "Insufficient data contain complete UUID");
}
UUID out;
auto valueI = 0u;
for (auto i = 0u; i < s.len();) {
if (s[i] == '-') {
@ -115,35 +121,39 @@ class UUID {
return OxError(1, "Invalid UUID");
}
oxRequire(val, detail::fromHex(seg));
m_value[valueI] = val;
out.m_value[valueI] = val;
i += 2;
++valueI;
}
return {};
return out;
}
[[nodiscard]]
constexpr UUIDStr toString() const noexcept {
UUIDStr out;
auto i = 0u;
auto valueI = 0u;
constexpr auto printChars = [](
ox::BString<36> *out, const Array<uint8_t, 16> &value, std::size_t cnt, unsigned i) {
for (; i < cnt; ++i) {
const auto v = value[i];
ox::BString<36> *out,
const Array<uint8_t, 16> &value,
std::size_t cnt,
unsigned valueI) {
for (auto i = 0u; i < cnt; ++i) {
const auto v = value[valueI];
const auto h = detail::toHex(v);
oxIgnoreError(out->append(h.c_str(), h.len()));
++valueI;
}
return i;
return valueI;
};
i = printChars(&out, m_value, 4, i);
valueI = printChars(&out, m_value, 4, valueI);
out += "-";
i = printChars(&out, m_value, 2, i);
valueI = printChars(&out, m_value, 2, valueI);
out += "-";
i = printChars(&out, m_value, 2, i);
valueI = printChars(&out, m_value, 2, valueI);
out += "-";
i = printChars(&out, m_value, 2, i);
valueI = printChars(&out, m_value, 2, valueI);
out += "-";
i = printChars(&out, m_value, 6, i);
valueI = printChars(&out, m_value, 6, valueI);
return out;
}
};