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

522 lines
13 KiB
C
Raw Normal View History

2015-10-27 13:47:39 +01:00
/*
* Copyright (C) 2015 Cenk Gündoğan <cnkgndgn@gmail.com>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
*/
#include <errno.h>
#include <stdint.h>
#include <string.h>
2015-10-27 13:47:39 +01:00
#include "embUnit/embUnit.h"
#include "fmt.h"
#include "tests-fmt.h"
static void test_fmt_byte_hex(void)
{
char out[3] = "--";
TEST_ASSERT_EQUAL_INT(2, fmt_byte_hex(out, 0));
TEST_ASSERT_EQUAL_STRING("00", (char *) out);
TEST_ASSERT_EQUAL_INT(2, fmt_byte_hex(out, 128));
TEST_ASSERT_EQUAL_STRING("80", (char *) out);
TEST_ASSERT_EQUAL_INT(2, fmt_byte_hex(out, 255));
TEST_ASSERT_EQUAL_STRING("FF", (char *) out);
}
static void test_fmt_bytes_hex(void)
{
char out[15] = "--------------";
uint8_t val[7] = { 0xAA, 9, 8, 7, 6, 0xA8, 0xEF};
uint8_t bytes = 0;
bytes = fmt_bytes_hex(out, val, 0);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(0, bytes);
TEST_ASSERT_EQUAL_STRING("", (char *) out);
bytes = fmt_bytes_hex(out, val, 1);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(2, bytes);
TEST_ASSERT_EQUAL_STRING("AA", (char *) out);
bytes = fmt_bytes_hex(out, val, 2);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(4, bytes);
TEST_ASSERT_EQUAL_STRING("AA09", (char *) out);
bytes = fmt_bytes_hex(out, val, 3);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(6, bytes);
TEST_ASSERT_EQUAL_STRING("AA0908", (char *) out);
bytes = fmt_bytes_hex(out, val, 4);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(8, bytes);
TEST_ASSERT_EQUAL_STRING("AA090807", (char *) out);
bytes = fmt_bytes_hex(out, val, 5);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(10, bytes);
TEST_ASSERT_EQUAL_STRING("AA09080706", (char *) out);
bytes = fmt_bytes_hex(out, val, 6);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(12, bytes);
TEST_ASSERT_EQUAL_STRING("AA09080706A8", (char *) out);
bytes = fmt_bytes_hex(out, val, 7);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(14, bytes);
TEST_ASSERT_EQUAL_STRING("AA09080706A8EF", (char *) out);
}
2015-10-27 13:47:39 +01:00
static void test_fmt_bytes_hex_reverse(void)
{
2017-08-23 10:40:30 +02:00
char out[9] = "--------";
2015-10-27 13:47:39 +01:00
uint8_t val[4] = { 9, 8, 7, 6 };
uint8_t bytes = 0;
bytes = fmt_bytes_hex_reverse(out, val, 1);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(2, bytes);
TEST_ASSERT_EQUAL_STRING("09", (char *) out);
bytes = fmt_bytes_hex_reverse(out, val, 2);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(4, bytes);
TEST_ASSERT_EQUAL_STRING("0809", (char *) out);
bytes = fmt_bytes_hex_reverse(out, val, 3);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(6, bytes);
TEST_ASSERT_EQUAL_STRING("070809", (char *) out);
bytes = fmt_bytes_hex_reverse(out, val, 4);
out[bytes] = '\0';
TEST_ASSERT_EQUAL_INT(8, bytes);
TEST_ASSERT_EQUAL_STRING("06070809", (char *) out);
}
static void test_fmt_hex_bytes(void)
{
uint8_t val = 0;
uint8_t bytes = fmt_hex_bytes(&val, "");
TEST_ASSERT_EQUAL_INT(0, bytes);
TEST_ASSERT_EQUAL_INT(0, val);
bytes = fmt_hex_bytes(&val, "A");
TEST_ASSERT_EQUAL_INT(0, val);
TEST_ASSERT_EQUAL_INT(0, bytes);
char hex2[3] = "00";
uint8_t val1[1] = { 0 };
bytes = fmt_hex_bytes(val1, hex2);
TEST_ASSERT_EQUAL_INT(1, bytes);
TEST_ASSERT_EQUAL_INT(0, val1[0]);
memcpy(hex2, "AB", 2);
hex2[2] = '\0';
val1[0] = 0;
bytes = fmt_hex_bytes(val1, hex2);
TEST_ASSERT_EQUAL_INT(1, bytes);
TEST_ASSERT_EQUAL_INT(0xAB, val1[0]);
memcpy(hex2, "CD", 2);
hex2[2] = '\0';
val1[0] = 0;
bytes = fmt_hex_bytes(val1, hex2);
TEST_ASSERT_EQUAL_INT(1, bytes);
TEST_ASSERT_EQUAL_INT(0xCD, val1[0]);
memcpy(hex2, "EF", 2);
hex2[2] = '\0';
val1[0] = 0;
bytes = fmt_hex_bytes(val1, hex2);
TEST_ASSERT_EQUAL_INT(1, bytes);
TEST_ASSERT_EQUAL_INT(0xEF, val1[0]);
char hex6[] = "0102aF";
uint8_t val3[3];
bytes = fmt_hex_bytes(val3, hex6);
TEST_ASSERT_EQUAL_INT(3, bytes);
TEST_ASSERT_EQUAL_INT(1, val3[0]);
TEST_ASSERT_EQUAL_INT(2, val3[1]);
TEST_ASSERT_EQUAL_INT(0xAF, val3[2]);
}
2015-10-27 13:47:39 +01:00
static void test_fmt_u32_hex(void)
{
char out[9] = "--------";
uint32_t val = 3735928559;
TEST_ASSERT_EQUAL_INT(8, fmt_u32_hex(out, val));
TEST_ASSERT_EQUAL_STRING("DEADBEEF", (char *) out);
}
static void test_fmt_u64_hex(void)
{
char out[17] = "----------------";
uint64_t val = 1002843385516306400;
TEST_ASSERT_EQUAL_INT(16, fmt_u64_hex(out, val));
TEST_ASSERT_EQUAL_STRING("0DEAD0BEEF0CAFE0", (char *) out);
}
static void test_fmt_u32_dec(void)
{
char out[11] = "----------";
2015-10-27 13:47:39 +01:00
uint32_t val = 12345678;
uint8_t chars = 0;
chars = fmt_u32_dec(out, val);
TEST_ASSERT_EQUAL_INT(8, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("12345678", (char *) out);
memset(out, '-', sizeof(out));
val = 1234567890;
chars = fmt_u32_dec(out, val);
TEST_ASSERT_EQUAL_INT(10, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("1234567890", (char *) out);
2015-10-27 13:47:39 +01:00
}
static void test_fmt_u16_dec(void)
{
char out[5] = "----";
uint16_t val = 6556;
uint8_t chars = 0;
chars = fmt_u16_dec(out, val);
TEST_ASSERT_EQUAL_INT(4, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("6556", (char *) out);
}
static void test_fmt_s32_dec_a(void)
2015-10-27 13:47:39 +01:00
{
char out[16] = "zzzzzzzzzzzzzzz";
2015-10-27 13:47:39 +01:00
int32_t val = 9876;
uint8_t chars = 0;
chars = fmt_s32_dec(out, val);
TEST_ASSERT_EQUAL_INT(4, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("9876", (char *) out);
val = -9876;
chars = fmt_s32_dec(out, val);
TEST_ASSERT_EQUAL_INT(5, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("-9876", (char *) out);
}
static void test_fmt_s32_dec_b(void)
{
char out[16] = "zzzzzzzzzzzzzzz";
int32_t val = 2147483647;
uint8_t chars = 0;
chars = fmt_s32_dec(out, val);
TEST_ASSERT_EQUAL_INT(10, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("2147483647", (char *) out);
val = -2147483648;
chars = fmt_s32_dec(out, val);
TEST_ASSERT_EQUAL_INT(11, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("-2147483648", (char *) out);
}
static void test_fmt_u64_dec_a(void)
{
2017-08-23 10:40:30 +02:00
char out[21] = "--------------------";
uint64_t val = 0;
uint8_t chars = 0;
chars = fmt_u64_dec(out, val);
TEST_ASSERT_EQUAL_INT(1, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("0", (char *) out);
}
static void test_fmt_u64_dec_b(void)
{
char out[21] = "--------------------";
uint64_t val = 18446744073709551615LLU;
uint8_t chars = 0;
chars = fmt_u64_dec(out, val);
TEST_ASSERT_EQUAL_INT(20, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("18446744073709551615", (char *) out);
}
static void test_fmt_u64_dec_c(void)
{
char out[21] = "--------------------";
uint64_t val = 1234567890123456789LLU;
uint8_t chars = 0;
chars = fmt_u64_dec(out, val);
TEST_ASSERT_EQUAL_INT(19, chars);
out[chars] = '\0';
TEST_ASSERT_EQUAL_STRING("1234567890123456789", (char *) out);
}
static void test_fmt_s16_dec(void)
{
2017-08-23 10:40:30 +02:00
char out[7] = "------";
int16_t val;
size_t len;
val = 0;
len = fmt_s16_dec(out, val);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(1, len);
TEST_ASSERT_EQUAL_STRING("0", (char *)out);
val = -32000;
len = fmt_s16_dec(out, val);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(6, len);
TEST_ASSERT_EQUAL_STRING("-32000", (char *)out);
val = 12345;
len = fmt_s16_dec(out, val);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(5, len);
TEST_ASSERT_EQUAL_STRING("12345", (char *)out);
}
static void test_fmt_s16_dfp(void)
{
2017-08-23 10:40:30 +02:00
char out[9] = "--------";
int16_t val;
unsigned fpp;
size_t len;
val = 0;
fpp = 3;
len = fmt_s16_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(5, len);
TEST_ASSERT_EQUAL_STRING("0.000", (char *)out);
val = 12345;
fpp = 4;
len = fmt_s16_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(6, len);
TEST_ASSERT_EQUAL_STRING("1.2345", (char *)out);
val = 12030;
fpp = 3;
len = fmt_s16_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(6, len);
TEST_ASSERT_EQUAL_STRING("12.030", (char *)out);
val = -3548;
fpp = 2;
len = fmt_s16_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(6, len);
TEST_ASSERT_EQUAL_STRING("-35.48", (char *)out);
val = -23;
fpp = 4;
len = fmt_s16_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(7, len);
TEST_ASSERT_EQUAL_STRING("-0.0023", (char *)out);
val = 50;
fpp = 3;
len = fmt_s16_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(5, len);
TEST_ASSERT_EQUAL_STRING("0.050", (char *)out);
val = -12345;
fpp = 0;
len = fmt_s16_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(6, len);
TEST_ASSERT_EQUAL_STRING("-12345", (char *)out);
val = 31987;
2017-08-23 10:40:30 +02:00
fpp = 6;
len = fmt_s16_dfp(out, val, fpp);
out[len] = '\0';
2017-08-23 10:40:30 +02:00
TEST_ASSERT_EQUAL_INT(8, len);
TEST_ASSERT_EQUAL_STRING("0.031987", (char *)out);
}
static void test_fmt_s32_dfp(void)
{
char out[13] = "-------------";
int32_t val;
unsigned fpp;
size_t len;
val = 0;
2017-08-23 10:40:30 +02:00
fpp = 7;
len = fmt_s32_dfp(out, val, fpp);
out[len] = '\0';
2017-08-23 10:40:30 +02:00
TEST_ASSERT_EQUAL_INT(9, len);
TEST_ASSERT_EQUAL_STRING("0.0000000", (char *)out);
val = 123456789;
fpp = 7;
len = fmt_s32_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(10, len);
TEST_ASSERT_EQUAL_STRING("12.3456789", (char *)out);
val = 120030;
fpp = 3;
len = fmt_s32_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(7, len);
TEST_ASSERT_EQUAL_STRING("120.030", (char *)out);
val = -314159;
fpp = 5;
len = fmt_s32_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(8, len);
TEST_ASSERT_EQUAL_STRING("-3.14159", (char *)out);
val = -23;
2017-08-23 10:40:30 +02:00
fpp = 7;
len = fmt_s32_dfp(out, val, fpp);
out[len] = '\0';
2017-08-23 10:40:30 +02:00
TEST_ASSERT_EQUAL_INT(10, len);
TEST_ASSERT_EQUAL_STRING("-0.0000023", (char *)out);
val = 50;
fpp = 6;
len = fmt_s32_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(8, len);
TEST_ASSERT_EQUAL_STRING("0.000050", (char *)out);
val = -123456789;
fpp = 0;
len = fmt_s32_dfp(out, val, fpp);
out[len] = '\0';
TEST_ASSERT_EQUAL_INT(10, len);
TEST_ASSERT_EQUAL_STRING("-123456789", (char *)out);
}
2015-10-27 13:47:39 +01:00
static void test_fmt_strlen(void)
{
const char *empty_str = "";
const char *short_str = "short";
const char *long_str = "this is a long string";
TEST_ASSERT_EQUAL_INT(0, fmt_strlen(empty_str));
TEST_ASSERT_EQUAL_INT(5, fmt_strlen(short_str));
TEST_ASSERT_EQUAL_INT(21, fmt_strlen(long_str));
}
static void test_fmt_str(void)
{
const char *string1 = "string1";
char string2[] = "StRiNg2";
TEST_ASSERT_EQUAL_INT(fmt_strlen(string1), fmt_str(&string2[0], string1));
TEST_ASSERT_EQUAL_STRING(string1, &string2[0]);
}
static void test_scn_u32_dec(void)
{
const char *string1 = "123456789";
uint32_t val1 = 123456789;
uint32_t val2 = 12345;
TEST_ASSERT_EQUAL_INT(val1, scn_u32_dec(string1, 9));
TEST_ASSERT_EQUAL_INT(val2, scn_u32_dec(string1, 5));
}
static void test_fmt_lpad(void)
{
const char base[] = "abcd";
char string[9] = {0};
strcpy(string, base);
fmt_lpad(string, 4, 8, ' ');
TEST_ASSERT_EQUAL_STRING(" abcd", (char*)string);
fmt_lpad(string, 0, 0, '1');
TEST_ASSERT_EQUAL_STRING(" abcd", (char*)string);
fmt_lpad(string, 4, 0, '2');
TEST_ASSERT_EQUAL_STRING(" abcd", (char*)string);
fmt_lpad(string, 0, 4, '3');
TEST_ASSERT_EQUAL_STRING("3333abcd", (char*)string);
fmt_lpad(string, 8, 8, '4');
TEST_ASSERT_EQUAL_STRING("3333abcd", (char*)string);
fmt_lpad(string, 4, 8, 'x');
TEST_ASSERT_EQUAL_STRING((char*)string, "xxxx3333");
}
2015-10-27 13:47:39 +01:00
Test *tests_fmt_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_fmt_byte_hex),
new_TestFixture(test_fmt_bytes_hex),
2015-10-27 13:47:39 +01:00
new_TestFixture(test_fmt_bytes_hex_reverse),
new_TestFixture(test_fmt_hex_bytes),
2015-10-27 13:47:39 +01:00
new_TestFixture(test_fmt_u32_hex),
new_TestFixture(test_fmt_u64_hex),
new_TestFixture(test_fmt_u32_dec),
new_TestFixture(test_fmt_u64_dec_a),
new_TestFixture(test_fmt_u64_dec_b),
new_TestFixture(test_fmt_u64_dec_c),
2015-10-27 13:47:39 +01:00
new_TestFixture(test_fmt_u16_dec),
new_TestFixture(test_fmt_s32_dec_a),
new_TestFixture(test_fmt_s32_dec_b),
new_TestFixture(test_fmt_s16_dec),
new_TestFixture(test_fmt_s16_dfp),
new_TestFixture(test_fmt_s32_dfp),
2015-10-27 13:47:39 +01:00
new_TestFixture(test_fmt_strlen),
new_TestFixture(test_fmt_str),
new_TestFixture(test_scn_u32_dec),
new_TestFixture(test_fmt_lpad),
2015-10-27 13:47:39 +01:00
};
EMB_UNIT_TESTCALLER(fmt_tests, NULL, NULL, fixtures);
return (Test *)&fmt_tests;
}
void tests_fmt(void)
{
TESTS_RUN(tests_fmt_tests());
}
/** @} */