wsutil: Add some tests for word_to_hex()

This commit is contained in:
João Valverde 2021-09-16 15:34:45 +01:00
parent 24fd8c6740
commit 8304ced0e6
2 changed files with 77 additions and 28 deletions

View File

@ -34,6 +34,54 @@ void test_format_size(void)
#include "to_str.h"
void test_word_to_hex(void)
{
static char buf[32];
char *str; /* String is not NULL terminated. */
str = guint8_to_hex(buf, 0x34);
g_assert_true(str == buf + 2);
g_assert_cmpint(str[-1], ==, '4');
g_assert_cmpint(str[-2], ==, '3');
str = word_to_hex(buf, 0x1234);
g_assert_true(str == buf + 4);
g_assert_cmpint(str[-1], ==, '4');
g_assert_cmpint(str[-2], ==, '3');
g_assert_cmpint(str[-3], ==, '2');
g_assert_cmpint(str[-4], ==, '1');
str = dword_to_hex(buf, 0x1234);
g_assert_true(str == buf + 8);
g_assert_cmpint(str[-1], ==, '4');
g_assert_cmpint(str[-2], ==, '3');
g_assert_cmpint(str[-3], ==, '2');
g_assert_cmpint(str[-4], ==, '1');
g_assert_cmpint(str[-5], ==, '0');
g_assert_cmpint(str[-6], ==, '0');
g_assert_cmpint(str[-7], ==, '0');
g_assert_cmpint(str[-8], ==, '0');
str = qword_to_hex(buf, G_GUINT64_CONSTANT(0xFEDCBA987654321));
g_assert_true(str == buf + 16);
g_assert_cmpint(str[-1], ==, '1');
g_assert_cmpint(str[-2], ==, '2');
g_assert_cmpint(str[-3], ==, '3');
g_assert_cmpint(str[-4], ==, '4');
g_assert_cmpint(str[-5], ==, '5');
g_assert_cmpint(str[-6], ==, '6');
g_assert_cmpint(str[-7], ==, '7');
g_assert_cmpint(str[-8], ==, '8');
g_assert_cmpint(str[-9], ==, '9');
g_assert_cmpint(str[-10], ==, 'a');
g_assert_cmpint(str[-11], ==, 'b');
g_assert_cmpint(str[-12], ==, 'c');
g_assert_cmpint(str[-13], ==, 'd');
g_assert_cmpint(str[-14], ==, 'e');
g_assert_cmpint(str[-15], ==, 'f');
g_assert_cmpint(str[-16], ==, '0');
}
void test_bytes_to_str(void)
{
char *str;
@ -254,6 +302,7 @@ int main(int argc, char **argv)
g_test_add_func("/str_util/format_size", test_format_size);
g_test_add_func("/to_str/word_to_hex", test_word_to_hex);
g_test_add_func("/to_str/bytes_to_str", test_bytes_to_str);
g_test_add_func("/to_str/bytes_to_str_punct", test_bytes_to_str_punct);
g_test_add_func("/to_str/bytes_to_str_trunc1", test_bytes_to_string_trunc1);

View File

@ -23,8 +23,8 @@ extern "C" {
/**
* guint8_to_hex()
*
* Output guint8 hex represetation to 'out', and return pointer after last character (out + 4).
* It always output full representation (padded with 0).
* Output guint8 hex representation to 'out', and return pointer after last character (out + 2).
* It will always output full representation (padded with 0).
*
* String is not NUL terminated by this routine.
* There needs to be at least 2 bytes in the buffer.
@ -34,31 +34,20 @@ WS_DLL_PUBLIC char *guint8_to_hex(char *out, guint8 val);
/**
* word_to_hex()
*
* Output guint16 hex represetation to 'out', and return pointer after last character (out + 4).
* It always output full representation (padded with 0).
* Output guint16 hex representation to 'out', and return pointer after last character (out + 4).
* It will always output full representation (padded with 0).
*
* String is not NUL terminated by this routine.
* There needs to be at least 4 bytes in the buffer.
*/
WS_DLL_PUBLIC char *word_to_hex(char *out, guint16 word);
/**
* dword_to_hex()
*
* Output guint32 hex represetation to 'out', and return pointer after last character.
* It always output full representation (padded with 0).
*
* String is not NUL terminated by this routine.
* There needs to be at least 8 bytes in the buffer.
*/
WS_DLL_PUBLIC char *dword_to_hex(char *out, guint32 dword);
/**
* word_to_hex_punct()
*
* Output guint16 hex represetation to 'out', and return pointer after last character.
* Output guint16 hex representation to 'out', and return pointer after last character.
* Each byte will be separated with punct character (cannot be NUL).
* It always output full representation (padded with 0).
* It will always output full representation (padded with 0).
*
* String is not NUL terminated by this routine.
* There needs to be at least 5 bytes in the buffer.
@ -68,7 +57,7 @@ WS_DLL_PUBLIC char *word_to_hex_punct(char *out, guint16 word, char punct);
/**
* word_to_hex_npad()
*
* Output guint16 hex represetation to 'out', and return pointer after last character.
* Output guint16 hex representation to 'out', and return pointer after last character.
* Value is not padded.
*
* String is not NUL terminated by this routine.
@ -76,12 +65,23 @@ WS_DLL_PUBLIC char *word_to_hex_punct(char *out, guint16 word, char punct);
*/
WS_DLL_PUBLIC char *word_to_hex_npad(char *out, guint16 word);
/**
* dword_to_hex()
*
* Output guint32 hex representation to 'out', and return pointer after last character.
* It will always output full representation (padded with 0).
*
* String is not NUL terminated by this routine.
* There needs to be at least 8 bytes in the buffer.
*/
WS_DLL_PUBLIC char *dword_to_hex(char *out, guint32 dword);
/**
* dword_to_hex_punct()
*
* Output guint32 hex represetation to 'out', and return pointer after last character.
* Output guint32 hex representation to 'out', and return pointer after last character.
* Each byte will be separated with punct character (cannot be NUL).
* It always output full representation (padded with 0).
* It will always output full representation (padded with 0).
*
* String is not NUL terminated by this routine.
* There needs to be at least 11 bytes in the buffer.
@ -91,8 +91,8 @@ WS_DLL_PUBLIC char *dword_to_hex_punct(char *out, guint32 dword, char punct);
/**
* qword_to_hex()
*
* Output guint64 hex represetation to 'out', and return pointer after last character.
* It always output full representation (padded with 0).
* Output guint64 hex representation to 'out', and return pointer after last character.
* It will always output full representation (padded with 0).
*
* String is not NUL terminated by this routine.
* There needs to be at least 16 bytes in the buffer.
@ -102,9 +102,9 @@ WS_DLL_PUBLIC char *qword_to_hex(char *out, guint64 qword);
/**
* qword_to_hex_punct()
*
* Output guint64 hex represetation to 'out', and return pointer after last character.
* Output guint64 hex representation to 'out', and return pointer after last character.
* Each byte will be separated with punct character (cannot be NUL).
* It always output full representation (padded with 0).
* It will always output full representation (padded with 0).
*
* String is not NUL terminated by this routine.
* There needs to be at least 22 bytes in the buffer.
@ -114,8 +114,8 @@ WS_DLL_PUBLIC char *qword_to_hex_punct(char *out, guint64 qword, char punct);
/**
* bytes_to_hexstr()
*
* Output hex represetation of guint8 ad array, and return pointer after last character.
* It always output full representation (padded with 0).
* Output hex representation of guint8 array, and return pointer after last character.
* It will always output full representation (padded with 0).
*
* String is not NUL terminated by this routine.
* There needs to be at least len * 2 bytes in the buffer.
@ -125,9 +125,9 @@ WS_DLL_PUBLIC char *bytes_to_hexstr(char *out, const guint8 *ad, size_t len);
/**
* bytes_to_hexstr_punct()
*
* Output hex represetation of guint8 ad array, and return pointer after last character.
* Output hex representation of guint8 array, and return pointer after last character.
* Each byte will be separated with punct character (cannot be NUL).
* It always output full representation (padded with 0).
* It will always output full representation (padded with 0).
*
* String is not NUL terminated by this routine.
* There needs to be at least len * 3 - 1 bytes in the buffer.