Compare commits

...

4 Commits

Author SHA1 Message Date
dc7e503342
tests: Test character formatting
All checks were successful
continuous-integration/drone/push Build is passing
2023-05-13 12:50:10 +02:00
e481ce45d0
tests: Add way more tests to TestFormat now that formatting is fixed 2023-05-13 12:45:14 +02:00
e098a3269a
libluna: Treat a negative precision as if precision was omitted 2023-05-13 12:40:03 +02:00
38541e22e9
libluna: Fix flag shenanigans in cstyle_format
Finally, zero-pad and left-align work properly!
2023-05-13 12:39:31 +02:00
2 changed files with 203 additions and 10 deletions

View File

@ -8,16 +8,16 @@
extern "C" usize strlen(const char*);
typedef int flags_t;
#define FLAG_ZERO_PAD 1 << 0
#define FLAG_LEFT_ALIGN 1 << 1
#define FLAG_BLANK_SIGNED 1 << 2
#define FLAG_ALTERNATE 1 << 3
#define FLAG_SIGN 1 << 4
#define FLAG_USE_PRECISION 1 << 5
#define FLAG_LONG 1 << 6
#define FLAG_LONG_LONG 1 << 7
#define FLAG_SHORT 1 << 8
#define FLAG_CHAR 1 << 9
#define FLAG_ZERO_PAD (1 << 0)
#define FLAG_LEFT_ALIGN (1 << 1)
#define FLAG_BLANK_SIGNED (1 << 2)
#define FLAG_ALTERNATE (1 << 3)
#define FLAG_SIGN (1 << 4)
#define FLAG_USE_PRECISION (1 << 5)
#define FLAG_LONG (1 << 6)
#define FLAG_LONG_LONG (1 << 7)
#define FLAG_SHORT (1 << 8)
#define FLAG_CHAR (1 << 9)
struct format_state
{
@ -139,7 +139,10 @@ static usize parse_precision(const char** format, flags_t& flags, va_list ap)
const int precision = va_arg(ap, int);
if (precision >= 0) result = (usize)precision;
else
{
result = 0;
flags &= ~FLAG_USE_PRECISION;
}
(*format)++;
}
}

View File

@ -82,6 +82,15 @@ TestResult test_positive_signed_integer_format()
test_success;
}
TestResult test_positive_signed_integer_format_with_empty_sign()
{
auto fmt = TRY(String::format("% d"_sv, 653));
validate(fmt.view() == " 653");
test_success;
}
TestResult test_format_zero_with_explicit_zero_precision()
{
auto fmt = TRY(String::format("%.0d"_sv, 0));
@ -118,6 +127,168 @@ TestResult test_uppercase_hex_format()
test_success;
}
TestResult test_integer_format_with_width()
{
auto fmt = TRY(String::format("%6d"_sv, 42));
validate(fmt.view() == " 42");
test_success;
}
TestResult test_zero_padded_integer_format_with_width()
{
auto fmt = TRY(String::format("%06d"_sv, 42));
validate(fmt.view() == "000042");
test_success;
}
TestResult test_integer_format_with_precision()
{
auto fmt = TRY(String::format("%.6d"_sv, 42));
validate(fmt.view() == "000042");
test_success;
}
TestResult test_alternate_integer_format_with_width()
{
auto fmt = TRY(String::format("%#6x"_sv, 66));
validate(fmt.view() == " 0x42");
test_success;
}
TestResult test_zero_padded_alternate_integer_format_with_width()
{
auto fmt = TRY(String::format("%0#6x"_sv, 66));
validate(fmt.view() == "0x0042");
test_success;
}
TestResult test_alternate_integer_format_with_precision()
{
auto fmt = TRY(String::format("%#.6x"_sv, 66));
validate(fmt.view() == "0x000042");
test_success;
}
TestResult test_integer_format_left_align()
{
auto fmt = TRY(String::format("%-6d"_sv, 42));
validate(fmt.view() == "42 ");
test_success;
}
TestResult test_integer_format_left_align_override_zero_pad()
{
auto fmt = TRY(String::format("%-06d"_sv, 42));
validate(fmt.view() == "42 ");
test_success;
}
TestResult test_integer_format_left_align_with_alternate_format()
{
auto fmt = TRY(String::format("%#-6o"_sv, 0755));
validate(fmt.view() == "0755 ");
test_success;
}
TestResult test_integer_format_left_align_with_alternate_format_and_precision()
{
auto fmt = TRY(String::format("%#-8.5x"_sv, 0x44bb));
validate(fmt.view() == "0x044bb ");
test_success;
}
TestResult test_va_integer_width()
{
auto fmt = TRY(String::format("%0*d"_sv, 4, 42));
validate(fmt.view() == "0042");
test_success;
}
TestResult test_va_integer_precision()
{
auto fmt = TRY(String::format("%0.*d"_sv, 4, 42));
validate(fmt.view() == "0042");
test_success;
}
TestResult test_no_precision_assumed_as_zero()
{
auto fmt = TRY(String::format("%.d"_sv, 42));
validate(fmt.view() == "42");
test_success;
}
TestResult test_no_precision_assumed_as_explicit_zero()
{
auto fmt = TRY(String::format("%.d"_sv, 0));
validate(fmt.view() == "");
test_success;
}
TestResult test_negative_va_width()
{
auto fmt = TRY(String::format("%*d"_sv, -4, 42));
validate(fmt.view() == "42 ");
test_success;
}
TestResult test_negative_va_precision()
{
auto fmt = TRY(String::format("%.*d"_sv, -6, 0));
validate(fmt.view() == "0");
test_success;
}
TestResult test_verbatim_percent_format()
{
auto fmt = TRY(String::format("%%"_sv));
validate(fmt.view() == "%");
test_success;
}
TestResult test_format_character()
{
auto fmt = TRY(String::format("exampl%c"_sv, 'e'));
validate(fmt.view() == "example");
test_success;
}
Result<void> test_main()
{
test_prelude;
@ -131,10 +302,29 @@ Result<void> test_main()
run_test(test_basic_signed_integer_format);
run_test(test_negative_signed_integer_format);
run_test(test_positive_signed_integer_format);
run_test(test_positive_signed_integer_format_with_empty_sign);
run_test(test_format_zero_with_explicit_zero_precision);
run_test(test_octal_format);
run_test(test_hex_format);
run_test(test_uppercase_hex_format);
run_test(test_integer_format_with_width);
run_test(test_zero_padded_integer_format_with_width);
run_test(test_integer_format_with_precision);
run_test(test_alternate_integer_format_with_width);
run_test(test_zero_padded_alternate_integer_format_with_width);
run_test(test_alternate_integer_format_with_precision);
run_test(test_integer_format_left_align);
run_test(test_integer_format_left_align_override_zero_pad);
run_test(test_integer_format_left_align_with_alternate_format);
run_test(test_integer_format_left_align_with_alternate_format_and_precision);
run_test(test_va_integer_width);
run_test(test_va_integer_precision);
run_test(test_no_precision_assumed_as_zero);
run_test(test_no_precision_assumed_as_explicit_zero);
run_test(test_negative_va_width);
run_test(test_negative_va_precision);
run_test(test_verbatim_percent_format);
run_test(test_format_character);
return {};
}