From 660f0ccb6b7e6fff8a6d870b720b5bd0dd6898a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 20 Dec 2021 23:05:05 +0100 Subject: find-extreme on empty list now an error. --- module/calp/util.scm | 3 +-- tests/util.scm | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/module/calp/util.scm b/module/calp/util.scm index 96ca2f01..6cee1b0f 100644 --- a/module/calp/util.scm +++ b/module/calp/util.scm @@ -252,8 +252,7 @@ ;; Ord b => (list a) [, (b, b -> bool), (a -> b)] -> a, (list a) (define*-public (find-extreme items optional: (< <) (access identity)) (if (null? items) - ;; Vad fan retunerar man här? - (values #f '()) + (error "Can't find extreme in an empty list") (fold-values (lambda (c min other) (if (< (access c) (access min)) diff --git a/tests/util.scm b/tests/util.scm index 57d2e538..de8d4b2a 100644 --- a/tests/util.scm +++ b/tests/util.scm @@ -2,7 +2,8 @@ ;; Checks some prodecuders from (calp util) ;;; Code: -(((calp util) filter-sorted set/r!)) +(((calp util) filter-sorted set/r! + find-min find-max)) (test-equal "Filter sorted" '(3 4 5) @@ -16,3 +17,22 @@ (test-error 'syntax-error (test-read-eval-string "(set/r! x err not)")) + + +(call-with-values (lambda () (find-min (iota 10))) + (lambda (extreme rest) + (test-equal "Found correct minimum" + 0 extreme) + (test-equal "Removed \"something\" from the set" + 9 (length rest)))) + + +(call-with-values (lambda () (find-max '("Hello" "Test" "Something long") string-length)) + (lambda (extreme rest) + (test-equal "Found the longest string" "Something long" extreme) + (test-equal "Removed the string" 2 (length rest)) + (test-assert "Other members left 1" (member "Hello" rest)) + (test-assert "Other members left 2" (member "Test" rest)))) + + +(test-error 'misc-error (find-extreme '())) -- cgit v1.2.3