1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00
libkernaux/tests/printf_gen.jinja

77 lines
1.7 KiB
Django/Jinja

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define KERNAUX_ACCESS_PROTECTED
#include <kernaux/generic/file.h>
#include <kernaux/printf.h>
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
static char buffer[BUFFER_SIZE];
static size_t buffer_index;
static int test_putc(__attribute__((unused)) void *const file, unsigned char c)
{
if (buffer_index >= BUFFER_SIZE) {
printf("Buffer overflow!\n");
abort();
}
buffer[buffer_index++] = c;
return 1;
}
static const struct KernAux_File file = {
.putc = test_putc,
.puts = NULL,
.write = NULL,
};
static void test(const char *const expected, const char *const format, ...)
{
va_list va;
int result;
memset(buffer, '\0', sizeof(buffer));
buffer_index = 0;
va_start(va, format);
result = kernaux_vfprintf(&file, format, va);
va_end(va);
assert((size_t)result == strlen(expected));
assert(strcmp(expected, buffer) == 0);
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);
}
void test_main()
{
memset(buffer, '\0', sizeof(buffer));
buffer_index = 0;
kernaux_fprintf(&file, "Hello, World!");
assert(strcmp("Hello, World!", buffer) == 0);
{% for case in cases %}
{% if case.float %}
#ifdef ENABLE_FLOAT
{% endif %}
printf("%s\n", {{ escape_str(case.result) }});
test({{ escape_str(case.result) }}, {{ escape_str(fmt(case.args)) }}{{ values(case.args) }});
{% if case.float %}
#endif
{% endif %}
{% endfor %}
}