cc_semaphore_ut.cpp 988 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include "cc_semaphore.h"
  3. #include <library/cpp/deprecated/atomic/atomic.h>
  4. namespace {
  5. struct TTestSemaphore: public TComplexConditionSemaphore<TTestSemaphore> {
  6. TAtomic Current;
  7. TTestSemaphore()
  8. : Current(0)
  9. {
  10. }
  11. bool TryWait() {
  12. return AtomicGet(Current) > 0;
  13. }
  14. void Aquire() {
  15. Wait();
  16. AtomicDecrement(Current);
  17. }
  18. void Release() {
  19. AtomicIncrement(Current);
  20. Updated();
  21. }
  22. };
  23. }
  24. Y_UNIT_TEST_SUITE(TComplexConditionSemaphore) {
  25. Y_UNIT_TEST(Simple) {
  26. TTestSemaphore sema;
  27. UNIT_ASSERT(!sema.TryWait());
  28. sema.Release();
  29. UNIT_ASSERT(sema.TryWait());
  30. sema.Release();
  31. UNIT_ASSERT(sema.TryWait());
  32. sema.Aquire();
  33. UNIT_ASSERT(sema.TryWait());
  34. sema.Aquire();
  35. UNIT_ASSERT(!sema.TryWait());
  36. }
  37. }