From a0517fb9dd8adb7bd9cd9fac2b141646c733e249 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 21 Jun 2022 21:18:28 +0300 Subject: [PATCH] Cross-platform tests (#86) --- .github/workflows/main.yml | 23 +++++++++++++ .gitignore | 1 + bindings/mruby/.rubocop.yml | 4 +++ bindings/mruby/test/printf.rb | 8 ++++- .../spec/lib/kernaux/printf/sprintf_spec.rb | 8 ++++- common/printf_orig.yml | 2 +- configure.ac | 2 -- tests/Makefile.am | 12 +++++++ tests/printf_gen.py | 7 +++- tests/test_printf_reg.c | 34 +++++++++++++++++++ 10 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 tests/test_printf_reg.c diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 126e8ab..5e1c907 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -29,6 +29,29 @@ jobs: - name: install run: sudo make install + cross: + runs-on: ubuntu-latest + strategy: + matrix: + cross: + - host: 'i386-unknown-elf' + apt: 'crossbuild-essential-i386' + cprefix: 'i686-linux-gnu-' + steps: + - uses: actions/checkout@v2 + - name: dependencies + run: sudo apt-get --yes install ${{matrix.cross.apt}} + - name: autogen + run: ./autogen.sh + - name: configure + run: ./configure --host='${{matrix.cross.host}}' --enable-tests --enable-tests-python AR='${{matrix.cross.cprefix}}ar' CC='${{matrix.cross.cprefix}}gcc' LD='${{matrix.cross.cprefix}}ld' RANLIB='${{matrix.cross.cprefix}}ranlib' + - name: make + run: make + - name: check + run: make check || (./test-suite-log && false) + - name: install + run: sudo make install + cond: runs-on: ubuntu-latest strategy: diff --git a/.gitignore b/.gitignore index fa56d74..8f45376 100644 --- a/.gitignore +++ b/.gitignore @@ -147,4 +147,5 @@ /tests/test_printf_fmt_gen.c /tests/test_printf_gen /tests/test_printf_gen.c +/tests/test_printf_reg /tests/test_units_human diff --git a/bindings/mruby/.rubocop.yml b/bindings/mruby/.rubocop.yml index 1b268e5..084bd23 100644 --- a/bindings/mruby/.rubocop.yml +++ b/bindings/mruby/.rubocop.yml @@ -25,6 +25,10 @@ Metrics/BlockLength: - 'Rakefile' - 'test/**/*.rb' +Metrics/BlockNesting: + Exclude: + - 'test/printf.rb' + Security/Eval: Exclude: - 'test/**/*.rb' diff --git a/bindings/mruby/test/printf.rb b/bindings/mruby/test/printf.rb index 5cffaab..922567e 100644 --- a/bindings/mruby/test/printf.rb +++ b/bindings/mruby/test/printf.rb @@ -56,7 +56,13 @@ if KernAux::Version.with_printf? else arg.map do |item| if item.is_a? Array - item[0] + if item.length == 1 + item[0] + elsif item[0] == 'long long' + item[1] + else + raise "Unknown format: #{args.inspect}" + end else item end diff --git a/bindings/ruby/spec/lib/kernaux/printf/sprintf_spec.rb b/bindings/ruby/spec/lib/kernaux/printf/sprintf_spec.rb index 7010b39..ba11072 100644 --- a/bindings/ruby/spec/lib/kernaux/printf/sprintf_spec.rb +++ b/bindings/ruby/spec/lib/kernaux/printf/sprintf_spec.rb @@ -36,7 +36,13 @@ KernAux::Version.with_printf? and RSpec.describe KernAux, '.sprintf' do else arg.map do |item| if item.is_a? Array - item[0] + if item.length == 1 + item[0] + elsif item[0] == 'long long' + item[1] + else + raise "Unknown format: #{args.inspect}" + end else item end diff --git a/common/printf_orig.yml b/common/printf_orig.yml index 31513d6..5a4d828 100644 --- a/common/printf_orig.yml +++ b/common/printf_orig.yml @@ -196,7 +196,7 @@ - result: '0' args: [['%#.1x', 0]] - result: '' - args: [['%#.0llx', 0]] + args: [['%#.0llx', ['long long', 0]]] - result: '0x0000614e' args: [['%#.8x', 0x614e]] - result: '0b110' diff --git a/configure.ac b/configure.ac index 6e082a2..0907007 100644 --- a/configure.ac +++ b/configure.ac @@ -151,8 +151,6 @@ AS_IF([test "$with_libc" = yes], [with_libc=yes], [with_libc # Test args # ############# -AS_IF([test "$enable_tests" = yes -a "$host_cpu" != "$build_cpu" ], AC_MSG_ERROR([can not build cross-platform tests])) -AS_IF([test "$enable_tests_python" = yes -a "$host_cpu" != "$build_cpu" ], AC_MSG_ERROR([can not build cross-platform tests])) AS_IF([test "$enable_tests" = yes -a "$enable_freestanding" = yes], AC_MSG_ERROR([can not build freestanding tests])) AS_IF([test "$enable_tests_python" = yes -a "$enable_freestanding" = yes], AC_MSG_ERROR([can not build freestanding tests])) AS_IF([test "$enable_tests" = yes -a "$with_libc" = yes], AC_MSG_ERROR([can not use package `libc' with tests])) diff --git a/tests/Makefile.am b/tests/Makefile.am index 6031286..b678d57 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -335,6 +335,18 @@ CLEANFILES += test_printf_gen.c test_printf_gen.c: printf_gen.py printf_gen.jinja $(top_srcdir)/common/printf.yml $(top_srcdir)/common/printf_orig.yml python3 printf_gen.py +################### +# test_printf_reg # +################### + +if WITH_PRINTF +TESTS += test_printf_reg +test_printf_reg_LDADD = $(top_builddir)/libkernaux.la +test_printf_reg_SOURCES = \ + main.c \ + test_printf_reg.c +endif + #################### # test_units_human # #################### diff --git a/tests/printf_gen.py b/tests/printf_gen.py index 343e792..bc322a1 100644 --- a/tests/printf_gen.py +++ b/tests/printf_gen.py @@ -65,7 +65,12 @@ def values(args): if type(arg[1]) is str: values += ', ' + escape_str(arg[1]) elif type(arg[1]) is list: - values += ', ' + escape_char(arg[1][0]) + if len(arg[1]) == 1: + values += ', ' + escape_char(arg[1][0]) + elif arg[1][0] == 'long long': + values += ', (long long)' + str(arg[1][1]) + else: + raise RuntimeError('unknown format: ' + str(args)) else: values += ', ' + str(arg[1]) diff --git a/tests/test_printf_reg.c b/tests/test_printf_reg.c new file mode 100644 index 0000000..75f8ec5 --- /dev/null +++ b/tests/test_printf_reg.c @@ -0,0 +1,34 @@ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include +#include +#include + +void test_main() +{ + char buffer[1000]; + + // Sanity check + { + memset(buffer, 0xff, sizeof(buffer)); + const int result = + kernaux_snprintf(buffer, sizeof(buffer), "%s", "Hello, World!"); + fprintf(stderr, "%d:%s\n", result, buffer); + assert(result == 13); + assert(strcmp(buffer, "Hello, World!") == 0); + } + + // i386 requires "(long long)0" instead of just "0" + { + memset(buffer, 0xff, sizeof(buffer)); + const int result = + kernaux_snprintf(buffer, sizeof(buffer), "%#.0llx", (long long)0); + fprintf(stderr, "%d:%s\n", result, buffer); + assert(result == 0); + assert(strcmp(buffer, "") == 0); + } +}