1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

fmt: Add fmt_s64_dec, print_s64_dec

This commit is contained in:
Joakim Nohlgård 2018-02-06 08:14:26 +01:00
parent 8ecf3c6bb6
commit 5ec44c82ba
3 changed files with 105 additions and 0 deletions

View File

@ -234,6 +234,18 @@ size_t fmt_u16_dec(char *out, uint16_t val)
return fmt_u32_dec(out, val);
}
size_t fmt_s64_dec(char *out, int64_t val)
{
unsigned negative = (val < 0);
if (negative) {
if (out) {
*out++ = '-';
}
val = -val;
}
return fmt_u64_dec(out, val) + negative;
}
size_t fmt_s32_dec(char *out, int32_t val)
{
unsigned negative = (val < 0);
@ -441,6 +453,13 @@ void print_u64_dec(uint64_t val)
print(buf, len);
}
void print_s64_dec(uint64_t val)
{
char buf[20]; /* "-9223372036854775808" */
size_t len = fmt_s64_dec(buf, val);
print(buf, len);
}
void print_float(float f, unsigned precision)
{
char buf[19];

View File

@ -177,6 +177,21 @@ size_t fmt_u64_dec(char *out, uint64_t val);
*/
size_t fmt_u16_dec(char *out, uint16_t val);
/**
* @brief Convert a int64 value to decimal string.
*
* Will add a leading "-" if @p val is negative.
*
* If @p out is NULL, will only return the number of bytes that would have
* been written.
*
* @param[out] out Pointer to output buffer, or NULL
* @param[in] val Value to convert
*
* @return nr of characters written to (or needed in) @p out
*/
size_t fmt_s64_dec(char *out, int64_t val);
/**
* @brief Convert a int32 value to decimal string.
*

View File

@ -275,6 +275,74 @@ static void test_fmt_s32_dec_b(void)
TEST_ASSERT_EQUAL_STRING("zzz", &out[12]);
}
static void test_fmt_s64_dec_a(void)
{
char out[24] = "zzzzzzzzzzzzzzzzzzzzzzz";
int64_t val = 9876;
uint8_t chars = 0;
chars = fmt_s64_dec(out, val);
TEST_ASSERT_EQUAL_INT(4, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("9876", (char *) out);
val = -9876;
chars = fmt_s64_dec(out, val);
TEST_ASSERT_EQUAL_INT(5, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("-9876", (char *) out);
/* check that the buffer was not overflowed */
TEST_ASSERT_EQUAL_STRING("zzzzzzzzzzzzzzzzz", &out[6]);
}
static void test_fmt_s64_dec_b(void)
{
char out[24] = "zzzzzzzzzzzzzzzzzzzzzzz";
int64_t val = 2147483647;
uint8_t chars = 0;
chars = fmt_s64_dec(out, val);
TEST_ASSERT_EQUAL_INT(10, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("2147483647", (char *) out);
val = -2147483648;
chars = fmt_s64_dec(out, val);
TEST_ASSERT_EQUAL_INT(11, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("-2147483648", (char *) out);
/* check that the buffer was not overflowed */
TEST_ASSERT_EQUAL_STRING("zzzzzzzzzzz", &out[12]);
}
static void test_fmt_s64_dec_c(void)
{
char out[24] = "zzzzzzzzzzzzzzzzzzzzzzz";
int64_t val = 9223372036854775807ll;
uint8_t chars = 0;
chars = fmt_s64_dec(out, val);
TEST_ASSERT_EQUAL_INT(19, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("9223372036854775807", (char *) out);
/* typing -9223372036854775808 as a decimal literal causes a compiler warning
* "integer constant is so large that it is unsigned"
* because of compiler internal workings and the C standard, using
* -9223372036854775807-1 works around this peculiarity.
* See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661 */
val = -9223372036854775807ll - 1;
chars = fmt_s64_dec(out, val);
TEST_ASSERT_EQUAL_INT(20, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("-9223372036854775808", (char *) out);
/* check that the buffer was not overflowed */
TEST_ASSERT_EQUAL_STRING("zz", &out[21]);
}
static void test_fmt_u64_dec_a(void)
{
char out[24] = "zzzzzzzzzzzzzzzzzzzzzzz";
@ -553,6 +621,9 @@ Test *tests_fmt_tests(void)
new_TestFixture(test_fmt_u16_dec),
new_TestFixture(test_fmt_s32_dec_a),
new_TestFixture(test_fmt_s32_dec_b),
new_TestFixture(test_fmt_s64_dec_a),
new_TestFixture(test_fmt_s64_dec_b),
new_TestFixture(test_fmt_s64_dec_c),
new_TestFixture(test_fmt_s16_dec),
new_TestFixture(test_fmt_s16_dfp),
new_TestFixture(test_fmt_s32_dfp),