【问题标题】:RackUnit check-eq? returns false for equal StringsRackUnit check-eq?对相等的字符串返回 false
【发布时间】:2018-11-19 19:28:15
【问题描述】:

我对 RackUnit 的 check-eq? 为相等的字符串返回 false 感到有些困惑。

这是我正在尝试的代码:

#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")

运行此测试会导致此错误:

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

我以为我理解了eq? 的含义,但似乎我仍然缺少一些东西……为什么会失败?

我知道get-output-string 调用了bytes->string/utf8,它返回了通过string? 合约的东西,所以我认为这在与文字字符串比较时应该可以工作。

【问题讨论】:

    标签: racket rackunit


    【解决方案1】:

    哦,看来我把 eq?equal? 搞混了...

    eq? 按内存引用进行比较。

    越宽松的越长,equal? 大致按值比较。

    文档,像往常一样,explain this 很好。 Here 更多关于 eq?equal? 和他们奇怪的朋友 eqv? 的详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2016-12-18
      相关资源
      最近更新 更多