From 7c64d23b0af1f33292fa47f9201ad42bb984af60 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 21 Feb 2025 14:06:05 +0000 Subject: Add unit tests for lib.functions.remove-at-indices --- lib/functions.lisp | 4 ++-- tests/functions.lisp | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/functions.lisp b/lib/functions.lisp index 4e18a0a..65dd7f5 100644 --- a/lib/functions.lisp +++ b/lib/functions.lisp @@ -55,8 +55,8 @@ where key x in A has associations {y in LST : INDICATOR(y) = x}." "Given string INP, attempt to parse an integer. Return NIL otherwise." (parse-integer inp :junk-allowed t)) -(fn remove-at-indices (indices lst) (-> (list list) list) +(fn remove-at-indices (indices lst) (-> (list sequence) list) (loop :for i :from 0 :to (1- (length lst)) - :for item :in lst + :for item :in (coerce lst 'list) :if (not (member i indices)) :collect item)) diff --git a/tests/functions.lisp b/tests/functions.lisp index b629c1f..ac8599d 100644 --- a/tests/functions.lisp +++ b/tests/functions.lisp @@ -93,3 +93,22 @@ (is equal 3 (->> (assoc #\t res) cdr length)) (is equal 2 (->> (assoc #\space res) cdr length)) (is equal 2 (->> (assoc #\s res) cdr length)))) + +(define-test (function-test remove-at-indices) + :depends-on (range) + :compile-at :execute + (fail (remove-at-indices "a string" "another string")) + (true (null (remove-at-indices nil nil))) + (is equal '(1 2 3) (remove-at-indices nil '(1 2 3))) + (is equal '(2) (remove-at-indices '(0 2) '(1 2 3))) + (let* ((inp (range 100 200)) + (t1 (remove-at-indices (range 0 100 2) inp)) + (t2 (remove-at-indices (range 1 100 2) inp))) + (is equal 50 (length t1)) + (is equal 50 (length t2)) + (true (every (lambda (n) (not (member n t2))) t1)) + (true (every (lambda (n) (not (member n t1))) t2))) + (let* ((indices (list 0 5 6 7 8 9 10)) + (str-lst (remove-at-indices indices "Hello World")) + (ret (coerce str-lst 'string))) + (is string= "ello" ret))) -- cgit v1.2.3-13-gbd6f