aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/actor/ring_buffer_ut.cpp
blob: 9b2f46957b4237ec6604aa8691a0ae35642da8be (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <library/cpp/testing/unittest/registar.h>
 
#include "ring_buffer.h" 
 
#include <util/random/random.h>

Y_UNIT_TEST_SUITE(RingBuffer) {
    struct TRingBufferTester { 
        TRingBuffer<unsigned> RingBuffer; 
 
        unsigned NextPush; 
        unsigned NextPop; 
 
        TRingBufferTester()
            : NextPush()
            , NextPop()
        {
        }
 
        void Push() { 
            //Cerr << "push " << NextPush << "\n"; 
            RingBuffer.Push(NextPush); 
            NextPush += 1; 
        } 
 
        void Pop() { 
            //Cerr << "pop " << NextPop << "\n"; 
            unsigned popped = RingBuffer.Pop(); 
            UNIT_ASSERT_VALUES_EQUAL(NextPop, popped); 
            NextPop += 1; 
        } 
 
        bool Empty() const { 
            UNIT_ASSERT_VALUES_EQUAL(RingBuffer.Size(), NextPush - NextPop); 
            UNIT_ASSERT_VALUES_EQUAL(RingBuffer.Empty(), RingBuffer.Size() == 0); 
            return RingBuffer.Empty(); 
        } 
    }; 
 
    void Iter() { 
        TRingBufferTester rb; 
 
        while (rb.NextPush < 1000) { 
            rb.Push(); 
            while (!rb.Empty() && RandomNumber<bool>()) { 
                rb.Pop(); 
            } 
        } 
 
        while (!rb.Empty()) { 
            rb.Pop(); 
        } 
    } 
 
    Y_UNIT_TEST(Random) {
        for (unsigned i = 0; i < 100; ++i) { 
            Iter(); 
        } 
    } 
}