github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/ternary.c (about) 1 #include "tests.h" 2 #include <stdio.h> 3 4 void f_empty() 5 { 6 return; 7 }; 8 9 int ret_two(int a) 10 { 11 return 2 + a; 12 } 13 14 int ret_s(int a) 15 { 16 return a > 0; 17 } 18 19 int ret_ter(int a) 20 { 21 int (*v)(int); 22 v = ret_s; 23 return v ? 1 : ret_two(0); 24 } 25 26 void test_return_ternary() 27 { 28 is_eq(ret_ter(10), 1); 29 is_eq(ret_ter(-1), 1); 30 } 31 32 int main() 33 { 34 plan(17); 35 36 int a = 'a' == 65 ? 10 : 100; 37 float b = 10 == 10 ? 1.0 : 2.0; 38 char* c = 'x' == 5 ? "one" : "two"; 39 char d = a == 100 ? 'x' : 1; 40 41 is_eq(a, 100); 42 is_eq(b, 1); 43 is_streq(c, "two"); 44 is_eq(d, 'x'); 45 46 is_false(0 ? 1 : 0); 47 is_false(NULL ? 1 : 0); 48 is_true('x' ? 1 : 0); 49 50 a = a == 10 ? b == 1.0 ? 1 : 2 : 2; 51 52 if (a == (a == 2 ? 5 : 10)) { 53 fail(__func__); 54 } else { 55 pass(__func__); 56 } 57 58 diag("CStyleCast <ToVoid>"); 59 { 60 double a, b; 61 0 ? (void)(a) : (void)(b); 62 (void)(a), (void)(b); 63 } 64 { 65 double a; 66 0 ? (void)(a) : f_empty(); 67 (void)(a); 68 } 69 { 70 double b; 71 0 ? f_empty() : (void)(b); 72 (void)(b); 73 } 74 { 75 ; 76 0 ? f_empty() : f_empty(); 77 } 78 pass("Ok - ToVoid"); 79 { 80 diag("oper ++"); 81 int b = 42; 82 int addr = 0; 83 b = addr++ ? 1 : 2; 84 is_eq(addr, 1); 85 is_eq(b, 2); 86 } 87 88 test_return_ternary(); 89 90 diag("postfix else"); 91 { 92 int a = 12; 93 int d; 94 d = (1 == 0) ? --a : ++a; 95 is_eq(d, 13); 96 d = (1 != 0) ? --a : ++a; 97 is_eq(d, 12); 98 } 99 100 diag("ternaty without middle"); 101 { 102 int a = 42; 103 a = 19 > -9 ?: 23; 104 is_eq(a, 1); 105 a = 54; 106 a = 4 < -1 ?: 25; 107 is_eq(a, 25); 108 } 109 110 done_testing(); 111 }