Skip to content

Commit 7fa4981

Browse files
author
Andrei Nasonov
committed
AI review: move code to a separate file, more asserts
1 parent 92414f2 commit 7fa4981

5 files changed

Lines changed: 116 additions & 71 deletions

File tree

cloud/storage/core/libs/file_backed_containers/file_ring_buffer_accessor_ut.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "file_ring_buffer_accessor.h"
44

5+
#include <cloud/storage/core/libs/file_backed_containers/test/util.h>
6+
57
#include <library/cpp/testing/unittest/registar.h>
68

79
#include <util/stream/output.h>
@@ -142,11 +144,6 @@ struct TTestFileRingBufferAccessor: public TFileRingBufferAccessor
142144

143145
////////////////////////////////////////////////////////////////////////////////
144146

145-
static bool operator==(bool lhs, const TResultOrError<bool>& rhs)
146-
{
147-
return !HasError(rhs) && lhs == rhs.GetResult();
148-
}
149-
150147
Y_UNIT_TEST_SUITE(TFileRingBufferAccessorTest)
151148
{
152149
Y_UNIT_TEST(ShouldValidateEmptyFile)
@@ -528,12 +525,17 @@ Y_UNIT_TEST_SUITE(TFileRingBufferAccessorTest)
528525
b.Execute(
529526
[](TFileRingBuffer& rb)
530527
{
531-
while (rb.PushBack("ABCD").GetResult()) {
528+
while (true) {
532529
// Add elements until the buffer is full
530+
auto res = rb.PushBack("ABCD");
531+
UNIT_ASSERT(!HasError(res));
532+
if (!res.GetResult()) {
533+
break;
534+
}
533535
}
534536

535-
rb.PopFront();
536-
rb.PopFront();
537+
UNIT_ASSERT_VALUES_EQUAL(true, rb.PopFront());
538+
UNIT_ASSERT_VALUES_EQUAL(true, rb.PopFront());
537539

538540
UNIT_ASSERT_VALUES_EQUAL(true, rb.PushBack("wrap"));
539541
},
@@ -548,14 +550,19 @@ Y_UNIT_TEST_SUITE(TFileRingBufferAccessorTest)
548550
b.Execute(
549551
[](TFileRingBuffer& rb)
550552
{
551-
while (rb.PushBack("ABCD").GetResult()) {
553+
while (true) {
552554
// Add elements until the buffer is full
555+
auto res = rb.PushBack("ABCD");
556+
UNIT_ASSERT(!HasError(res));
557+
if (!res.GetResult()) {
558+
break;
559+
}
553560
}
554561

555-
rb.PopFront();
556-
rb.PopFront();
562+
UNIT_ASSERT_VALUES_EQUAL(true, rb.PopFront());
563+
UNIT_ASSERT_VALUES_EQUAL(true, rb.PopFront());
557564

558-
rb.Alloc(4);
565+
UNIT_ASSERT(!HasError(rb.Alloc(4)));
559566
},
560567
ver);
561568

@@ -731,6 +738,7 @@ Y_UNIT_TEST_SUITE(TFileRingBufferAccessorTest)
731738

732739
} // namespace NCloud
733740

741+
734742
template <>
735743
void Out<NCloud::EFileRingBufferAccessorValidationStatus>(
736744
IOutputStream& out,

cloud/storage/core/libs/file_backed_containers/file_ring_buffer_ut.cpp

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "file_ring_buffer.h"
22
#include "file_ring_buffer_accessor.h"
33

4+
#include <cloud/storage/core/libs/file_backed_containers/test/util.h>
5+
46
#include <library/cpp/string_utils/base64/base64.h>
57
#include <library/cpp/testing/unittest/registar.h>
68

@@ -14,21 +16,6 @@ namespace NCloud {
1416

1517
using EVersion = EFileRingBufferVersion;
1618

17-
static bool operator==(bool lhs, const TResultOrError<bool>& rhs)
18-
{
19-
return !HasError(rhs) && lhs == rhs.GetResult();
20-
}
21-
22-
static bool operator==(ui32 lhs, const TResultOrError<ui32>& rhs)
23-
{
24-
return !HasError(rhs) && lhs == rhs.GetResult();
25-
}
26-
27-
static bool operator==(TString lhs, const TResultOrError<TStringBuf>& rhs)
28-
{
29-
return !HasError(rhs) && lhs == TString(rhs.GetResult());
30-
}
31-
3219
namespace {
3320

3421
////////////////////////////////////////////////////////////////////////////////
@@ -448,8 +435,13 @@ Y_UNIT_TEST_SUITE(TFileRingBufferTest)
448435
} else {
449436
UNIT_ASSERT_VALUES_EQUAL(Dump(ri), Dump(*rb));
450437
// Cerr << "POP\t" << ri.Front() << Endl;
451-
ri.PopFront();
452-
rb->PopFront();
438+
auto riResult = ri.PopFront();
439+
auto rbResult = rb->PopFront();
440+
UNIT_ASSERT(!HasError(riResult));
441+
UNIT_ASSERT(!HasError(rbResult));
442+
UNIT_ASSERT_VALUES_EQUAL(
443+
riResult.GetResult(),
444+
rbResult.GetResult());
453445
}
454446

455447
// Cerr << ri.Size() << " " << remainingBytes << Endl;
@@ -1158,7 +1150,7 @@ Y_UNIT_TEST_SUITE(TFileRingBufferTest)
11581150
UNIT_ASSERT(!HasError(alloc));
11591151
data.copy(alloc.GetResult(), data.size());
11601152

1161-
rb->PopFront();
1153+
UNIT_ASSERT_VALUES_EQUAL(true, rb->PopFront());
11621154

11631155
UNIT_ASSERT(!HasError(rb->Commit()));
11641156

@@ -1220,8 +1212,8 @@ Y_UNIT_TEST_SUITE(TFileRingBufferTest)
12201212
UNIT_ASSERT(ptr4 != nullptr);
12211213
UNIT_ASSERT_VALUES_EQUAL(2, rb->GetTag(ptr4));
12221214

1223-
rb->PopFront();
1224-
rb->PopFront();
1215+
UNIT_ASSERT_VALUES_EQUAL(true, rb->PopFront());
1216+
UNIT_ASSERT_VALUES_EQUAL(true, rb->PopFront());
12251217
UNIT_ASSERT(rb->Empty());
12261218

12271219
// Reuse entry
@@ -1273,7 +1265,7 @@ Y_UNIT_TEST_SUITE(TFileRingBufferTest)
12731265
8,
12741266
srcVersion);
12751267

1276-
rb->SetMetadata("abc");
1268+
UNIT_ASSERT_VALUES_EQUAL(true, rb->SetMetadata("abc"));
12771269
UNIT_ASSERT_VALUES_EQUAL(true, rb->PushBack("123"));
12781270
UNIT_ASSERT_VALUES_EQUAL(true, rb->PushBack("4"));
12791271
UNIT_ASSERT_VALUES_EQUAL(true, rb->PushBack("xz"));
@@ -1362,41 +1354,3 @@ Y_UNIT_TEST_SUITE(TFileRingBufferTest)
13621354
}
13631355

13641356
} // namespace NCloud
1365-
1366-
////////////////////////////////////////////////////////////////////////////////
1367-
1368-
template <>
1369-
void Out<NCloud::TResultOrError<bool>>(
1370-
IOutputStream& os,
1371-
const NCloud::TResultOrError<bool>& value)
1372-
{
1373-
if (HasError(value)) {
1374-
os << value.GetError();
1375-
} else {
1376-
os << value.GetResult();
1377-
}
1378-
}
1379-
1380-
template <>
1381-
void Out<NCloud::TResultOrError<ui32>>(
1382-
IOutputStream& os,
1383-
const NCloud::TResultOrError<ui32>& value)
1384-
{
1385-
if (HasError(value)) {
1386-
os << value.GetError();
1387-
} else {
1388-
os << value.GetResult();
1389-
}
1390-
}
1391-
1392-
template <>
1393-
void Out<NCloud::TResultOrError<TStringBuf>>(
1394-
IOutputStream& os,
1395-
const NCloud::TResultOrError<TStringBuf>& value)
1396-
{
1397-
if (HasError(value)) {
1398-
os << value.GetError();
1399-
} else {
1400-
os << value.GetResult();
1401-
}
1402-
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "util.h"
2+
3+
////////////////////////////////////////////////////////////////////////////////
4+
5+
bool operator==(bool lhs, const NCloud::TResultOrError<bool>& rhs)
6+
{
7+
return !HasError(rhs) && lhs == rhs.GetResult();
8+
}
9+
10+
bool operator==(ui32 lhs, const NCloud::TResultOrError<ui32>& rhs)
11+
{
12+
return !HasError(rhs) && lhs == rhs.GetResult();
13+
}
14+
15+
bool operator==(
16+
TString lhs,
17+
const NCloud::TResultOrError<TStringBuf>& rhs)
18+
{
19+
return !HasError(rhs) && lhs == TString(rhs.GetResult());
20+
}
21+
22+
template <>
23+
void Out<NCloud::TResultOrError<bool>>(
24+
IOutputStream& os,
25+
const NCloud::TResultOrError<bool>& value)
26+
{
27+
if (HasError(value)) {
28+
os << value.GetError();
29+
} else {
30+
os << value.GetResult();
31+
}
32+
}
33+
34+
template <>
35+
void Out<NCloud::TResultOrError<ui32>>(
36+
IOutputStream& os,
37+
const NCloud::TResultOrError<ui32>& value)
38+
{
39+
if (HasError(value)) {
40+
os << value.GetError();
41+
} else {
42+
os << value.GetResult();
43+
}
44+
}
45+
46+
template <>
47+
void Out<NCloud::TResultOrError<TStringBuf>>(
48+
IOutputStream& os,
49+
const NCloud::TResultOrError<TStringBuf>& value)
50+
{
51+
if (HasError(value)) {
52+
os << value.GetError();
53+
} else {
54+
os << value.GetResult();
55+
}
56+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <cloud/storage/core/libs/common/error.h>
4+
5+
#include <util/generic/string.h>
6+
7+
////////////////////////////////////////////////////////////////////////////////
8+
9+
bool operator==(bool lhs, const NCloud::TResultOrError<bool>& rhs);
10+
bool operator==(ui32 lhs, const NCloud::TResultOrError<ui32>& rhs);
11+
bool operator==(TString lhs, const NCloud::TResultOrError<TStringBuf>& rhs);
12+
13+
template <>
14+
void Out<NCloud::TResultOrError<bool>>(
15+
IOutputStream& os,
16+
const NCloud::TResultOrError<bool>& value);
17+
18+
template <>
19+
void Out<NCloud::TResultOrError<ui32>>(
20+
IOutputStream& os,
21+
const NCloud::TResultOrError<ui32>& value);
22+
23+
template <>
24+
void Out<NCloud::TResultOrError<TStringBuf>>(
25+
IOutputStream& os,
26+
const NCloud::TResultOrError<TStringBuf>& value);

cloud/storage/core/libs/file_backed_containers/ut/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ SRCS(
1818
file_ring_buffer_accessor_ut.cpp
1919
file_ring_buffer_format_ut.cpp
2020
persistent_table_ut.cpp
21+
test/util.cpp
2122
)
2223

2324
PEERDIR(

0 commit comments

Comments
 (0)