spinlock_ut.cpp 957 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include "spinlock.h"
  3. Y_UNIT_TEST_SUITE(TSpinLock) {
  4. template <typename TLock>
  5. void TestLock() {
  6. TLock lock;
  7. UNIT_ASSERT(!lock.IsLocked());
  8. lock.Acquire();
  9. UNIT_ASSERT(lock.IsLocked());
  10. lock.Release();
  11. UNIT_ASSERT(!lock.IsLocked());
  12. UNIT_ASSERT(lock.TryAcquire());
  13. UNIT_ASSERT(lock.IsLocked());
  14. UNIT_ASSERT(!lock.TryAcquire());
  15. UNIT_ASSERT(lock.IsLocked());
  16. lock.Release();
  17. UNIT_ASSERT(!lock.IsLocked());
  18. // Lockable requirements
  19. lock.lock();
  20. UNIT_ASSERT(lock.IsLocked());
  21. UNIT_ASSERT(!lock.try_lock());
  22. lock.unlock();
  23. UNIT_ASSERT(!lock.IsLocked());
  24. }
  25. Y_UNIT_TEST(TSpinLock_IsLocked) {
  26. TestLock<TSpinLock>();
  27. }
  28. Y_UNIT_TEST(TAdaptiveLock_IsLocked) {
  29. TestLock<TAdaptiveLock>();
  30. }
  31. } // Y_UNIT_TEST_SUITE(TSpinLock)