From dafdd86a5fdfe7126c66462e23c3f2d1fbb7b08c Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 10 Sep 2019 19:06:58 -0700 Subject: bracket: bug: wrong result when function is applied. Reported by user vapnik spaknik. * lib.c (bracket): Don't rely on the index variable to step through the arguments, because it only counts fixed arguments. The args_get function doesn't increment the index beyond args->fill; when popping arguments from args->list, index stays unmodified. * tests/016/arith.tl: Tests for bracket added. --- lib.c | 8 ++++---- tests/016/arith.tl | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib.c b/lib.c index c76c4864..3b2aa4af 100644 --- a/lib.c +++ b/lib.c @@ -3341,15 +3341,15 @@ val clamp(val low, val high, val num) val bracket(val larg, struct args *args) { - cnum index = 0; + cnum index = 0, i = 0; - while (args_more(args, index)) { + for (; args_more(args, index); i++) { val rarg = args_get(args, &index); if (less(larg, rarg)) - return num(index - 1); + return num(i); } - return num(index); + return num(i); } val string_own(wchar_t *str) diff --git a/tests/016/arith.tl b/tests/016/arith.tl index eaa7fe2f..6a99b24a 100644 --- a/tests/016/arith.tl +++ b/tests/016/arith.tl @@ -57,3 +57,17 @@ (test (digits 3 2) (1 1)) (test (digits 7 2) (1 1 1)) (test (digits 8 2) (1 0 0 0)) + +(test (bracket 0 10 20 30) 0) +(test (bracket 9 10 20 30) 0) +(test (bracket 10 10 20 30) 1) +(test (bracket 15 10 20 30) 1) +(test (bracket 25 10 20 30) 2) +(test (bracket 30 10 20 30) 3) + +(test [apply bracket '(0 10 20 30)] 0) +(test [apply bracket '(9 10 20 30)] 0) +(test [apply bracket '(10 10 20 30)] 1) +(test [apply bracket '(15 10 20 30)] 1) +(test [apply bracket '(25 10 20 30)] 2) +(test [apply bracket '(30 10 20 30)] 3) -- cgit v1.2.3