# Unit tests and string comparisons

**URL:** <https://www.ruby-forum.com/t/unit-tests-and-string-comparisons/61124>\
**Category:** Ruby\
**Created:** [January 12, 2006, 1:57am UTC](https://www.ruby-forum.com/t/unit-tests-and-string-comparisons/61124 "2006-01-12T01:57:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [January 12, 2006, 1:57am UTC](https://www.ruby-forum.com/t/unit-tests-and-string-comparisons/61124/1 "2006-01-12T01:57:32Z")

</div>

Hi,

I’m testing the equality of two rather large strings. They look the  
same to me, but doing

assert\_equal “big ass long string”, “big ass long string”

says that they’re different. And they look very similar to me. I  
know that they’re different though, as doing “big ass long string”.sum  
gives a different checksum than the other one.

Any ideas on how I can easily view the differences between two long  
strings?

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [January 12, 2006, 2:06am UTC](https://www.ruby-forum.com/t/unit-tests-and-string-comparisons/61124/2 "2006-01-12T02:06:35Z")

</div>

On Jan 11, 2006, at 4:57 PM, Joe Van D. wrote:

> strings?  
> Use unit\_diff from ZenTest.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [January 12, 2006, 2:12am UTC](https://www.ruby-forum.com/t/unit-tests-and-string-comparisons/61124/3 "2006-01-12T02:12:36Z")

</div>

On Thu, 12 Jan 2006, Joe Van D. wrote:

> Any ideas on how I can easily view the differences between two long strings?

stupid - but works:

harp:~ \> cat a.rb  
a = “foobar”  
b = “foobaz”

def sdiff a, b  
a = a.split //  
b = b.split //  
a.zip(b).each\_with\_index{|cc,idx| printf “%d: %s\n”, idx,  
cc.inspect unless cc.uniq.size == 1}  
end

sdiff a, b

harp:~ \> ruby a.rb  
5: [“r”, “z”]

or just dump them to two files using ‘od -c’ and then use ‘diff -u’.

regards.

-a

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [January 12, 2006, 4:13am UTC](https://www.ruby-forum.com/t/unit-tests-and-string-comparisons/61124/4 "2006-01-12T04:13:39Z")

</div>

Just for fun… here’s another quick visual method:

s1 = ‘abcdefghijk1mnOpqrstuvwxyz’  
s2 = ‘abcdefghijklmn0pqrstuvwxyz’

puts s1, s2

# XOR the binary of each character, will be 0 if the same

s1.size.times {|i| print s1[i]^s2[i].to\_i==0?’-’:’#’}

Results:  
abcdefghijk1mnOpqrstuvwxyz  
abcdefghijklmn0pqrstuvwxyz  
-----------#–#-----------

(above looks better in a fixed font display)

-Joby

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [January 12, 2006, 2:15am UTC](https://www.ruby-forum.com/t/unit-tests-and-string-comparisons/61124/5 "2006-01-12T02:15:37Z")

</div>

On 1/12/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > know that they’re different though, as doing “big ass long string”.sum  
> > def sdiff a, b
> 
> or just dump them to two files using ‘od -c’ and then use ‘diff -u’.

Thanks, I ended up doing something similar. I thought I recalled  
reading a post here (or maybe on the Rails mailing list) about some  
guy who was comparing large hashes and was having difficulties seeing  
the differences between the two in the unit test output. This is sort  
of a related problem, I guess.

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [January 12, 2006, 6:20am UTC](https://www.ruby-forum.com/t/unit-tests-and-string-comparisons/61124/6 "2006-01-12T06:20:22Z")

</div>

On Jan 11, 2006, at 5:14 PM, Joe Van D. wrote:

> > > says that they’re different. And they look very similar to me. I  
> > > a = “foobar”
> 
> of a related problem, I guess.  
> uh, unit\_diff, from ZenTest.

[http://rubyforge.org/frs/?group\_id=419&release\_id=3133](http://rubyforge.org/frs/?group_id=419&release_id=3133)

No, really.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://segment7.net](http://segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![joe-dumoulin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9dc877/32.png) [@joe-dumoulin](https://www.ruby-forum.com/u/joe-dumoulin)\
**Post date:** [January 12, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/unit-tests-and-string-comparisons/61124/7 "2006-01-12T14:52:06Z")

</div>

On Jan 11, 2006, at 11:17 PM, Eric H. wrote:

> uh, unit\_diff, from ZenTest.
> 
> [http://rubyforge.org/frs/?group\_id=419&release\_id=3133](http://rubyforge.org/frs/?group_id=419&release_id=3133)
> 
> No, really.

I have to agree. This does what you all are building code to do. 🙂

James Edward G. II
