[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

unit tests and string comparisons

Joe Van Dyk

1/12/2006 12:57:00 AM

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?


6 Answers

Eric Hodel

1/12/2006 1:04:00 AM

0

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

> 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?

Use unit_diff from ZenTest.

--
Eric Hodel - drbrain@segment7.net - http://se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...




Ara.T.Howard

1/12/2006 1:10:00 AM

0

Joe Van Dyk

1/12/2006 1:15:00 AM

0

On 1/12/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> On Thu, 12 Jan 2006, Joe Van Dyk wrote:
>
> > 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?
>
> 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'.

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.


Joby Bednar

1/12/2006 3:13:00 AM

0

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

Eric Hodel

1/12/2006 5:17:00 AM

0

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

> On 1/12/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
>> On Thu, 12 Jan 2006, Joe Van Dyk wrote:
>>
>>> 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?
>>
>> 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'.
>
> 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.

uh, unit_diff, from ZenTest.

http://rubyforge.org/frs/?group_id=419&relea...

No, really.

--
Eric Hodel - drbrain@segment7.net - http://se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...




James Gray

1/12/2006 1:50:00 PM

0

On Jan 11, 2006, at 11:17 PM, Eric Hodel wrote:

> uh, unit_diff, from ZenTest.
>
> http://rubyforge.org/frs/?group_id=419&relea...
>
> No, really.

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

James Edward Gray II