summaryrefslogtreecommitdiffstats
path: root/Tests/TestApp/src/RingBufferTest.cpp
blob: b84ac2530e73e1348ce40cf6eab6e372156ff7cc (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*     Copyright 2015-2019 Egor Yusov
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
 *
 *  In no event and under no legal theory, whether in tort (including negligence), 
 *  contract, or otherwise, unless required by applicable law (such as deliberate 
 *  and grossly negligent acts) or agreed to in writing, shall any Contributor be
 *  liable for any damages, including any direct, indirect, special, incidental, 
 *  or consequential damages of any character arising as a result of this License or 
 *  out of the use or inability to use the software (including but not limited to damages 
 *  for loss of goodwill, work stoppage, computer failure or malfunction, or any and 
 *  all other commercial damages or losses), even if such Contributor has been advised 
 *  of the possibility of such damages.
 */

// EngineSandbox.cpp : Defines the entry point for the application.
//

#include "pch.h"
#include "RingBuffer.h"
#include "DefaultRawMemoryAllocator.h"
#include "DebugUtilities.h"
#include "UnitTestBase.h"

using namespace Diligent;

class RingBufferTest : public UnitTestBase
{
public:
    RingBufferTest();
};

static RingBufferTest TheRingBufferTest;

RingBufferTest::RingBufferTest() :
    UnitTestBase("Ring buffer test")
{
    auto &Allocator = DefaultRawMemoryAllocator::GetAllocator();
    {
        RingBuffer RB(1023, Allocator);

        auto Offset = RB.Allocate(120, 16);
        //
        //  O          h
        //  |          |                                      |
        //  0         128
        VERIFY_EXPR(Offset == 0);


        Offset = RB.Allocate(10, 1);
        //
        //  t          O   h
        //  |          |   |                                  |
        //  0         128 138
        VERIFY_EXPR(Offset == 128);

        Offset = RB.Allocate(10, 32);
        //
        //  t                  O   h 
        //  |                  |   |                          |
        //  0         128 138 160 192
        VERIFY_EXPR(Offset == 160);

        Offset = RB.Allocate(17, 1);
        //
        //  t                      O   h
        //  |                      |   |                      |
        //  0         128 138 160 192 209
        VERIFY_EXPR(Offset == 192);

        Offset = RB.Allocate(65, 64);
        //                          
        //  t                               O    h   
        //  |                               |    |            |
        //  0         128 138 160 192 209  256  384
        VERIFY_EXPR(Offset == 256);

        RB.FinishCurrentFrame(1);
        //                          
        //  t          h1                            
        //  |          |                                      |
        //  0         384                          


        Offset = RB.Allocate(100, 256);
        //                          
        //  t          h1    O      h                
        //  |          |     |      |                         |
        //  0         384   512    768             
        VERIFY_EXPR(Offset == 512);


        Offset = RB.Allocate(127, 1);
        //                          
        //  t          h1           O         h       
        //  |          |            |         |               |
        //  0         384   512    768      895 = 1023-128   
        VERIFY_EXPR(Offset == 768);

        Offset = RB.Allocate(128, 2);
        VERIFY_EXPR(Offset == RingBuffer::InvalidOffset);

        Offset = RB.Allocate(128, 1);
        //                          
        //  t          h1                      O              h
        //  |          |                       |              |
        //  0         384   512    768        895           1023
        VERIFY_EXPR(Offset == 895);

        RB.FinishCurrentFrame(2);
        //                          
        //  t          h1                                     h2,h
        //  |          |                                      |
        //  0         384                                   1023
        VERIFY_EXPR(RB.IsFull());

        Offset = RB.Allocate(1, 1);
        VERIFY_EXPR(Offset == RingBuffer::InvalidOffset);

        RingBuffer RB1(std::move(RB));
        VERIFY_EXPR(RB.IsEmpty());
        RB1.ReleaseCompletedFrames(1);
        //                          
        //             t                                      h2
        //  |          |                                      |
        //  0         384                                   1023

        RB1.ReleaseCompletedFrames(2);
        //                          
        //                                                   h,t
        //  |                                                 |
        //  0                                               1023
        VERIFY_EXPR(RB1.IsEmpty());
        VERIFY_EXPR(RB1.GetUsedSize() == 0);

        Offset = RB1.Allocate(256,1);
        //                          
        //  O        h                                        t
        //  |        |                                        |
        //  0       256                                     1023
        VERIFY_EXPR(Offset == 0);


        Offset = RB1.Allocate(256, 16);
        RB1.ReleaseCompletedFrames(0);
        //                          
        //           O      h                                 t
        //  |        |      |                                 |
        //  0       256    512                              1023
        VERIFY_EXPR(Offset == 256);
        RB1.FinishCurrentFrame(2);
        RB1.FinishCurrentFrame(3); // ignored
        //                          
        //                  h2                                t
        //  |               |                                 |
        //  0              512                              1023
        RB1.ReleaseCompletedFrames(2);
        //                          
        // h,t                                                 
        //  |                                                 |
        //  0                                               1023
        
        RB1.ReleaseCompletedFrames(3);

        VERIFY_EXPR(RB1.GetUsedSize() == 0);
        VERIFY_EXPR(RB1.IsEmpty());

        Offset = RB1.Allocate(512,1);
        //                          
        //  O               h                                  
        //  |               |                                 |
        //  0              512                              1023
        VERIFY_EXPR(Offset == 0);

        RB1.FinishCurrentFrame(4);
        RB1.FinishCurrentFrame(5);
        //                          
        //  t               h4   
        //  |               |                                 |
        //  0              512                              1023

        Offset = RB1.Allocate(129,1);
        //                          
        //  t              h4,O       h
        //  |               |         |                       |
        //  0              512       641                    1023
        VERIFY_EXPR(Offset == 512);

        RB1.ReleaseCompletedFrames(4);
        //                          
        //                  t         h
        //  |               |         |                       |
        //  0              512       641                    1023


        Offset = RB1.Allocate(128,128);
        //                          
        //                  t                  O      h        
        //  |               |                  |      |       |
        //  0              512       641      768    896    1023
        VERIFY_EXPR(Offset == 768);

        Offset = RB1.Allocate(513,64);
        VERIFY_EXPR(Offset == RingBuffer::InvalidOffset);

        Offset = RB1.Allocate(513,1);
        VERIFY_EXPR(Offset == RingBuffer::InvalidOffset);

        Offset = RB1.Allocate(255,1);
        //                          
        //  O     h         t                                  
        //  |     |         |                                 |
        //  0    255       512                              1023
        VERIFY_EXPR(Offset == 0);

        RB1.FinishCurrentFrame(6);
        //                          
        //  O    h,h6       t                                  
        //  |     |         |                                 |
        //  0    255       512                              1023


        Offset = RB1.Allocate(256, 2);
        //                          
        //       h6   O    t,h                                
        //  |     |   |     |                                 |
        //  0    255 256   512                              1023
        VERIFY_EXPR(Offset == 256);

        VERIFY_EXPR(RB1.IsFull());
        Offset = RB1.Allocate(1,1);
        VERIFY_EXPR(Offset == RingBuffer::InvalidOffset);
        RB1.ReleaseCompletedFrames(6);
        //                          
        //        t         h                                
        //  |     |         |                                 |
        //  0    255       512                              1023

        Offset = RB1.Allocate(511, 1);
        //                          
        //        t         O                                 h
        //  |     |         |                                 |
        //  0    255       512                              1023
        VERIFY_EXPR(Offset == 512);

        Offset = RB1.Allocate(191,1);
        //                          
        //  O       h          t                             
        //  |       |          |                              |
        //  0      191        255                           1023
        VERIFY_EXPR(Offset == 0);

        Offset = RB1.Allocate(64,2);
        VERIFY_EXPR(Offset == RingBuffer::InvalidOffset);

        Offset = RB1.Allocate(64,1);
        //                          
        //          O         t,h                           
        //  |       |          |                              |
        //  0      191        255                           1023
        VERIFY_EXPR(Offset == 191);

        Offset = RB1.Allocate(1,1);
        VERIFY_EXPR(Offset == RingBuffer::InvalidOffset);

        RB1.FinishCurrentFrame(7);
        RB1.ReleaseCompletedFrames(7);
    }

    {
        RingBuffer RB(1024, Allocator);
        auto offset = RB.Allocate(512, 1);
        RB.FinishCurrentFrame(0);
        RB.FinishCurrentFrame(1);
        RB.ReleaseCompletedFrames(1);
        RB.FinishCurrentFrame(2);
        RB.FinishCurrentFrame(3);
        offset = RB.Allocate(512, 1);
        RB.FinishCurrentFrame(4);
        RB.ReleaseCompletedFrames(2);
        RB.ReleaseCompletedFrames(3);
        RB.ReleaseCompletedFrames(4);
        offset = RB.Allocate(512, 1);
        RB.FinishCurrentFrame(5);
        RB.ReleaseCompletedFrames(5);
    }

    SetStatus(TestResult::Succeeded);
}