From cfdf6aef9dbbd0f3c3234919ea8d8ea1bf2304c0 Mon Sep 17 00:00:00 2001 From: mrdudz Date: Thu, 17 Nov 2022 20:41:36 +0100 Subject: [PATCH] add some tests on comparing floats vs ints. not all cases working --- test/val/float-cmp-const-const.c | 77 ++++++++++++++++++++++++++++++-- test/val/float-cmp-const-var.c | 40 +++++++++++++++-- test/val/float-cmp-var-const.c | 52 +++++++++++++++++++-- 3 files changed, 158 insertions(+), 11 deletions(-) diff --git a/test/val/float-cmp-const-const.c b/test/val/float-cmp-const-const.c index 1fe81b1b7..9a4b444f5 100644 --- a/test/val/float-cmp-const-const.c +++ b/test/val/float-cmp-const-const.c @@ -39,27 +39,94 @@ int result = 0; result++; \ } -// when making individual sub tests work, remove them here and uncomment them -// in val/float-cmp.c void constconst(void) { - //------------------------------------------------------------------------- - // float constant vs float const printf("const vs const\n"); expect("1.5f == 1.6f is", 0, (1.5f == 1.6f)); expect("1.6f == 1.5f is", 0, (1.6f == 1.5f)); + expect("1.6f == 1.6f is", 1, (1.6f == 1.6f)); expect("1.5f != 1.6f is", 1, (1.5f != 1.6f)); expect("1.6f != 1.5f is", 1, (1.6f != 1.5f)); + expect("1.6f != 1.6f is", 0, (1.6f != 1.6f)); expect("1.5f < 1.6f is", 1, (1.5f < 1.6f)); + expect("1.6f < 1.5f is", 0, (1.6f < 1.5f)); + expect("1.6f < 1.6f is", 0, (1.6f < 1.6f)); + expect("1.5f > 1.6f is", 0, (1.5f > 1.6f)); expect("1.6f > 1.5f is", 1, (1.6f > 1.5f)); + expect("1.6f > 1.6f is", 0, (1.6f > 1.6f)); + expect("1.5f <= 1.6f is", 1, (1.5f <= 1.6f)); expect("1.6f <= 1.5f is", 0, (1.6f <= 1.5f)); + expect("1.6f <= 1.6f is", 1, (1.6f <= 1.6f)); expect("1.5f >= 1.6f is", 0, (1.5f >= 1.6f)); + expect("1.6f >= 1.5f is", 1, (1.6f >= 1.5f)); + expect("1.6f >= 1.6f is", 1, (1.6f >= 1.6f)); +} + +void intconstconst(void) +{ + printf("int const vs const\n"); + + // FIXME: Comparing types 'int' with 'float' is invalid +#if 0 + expect("10 == 1.6f is", 0, (10 == 1.6f)); + expect("20 == 1.5f is", 0, (20 == 1.5f)); + expect("20 == 1.6f is", 1, (20 == 1.6f)); + + expect("10 != 1.6f is", 1, (10 != 1.6f)); + expect("20 != 1.5f is", 1, (20 != 1.5f)); + expect("20 != 1.6f is", 0, (20 != 1.6f)); + + expect("10 < 1.6f is", 1, (10 < 1.6f)); + expect("20 < 1.5f is", 0, (20 < 1.5f)); + expect("20 < 1.6f is", 0, (20 < 1.6f)); + + expect("10 > 1.6f is", 0, (10 > 1.6f)); + expect("20 > 1.5f is", 1, (20 > 1.5f)); + expect("20 > 1.6f is", 0, (20 > 1.6f)); + + expect("10 <= 1.6f is", 1, (10 <= 1.6f)); + expect("20 <= 1.5f is", 0, (20 <= 1.5f)); + expect("20 <= 1.6f is", 1, (20 <= 1.6f)); + + expect("10 >= 1.6f is", 0, (10 >= 1.6f)); + expect("20 >= 1.5f is", 1, (20 >= 1.5f)); + expect("20 >= 1.6f is", 1, (20 >= 1.6f)); +#endif +} + +void constintconst(void) +{ + printf("const vs int const\n"); + + expect("10.0f == 20 is", 0, (10.0f == 20)); + expect("20.0f == 10 is", 0, (20.0f == 10)); + expect("20.0f == 20 is", 1, (20.0f == 20)); + + expect("10.0f != 20 is", 1, (10.0f != 20)); + expect("20.0f != 10 is", 1, (20.0f != 10)); + expect("20.0f != 20 is", 0, (20.0f != 20)); + + expect("10.0f < 20 is", 1, (10.0f < 20)); + expect("20.0f < 10 is", 0, (20.0f < 10)); + expect("20.0f < 20 is", 0, (20.0f < 20)); + + expect("10.0f > 20 is", 0, (10.0f > 20)); + expect("20.0f > 10 is", 1, (20.0f > 10)); + expect("20.0f > 20 is", 0, (20.0f > 20)); + + expect("10.0f <= 20 is", 1, (10.0f <= 20)); + expect("20.0f <= 10 is", 0, (20.0f <= 10)); + expect("20.0f <= 20 is", 1, (20.0f <= 20)); + + expect("10.0f >= 20 is", 0, (10.0f >= 20)); + expect("20.0f >= 10 is", 1, (20.0f >= 10)); + expect("20.0f >= 20 is", 1, (20.0f >= 20)); } //------------------------------------------------------------------------- @@ -69,6 +136,8 @@ int main(void) printf("float-cmp-const-const\n"); constconst(); + intconstconst(); + constintconst(); printf("float-cmp-const-const (res: %d)\n", result); return result; diff --git a/test/val/float-cmp-const-var.c b/test/val/float-cmp-const-var.c index 615778ff5..30bdc10c0 100644 --- a/test/val/float-cmp-const-var.c +++ b/test/val/float-cmp-const-var.c @@ -39,9 +39,6 @@ int result = 0; result++; \ } -// float constant vs float variable -// when making individual sub tests work, remove them here and uncomment them -// in val/float-cmp.c void constvar(void) { printf("const vs var\n"); @@ -69,8 +66,44 @@ void constvar(void) expect("1.5f >= 1.6f is", 0, (1.5f >= fp1)); expect("1.6f >= 1.5f is", 1, (1.6f >= fp2)); expect("1.6f >= 1.6f is", 1, (1.6f >= fp1)); + } +void intconstvar(void) +{ + printf("int const vs var\n"); + + fp1 = 20.0f; + fp2 = 10.0f; + + // Comparing types 'int' with 'float' is invalid +#if 0 + expect("10 == 20 is", 0, (10 == fp1)); + expect("20 == 10 is", 0, (20 == fp2)); + expect("20 == 20 is", 1, (20 == fp1)); + + expect("10 != 20 is", 1, (10 != fp1)); + expect("20 != 10 is", 1, (20 != fp2)); + expect("20 != 20 is", 0, (20 != fp1)); + + expect("10 < 20 is", 1, (10 < fp1)); + expect("20 < 10 is", 0, (20 < fp2)); + expect("20 < 20 is", 0, (20 < fp1)); + + expect("10 > 20 is", 0, (10 > fp1)); + expect("20 > 10 is", 1, (20 > fp2)); + expect("20 > 20 is", 0, (20 > fp1)); + + expect("10 <= 20 is", 1, (10 <= fp1)); + expect("20 <= 10 is", 0, (20 <= fp2)); + expect("20 <= 20 is", 1, (20 <= fp1)); + + expect("10 >= 20 is", 0, (10 >= fp1)); + expect("20 >= 10 is", 1, (20 >= fp2)); + expect("20 >= 20 is", 1, (20 >= fp1)); +#endif + +} //------------------------------------------------------------------------- int main(void) @@ -80,6 +113,7 @@ int main(void) fp1 = 1.6f; fp2 = 1.5f; constvar(); + intconstvar(); printf("float-cmp-const-var (res: %d)\n", result); return result; diff --git a/test/val/float-cmp-var-const.c b/test/val/float-cmp-var-const.c index 299542c72..91fd35f90 100644 --- a/test/val/float-cmp-var-const.c +++ b/test/val/float-cmp-var-const.c @@ -31,10 +31,12 @@ unsigned long var_ulong; int result = 0; #define expect(msg, exp, val) \ - printf("%s %s%s\n", \ + printf("%s %s%s (is:%d want:%d)\n", \ msg, \ val ? "true" : "false", \ - (exp != val) ? " (failed)" : ""); \ + (exp != val) ? " (failed)" : "", \ + exp, val \ + ); \ if (exp != val) { \ result++; \ } @@ -42,8 +44,6 @@ int result = 0; //------------------------------------------------------------------------- // float variable vs float constant -// when making individual sub tests work, remove them here and uncomment them -// in val/float-cmp.c void varconst(void) { printf("var vs const\n"); @@ -51,19 +51,62 @@ void varconst(void) fp1 = 1.6f; fp2 = 1.5f; + expect("1.5f == 1.6f is", 0, (fp2 == 1.6f)); + expect("1.6f == 1.5f is", 0, (fp1 == 1.5f)); expect("1.6f == 1.6f is", 1, (fp1 == 1.6f)); + expect("1.5f != 1.6f is", 1, (fp2 != 1.6f)); + expect("1.6f != 1.5f is", 1, (fp1 != 1.5f)); expect("1.6f != 1.6f is", 0, (fp1 != 1.6f)); expect("1.5f < 1.6f is", 1, (fp2 < 1.6f)); + expect("1.6f < 1.5f is", 0, (fp1 < 1.5f)); + expect("1.6f < 1.6f is", 0, (fp1 < 1.6f)); expect("1.5f > 1.6f is", 0, (fp2 > 1.6f)); + expect("1.6f > 1.5f is", 1, (fp1 > 1.5f)); expect("1.6f > 1.6f is", 0, (fp1 > 1.6f)); expect("1.5f <= 1.6f is", 1, (fp2 <= 1.6f)); + expect("1.6f <= 1.5f is", 0, (fp1 <= 1.5f)); expect("1.6f <= 1.6f is", 1, (fp1 <= 1.6f)); expect("1.5f >= 1.6f is", 0, (fp2 >= 1.6f)); + expect("1.6f >= 1.5f is", 1, (fp1 >= 1.5f)); + expect("1.6f >= 1.6f is", 1, (fp1 >= 1.6f)); + +} + +void varintconst(void) +{ + printf("var vs int const\n"); + + fp1 = 10.0f; + fp2 = 20.0f; + + expect("20 == 10 is", 0, (fp2 == 10)); + expect("10 == 20 is", 0, (fp1 == 20)); + expect("10 == 10 is", 1, (fp1 == 10)); + + expect("20 != 10 is", 1, (fp2 != 10)); + expect("10 != 20 is", 1, (fp1 != 20)); + expect("10 != 10 is", 0, (fp1 != 10)); + +// expect("20 < 10 is", 1, (fp2 < 10)); +// expect("10 < 20 is", 0, (fp1 < 20)); + expect("10 < 10 is", 0, (fp1 < 10)); + +// expect("20 > 10 is", 0, (fp2 > 10)); +// expect("10 > 20 is", 1, (fp1 > 20)); + expect("10 > 10 is", 0, (fp1 > 10)); + +// expect("20 <= 10 is", 1, (fp2 <= 10)); +// expect("10 <= 20 is", 0, (fp1 <= 20)); + expect("10 <= 10 is", 1, (fp1 <= 10)); + +// expect("20 >= 10 is", 0, (fp2 >= 10)); +// expect("10 >= 20 is", 1, (fp1 >= 20)); + expect("10 >= 10 is", 1, (fp1 >= 10)); } @@ -72,6 +115,7 @@ int main(void) printf("float-cmp-var-const\n"); varconst(); + varintconst(); printf("float-cmp-var-const (res: %d)\n", result); return result;