More log tests

This commit is contained in:
SChernykh 2023-09-17 10:38:46 +02:00
parent 513c2dfc3b
commit ed56206c30
3 changed files with 44 additions and 2 deletions

View file

@ -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;

View file

@ -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)
{

View file

@ -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());
}
}