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
Text
Raw Normal View History

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2022-06-25 11:43:18 -04:00
#define KERNAUX_ACCESS_PROTECTED
2022-11-28 20:19:35 -05:00
#include <kernaux/macro.h>
2022-06-25 11:43:18 -04:00
#include <kernaux/printf.h>
#include <assert.h>
2022-06-25 11:43:18 -04:00
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
2022-11-26 14:04:36 -05:00
static const char *const data = "foobar";
static char buffer[BUFFER_SIZE];
static size_t buffer_index;
2022-12-03 05:23:52 -05:00
static void test_putc(char c, void *arg KERNAUX_UNUSED)
{
if (buffer_index >= BUFFER_SIZE) {
printf("Buffer overflow!\n");
abort();
}
2022-06-25 11:43:18 -04:00
buffer[buffer_index++] = c;
}
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);
2022-11-26 14:04:36 -05:00
result = kernaux_vfprintf(test_putc, (void*)data, format, va);
va_end(va);
2022-12-01 14:42:43 -05:00
printf("Exp: %s\n", expected);
printf("Got: %s\n\n", buffer);
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);
2022-12-01 14:42:43 -05:00
assert((size_t)result == strlen(expected));
assert(strcmp(expected, buffer) == 0);
}
2022-06-20 14:01:56 -04:00
void test_main()
{
memset(buffer, '\0', sizeof(buffer));
buffer_index = 0;
2022-11-26 14:04:36 -05:00
kernaux_fprintf(test_putc, (void*)data, "Hello, World!");
assert(strcmp("Hello, World!", buffer) == 0);
{% for case in cases %}
{% if case.float %}
#ifdef ENABLE_FLOAT
{% endif %}
test({{ escape_str(case.result) }}, {{ escape_str(fmt(case.args)) }}{{ values(case.args) }});
{% if case.float %}
#endif
{% endif %}
{% endfor %}
}