From abd90ccd3dd934fde69f94a6ad146a8991ecc439 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sun, 3 Jul 2022 11:18:11 +0900 Subject: [PATCH] Fixup, complete tuple comparisons --- src/obj_tuple.c | 45 ++++- test/testTupleComparisons.krk | 25 +++ test/testTupleComparisons.krk.expect | 245 +++++++++++++++++++++++++++ 3 files changed, 309 insertions(+), 6 deletions(-) create mode 100644 test/testTupleComparisons.krk create mode 100644 test/testTupleComparisons.krk.expect diff --git a/src/obj_tuple.c b/src/obj_tuple.c index d99490b..38bd11f 100644 --- a/src/obj_tuple.c +++ b/src/obj_tuple.c @@ -121,12 +121,12 @@ KRK_METHOD(tuple,__lt__,{ KrkValue a = self->values.values[i]; KrkValue b = them->values.values[i]; KrkValue ltComp = krk_operator_lt(a,b); - if (IS_BOOLEAN(ltComp) && AS_BOOLEAN(ltComp)) continue; + if (IS_BOOLEAN(ltComp) && AS_BOOLEAN(ltComp)) return BOOLEAN_VAL(1); KrkValue gtComp = krk_operator_gt(a,b); if (IS_BOOLEAN(gtComp) && AS_BOOLEAN(gtComp)) return BOOLEAN_VAL(0); - if (i + 1 == lesser) return BOOLEAN_VAL((self->values.count < them->values.count)); + /* continue on == */ } - return BOOLEAN_VAL((self->values.count <= them->values.count)); + return BOOLEAN_VAL((self->values.count < them->values.count)); }) KRK_METHOD(tuple,__gt__,{ @@ -138,11 +138,42 @@ KRK_METHOD(tuple,__gt__,{ KrkValue a = self->values.values[i]; KrkValue b = them->values.values[i]; KrkValue gtComp = krk_operator_gt(a,b); - if (IS_BOOLEAN(gtComp) && AS_BOOLEAN(gtComp)) continue; + if (IS_BOOLEAN(gtComp) && AS_BOOLEAN(gtComp)) return BOOLEAN_VAL(1); + KrkValue ltComp = krk_operator_lt(a,b); + if (IS_BOOLEAN(ltComp) && AS_BOOLEAN(ltComp)) return BOOLEAN_VAL(0); + } + return BOOLEAN_VAL((self->values.count > them->values.count)); +}) + +KRK_METHOD(tuple,__le__,{ + METHOD_TAKES_EXACTLY(1); + if (!IS_tuple(argv[1])) return NOTIMPL_VAL(); + KrkTuple * them = AS_tuple(argv[1]); + size_t lesser = self->values.count < them->values.count ? self->values.count : them->values.count; + for (size_t i = 0; i < lesser; ++i) { + KrkValue a = self->values.values[i]; + KrkValue b = them->values.values[i]; + KrkValue ltComp = krk_operator_lt(a,b); + if (IS_BOOLEAN(ltComp) && AS_BOOLEAN(ltComp)) return BOOLEAN_VAL(1); + KrkValue gtComp = krk_operator_gt(a,b); + if (IS_BOOLEAN(gtComp) && AS_BOOLEAN(gtComp)) return BOOLEAN_VAL(0); + /* continue on == */ + } + return BOOLEAN_VAL((self->values.count <= them->values.count)); +}) + +KRK_METHOD(tuple,__ge__,{ + METHOD_TAKES_EXACTLY(1); + if (!IS_tuple(argv[1])) return NOTIMPL_VAL(); + KrkTuple * them = AS_tuple(argv[1]); + size_t lesser = self->values.count < them->values.count ? self->values.count : them->values.count; + for (size_t i = 0; i < lesser; ++i) { + KrkValue a = self->values.values[i]; + KrkValue b = them->values.values[i]; + KrkValue gtComp = krk_operator_gt(a,b); + if (IS_BOOLEAN(gtComp) && AS_BOOLEAN(gtComp)) return BOOLEAN_VAL(1); KrkValue ltComp = krk_operator_lt(a,b); if (IS_BOOLEAN(ltComp) && AS_BOOLEAN(ltComp)) return BOOLEAN_VAL(0); - /* Must be equal */ - if (i + 1 == lesser) return BOOLEAN_VAL((self->values.count > them->values.count)); } return BOOLEAN_VAL((self->values.count >= them->values.count)); }) @@ -247,6 +278,8 @@ void _createAndBind_tupleClass(void) { BIND_METHOD(tuple,__eq__); BIND_METHOD(tuple,__lt__); BIND_METHOD(tuple,__gt__); + BIND_METHOD(tuple,__le__); + BIND_METHOD(tuple,__ge__); BIND_METHOD(tuple,__hash__); krk_defineNative(&tuple->methods, "__init__", _tuple_init); krk_defineNative(&tuple->methods, "__str__", FUNC_NAME(tuple,__repr__)); diff --git a/test/testTupleComparisons.krk b/test/testTupleComparisons.krk new file mode 100644 index 0000000..a4e09ba --- /dev/null +++ b/test/testTupleComparisons.krk @@ -0,0 +1,25 @@ +def test(tuples=None,comparisons=None): + tuples = [ + (), + (1,2,3), + (4,5,6), + (1,2), + (1,3), + (0,), + (47,), + ] + + comparisons = [ + lambda a, b: a < b, + lambda a, b: a > b, + lambda a, b: a <= b, + lambda a, b: a >= b, + lambda a, b: a == b, + ] + + for i in tuples: + for j in tuples: + for c in comparisons: + print(c(i,j)) + +test() diff --git a/test/testTupleComparisons.krk.expect b/test/testTupleComparisons.krk.expect new file mode 100644 index 0000000..deccc65 --- /dev/null +++ b/test/testTupleComparisons.krk.expect @@ -0,0 +1,245 @@ +False +False +True +True +True +True +False +True +False +False +True +False +True +False +False +True +False +True +False +False +True +False +True +False +False +True +False +True +False +False +True +False +True +False +False +False +True +False +True +False +False +False +True +True +True +True +False +True +False +False +False +True +False +True +False +True +False +True +False +False +False +True +False +True +False +True +False +True +False +False +False +True +False +True +False +False +True +False +True +False +False +False +True +True +True +False +True +False +True +False +False +True +False +True +False +False +True +False +True +False +True +False +True +False +False +False +True +False +True +False +True +False +True +False +False +True +False +True +False +False +False +False +True +True +True +True +False +True +False +False +False +True +False +True +False +True +False +True +False +False +False +True +False +True +False +False +True +False +True +False +True +False +True +False +False +False +True +False +True +False +False +False +True +True +True +False +True +False +True +False +True +False +True +False +False +False +True +False +True +False +True +False +True +False +False +True +False +True +False +False +True +False +True +False +False +True +False +True +False +False +False +False +True +True +True +True +False +True +False +False +False +True +False +True +False +False +True +False +True +False +False +True +False +True +False +False +True +False +True +False +False +True +False +True +False +False +True +False +True +False +False +False +True +True +True