mirror of https://github.com/ARMmbed/mbed-os.git
Add UT for CellularUtil::hex_to_char and ::hex_str_to_char_str
Also added checks for pointer validity.pull/11495/head
parent
0b9e80f76e
commit
e824714cd8
|
@ -53,6 +53,47 @@ TEST_F(Testutil, test_util_binary_str_to_uint)
|
||||||
EXPECT_TRUE(0 == binary_str_to_uint(binary_str, 0));
|
EXPECT_TRUE(0 == binary_str_to_uint(binary_str, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(Testutil, hex_to_char)
|
||||||
|
{
|
||||||
|
char output;
|
||||||
|
|
||||||
|
// 0
|
||||||
|
hex_to_char("00", output);
|
||||||
|
EXPECT_EQ((char)0x00, output);
|
||||||
|
|
||||||
|
// <128
|
||||||
|
hex_to_char("10", output);
|
||||||
|
EXPECT_EQ((char)0x10, output);
|
||||||
|
|
||||||
|
// =128
|
||||||
|
hex_to_char("80", output);
|
||||||
|
EXPECT_EQ((char)0x80, output);
|
||||||
|
|
||||||
|
// >128
|
||||||
|
hex_to_char("FF", output);
|
||||||
|
EXPECT_EQ((char)0xFF, output);
|
||||||
|
|
||||||
|
// Null -> output is not modified
|
||||||
|
hex_to_char(NULL, output);
|
||||||
|
EXPECT_EQ((char)0xFF, output);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(Testutil, hex_str_to_char_str)
|
||||||
|
{
|
||||||
|
char input[] = "0165AABBCC";
|
||||||
|
char output[32];
|
||||||
|
EXPECT_EQ(5, hex_str_to_char_str(input, strlen(input), output));
|
||||||
|
EXPECT_EQ((char)0x01, output[0]);
|
||||||
|
EXPECT_EQ((char)0x65, output[1]);
|
||||||
|
EXPECT_EQ((char)0xAA, output[2]);
|
||||||
|
EXPECT_EQ((char)0xBB, output[3]);
|
||||||
|
EXPECT_EQ((char)0xCC, output[4]);
|
||||||
|
|
||||||
|
// NULL params
|
||||||
|
EXPECT_EQ(0, hex_str_to_char_str(NULL, 2, output));
|
||||||
|
EXPECT_EQ(0, hex_str_to_char_str(input, strlen(input), NULL));
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(Testutil, test_util_uint_to_binary_string)
|
TEST_F(Testutil, test_util_uint_to_binary_string)
|
||||||
{
|
{
|
||||||
char str[33];
|
char str[33];
|
||||||
|
|
|
@ -278,11 +278,13 @@ int hex_str_to_int(const char *hex_string, int hex_string_length)
|
||||||
int hex_str_to_char_str(const char *str, uint16_t len, char *buf)
|
int hex_str_to_char_str(const char *str, uint16_t len, char *buf)
|
||||||
{
|
{
|
||||||
int strcount = 0;
|
int strcount = 0;
|
||||||
for (int i = 0; i + 1 < len; i += 2) {
|
if (str && buf) {
|
||||||
char tmp;
|
for (int i = 0; i + 1 < len; i += 2) {
|
||||||
hex_to_char(str + i, tmp);
|
char tmp;
|
||||||
buf[strcount] = tmp;
|
hex_to_char(str + i, tmp);
|
||||||
strcount++;
|
buf[strcount] = tmp;
|
||||||
|
strcount++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return strcount;
|
return strcount;
|
||||||
|
@ -290,9 +292,11 @@ int hex_str_to_char_str(const char *str, uint16_t len, char *buf)
|
||||||
|
|
||||||
void hex_to_char(const char *hex, char &buf)
|
void hex_to_char(const char *hex, char &buf)
|
||||||
{
|
{
|
||||||
int upper = hex_str_to_int(hex, 1);
|
if (hex) {
|
||||||
int lower = hex_str_to_int(hex + 1, 1);
|
int upper = hex_str_to_int(hex, 1);
|
||||||
buf = ((upper << 4) & 0xF0) | (lower & 0x0F);
|
int lower = hex_str_to_int(hex + 1, 1);
|
||||||
|
buf = ((upper << 4) & 0xF0) | (lower & 0x0F);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void uint_to_binary_str(uint32_t num, char *str, int str_size, int bit_cnt)
|
void uint_to_binary_str(uint32_t num, char *str, int str_size, int bit_cnt)
|
||||||
|
|
Loading…
Reference in New Issue