Skip to content

Commit

Permalink
More log tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SChernykh committed Sep 17, 2023
1 parent 513c2df commit ed56206
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,12 @@ struct Stream
static_assert(1 < base && base <= 64, "Invalid base");

const bool negative = is_negative(data);
std::make_unsigned_t<T> udata = abs(data);

char buf[32];
size_t k = sizeof(buf);
int w = m_numberWidth;

std::make_unsigned_t<T> udata = static_cast<std::make_unsigned_t<T>>(data);

do {
buf[--k] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/"[udata % base];
udata /= base;
Expand Down
6 changes: 6 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ template<typename T> struct is_negative_helper<T, true> { static FORCEINLINE bo

template<typename T> FORCEINLINE bool is_negative(T x) { return is_negative_helper<T, std::is_signed<T>::value>::value(x); }

template<typename T, bool is_signed> struct abs_helper {};
template<typename T> struct abs_helper<T, false> { static FORCEINLINE T value(T x) { return x; } };
template<typename T> struct abs_helper<T, true> { static FORCEINLINE std::make_unsigned_t<T> value(T x) { return (x < 0) ? -x : x; } };

template<typename T> FORCEINLINE std::make_unsigned_t<T> abs(T x) { return abs_helper<T, std::is_signed<T>::value>::value(x); }

template<typename T, typename U>
FORCEINLINE void writeVarint(T value, U&& callback)
{
Expand Down
37 changes: 37 additions & 0 deletions tests/src/log_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,41 @@ TEST(log, pad_right)
ASSERT_EQ(buf[N], '\0');
}

template<typename T, bool hex>
void check_number(T value)
{
constexpr size_t N = 64;

char buf[N];
memset(buf, -1, N);
log::Stream s(buf);
s << log::BasedValue<T, hex ? 16 : 10>(value) << '\0';

char buf2[N];
memset(buf2, -1, N);
snprintf(buf2, N, hex ? "%llx" : (std::is_signed<T>::value ? "%lld" : "%llu"), value);

ASSERT_EQ(memcmp(buf, buf2, N), 0);
}

TEST(log, numbers)
{
for (int64_t i = -1024; i <= 1024; ++i) {
check_number<int64_t, false>(i);

if (i >= 0) {
check_number<int64_t, true>(i);
check_number<uint64_t, false>(i);
check_number<uint64_t, true>(i);
}
}

check_number<int64_t, false>(std::numeric_limits<int64_t>::min());
check_number<int64_t, false>(std::numeric_limits<int64_t>::max());
check_number<int64_t, true>(std::numeric_limits<int64_t>::max());

check_number<uint64_t, false>(std::numeric_limits<uint64_t>::max());
check_number<uint64_t, true>(std::numeric_limits<uint64_t>::max());
}

}

0 comments on commit ed56206

Please sign in to comment.