From 78b87f79546a0ef20ef1c76c1f3b32b289ea9e83 Mon Sep 17 00:00:00 2001 From: Francois-Rene Rideau Date: Sat, 18 Nov 2023 01:45:27 +0000 Subject: [PATCH] Fix tests --- doc/reference/std/misc/vector.md | 33 +++++++++++++++++++++++++++++++- src/std/misc/number-test.ss | 4 ++-- src/std/misc/vector-test.ss | 16 ++++++++++------ src/std/misc/vector.ss | 10 +++++----- 4 files changed, 49 insertions(+), 14 deletions(-) diff --git a/doc/reference/std/misc/vector.md b/doc/reference/std/misc/vector.md index 5f51d70cac..db7e9e5165 100644 --- a/doc/reference/std/misc/vector.md +++ b/doc/reference/std/misc/vector.md @@ -10,7 +10,6 @@ that complement those provided by RnRS, Gambit and `:std/srfi/43`. ::: ## vector-ref-set! - ``` scheme (def vector-ref-set! vector-set!) (set! (vector-ref v i) x) @@ -28,8 +27,40 @@ This binding enables you to use `set!` with `vector-ref`. ::: ### vector-least-index +``` scheme +(vector-least-index pred vector [start: 0] [end: #f]) +``` + +Given a predicate `pred` on the elements of given `vector`, that is “increasing”, +i.e. if true for a given element, true on all subsequent elements, and optionally +a `start` (inclusive, defaults to `0`) and an end (exclusive, defaults to `#f` +which designates the vector length), return the least index of a vector element +in the interval [start, env) that satisfies the predicate, or the end if none does. + +::: tip Examples: +``` scheme +> (vector-least-index (cut < 10) #(35 21 16 11 10 9 7 4 1)) +5 +``` +::: ### vector-most-index +``` scheme +(vector-most-index pred vector [start: 0] [end: #f]) +``` + +Given a predicate `pred` on the elements of given `vector`, that is “decreasing”, +i.e. if false for a given element, false on all subsequent elements, and optionally +a `start` (inclusive, defaults to `0`) and an end (exclusive, defaults to `#f` +which designates the vector length), return the most index of a vector element +in the interval [start, env) that satisfies the predicate, or the end if none does. + +::: tip Examples: +``` scheme +> (vector-most-index (cut < 10) #(2 3 5 7 11 13 17 19 23)) +4 +``` +::: ### maybe-subvector diff --git a/src/std/misc/number-test.ss b/src/std/misc/number-test.ss index 2e05b4e112..3e33d137f8 100644 --- a/src/std/misc/number-test.ss +++ b/src/std/misc/number-test.ss @@ -189,8 +189,8 @@ (check (least-integer (cut > <> 13.5) 0 20) => 14) (check (least-integer true 0 20) => 0) (check (least-integer false 0 20) => 20) - (check (most-integer true 0 20) => 20) - (check (most-integer false 0 20) => 0) + (check (most-integer true 0 20) => 8) + (check (most-integer false 0 20) => -1) (check (most-integer (cut < <> 13.5) 0 20) => 13)) (test-case "bezout, invert-mod, div-mod, mult-mod" diff --git a/src/std/misc/vector-test.ss b/src/std/misc/vector-test.ss index d22d3507c6..0037a3ad1e 100644 --- a/src/std/misc/vector-test.ss +++ b/src/std/misc/vector-test.ss @@ -11,8 +11,12 @@ (def foo (vector 1 2 3)) (set! (vector-ref foo 1) 4) (check foo => #(1 4 3)) - (check (vector-least-index (cut < 10) #(35 21 16 11 10 9 7 4 1)) => 5) - (check (vector-most-index (cut < 10) #(2 3 5 7 11 13 17 19 23)) => 4) + (check (vector-least-index (cut < <> 10) #(35 21 16 11 10 9 7 4 1)) => 5) + (check (vector-least-index true #(35 21 16 11 10 9 7 4 1)) => 0) + (check (vector-least-index false #(35 21 16 11 10 9 7 4 1)) => 9) + (check (vector-most-index (cut < <> 10) #(2 3 5 7 11 13 17 19 23)) => 4) + (check (vector-most-index true #(2 3 5 7 11 13 17 19 23)) => 9) + (check (vector-most-index false #(2 3 5 7 11 13 17 19 23)) => 0) (check (maybe-subvector #(1 3 5 7) 2) => #(5 7)) (check (eq? foo (maybe-subvector foo 0 3)) => #t) (check (with-list-builder (c) @@ -22,20 +26,20 @@ (check (with-list-builder (c) (subvector-for-each/index (lambda (x y) (c [x y])) #(a b c d e f g h) start: 5)) - => '((f 5) (g 6) (h 7))) + => '((5 f) (6 g) (7 h))) (check (with-list-builder (c) (subvector-reverse-for-each c #(a b c d e f g h) start: 2 end: 5)) => '(e d c)) (check (with-list-builder (c) (subvector-reverse-for-each/index (lambda (x y) (c [x y])) #(a b c d e f g h) start: 5)) - => '((h 7) (g 6) (f 5))) + => '((7 h) (6 g) (5 f))) (check (subvector->list #(a b c d e f g h) start: 5) - => '(c d e)) + => '(f g h)) (check (cons->vector '(a . b)) => #(a b)) (check (cons->vector 'foo) => #f) (check (vector-filter odd? #(1 2 3 4 5 6 7 8 9) start: 1 end: 7) => #(3 5 7)) - )) + ))) diff --git a/src/std/misc/vector.ss b/src/std/misc/vector.ss index 52e704e33f..eeffcd0d9c 100644 --- a/src/std/misc/vector.ss +++ b/src/std/misc/vector.ss @@ -39,7 +39,7 @@ ;;; return the most index such that all previous vector elements in the interval [start, env) ;;; satisfy the predicate, or the start if none does. (def (vector-most-index pred? vector start: (start 0) end: (end (vector-length vector))) - (most-integer (lambda (i) (pred? (vector-ref vector i))) start end)) + (least-integer (lambda (i) (not (pred? (vector-ref vector i)))) start end)) ;;; Copy a vector if necessary: return the same if no change in start and end requested. (def (maybe-subvector vector (start 0) (end #f)) @@ -63,9 +63,9 @@ (function (vector-ref vector i))))) (def (subvector-reverse-for-each/index function vector start: (start 0) end: (end #f)) - (def end (or end (vector-length vector))) + (let (end (or end (vector-length vector))) (for ((i (in-iota (- end start) (- end 1) -1))) - (function i (vector-ref vector i)))) + (function i (vector-ref vector i))))) (def (subvector->list vector start: (start 0) end: (end #f)) (with-list-builder (c!) (subvector-for-each c! vector start: start end: end))) @@ -82,5 +82,5 @@ (list->vector (with-list-builder (c) (for (i (in-range start end)) - (def e (vector-ref v i)) - (when (pred? e) (c e)))))) + (let (e (vector-ref v i)) + (when (pred? e) (c e)))))))