/** \file camlibs/ptp2/n-param-test.c * \brief Test the __VA_ARGS__ and va_args related hacks for ptp_init_container() * * The ptp_init_container() function and the PTPContainer type are a * bit ugly (like assuming consecutive struct members are laid out * like an array), so this makes sure that at least the macros calling * ptp_init_container() actually do what they are intended to do when * counting the arguments and passing them on. * * \author Copyright (C) 2021 Hans Ulrich Niedermann * * \copyright GNU Lesser General Public License 2 or later * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA * * Usage: * $ clang -std=c99 -pedantic -Wall -Wextra -Weverything -o n-param-test-clang n-param-test.c && ./n-param-test-clang * $ gcc -std=c99 -pedantic -Wall -Wextra -o n-param-test-gcc n-param-test.c && ./n-param-test-gcc * */ #include #include #include // #define TRY_CASES_OUTSIDE_VALID_RANGE #define N_PARAM_SEQ(DUMMY, \ Pa, Pb, Pc, Pd, Pe, \ P0, P1, P2, P3, P4, P5, \ N, ...) \ N #define N_PARAM(...) \ N_PARAM_SEQ(__VA_ARGS__, \ "a", "b", "c", "d", "e", \ 5, 4, 3, 2, 1, 0, NULL) #define FOO_INIT(PTR, ...) \ foo_func(PTR, N_PARAM(-666, __VA_ARGS__), __VA_ARGS__) static void foo_func(void *ptr, int n_param, ...) { va_list args; (void) ptr; printf("%s: n_param=%d\n", "foo_func", n_param); assert(n_param <= 5); va_start(args, n_param); int code = va_arg(args, int); printf(" code = %d\n", code); for (int i=0; i