From 78fb84c4dd64c9ea23a27913a6cc5bca602d8d1b Mon Sep 17 00:00:00 2001 From: Enzo Date: Mon, 14 Dec 2020 11:01:11 +0100 Subject: [PATCH] gen: fix comparing signed int to u32/u64 (#7312) --- vlib/v/gen/cgen.v | 2 +- vlib/v/tests/infix_expr_test.v | 83 ++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/infix_expr_test.v diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 68f95c21bf..a9fcc472b6 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -20,7 +20,7 @@ const ( // same order as in token.Kind cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le'] // when operands are switched - cmp_rev = ['eq', 'ne', 'le', 'ge', 'lt', 'gt'] + cmp_rev = ['eq', 'ne', 'lt', 'gt', 'le', 'ge'] ) struct Gen { diff --git a/vlib/v/tests/infix_expr_test.v b/vlib/v/tests/infix_expr_test.v new file mode 100644 index 0000000000..38eeb44eda --- /dev/null +++ b/vlib/v/tests/infix_expr_test.v @@ -0,0 +1,83 @@ +fn test_cmp_signed_and_u32() { + // == + assert int(1) == u32(1) + // != + assert int(1) != u32(2) + // > + assert !(int(1) > u32(1)) + assert int(1) > u32(0) + // >= + assert !(int(0) >= u32(1)) + assert int(1) >= u32(1) + assert int(1) >= u32(0) + // < + assert !(int(1) < u32(1)) + assert int(0) < u32(1) + // <= + assert int(0) <= u32(1) + assert int(1) <= u32(1) + assert !(int(1) <= u32(0)) +} + +fn test_cmp_u32_and_signed() { + // == + assert u32(1) == int(1) + // != + assert u32(2) != int(1) + // > + assert !(u32(1) > int(1)) + assert u32(1) > int(0) + // >= + assert u32(1) >= int(0) + assert u32(1) >= int(1) + assert !(u32(0) >= int(1)) + // < + assert !(u32(1) < int(1)) + assert u32(0) < int(1) + // <= + assert u32(0) <= int(1) + assert u32(1) <= int(1) + assert !(u32(1) <= int(0)) +} + +fn test_cmp_signed_and_u64() { + // == + assert int(1) == u64(1) + // != + assert int(1) != u64(2) + // > + assert !(int(1) > u64(1)) + assert int(1) > u64(0) + // >= + assert !(int(0) >= u64(1)) + assert int(1) >= u64(1) + assert int(1) >= u64(0) + // < + assert !(int(1) < u64(1)) + assert int(0) < u64(1) + // <= + assert int(0) <= u64(1) + assert int(1) <= u64(1) + assert !(int(1) <= u64(0)) +} + +fn test_cmp_u64_and_signed() { + // == + assert u64(1) == int(1) + // != + assert u64(2) != int(1) + // > + assert !(u64(1) > int(1)) + assert u64(1) > int(0) + // >= + assert u64(1) >= int(0) + assert u64(1) >= int(1) + assert !(u64(0) >= int(1)) + // < + assert !(u64(1) < int(1)) + assert u64(0) < int(1) + // <= + assert u64(0) <= int(1) + assert u64(1) <= int(1) + assert !(u64(1) <= int(0)) +}