unit testing with class and floats

This is a discussion on unit testing with class and floats within the RUBY forums in Programming Languages category; Hi there im learning unit testing and have just recently started using classes (very basic no superclass etc) and now running into problems. I have a function which returns a class ojbect that contains float variables. The assert_equal is constantly failing. However when it prints out a summary of the two things i compared in the assert they are exaclty the same. So im wondering if its something to do with classes, perhaps i have to define a method in the class which tells assert how to compare objects of that class or its to do with the fact there ...

Go Back   Application Development Forum > Programming Languages > RUBY

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 04:51 AM
Adam Akhtar
Guest
 
Default unit testing with class and floats

Hi there

im learning unit testing and have just recently started using classes
(very basic no superclass etc) and now running into problems.

I have a function which returns a class ojbect that contains float
variables.
The assert_equal is constantly failing. However when it prints out a
summary of the two things i compared in the assert they are exaclty the
same.

So im wondering if its something to do with classes, perhaps i have to
define a method in the class which tells assert how to compare objects
of that class

or its to do with the fact there are floats in the object (i checked
previous posts and google and found out there are issues with floats and
assert)

or perhaps both.
--
Posted via http://www.ruby-forum.com/.

Reply With Quote
  #2  
Old 08-27-2008, 04:55 AM
Adam Akhtar
Guest
 
Default Re: unit testing with class and floats

sorry a (simplified) example

def simpleclass
attr_accessor :big_num #its a float!!!
end

expected = simpleclass.new(105.234)

assert_equal(expected, somefunction(blahblah))

i expect somefunction to return a class with 105.234

in the summary it shows that the function does in fact do that but it
still fails it.


--
Posted via http://www.ruby-forum.com/.

Reply With Quote
  #3  
Old 08-27-2008, 05:03 AM
Farrel Lifson
Guest
 
Default Re: unit testing with class and floats

2008/8/27 Adam Akhtar <adamtemporary@gmail.com>:
> sorry a (simplified) example
>
> def simpleclass
> attr_accessor :big_num #its a float!!!
> end
>
> expected = simpleclass.new(105.234)
>
> assert_equal(expected, somefunction(blahblah))
>
> i expect somefunction to return a class with 105.234
>
> in the summary it shows that the function does in fact do that but it
> still fails it.


When comparing floats there is a chance that although the floats might
be represented the same on screen there might be differences in their
storage. For instance 1.0 and 1.0000001 might look the same on the
screen but will not be the same if compared with ==.

You should rather use assert_in_delta when comparing two floats.

Farrel
--
Aimred - Ruby Development and Consulting
http://www.aimred.com

Reply With Quote
  #4  
Old 08-28-2008, 01:03 AM
Adam Akhtar
Guest
 
Default Re: unit testing with class and floats

ahhh i thought you could only use assert_in_delta with only floats
directly. Ill try passing my class objects with it.

Thanks



--
Posted via http://www.ruby-forum.com/.

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:23 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.