test_stdarg.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* This file is part of asmc, a bootstrapping OS with minimal seed
  2. Copyright (C) 2018-2019 Giovanni Mascellani <gio@debian.org>
  3. https://gitlab.com/giomasce/asmc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include <stdarg.h>
  15. int compute_int_sum(int count, ...) {
  16. va_list v;
  17. va_start(v, count);
  18. int i;
  19. int sum = 0;
  20. for (i = 0; i < count; i++) {
  21. sum += va_arg(v, int);
  22. }
  23. va_end(v);
  24. return sum;
  25. }
  26. long long compute_llong_sum(int count, ...) {
  27. va_list v;
  28. va_start(v, count);
  29. int i;
  30. long long sum = 0;
  31. for (i = 0; i < count; i++) {
  32. sum += va_arg(v, long long);
  33. }
  34. va_end(v);
  35. return sum;
  36. }
  37. int compute_int_sum_twice(int count, ...) {
  38. va_list v;
  39. va_list v2;
  40. va_start(v, count);
  41. va_copy(v2, v);
  42. int i;
  43. int sum = 0;
  44. for (i = 0; i < count; i++) {
  45. sum += va_arg(v, int);
  46. }
  47. va_end(v);
  48. for (i = 0; i < count; i++) {
  49. sum += va_arg(v2, int);
  50. }
  51. va_end(v2);
  52. return sum;
  53. }
  54. int test_stdarg() {
  55. if (compute_int_sum(5, 1, 2, 3, 4, 5) != 15) return 0;
  56. if (compute_llong_sum(5, 1ll, 2ll, 3ll, 4ll, 5ll) != 15) return 0;
  57. if (compute_int_sum_twice(5, 1, 2, 3, 4, 5) != 30) return 0;
  58. return 1;
  59. }