I'm write unit test cases for my cocoa class. Below is the test case. I can't figure out why this test case fails, even though the expect
and diff
in the assert message are the same. I suspect it may have something to do with how double or NSTimeInterval is calculated
N开发者_如何学JAVASTimeInterval day = 60*60*24;
NSDate * current = [NSDate date];
NSDate * daysAgo = [NSDate dateWithTimeInterval:-6*day sinceDate:current];
NSTimeInterval diff = [current timeIntervalSinceDate:daysAgo];
NSTimeInterval expect = 6 * day;
STAssertEquals(expect, diff, @"Failed expecting: %ld, getting %ld", expect, diff);
You're comparing NSTimeIntervals
, which are typedef'ed to double
. Read e.g. http://floating-point-gui.de/ (or http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html) to find out why you cannot use exact comparisons for float/double computations. You should always compare intervals, e.g. by using
STAssertEqualsWithAccuracy(expect, diff, 0.001, @"Failed expecting: %ld, getting %ld", expect, diff);
精彩评论