mirror of
				https://github.com/tailix/libkernaux.git
				synced 2025-10-30 23:47:50 -04:00 
			
		
		
		
	More tests for "printf"
This commit is contained in:
		
							parent
							
								
									edf40ddb83
								
							
						
					
					
						commit
						16c164754e
					
				
					 3 changed files with 41 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -7,18 +7,28 @@
 | 
			
		|||
 | 
			
		||||
#define BUFFER_SIZE 1024
 | 
			
		||||
 | 
			
		||||
static const char *const data = "foobar";
 | 
			
		||||
 | 
			
		||||
static char buffer[BUFFER_SIZE];
 | 
			
		||||
static size_t buffer_index = 0;
 | 
			
		||||
 | 
			
		||||
static void my_putchar(const char chr, void *arg __attribute__((unused)))
 | 
			
		||||
static void my_putchar(const char chr, void *arg)
 | 
			
		||||
{
 | 
			
		||||
    assert(arg == data);
 | 
			
		||||
    if (buffer_index >= BUFFER_SIZE) abort();
 | 
			
		||||
    buffer[buffer_index++] = chr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    kernaux_printf(my_putchar, NULL, "Hello, %s! Session ID: %u.", "Alex", 123);
 | 
			
		||||
    const int result = kernaux_printf(
 | 
			
		||||
        my_putchar,
 | 
			
		||||
        (char*)data,
 | 
			
		||||
        "Hello, %s! Session ID: %u.",
 | 
			
		||||
        "Alex",
 | 
			
		||||
        123
 | 
			
		||||
    );
 | 
			
		||||
    assert((size_t)result == strlen(buffer));
 | 
			
		||||
    assert(strcmp(buffer, "Hello, Alex! Session ID: 123.") == 0);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,26 +7,31 @@
 | 
			
		|||
 | 
			
		||||
#define BUFFER_SIZE 1024
 | 
			
		||||
 | 
			
		||||
static const char *const data = "foobar";
 | 
			
		||||
 | 
			
		||||
static char buffer[BUFFER_SIZE];
 | 
			
		||||
static size_t buffer_index = 0;
 | 
			
		||||
 | 
			
		||||
static void my_putchar(const char chr, void *arg __attribute__((unused)))
 | 
			
		||||
static void my_putchar(const char chr, void *arg)
 | 
			
		||||
{
 | 
			
		||||
    assert(arg == data);
 | 
			
		||||
    if (buffer_index >= BUFFER_SIZE) abort();
 | 
			
		||||
    buffer[buffer_index++] = chr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void my_printf(const char *const format, ...)
 | 
			
		||||
static int my_printf(const char *const format, ...)
 | 
			
		||||
{
 | 
			
		||||
    va_list va;
 | 
			
		||||
    va_start(va, format);
 | 
			
		||||
    kernaux_vprintf(my_putchar, NULL, format, va);
 | 
			
		||||
    const int result = kernaux_vprintf(my_putchar, (char*)data, format, va);
 | 
			
		||||
    va_end(va);
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    my_printf("Hello, %s! Session ID: %u.", "Alex", 123);
 | 
			
		||||
    const int result = my_printf("Hello, %s! Session ID: %u.", "Alex", 123);
 | 
			
		||||
    assert((size_t)result == strlen(buffer));
 | 
			
		||||
    assert(strcmp(buffer, "Hello, Alex! Session ID: 123.") == 0);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,13 +11,15 @@
 | 
			
		|||
 | 
			
		||||
#define BUFFER_SIZE 1024
 | 
			
		||||
 | 
			
		||||
static const char *const data = "foobar";
 | 
			
		||||
 | 
			
		||||
static char buffer[BUFFER_SIZE];
 | 
			
		||||
static size_t buffer_index;
 | 
			
		||||
 | 
			
		||||
static void test_putchar(
 | 
			
		||||
    const char chr,
 | 
			
		||||
    void *const arg __attribute__((unused))
 | 
			
		||||
) {
 | 
			
		||||
static void test_putchar(const char chr, void *const arg)
 | 
			
		||||
{
 | 
			
		||||
    assert(arg == data);
 | 
			
		||||
 | 
			
		||||
    if (buffer_index >= BUFFER_SIZE) {
 | 
			
		||||
        printf("Buffer overflow!\n");
 | 
			
		||||
        abort();
 | 
			
		||||
| 
						 | 
				
			
			@ -28,12 +30,23 @@ static void test_putchar(
 | 
			
		|||
 | 
			
		||||
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_list va;
 | 
			
		||||
    va_start(va, format);
 | 
			
		||||
    kernaux_vprintf(test_putchar, NULL, format, va);
 | 
			
		||||
    result = kernaux_vprintf(test_putchar, (char*)data, 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);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +54,7 @@ int main()
 | 
			
		|||
{
 | 
			
		||||
    memset(buffer, '\0', sizeof(buffer));
 | 
			
		||||
    buffer_index = 0;
 | 
			
		||||
    kernaux_printf(test_putchar, NULL, "Hello, World!");
 | 
			
		||||
    kernaux_printf(test_putchar, (char*)data, "Hello, World!");
 | 
			
		||||
    assert(strcmp("Hello, World!", buffer) == 0);
 | 
			
		||||
 | 
			
		||||
    test("", "");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue