/core/externals/google-toolbox-for-mac/UnitTesting/GTMTestTimerTest.m

http://macfuse.googlecode.com/ · Objective C · 56 lines · 29 code · 6 blank · 21 comment · 0 complexity · 47fbd7322bec0d1bc3f6cfcffdfe0829 MD5 · raw file

  1. //
  2. // GTMTestTimerTest.m
  3. //
  4. // Copyright 2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMSenTestCase.h"
  19. #import "GTMTestTimer.h"
  20. @interface GTMTestTimerTest : GTMTestCase
  21. @end
  22. @implementation GTMTestTimerTest
  23. - (void)testTimer {
  24. GTMTestTimer *timer = GTMTestTimerCreate();
  25. STAssertNotNULL(timer, nil);
  26. GTMTestTimerRetain(timer);
  27. GTMTestTimerRelease(timer);
  28. STAssertEqualsWithAccuracy(GTMTestTimerGetSeconds(timer), 0.0, 0.0, nil);
  29. GTMTestTimerStart(timer);
  30. STAssertTrue(GTMTestTimerIsRunning(timer), nil);
  31. NSRunLoop *loop = [NSRunLoop currentRunLoop];
  32. [loop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  33. GTMTestTimerStop(timer);
  34. // We use greater than (and an almost absurd less than) because
  35. // these tests are very dependant on machine load, and we don't want
  36. // automated tests reporting false negatives.
  37. STAssertGreaterThan(GTMTestTimerGetSeconds(timer), 0.1, nil);
  38. STAssertGreaterThan(GTMTestTimerGetMilliseconds(timer), 100.0,nil);
  39. STAssertGreaterThan(GTMTestTimerGetMicroseconds(timer), 100000.0, nil);
  40. // Check to make sure we're not WAY off the mark (by a factor of 10)
  41. STAssertLessThan(GTMTestTimerGetMicroseconds(timer), 1000000.0, nil);
  42. [loop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  43. GTMTestTimerStart(timer);
  44. [loop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  45. STAssertGreaterThan(GTMTestTimerGetSeconds(timer), 0.2, nil);
  46. GTMTestTimerStop(timer);
  47. STAssertEquals(GTMTestTimerGetIterations(timer), (NSUInteger)2, nil);
  48. GTMTestTimerRelease(timer);
  49. }
  50. @end