143 lines
3.5 KiB
C++
143 lines
3.5 KiB
C++
#include <cstdint>
|
|
#include <inttypes.h>
|
|
#include <iostream>
|
|
|
|
#include <di/stdio.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
extern "C" {
|
|
#include "stdio.c"
|
|
}
|
|
|
|
static size_t str_size = 256;
|
|
static char *str = NULL;
|
|
|
|
void test_reset_str(void)
|
|
{
|
|
memset(str, '+', str_size);
|
|
str[str_size - 1] = 0;
|
|
}
|
|
|
|
TEST(stdio, test_init) {
|
|
str = (char *)malloc(str_size);
|
|
}
|
|
|
|
TEST(stdio, di_snprintf_string) {
|
|
/* Write simple string with snprintf (without dst_len overflow) */
|
|
di_snprintf(str, str_size, "Simple string test");
|
|
ASSERT_STREQ("Simple string test", str);
|
|
|
|
test_reset_str();
|
|
|
|
/* Write simple string with snprintf (with dst_len overflow) */
|
|
di_snprintf(str, 7, "Simple string test");
|
|
ASSERT_STREQ("Simple", str);
|
|
|
|
/* Write simple string with snprintf (with src_len overflow) */
|
|
di_snprintf(str, 5, "12345678");
|
|
ASSERT_STREQ("1234", str);
|
|
}
|
|
|
|
TEST(stdio, di_snprintf_fmt_char) {
|
|
/* Write simple string with multiple appended characters (without dst_len overflow) */
|
|
di_snprintf(str, str_size, "%c-%c-%c-%c", 'a', 'b', 'c', 'd');
|
|
ASSERT_STREQ("a-b-c-d", str);
|
|
|
|
test_reset_str();
|
|
}
|
|
|
|
TEST(stdio, di_snprintf_fmt_float) {
|
|
/* Write simple string with a floating point */
|
|
di_snprintf(str, str_size, "%f", 1293.4923);
|
|
ASSERT_STREQ("1293.492", str);
|
|
|
|
test_reset_str();
|
|
}
|
|
|
|
TEST(stdio, di_snprintf_fmt_float2) {
|
|
/* Write simple string with a floating point */
|
|
di_snprintf(str, str_size, "%f", 0.003);
|
|
ASSERT_STREQ("0.003", str);
|
|
|
|
test_reset_str();
|
|
}
|
|
|
|
TEST(stdio, di_snprintf_fmt_float3) {
|
|
/* Write simple string with a floating point */
|
|
di_snprintf(str, str_size, "%f", 0.0000001);
|
|
ASSERT_STREQ("0.000", str);
|
|
|
|
test_reset_str();
|
|
}
|
|
|
|
TEST(stdio, di_snprintf_fmt_string) {
|
|
/* Write fmt string with multiple appended characters (without dst_len overflow) */
|
|
di_snprintf(str, str_size, "%s %s!", "Hello", "world");
|
|
ASSERT_STREQ("Hello world!", str);
|
|
|
|
test_reset_str();
|
|
|
|
/* Write fmt string with multiple appended characters (with dst_len overflow) */
|
|
di_snprintf(str, 6, "%s %s!", "Hello", "world");
|
|
ASSERT_STREQ("Hello", str);
|
|
}
|
|
|
|
TEST(stdio, di_snprintf_mixed) {
|
|
uint64_t time = 1234567890;
|
|
const char *tstr = "hello world!";
|
|
|
|
di_snprintf(str, str_size, "[%u] %s", time, tstr);
|
|
ASSERT_STREQ("[1234567890] hello world!", str);
|
|
|
|
test_reset_str();
|
|
}
|
|
|
|
TEST(stdio, di_snprintf_inttypes) {
|
|
di_snprintf(str, str_size, "%u", (uint32_t)INT32_MAX + 5U);
|
|
// 2147483647 + 5
|
|
ASSERT_STREQ("2147483652", str);
|
|
test_reset_str();
|
|
|
|
di_snprintf(str, str_size, "%d", (int32_t)INT32_MIN);
|
|
ASSERT_STREQ("-2147483648", str);
|
|
test_reset_str();
|
|
|
|
// The snprintf uses %d which is int32, this translates UINT32_MAX (= 0xFFFFFFFF) to -1
|
|
di_snprintf(str, str_size, "%d", (uint32_t)UINT32_MAX);
|
|
ASSERT_STREQ("-1", str);
|
|
test_reset_str();
|
|
|
|
di_snprintf(str, str_size, "%u", (int32_t)INT32_MIN);
|
|
ASSERT_STREQ("2147483648", str);
|
|
test_reset_str();
|
|
}
|
|
|
|
TEST(stdio, di_snprintf_hexinttypes) {
|
|
di_snprintf(str, str_size, "%x", (uint32_t)INT32_MAX + 5U);
|
|
ASSERT_STREQ("80000004", str);
|
|
test_reset_str();
|
|
|
|
di_snprintf(str, str_size, "%x", (uint32_t)UINT32_MAX);
|
|
ASSERT_STREQ("ffffffff", str);
|
|
test_reset_str();
|
|
|
|
di_snprintf(str, str_size, "%X", (uint32_t)UINT32_MAX);
|
|
ASSERT_STREQ("FFFFFFFF", str);
|
|
test_reset_str();
|
|
|
|
di_snprintf(str, str_size, "%x", (int32_t)INT32_MIN);
|
|
ASSERT_STREQ("80000000", str);
|
|
test_reset_str();
|
|
}
|
|
|
|
TEST(stdio, atof) {
|
|
ASSERT_FLOAT_EQ( 1.234, di_atof("1.234"));
|
|
ASSERT_FLOAT_EQ(-5.678, di_atof("-5.678"));
|
|
ASSERT_FLOAT_EQ( 6.123, di_atof("+6.123"));
|
|
ASSERT_FLOAT_EQ( 5.0, di_atof("+5.00e3")); // exponent is not supported
|
|
}
|
|
|
|
TEST(stdio, test_cleanup) {
|
|
free(str);
|
|
}
|