# Can't see my error on unit test, assertion dump is identical

**URL:** <https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213>\
**Category:** Help!\
**Created:** [April 22, 2020, 10:14pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213 "2020-04-22T22:14:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 22, 2020, 10:14pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/1 "2020-04-22T22:14:53Z")

</div>

```prolog
ast: ?- run_tests(buffer_tokenising:bulk_comments).
% PL-Unit: buffer_tokenising:bulk_comments 
ERROR: /home/sean/Documents/code/prolog/the-f-word-project/tokeniser.plt:118:
        test bulk_comments: wrong answer (compared using ==)
ERROR: Expected: [com1((0,1,0),";line1"),o_list((7,2,0)),token((8,2,1),"HELLO"),c_list((13,2,6)),comn((14,2,7),"/* haha */"),o_map((24,2,17)),token((25,2,18),"WORLD"),c_map((30,2,23))]
ERROR: Got: [com1((0,1,0),";line1"),o_list((7,2,0)),token((8,2,1),"HELLO"),c_list((13,2,6)),comn((14,2,7),"/* haha */"),o_map((24,2,17)),token((25,2,18),"WORLD"),c_map((30,2,23))]
 done
% 1 test failed
% 0 tests passed

```

I have been scratching my head for hours now, here is the test, the tokeniser is “sound” as lots of the other tests are still passing fine but this one…

```prolog
test(bulk_comments, [true(Out==Exp)]) :-
    Exp = [com1( (0,1,0), ";line1"),
           o_list((7,2,0)),
           token( (8,2,1), "HELLO"),
           c_list((13,2,6)),
           comn( (14,2,7), "/* haha */"),
           o_map( (24,2,17)),
           token( (25,2,18), "WORLD"),
           c_map( (30,2,23))
          ],
    tokenise(`;line1\n[HELLO]/* haha */{WORLD}`, Out, []).

```

I have read and re-read the unit test notes but I just can’t see why it would be saying failed…  
☹

---

<div class="post-metadata">

**Author:** ![Ian](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ian/32/1026_2.png) [@Ian](https://swi-prolog.discourse.group/u/Ian)\
**Post date:** [April 22, 2020, 10:24pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/2 "2020-04-22T22:24:12Z")

</div>

You are testing using `==` operator, which is only true if the Out and Exp point to the exact same structure in memory. You probably just want to compare using `=` instead.

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 22, 2020, 10:26pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/3 "2020-04-22T22:26:25Z")

</div>

**All** of my tests use ==/2 and most are fine…have I been incredibly lucky…!!! I will change it and see what happens!

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 22, 2020, 10:31pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/4 "2020-04-22T22:31:12Z")

</div>

The doc for ==/2 says:

> True if Term1 is equivalent to Term2. A variable is only identical to a sharing variable.

That last term…is it possible that all my tests have ended up “sharing” a common internal structure if the expectation was the same as the output, would it be referencing the same internal memory with me being totally unaware???

Running the test with =/2 has made no difference to the result:

```prolog
ERROR: /home/sean/Documents/code/prolog/the-f-word-project/tokeniser.plt:118:
        test bulk_comments: wrong answer (compared using =)
ERROR: Expected: [com1((0,1,0),";line1"),o_list((7,2,0)),token((8,2,1),"HELLO"),c_list((13,2,6)),comn((14,2,7),"/* haha */"),o_map((24,2,17)),token((25,2,18),"WORLD"),c_map((30,2,23))]
ERROR: Got: [com1((0,1,0),";line1"),o_list((7,2,0)),token((8,2,1),"HELLO"),c_list((13,2,6)),comn((14,2,7),"/* haha */"),o_map((24,2,17)),token((25,2,18),"WORLD"),c_map((30,2,23))]

```

back t’drawing board…

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [April 22, 2020, 10:31pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/5 "2020-04-22T22:31:17Z")

</div>

No! Never compare with **term unification** unless you further test the bindings resulting from the unification. If you have a bug where a binding is missing, unification will succeed and will mask the bug.

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 22, 2020, 10:31pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/6 "2020-04-22T22:31:45Z")

</div>

That’s why I used ==/2, I read that in the docs somewhere last week

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [April 22, 2020, 10:32pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/7 "2020-04-22T22:32:43Z")

</div>

> [@Ian](#):
>
> You are testing using `==` operator, which is only true if the Out and Exp point to the exact same structure in memory.

Not quite. `=/2` is the same as `==/2` if there are no variables. You can see this the following, which succeeds even if the two arguments are not the same location in memory:

```prolog
?- X =..[foo,bar], X == foo(bar).
X = foo(bar).

```

@emacstheviking - Do you perhaps have a `portray` that is confusing things? You might try outputting the two values using `write_canonical`.

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 22, 2020, 10:40pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/8 "2020-04-22T22:40:06Z")

