It's definitely due to printing the values. Notice that it also happens
in DrRacket when "show sharing in values" is selected on the Choose
Language dialog.
I'm not aware of a way to prevent the reader from creating objects with
shared structure. You can explicitly disallow use of the #0= sharing
syntax for input, but that has no impact on the object graph output by
the reader.
If you deliberately create an unshared structure (by copying), the check
passes in racket. e.g.,
:
(define shared (list (game-state p p p)))
(define unshared (list (game-state (struct-copy posn p)
(struct-copy posn p)
(struct-copy posn p))))
(check-equal? (~v shared) "(list (game-state (posn 150 100) (posn
150 100) (posn 150 100)))")
(check-equal? (~v unshared) "(list (game-state (posn 150 100) (posn
150 100) (posn 150 100)))")
:
When you substitute this in your code you get the check failure only on
the structure sharing version.
Also notice that
(check-equal? (~v shared) "(list (game-state #0=(posn 150 100) #0#
#0#))")
works consistently. The problem there is being certain your check
string exactly matches the #?# numbers as they would be printed. AFAIK,
the numbers reset to zero for each object, but with a complicated graph,
it's easy to make a mistake writing the check string.
For checking individual objects, my advice is to use check-pred.
Although check-equal? works with static objects, IMO it's really meant
for tests that aren't normally executed - e.g., for comparing results of
different computation methods: the fast "clever" version vs the slow
"brute force" version. MMV ... people have different ideas regarding
appropriate uses of testing.
Hope this helps,
George
On 11/27/2015 7:13 AM, Paolo Giarrusso wrote:
Here's an example showing that this test will not behave consistently in
DrRacket and Racket, because `print` won't.
Is that a bug, or is it a "bad practice (tm)" that I ever use `print` programmatically
and expect specific output? Take the question as "me learning Racket, somewhat
chaotically" :-).
(This question arose in the context of another thread, but I'd like the version
without the context).
```
#lang racket/base
(require rackunit
racket/format)
(struct posn (x y) #:transparent)
(struct game-state (speed circle target) #:transparent)
(define p (posn 150 100))
(list (game-state p p p))
; This test works in DrRacket and fails with `racket -t $ThisFile`
(check-equal? (~v (list (game-state p p p))) "(list (game-state (posn 150 100) (posn
150 100) (posn 150 100)))")
```
The `print` statement produces `(list (game-state (posn 150 100) (posn 150 100)
(posn 150 100)))` in DrRacket and `(list (game-state #0=(posn 150 100) #0#
#0#))` otherwise:
```
$ racket -t printing.rkt
(list (game-state #0=(posn 150 100) #0# #0#))
--------------------
FAILURE
actual: "(list (game-state #0=(posn 150 100) #0# #0#))"
expected: "(list (game-state (posn 150 100) (posn 150 100) (posn 150 100)))"
name: check-equal?
location:
(#<path:/Users/pgiarrusso/AeroFS/Repos/racket-playground-bluevelvet/printing.rkt>
13 0 279 112)
expression: (check-equal? (~v (list (game-state p p p))) "(list (game-state (posn
150 100) (posn 150 100) (posn 150 100)))")
Check failure
--------------------
```
I thought this boiled down to `print-graph`, but it's already false. I also
looked into `(current-print)`, thanks Benjamin Greenman's suggestion, but that
seems to only affect the REPL behavior, not print's.
Cheers,
Paolo
--
You received this message because you are subscribed to the Google Groups "Racket
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.