1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2025-02-17 15:45:32 -05:00

Add basic implementation and tests to "kernaux_printf_va"

This commit is contained in:
Alex Kotov 2020-12-07 03:04:46 +05:00
parent ceff5cf197
commit 51d58ad366
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 43 additions and 2 deletions

View file

@ -3,7 +3,7 @@
#include <kernaux/printf.h>
#include <kernaux/stdlib.h>
void kernaux_printf(void (*putchar)(char), const char *format, ...)
void kernaux_printf(void (*putchar)(char), const char *const format, ...)
{
va_list va;
va_start(va, format);
@ -13,7 +13,14 @@ void kernaux_printf(void (*putchar)(char), const char *format, ...)
void kernaux_printf_va(
void (*const putchar)(char),
const char *format,
const char *const format,
va_list va
) {
for (const char *current_ptr = format; *current_ptr; ++current_ptr) {
const char current = *current_ptr;
putchar(current);
}
putchar('\0');
}

Binary file not shown.

View file

@ -1,6 +1,40 @@
#include <kernaux/printf.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
static char buffer[BUFFER_SIZE];
static unsigned int buffer_index;
static void test_putchar(const char chr)
{
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, ...)
{
memset(buffer, '\0', sizeof(buffer));
buffer_index = 0;
va_list va;
va_start(va, format);
kernaux_printf_va(test_putchar, format, va);
va_end(va);
assert(strcmp(expected, buffer) == 0);
}
int main()
{
test("", "");
test("Hello, World!", "Hello, World!");
return 0;
}