jlibtests.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. /*
  14. * Jlib regression tests
  15. *
  16. */
  17. #ifdef _USE_CPPUNIT
  18. #include "jsem.hpp"
  19. #include "unittests.hpp"
  20. class JlibSemTest : public CppUnit::TestFixture
  21. {
  22. public:
  23. CPPUNIT_TEST_SUITE(JlibSemTest);
  24. CPPUNIT_TEST(testSetup);
  25. CPPUNIT_TEST(testSimple);
  26. CPPUNIT_TEST(testCleanup);
  27. CPPUNIT_TEST_SUITE_END();
  28. protected:
  29. void testSetup()
  30. {
  31. }
  32. void testCleanup()
  33. {
  34. }
  35. void testTimedAvailable(Semaphore & sem)
  36. {
  37. unsigned now = msTick();
  38. sem.wait(100);
  39. unsigned taken = msTick() - now;
  40. //Shouldn't cause a reschedule, definitely shouldn't wait for 100s
  41. ASSERT(taken < 5);
  42. }
  43. void testTimedElapsed(Semaphore & sem, unsigned time)
  44. {
  45. unsigned now = msTick();
  46. sem.wait(time);
  47. unsigned taken = msTick() - now;
  48. ASSERT(taken >= time && taken < 2*time);
  49. }
  50. void testSimple()
  51. {
  52. //Some very basic semaphore tests.
  53. Semaphore sem;
  54. sem.signal();
  55. sem.wait();
  56. testTimedElapsed(sem, 100);
  57. sem.signal();
  58. testTimedAvailable(sem);
  59. sem.reinit(2);
  60. sem.wait();
  61. testTimedAvailable(sem);
  62. testTimedElapsed(sem, 5);
  63. }
  64. };
  65. CPPUNIT_TEST_SUITE_REGISTRATION( JlibSemTest );
  66. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( JlibSemTest, "JlibSemTest" );
  67. #endif // _USE_CPPUNIT