1
0

test_ternary.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 <stdbool.h>
  15. int my_abs(int x) {
  16. return x >= 0 ? x : -x;
  17. }
  18. int test_ternary() {
  19. if (my_abs(0) != 0) return 0;
  20. if (my_abs(1) != 1) return 0;
  21. if (my_abs(100) != 100) return 0;
  22. if (my_abs(-1) != 1) return 0;
  23. if (my_abs(-100) != 100) return 0;
  24. return 1;
  25. }
  26. int test_ternary_ptr() {
  27. char x = 'x';
  28. char y = 'y';
  29. if (*(1 ? &x : &y) != 'x') return 0;
  30. if (*(0 ? &x : &y) != 'y') return 0;
  31. return 1;
  32. }
  33. int test_ternary_void() {
  34. int x = 0;
  35. int y = 0;
  36. 1 ? x++ : y++;
  37. if (x != 1) return 0;
  38. if (y != 0) return 0;
  39. 0 ? x++ : y++;
  40. if (x != 1) return 0;
  41. if (y != 1) return 0;
  42. return 1;
  43. }
  44. int test_bool() {
  45. bool x = 0;
  46. if (x != 0) return 0;
  47. if (x == 1) return 0;
  48. if (x == 100) return 0;
  49. x = 1;
  50. if (x == 0) return 0;
  51. if (x != 1) return 0;
  52. if (x == 100) return 0;
  53. x = 100;
  54. if (x == 0) return 0;
  55. if (x != 1) return 0;
  56. if (x == 100) return 0;
  57. return 1;
  58. }