2022-05-24 07:14:12 -04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <kernaux/printf.h>
|
|
|
|
|
2022-06-14 08:03:17 -04:00
|
|
|
#ifdef WITH_IO
|
|
|
|
#include <kernaux/io.h>
|
2022-06-07 14:08:47 -04:00
|
|
|
#endif
|
|
|
|
|
2022-05-24 07:14:12 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 1024
|
|
|
|
|
|
|
|
static char buffer[BUFFER_SIZE];
|
|
|
|
static size_t buffer_index;
|
|
|
|
|
2022-06-14 08:03:17 -04:00
|
|
|
#ifdef WITH_IO
|
2022-06-07 14:08:47 -04:00
|
|
|
|
|
|
|
static const char *const data = "foobar";
|
|
|
|
|
2022-05-24 07:14:12 -04:00
|
|
|
static void test_putchar(const char chr, void *const arg)
|
|
|
|
{
|
|
|
|
assert(arg == data);
|
|
|
|
|
|
|
|
if (buffer_index >= BUFFER_SIZE) {
|
|
|
|
printf("Buffer overflow!\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[buffer_index++] = chr;
|
|
|
|
}
|
|
|
|
|
2022-06-14 08:03:17 -04:00
|
|
|
#endif // WITH_IO
|
2022-06-07 14:08:47 -04:00
|
|
|
|
2022-05-24 07:14:12 -04:00
|
|
|
static void test(const char *const expected, const char *const format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
int result;
|
|
|
|
|
2022-06-14 08:03:17 -04:00
|
|
|
#ifdef WITH_IO
|
2022-05-24 07:14:12 -04:00
|
|
|
memset(buffer, '\0', sizeof(buffer));
|
|
|
|
buffer_index = 0;
|
|
|
|
va_start(va, format);
|
2022-06-25 10:30:24 -04:00
|
|
|
struct KernAux_OldFile file = KernAux_OldFile_create(test_putchar);
|
2022-06-07 14:08:47 -04:00
|
|
|
result = kernaux_vfprintf(&file, (char*)data, format, va);
|
2022-05-24 07:14:12 -04:00
|
|
|
va_end(va);
|
|
|
|
assert((size_t)result == strlen(expected));
|
|
|
|
assert(strcmp(expected, buffer) == 0);
|
2022-06-07 14:08:47 -04:00
|
|
|
#endif
|
2022-05-24 07:14:12 -04:00
|
|
|
|
|
|
|
memset(buffer, '\0', sizeof(buffer));
|
|
|
|
buffer_index = 0;
|
|
|
|
va_start(va, format);
|
|
|
|
result = kernaux_vsnprintf(buffer, sizeof(buffer), format, va);
|
|
|
|
va_end(va);
|
|
|
|
assert((size_t)result == strlen(expected));
|
|
|
|
assert(strcmp(expected, buffer) == 0);
|
|
|
|
}
|
|
|
|
|
2022-06-20 14:01:56 -04:00
|
|
|
void test_main()
|
2022-05-24 07:14:12 -04:00
|
|
|
{
|
2022-06-14 08:03:17 -04:00
|
|
|
#ifdef WITH_IO
|
2022-05-24 07:51:08 -04:00
|
|
|
memset(buffer, '\0', sizeof(buffer));
|
|
|
|
buffer_index = 0;
|
2022-06-25 10:30:24 -04:00
|
|
|
struct KernAux_OldFile file = KernAux_OldFile_create(test_putchar);
|
2022-06-07 14:08:47 -04:00
|
|
|
kernaux_fprintf(&file, (char*)data, "Hello, World!");
|
2022-05-24 07:51:08 -04:00
|
|
|
assert(strcmp("Hello, World!", buffer) == 0);
|
2022-06-07 14:08:47 -04:00
|
|
|
#endif
|
2022-05-24 07:51:08 -04:00
|
|
|
|
2022-05-24 07:14:12 -04:00
|
|
|
{% for case in cases %}
|
2022-05-24 07:51:08 -04:00
|
|
|
{% if case.float %}
|
|
|
|
#ifdef ENABLE_FLOAT
|
|
|
|
{% endif %}
|
2022-05-24 07:14:12 -04:00
|
|
|
printf("%s\n", {{ escape_str(case.result) }});
|
|
|
|
test({{ escape_str(case.result) }}, {{ escape_str(fmt(case.args)) }}{{ values(case.args) }});
|
2022-05-24 07:51:08 -04:00
|
|
|
{% if case.float %}
|
|
|
|
#endif
|
2022-05-24 07:14:12 -04:00
|
|
|
{% endif %}
|
|
|
|
{% endfor %}
|
|
|
|
}
|