RackUnit check-eq? returns false for equal Strings

88 Views Asked by At

I am a little bit puzzled with RackUnit's check-eq? returning false for equal strings.

This is the code I am trying:

#lang racket

(require rackunit)

(define (get-output proc)
  (let ([out (open-output-string)])
    (parameterize ([current-output-port out])
      (proc)
      (get-output-string out))))

(define output (get-output
                (λ () (display "hello"))))

(check-eq? output "hello")

Running this test results in this error:

--------------------
. FAILURE
name:       check-eq?
location:   unsaved-editor:14:0
actual:     "hello"
expected:   "hello"
--------------------

I thought I understood the meaning of eq? but it seems I'm still missing something... why is this failing?

I know that get-output-string calls bytes->string/utf8, which returns something that passes the string? contract, so I assumed this should work when comparing to a literal string.

1

There are 1 best solutions below

0
Renato On

Oh, looks like I mixed up eq? and equal?...

eq? compares by memory reference.

The more lenient one is the longer one, equal? which compares by value, roughly.

The docs, as usual, explain this quite well. Here more details about eq?, equal? and their weird friend, eqv?.