Luna/tests/libc/TestScanf.cpp

92 lines
1.6 KiB
C++

#include <stdio.h>
#include <string.h>
#include <test.h>
// FIXME: Add more tests.
TestResult test_basic_scanf()
{
char hello[21];
char world[21];
int parsed = sscanf("hello world", "%20s %20s", hello, world);
validate(parsed == 2);
validate(!strcmp(hello, "hello"));
validate(!strcmp(world, "world"));
test_success;
}
TestResult test_incomplete_scanf()
{
char hello[21];
char world[21];
int parsed = sscanf("hello ", "%20s %20s", hello, world);
validate(parsed == 1);
validate(!strcmp(hello, "hello"));
test_success;
}
TestResult test_integer_scanf()
{
int hour;
int min;
int parsed = sscanf("23:59", "%d:%d", &hour, &min);
validate(parsed == 2);
validate(hour == 23);
validate(min == 59);
test_success;
}
TestResult test_integer_auto_base_scanf()
{
int a;
int b;
int c;
int parsed = sscanf("65, \t0x23, 0755", "%i, %i, %i", &a, &b, &c);
validate(parsed == 3);
validate(a == 65);
validate(b == 0x23);
validate(c == 0755);
test_success;
}
TestResult test_scanf_characters_consumed()
{
int hour;
int min;
int nr_chars;
int parsed = sscanf("23:59", "%d:%d%n", &hour, &min, &nr_chars);
validate(parsed == 2);
validate(hour == 23);
validate(min == 59);
validate(nr_chars == 5);
test_success;
}
Result<void> test_main()
{
test_prelude;
run_test(test_basic_scanf);
run_test(test_incomplete_scanf);
run_test(test_integer_scanf);
run_test(test_integer_auto_base_scanf);
run_test(test_scanf_characters_consumed);
return {};
}