</div>

yes, I think I have `portray_test(true).` in my load.pl file… I will comment it out and see what happens with a brand new session…

Maybe getting somewhere now! Now you can see a problem indeed… my expectation is using backticks but the tokensiation is producing lists of character codes…I think I git confused somewhere!

```prolog
ERROR: /home/sean/Documents/code/prolog/the-f-word-project/tokeniser.plt:118:
        test bulk_comments: wrong answer (compared using =)
ERROR: Expected: [com1((0,1,0),";line1"),o_list((7,2,0)),token((8,2,1),"HELLO"),c_list((13,2,6)),comn((14,2,7),"/* haha */"),o_map((24,2,17)),token((25,2,18),"WORLD"),c_map((30,2,23))]
ERROR: Got: [com1((0,1,0),[59,108,105,110,101,49]),o_list((7,2,0)),token((8,2,1),[72,69,76,76,79]),c_list((13,2,6)),comn((14,2,7),[47,42,32,104,97,104,97,32,42,47]),o_map((24,2,17)),token((25,2,18),[87,7

```

Not sure what happened, I feel a bit dazed and confused because not all the tests fail, in fact out of 92 I have 9 fails and mostly they are because of work in progress apart from this one…

I need to re-read about strings, character codes etc…I think that might be the reason but I still can’t understand (yet) why other tests pass!!

Gotta love computers…

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 22, 2020, 10:41pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/9 "2020-04-22T22:41:36Z")

</div>

OH NOOOO…I _actually typed the "_\* in the test code, I just looked!!!

🤣

What the … ? that explains that one then…

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 22, 2020, 10:42pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/10 "2020-04-22T22:42:58Z")

</div>

```prolog
ast: ?- run_tests(buffer_tokenising:bulk_comments).
% PL-Unit: buffer_tokenising:bulk_comments . done
% test passed
true.

```

Thanks everybody, @peter.ludemann …the portray is staying off until further notice!  
👍

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [April 22, 2020, 10:50pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/11 "2020-04-22T22:50:25Z")

</div>

> [@emacstheviking](#):
>
> is it possible that all my tests have ended up “sharing” a common internal structure if the expectation was the same as the output

The definition of `==/2` doesn’t care whether there’s any structure sharing or not.(\*) If two things look the same _using write\_canonical and numbervars_ they are the same and there’s no way to tell them apart. Here’s an example (`X==Y` fails but `X=Y` succeeds):

```prolog
?- X=f(A,B), Y=f(A,C), Both=[X,Y], numbervars(Both), write_canonical(Both).
[f('$VAR'(0),'$VAR'(1)),f('$VAR'(0),'$VAR'(2))]

```

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [April 22, 2020, 11:10pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/12 "2020-04-22T23:10:33Z")

</div>

(I forgot to fill in my footnote)

(\*) In the early days of Prolog implementation, there was debate about “structure sharing” and “structure copying”. Nowadays, almost all implementations use “structure copying”. However, it is completely opaque to the user which implementation is used, although speed and memory usage can be affected.

Prolog has no equivalent to Java’s “==” or Python’s “is”, both of which are essentially pointer equivalency tests. [JavaScript probably also has something similar with its “==”, “===”, “Object.is”, but JavaScript is such a hot mess of bad language and library design that I avoid it as much as possible.]

---

<div class="post-metadata">

**Author:** ![emacstheviking](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/emacstheviking/32/228_2.png) [@emacstheviking](https://swi-prolog.discourse.group/u/emacstheviking)\
**Post date:** [April 22, 2020, 11:11pm UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/13 "2020-04-22T23:11:14Z")

</div>

I agree about JavaScript…PHP also fits that bill!

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [April 23, 2020, 7:06am UTC](https://swi-prolog.discourse.group/t/cant-see-my-error-on-unit-test-assertion-dump-is-identical/2213/14 "2020-04-23T07:06:28Z")

</div>

> [@peter.ludemann](#):
>
> Prolog has no equivalent to Java’s “==”

SWI-Prolog has [same\_term/2](https://www.swi-prolog.org/pldoc/doc_for?object=same_term/2), which is logically meaningless, but can help figuring out stuff related to destructive assignment such as using nb\_setarg/3. Related it has both copy\_term/2 and duplicate\_term/2. Copy\_term/2 merely guarantees that variables are fresh, i.e., it _shares_ ground subterms while duplicate\_term/2 really copies, regardless of groundness.
