| ... | @@ -871,6 +871,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void { | ... | @@ -871,6 +871,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 871 | \\} | 871 | \\} |
| 872 | }); | 872 | }); |
| 873 | | 873 | |
| | 874 | cases.add_2("==, !=, no if", // TODO remove this test after `if` conversion supported, and switch "==, !=" to addC_both |
| | 875 | \\int max(int a, int b) { |
| | 876 | \\ int c = (a == b); |
| | 877 | \\ int d = (a != b); |
| | 878 | \\ return (c != d); |
| | 879 | \\} |
| | 880 | , &[_][]const u8{ |
| | 881 | \\pub export fn max(a: c_int, b: c_int) c_int { |
| | 882 | \\ var c: c_int = (a == b); |
| | 883 | \\ var d: c_int = (a != b); |
| | 884 | \\ return (c != d); |
| | 885 | \\} |
| | 886 | }); |
| | 887 | |
| 874 | cases.addC("bitwise binary operators", | 888 | cases.addC("bitwise binary operators", |
| 875 | \\int max(int a, int b) { | 889 | \\int max(int a, int b) { |
| 876 | \\ return (a & b) ^ (a | b); | 890 | \\ return (a & b) ^ (a | b); |
| ... | @@ -881,6 +895,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void { | ... | @@ -881,6 +895,20 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 881 | \\} | 895 | \\} |
| 882 | }); | 896 | }); |
| 883 | | 897 | |
| | 898 | cases.add_2("bitwise binary operators, simpler parens", // TODO can combine with "bitwise binary operators" when parens are correctly preserved/not added in translate-c-2 |
| | 899 | \\int max(int a, int b) { |
| | 900 | \\ int c = (a & b); |
| | 901 | \\ int d = (a | b); |
| | 902 | \\ return (c ^ d); |
| | 903 | \\} |
| | 904 | , &[_][]const u8{ |
| | 905 | \\pub export fn max(a: c_int, b: c_int) c_int { |
| | 906 | \\ var c: c_int = (a & b); |
| | 907 | \\ var d: c_int = (a | b); |
| | 908 | \\ return (c ^ d); |
| | 909 | \\} |
| | 910 | }); |
| | 911 | |
| 884 | cases.addC("logical and, logical or", | 912 | cases.addC("logical and, logical or", |
| 885 | \\int max(int a, int b) { | 913 | \\int max(int a, int b) { |
| 886 | \\ if (a < b || a == b) | 914 | \\ if (a < b || a == b) |
| ... | @@ -897,6 +925,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void { | ... | @@ -897,6 +925,30 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 897 | \\} | 925 | \\} |
| 898 | }); | 926 | }); |
| 899 | | 927 | |
| | 928 | cases.add_2("comparison operators (no if)", // TODO Come up with less contrived tests? Make sure to cover all these comparisons. Can use `if` after it is added to translate-c-2 |
| | 929 | \\int test_comparisons(int a, int b) { |
| | 930 | \\ int c = (a < b); |
| | 931 | \\ int d = (a > b); |
| | 932 | \\ int e = (a <= b); |
| | 933 | \\ int f = (a >= b); |
| | 934 | \\ int g = (c < d); |
| | 935 | \\ int h = (e < f); |
| | 936 | \\ int i = (g < h); |
| | 937 | \\ return i; |
| | 938 | \\} |
| | 939 | , &[_][]const u8{ |
| | 940 | \\pub export fn test_comparisons(a: c_int, b: c_int) c_int { |
| | 941 | \\ var c: c_int = (a < b); |
| | 942 | \\ var d: c_int = (a > b); |
| | 943 | \\ var e: c_int = (a <= b); |
| | 944 | \\ var f: c_int = (a >= b); |
| | 945 | \\ var g: c_int = (c < d); |
| | 946 | \\ var h: c_int = (e < f); |
| | 947 | \\ var i: c_int = (g < h); |
| | 948 | \\ return i; |
| | 949 | \\} |
| | 950 | }); |
| | 951 | |
| 900 | cases.addC("logical and, logical or on none bool values", | 952 | cases.addC("logical and, logical or on none bool values", |
| 901 | \\int and_or_none_bool(int a, float b, void *c) { | 953 | \\int and_or_none_bool(int a, float b, void *c) { |
| 902 | \\ if (a && b) return 0; | 954 | \\ if (a && b) return 0; |