2020-12-06 23:47:53 -05:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2020-12-06 16:37:53 -05:00
|
|
|
#include <kernaux/printf.h>
|
|
|
|
|
2020-12-06 17:04:46 -05:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 1024
|
|
|
|
|
2022-01-20 06:58:08 -05:00
|
|
|
static const char *const data = "foobar";
|
|
|
|
|
2020-12-06 17:04:46 -05:00
|
|
|
static char buffer[BUFFER_SIZE];
|
2021-12-16 10:26:16 -05:00
|
|
|
static size_t buffer_index;
|
2020-12-06 17:04:46 -05:00
|
|
|
|
2022-01-20 06:58:08 -05:00
|
|
|
static void test_putchar(const char chr, void *const arg)
|
|
|
|
{
|
|
|
|
assert(arg == data);
|
|
|
|
|
2020-12-06 17:04:46 -05:00
|
|
|
if (buffer_index >= BUFFER_SIZE) {
|
|
|
|
printf("Buffer overflow!\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[buffer_index++] = chr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test(const char *const expected, const char *const format, ...)
|
|
|
|
{
|
2022-01-20 06:58:08 -05:00
|
|
|
va_list va;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
memset(buffer, '\0', sizeof(buffer));
|
|
|
|
buffer_index = 0;
|
|
|
|
va_start(va, format);
|
|
|
|
result = kernaux_vprintf(test_putchar, (char*)data, format, va);
|
|
|
|
va_end(va);
|
|
|
|
assert((size_t)result == strlen(expected));
|
|
|
|
assert(strcmp(expected, buffer) == 0);
|
|
|
|
|
2020-12-06 17:04:46 -05:00
|
|
|
memset(buffer, '\0', sizeof(buffer));
|
|
|
|
buffer_index = 0;
|
|
|
|
va_start(va, format);
|
2022-01-20 06:58:08 -05:00
|
|
|
result = kernaux_vsnprintf(buffer, sizeof(buffer), format, va);
|
2020-12-06 17:04:46 -05:00
|
|
|
va_end(va);
|
2022-01-20 06:58:08 -05:00
|
|
|
assert((size_t)result == strlen(expected));
|
2020-12-06 17:04:46 -05:00
|
|
|
assert(strcmp(expected, buffer) == 0);
|
|
|
|
}
|
|
|
|
|
2020-12-06 16:37:53 -05:00
|
|
|
int main()
|
|
|
|
{
|
2020-12-06 17:07:12 -05:00
|
|
|
memset(buffer, '\0', sizeof(buffer));
|
|
|
|
buffer_index = 0;
|
2022-01-20 06:58:08 -05:00
|
|
|
kernaux_printf(test_putchar, (char*)data, "Hello, World!");
|
2020-12-06 17:07:12 -05:00
|
|
|
assert(strcmp("Hello, World!", buffer) == 0);
|
|
|
|
|
2020-12-06 17:04:46 -05:00
|
|
|
test("", "");
|
|
|
|
test("Hello, World!", "Hello, World!");
|
2020-12-06 18:58:34 -05:00
|
|
|
test("Hello, Alex!", "Hello, %s!", "Alex");
|
|
|
|
test("Hello, Alex!", "Hello, %c%c%c%c!", 'A', 'l', 'e', 'x');
|
2020-12-06 19:00:24 -05:00
|
|
|
test("%", "%%");
|
2020-12-06 19:49:18 -05:00
|
|
|
test("%%", "%%%%");
|
|
|
|
test("%%%", "%%%%%%");
|
2020-12-06 19:04:24 -05:00
|
|
|
test("123", "%u", 123);
|
2020-12-06 19:49:18 -05:00
|
|
|
test("123456", "%u%u", 123, 456);
|
|
|
|
test("foo", "%s", "foo");
|
|
|
|
test("foobar", "%s%s", "foo", "bar");
|
|
|
|
test("a", "%c", 'a');
|
|
|
|
test("ab", "%c%c", 'a', 'b');
|
|
|
|
test("abc", "%c%c%c", 'a', 'b', 'c');
|
|
|
|
test("%123fooa", "%%%u%s%c", 123, "foo", 'a');
|
|
|
|
test("%123afoo", "%%%u%c%s", 123, 'a', "foo");
|
|
|
|
test("%a123foo", "%%%c%u%s", 'a', 123, "foo");
|
|
|
|
test("%afoo123", "%%%c%s%u", 'a', "foo", 123);
|
|
|
|
test("%foo123a", "%%%s%u%c", "foo", 123, 'a');
|
|
|
|
test("%fooa123", "%%%s%c%u", "foo", 'a', 123);
|
|
|
|
test("123%fooa", "%u%%%s%c", 123, "foo", 'a');
|
|
|
|
test("123%afoo", "%u%%%c%s", 123, 'a', "foo");
|
|
|
|
test("a%123foo", "%c%%%u%s", 'a', 123, "foo");
|
|
|
|
test("a%foo123", "%c%%%s%u", 'a', "foo", 123);
|
|
|
|
test("foo%123a", "%s%%%u%c", "foo", 123, 'a');
|
|
|
|
test("foo%a123", "%s%%%c%u", "foo", 'a', 123);
|
|
|
|
test("123foo%a", "%u%s%%%c", 123, "foo", 'a');
|
|
|
|
test("123a%foo", "%u%c%%%s", 123, 'a', "foo");
|
|
|
|
test("a123%foo", "%c%u%%%s", 'a', 123, "foo");
|
|
|
|
test("afoo%123", "%c%s%%%u", 'a', "foo", 123);
|
|
|
|
test("foo123%a", "%s%u%%%c", "foo", 123, 'a');
|
|
|
|
test("fooa%123", "%s%c%%%u", "foo", 'a', 123);
|
|
|
|
test("123fooa%", "%u%s%c%%", 123, "foo", 'a');
|
|
|
|
test("123afoo%", "%u%c%s%%", 123, 'a', "foo");
|
|
|
|
test("a123foo%", "%c%u%s%%", 'a', 123, "foo");
|
|
|
|
test("afoo123%", "%c%s%u%%", 'a', "foo", 123);
|
|
|
|
test("foo123a%", "%s%u%c%%", "foo", 123, 'a');
|
|
|
|
test("fooa123%", "%s%c%u%%", "foo", 'a', 123);
|
2020-12-06 17:04:46 -05:00
|
|
|
|
2022-01-20 18:45:18 -05:00
|
|
|
#ifdef ENABLE_FLOAT
|
|
|
|
test("1.200000", "%f", 1.2);
|
|
|
|
test("123.456789", "%f", 123.456789);
|
|
|
|
#endif
|
|
|
|
|
2020-12-06 16:37:53 -05:00
|
|
|
return 0;
|
|
|
|
}
|