mirror of
https://github.com/tailix/libkernaux.git
synced 2025-03-31 17:25:22 -04:00
Common: generate regular tests for printf
This commit is contained in:
parent
74128be3ff
commit
8f1fb23f5b
7 changed files with 143 additions and 65 deletions
|
@ -135,6 +135,7 @@ if ENABLE_TESTS_PYTHON
|
|||
TESTS += tests/test_printf_gen
|
||||
noinst_SCRIPTS = tests/printf_gen.py
|
||||
noinst_DATA = \
|
||||
tests/printf.yml \
|
||||
tests/printf_orig.yml
|
||||
endif
|
||||
endif
|
||||
|
@ -281,5 +282,5 @@ tests_test_units_human_SOURCES = \
|
|||
$(libkernaux_a_SOURCES) \
|
||||
tests/test_units_human.c
|
||||
|
||||
tests/test_printf_gen.c: tests/printf_gen.py tests/printf_orig.yml
|
||||
tests/test_printf_gen.c: tests/printf_gen.py tests/printf.yml tests/printf_orig.yml
|
||||
python3 tests/printf_gen.py
|
||||
|
|
|
@ -10,29 +10,34 @@ RSpec.describe KernAux, '.sprintf' do
|
|||
it { is_expected.to be_frozen }
|
||||
it { is_expected.to eq 'Hello, World!' }
|
||||
|
||||
context 'using original tests' do
|
||||
printf_yml =
|
||||
File.expand_path('../../../../../tests/printf_orig.yml', __dir__)
|
||||
[
|
||||
['', 'using regular tests'],
|
||||
['_orig', 'using original tests'],
|
||||
].each do |(suffix, description)|
|
||||
context description do
|
||||
printf_yml =
|
||||
File.expand_path("../../../../../tests/printf#{suffix}.yml", __dir__)
|
||||
|
||||
YAML.safe_load_file(printf_yml).each do |test|
|
||||
expected = test['result']
|
||||
YAML.safe_load_file(printf_yml).each do |test|
|
||||
expected = test['result']
|
||||
|
||||
args = test['args'].map do |arg|
|
||||
if arg.is_a? String
|
||||
arg
|
||||
else
|
||||
arg.map do |item|
|
||||
if item.is_a? Array
|
||||
item[0]
|
||||
else
|
||||
item
|
||||
args = test['args'].map do |arg|
|
||||
if arg.is_a? String
|
||||
arg
|
||||
else
|
||||
arg.map do |item|
|
||||
if item.is_a? Array
|
||||
item[0]
|
||||
else
|
||||
item
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "transforms #{args.inspect} to #{expected.inspect}" do
|
||||
expect(described_class.sprintf(*args)).to eq expected
|
||||
it "transforms #{args.inspect} to #{expected.inspect}" do
|
||||
expect(described_class.sprintf(*args)).to eq expected
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
95
tests/printf.yml
Normal file
95
tests/printf.yml
Normal file
|
@ -0,0 +1,95 @@
|
|||
- result: ''
|
||||
args: []
|
||||
- result: ''
|
||||
args: ['']
|
||||
- result: ''
|
||||
args: ['', '']
|
||||
|
||||
- result: 'Hello, World!'
|
||||
args: ['Hello, World!']
|
||||
- result: 'Hello, Alex!'
|
||||
args: ['Hello, ', ['%s', 'Alex'], '!']
|
||||
- result: 'Hello, Alex!'
|
||||
args: ['Hello, ', ['%c', ['A']], ['%c', ['l']], ['%c', ['e']], ['%c', ['x']], '!']
|
||||
|
||||
- result: '%'
|
||||
args: [['%%']]
|
||||
- result: '%%'
|
||||
args: [['%%'], ['%%']]
|
||||
- result: '%%%'
|
||||
args: [['%%'], ['%%'], ['%%']]
|
||||
|
||||
- result: '%'
|
||||
args: [['%*%', 20]]
|
||||
|
||||
- result: '123'
|
||||
args: [['%u', 123]]
|
||||
- result: '123456'
|
||||
args: [['%u', 123], ['%u', 456]]
|
||||
- result: 'foo'
|
||||
args: [['%s', 'foo']]
|
||||
- result: 'foobar'
|
||||
args: [['%s', 'foo'], ['%s', 'bar']]
|
||||
- result: 'a'
|
||||
args: [['%c', ['a']]]
|
||||
- result: 'ab'
|
||||
args: [['%c', ['a']], ['%c', ['b']]]
|
||||
- result: 'abc'
|
||||
args: [['%c', ['a']], ['%c', ['b']], ['%c', ['c']]]
|
||||
|
||||
- result: '%123fooa'
|
||||
args: [['%%'], ['%u', 123], ['%s', 'foo'], ['%c', ['a']]]
|
||||
- result: '%123afoo'
|
||||
args: [['%%'], ['%u', 123], ['%c', ['a']], ['%s', 'foo']]
|
||||
- result: '%a123foo'
|
||||
args: [['%%'], ['%c', ['a']], ['%u', 123], ['%s', 'foo']]
|
||||
- result: '%afoo123'
|
||||
args: [['%%'], ['%c', ['a']], ['%s', 'foo'], ['%u', 123]]
|
||||
- result: '%foo123a'
|
||||
args: [['%%'], ['%s', 'foo'], ['%u', 123], ['%c', ['a']]]
|
||||
- result: '%fooa123'
|
||||
args: [['%%'], ['%s', 'foo'], ['%c', ['a']], ['%u', 123]]
|
||||
- result: '123%fooa'
|
||||
args: [['%u', 123], ['%%'], ['%s', 'foo'], ['%c', ['a']]]
|
||||
- result: '123%afoo'
|
||||
args: [['%u', 123], ['%%'], ['%c', ['a']], ['%s', 'foo']]
|
||||
- result: 'a%123foo'
|
||||
args: [['%c', ['a']], ['%%'], ['%u', 123], ['%s', 'foo']]
|
||||
- result: 'a%foo123'
|
||||
args: [['%c', ['a']], ['%%'], ['%s', 'foo'], ['%u', 123]]
|
||||
- result: 'foo%123a'
|
||||
args: [['%s', 'foo'], ['%%'], ['%u', 123], ['%c', ['a']]]
|
||||
- result: 'foo%a123'
|
||||
args: [['%s', 'foo'], ['%%'], ['%c', ['a']], ['%u', 123]]
|
||||
- result: '123foo%a'
|
||||
args: [['%u', 123], ['%s', 'foo'], ['%%'], ['%c', ['a']]]
|
||||
- result: '123a%foo'
|
||||
args: [['%u', 123], ['%c', ['a']], ['%%'], ['%s', 'foo']]
|
||||
- result: 'a123%foo'
|
||||
args: [['%c', ['a']], ['%u', 123], ['%%'], ['%s', 'foo']]
|
||||
- result: 'afoo%123'
|
||||
args: [['%c', ['a']], ['%s', 'foo'], ['%%'], ['%u', 123]]
|
||||
- result: 'foo123%a'
|
||||
args: [['%s', 'foo'], ['%u', 123], ['%%'], ['%c', ['a']]]
|
||||
- result: 'fooa%123'
|
||||
args: [['%s', 'foo'], ['%c', ['a']], ['%%'], ['%u', 123]]
|
||||
- result: '123fooa%'
|
||||
args: [['%u', 123], ['%s', 'foo'], ['%c', ['a']], ['%%']]
|
||||
- result: '123afoo%'
|
||||
args: [['%u', 123], ['%c', ['a']], ['%s', 'foo'], ['%%']]
|
||||
- result: 'a123foo%'
|
||||
args: [['%c', ['a']], ['%u', 123], ['%s', 'foo'], ['%%']]
|
||||
- result: 'afoo123%'
|
||||
args: [['%c', ['a']], ['%s', 'foo'], ['%u', 123], ['%%']]
|
||||
- result: 'foo123a%'
|
||||
args: [['%s', 'foo'], ['%u', 123], ['%c', ['a']], ['%%']]
|
||||
- result: 'fooa123%'
|
||||
args: [['%s', 'foo'], ['%c', ['a']], ['%u', 123], ['%%']]
|
||||
|
||||
- result: '1.200000'
|
||||
args: [['%f', 1.2]]
|
||||
float: true
|
||||
|
||||
- result: '123.456789'
|
||||
args: [['%f', 123.456789]]
|
||||
float: true
|
|
@ -52,10 +52,21 @@ static void test(const char *const expected, const char *const format, ...)
|
|||
|
||||
int main()
|
||||
{
|
||||
memset(buffer, '\0', sizeof(buffer));
|
||||
buffer_index = 0;
|
||||
kernaux_printf(test_putchar, (char*)data, "Hello, World!");
|
||||
assert(strcmp("Hello, World!", buffer) == 0);
|
||||
|
||||
{% for case in cases %}
|
||||
{% if not case.fixme %}
|
||||
{% if case.float %}
|
||||
#ifdef ENABLE_FLOAT
|
||||
{% endif %}
|
||||
printf("%s\n", {{ escape_str(case.result) }});
|
||||
test({{ escape_str(case.result) }}, {{ escape_str(fmt(case.args)) }}{{ values(case.args) }});
|
||||
{% if case.float %}
|
||||
#endif
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
|
|
|
@ -3,17 +3,21 @@ from os import path
|
|||
from yaml import SafeLoader, safe_load
|
||||
|
||||
CASES_ORIG_FILENAME = 'printf_orig.yml'
|
||||
CASES_REG_FILENAME = 'printf.yml'
|
||||
TEMPLATE_FILENAME = 'printf_gen.jinja'
|
||||
TEST_FILENAME = 'test_printf_gen.c'
|
||||
|
||||
TESTS_DIRPATH = path.dirname(path.abspath(__file__))
|
||||
|
||||
CASES_ORIG_FILEPATH = path.join(TESTS_DIRPATH, CASES_ORIG_FILENAME)
|
||||
CASES_REG_FILEPATH = path.join(TESTS_DIRPATH, CASES_REG_FILENAME)
|
||||
TEST_FILEPATH = path.join(TESTS_DIRPATH, TEST_FILENAME)
|
||||
|
||||
def main():
|
||||
cases_orig = safe_load(open(CASES_ORIG_FILEPATH))
|
||||
cases = cases_orig
|
||||
cases_reg = safe_load(open(CASES_REG_FILEPATH))
|
||||
|
||||
cases = cases_reg + cases_orig
|
||||
|
||||
jinja_env = Environment(
|
||||
keep_trailing_newline=True,
|
||||
|
|
|
@ -38,8 +38,10 @@
|
|||
args: [['% 15d', -42]]
|
||||
- result: ' -42.987'
|
||||
args: [['% 15.3f', -42.987]]
|
||||
float: true
|
||||
- result: ' 42.987'
|
||||
args: [['% 15.3f', 42.987]]
|
||||
float: true
|
||||
- result: 'Hello testing'
|
||||
args: [['% s', 'Hello testing']]
|
||||
- result: ' 1024'
|
||||
|
@ -132,10 +134,13 @@
|
|||
args: [['%015d', -42]]
|
||||
- result: '000000000042.12'
|
||||
args: [['%015.2f', 42.1234]]
|
||||
float: true
|
||||
- result: '00000000042.988'
|
||||
args: [['%015.3f', 42.9876]]
|
||||
float: true
|
||||
- result: '-00000042.98760'
|
||||
args: [['%015.5f', -42.9876]]
|
||||
float: true
|
||||
|
||||
# - flag
|
||||
|
||||
|
@ -177,8 +182,10 @@
|
|||
args: [['%0-15d', -42]]
|
||||
- result: '-4.200e+01 '
|
||||
args: [['%0-15.3e', -42.0]]
|
||||
float: true
|
||||
- result: '-42.0 '
|
||||
args: [['%0-15.3g', -42.0]]
|
||||
float: true
|
||||
|
||||
# # flag
|
||||
|
||||
|
@ -340,6 +347,7 @@
|
|||
args: [['%-20u', 1024]]
|
||||
- result: '1024.1234 '
|
||||
args: [['%-20.4f', 1024.1234]]
|
||||
float: true
|
||||
- result: '4294966272 '
|
||||
args: [['%-20u', 4_294_966_272]]
|
||||
- result: '777 '
|
||||
|
|
|
@ -61,52 +61,6 @@ int main()
|
|||
kernaux_printf(test_putchar, (char*)data, "Hello, World!");
|
||||
assert(strcmp("Hello, World!", buffer) == 0);
|
||||
|
||||
test("", "");
|
||||
test("Hello, World!", "Hello, World!");
|
||||
test("Hello, Alex!", "Hello, %s!", "Alex");
|
||||
test("Hello, Alex!", "Hello, %c%c%c%c!", 'A', 'l', 'e', 'x');
|
||||
test("%", "%%");
|
||||
test("%%", "%%%%");
|
||||
test("%%%", "%%%%%%");
|
||||
test("123", "%u", 123);
|
||||
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);
|
||||
|
||||
#ifdef ENABLE_FLOAT
|
||||
test("1.200000", "%f", 1.2);
|
||||
test("123.456789", "%f", 123.456789);
|
||||
#endif
|
||||
|
||||
test("%", "%*%", 20);
|
||||
|
||||
// - flag
|
||||
// ...
|
||||
test("42", "%0-d", 42);
|
||||
|
|
Loading…
Add table
Reference in a new issue