chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Using epsilon in test egg


From: Alex Shinn
Subject: Re: [Chicken-users] Using epsilon in test egg
Date: Sun, 27 Jul 2014 13:34:40 +0900

On Sun, Jul 27, 2014 at 11:02 AM, Alex Charlton <address@hidden> wrote:

Matt Gushee writes:

> I guess my explanation wasn't entirely clear, but that's pretty much
> what I was already doing. I showed the equality predicate I was using,
> which tests individual values in the list with = . I think my mistake
> was in assuming that (current-test-epsilon) would apply to = in the
> test environment. I'm guessing now that that is not the case.

Ah, yes, I hadn’t understood how you were testing. You’re right that = does not get redefined to use current-test-epsilon. Instead you would have to use your own equality predicate that incorporates it. test defines its approx-equal? as:

    (define (approx-equal? a b epsilon)
      (cond
       ((> (abs a) (abs b))
        (approx-equal? b a epsilon))
       ((zero? b)
        (< (abs a) epsilon))
       (else
        (< (abs (/ (- a b) b)) epsilon))))

Which you could then add to your predicate like so:

    (define (list= l1 l2)
      (and (= (length l1) (length l2))
           (let loop ((l1* l1) (l2* l2))
             (cond
               ((null? l1*) #t)
               ((approx-equal? (car l1*) (car l2*) (current-test-epsilon))
                (loop (cdr l1*) (cdr l2*)))
               (else #f)))))

Easier:

(define list=
  (let ((approx=? (current-test-comparator)))
    (lambda (ls1 ls2)
      (and (= (length ls1) (length ls2))
             (every approx=? ls1 ls2)))))

then

(test-assert (list= '(0.655 0.843 0.200 1.0) (parse-color "167,215,51" #f)))

or

(define-syntax test-rgb
  (syntax-rules ()
    ((test-rgb expected expr)
     (test-rgb #f expected expr))
    ((test-rgb name expected expr)
     (parameterize ((current-test-comparator list=))
       (test name expected expr)))))

(test-rgb '(0.655 0.843 0.200 1.0) (parse-color "167,215,51" #f))

-- 
Alex


reply via email to

[Prev in Thread] Current Thread [Next in Thread]