| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| #include <config.h> |
|
|
| #include "unistdio.h" |
|
|
| #include <locale.h> |
| #include <stdarg.h> |
| #include <stdint.h> |
| #include <stdlib.h> |
| #include <string.h> |
|
|
| #include "unistr.h" |
| #include "macros.h" |
|
|
| static void |
| test_function (uint8_t * (*my_asnprintf) (uint8_t *, size_t *, const char *, ...)) |
| { |
| |
|
|
| { |
| const char *locale_string = "\304rger"; |
| { |
| size_t length; |
| uint8_t *result = |
| my_asnprintf (NULL, &length, "%s %d", locale_string, 33, 44, 55); |
| static const uint8_t expected[] = "\303\204rger 33"; |
| ASSERT (result != NULL); |
| ASSERT (u8_strcmp (result, expected) == 0); |
| ASSERT (length == u8_strlen (result)); |
| free (result); |
| } |
| { |
| size_t length; |
| uint8_t *result = |
| my_asnprintf (NULL, &length, "%10s %d", locale_string, 33, 44, 55); |
| static const uint8_t expected[] = " \303\204rger 33"; |
| ASSERT (result != NULL); |
| ASSERT (u8_strcmp (result, expected) == 0); |
| ASSERT (length == u8_strlen (result)); |
| free (result); |
| } |
| { |
| size_t length; |
| uint8_t *result = |
| my_asnprintf (NULL, &length, "%-10s %d", locale_string, 33, 44, 55); |
| static const uint8_t expected[] = "\303\204rger 33"; |
| ASSERT (result != NULL); |
| ASSERT (u8_strcmp (result, expected) == 0); |
| ASSERT (length == u8_strlen (result)); |
| free (result); |
| } |
| { |
| size_t length; |
| uint8_t *result = |
| my_asnprintf (NULL, &length, "%010s %d", locale_string, 33, 44, 55); |
| static const uint8_t expected[] = " \303\204rger 33"; |
| ASSERT (result != NULL); |
| ASSERT (u8_strcmp (result, expected) == 0); |
| ASSERT (length == u8_strlen (result)); |
| free (result); |
| } |
| } |
|
|
| |
|
|
| { |
| const char *locale_string = "h\351t\351rog\351n\351it\351"; |
| wchar_t wide_string[20]; |
| ASSERT (mbstowcs (wide_string, locale_string, SIZEOF (wide_string)) == 13); |
| { |
| size_t length; |
| uint8_t *result = |
| my_asnprintf (NULL, &length, "%ls %d", wide_string, 33, 44, 55); |
| static const uint8_t expected[] = "h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; |
| ASSERT (result != NULL); |
| ASSERT (u8_strcmp (result, expected) == 0); |
| ASSERT (length == u8_strlen (result)); |
| free (result); |
| } |
| { |
| size_t length; |
| uint8_t *result = |
| my_asnprintf (NULL, &length, "%20ls %d", wide_string, 33, 44, 55); |
| static const uint8_t expected[] = " h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; |
| ASSERT (result != NULL); |
| ASSERT (u8_strcmp (result, expected) == 0); |
| ASSERT (length == u8_strlen (result)); |
| free (result); |
| } |
| { |
| size_t length; |
| uint8_t *result = |
| my_asnprintf (NULL, &length, "%-20ls %d", wide_string, 33, 44, 55); |
| static const uint8_t expected[] = "h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; |
| ASSERT (result != NULL); |
| ASSERT (u8_strcmp (result, expected) == 0); |
| ASSERT (length == u8_strlen (result)); |
| free (result); |
| } |
| { |
| size_t length; |
| uint8_t *result = |
| my_asnprintf (NULL, &length, "%020ls %d", wide_string, 33, 44, 55); |
| static const uint8_t expected[] = " h\303\251t\303\251rog\303\251n\303\251it\303\251 33"; |
| ASSERT (result != NULL); |
| ASSERT (u8_strcmp (result, expected) == 0); |
| ASSERT (length == u8_strlen (result)); |
| free (result); |
| } |
| } |
| } |
|
|
| static uint8_t * |
| my_asnprintf (uint8_t *resultbuf, size_t *lengthp, const char *format, ...) |
| { |
| va_list args; |
| uint8_t *ret; |
|
|
| va_start (args, format); |
| ret = u8_vasnprintf (resultbuf, lengthp, format, args); |
| va_end (args); |
| return ret; |
| } |
|
|
| static void |
| test_vasnprintf () |
| { |
| test_function (my_asnprintf); |
| } |
|
|
| int |
| main (int argc, char *argv[]) |
| { |
| |
| if (setlocale (LC_ALL, "") == NULL) |
| return 1; |
|
|
| test_vasnprintf (); |
| return test_exit_status; |
| } |
|
|