aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/spinlock_ut.cpp
blob: f2793fd95f126fce96b44d9052f2287885bfc019 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <library/cpp/testing/unittest/registar.h>
 
#include "spinlock.h" 
 
Y_UNIT_TEST_SUITE(TSpinLock) {
    template <typename TLock> 
    void TestLock() { 
        TLock lock; 
        UNIT_ASSERT(!lock.IsLocked()); 
        lock.Acquire(); 
        UNIT_ASSERT(lock.IsLocked()); 
        lock.Release(); 
        UNIT_ASSERT(!lock.IsLocked()); 
 
        UNIT_ASSERT(lock.TryAcquire()); 
        UNIT_ASSERT(lock.IsLocked()); 
        UNIT_ASSERT(!lock.TryAcquire()); 
        UNIT_ASSERT(lock.IsLocked()); 
        lock.Release(); 
        UNIT_ASSERT(!lock.IsLocked()); 

        // Lockable requirements
        lock.lock();
        UNIT_ASSERT(lock.IsLocked());
        UNIT_ASSERT(!lock.try_lock());
        lock.unlock();
        UNIT_ASSERT(!lock.IsLocked());
    } 
 
    Y_UNIT_TEST(TSpinLock_IsLocked) {
        TestLock<TSpinLock>(); 
    } 
 
    Y_UNIT_TEST(TAdaptiveLock_IsLocked) {
        TestLock<TAdaptiveLock>(); 
    } 
}