aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/binsaver/ut_util/ut_util.h
blob: be8f89cc7c0ae871dd2426d62f02af25470f53e5 (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
61
62
63
64
65
66
67
68
69
70
71
#pragma once 
 
#include <library/cpp/binsaver/bin_saver.h>
#include <library/cpp/binsaver/mem_io.h>
#include <library/cpp/binsaver/util_stream_io.h>
 
#include <library/cpp/testing/unittest/registar.h>
 
#include <util/generic/vector.h> 
#include <util/stream/buffer.h> 
 
#include <functional> 
 
 
/* comparerChecksInside == true means comparer uses UNIT_ASSERT... inside itself 
 * comparerChecksInside == false means comparer returns if its arguments are equal 
 */ 
 
template <class T, class TComparer = std::equal_to<T>, bool comparerChecksInside = false> 
void UnitTestCheckWithComparer(const T& lhs, const T& rhs, const TComparer& comparer) { 
    if constexpr (comparerChecksInside) { 
        comparer(lhs, rhs); 
    } else { 
        UNIT_ASSERT(comparer(lhs, rhs)); 
    } 
} 
 
 
/* comparerChecksInside == true means comparer uses UNIT_ASSERT... inside itself 
 * comparerChecksInside == false means comparer returns true if its arguments are equal 
 */ 
 
template <typename T, typename TComparer = std::equal_to<T>, bool comparerChecksInside = false> 
void TestBinSaverSerializationToBuffer(const T& original, const TComparer& comparer = TComparer()) { 
    TBufferOutput out; 
    { 
        TYaStreamOutput yaOut(out); 
 
        IBinSaver f(yaOut, false, false); 
        f.Add(0, const_cast<T*>(&original)); 
    } 
    TBufferInput in(out.Buffer()); 
    T restored; 
    { 
        TYaStreamInput yaIn(in); 
        IBinSaver f(yaIn, true, false); 
        f.Add(0, &restored); 
    } 
    UnitTestCheckWithComparer<T, TComparer, comparerChecksInside>(original, restored, comparer); 
} 
 
template <typename T, typename TComparer = std::equal_to<T>, bool comparerChecksInside = false> 
void TestBinSaverSerializationToVector(const T& original, const TComparer& comparer = TComparer()) { 
    TVector<char> out; 
    SerializeToMem(&out, *const_cast<T*>(&original)); 
    T restored; 
    SerializeFromMem(&out, restored); 
    UnitTestCheckWithComparer<T, TComparer, comparerChecksInside>(original, restored, comparer); 
 
    TVector<TVector<char>> out2D; 
    SerializeToMem(&out2D, *const_cast<T*>(&original)); 
    T restored2D; 
    SerializeFromMem(&out2D, restored2D); 
    UnitTestCheckWithComparer<T, TComparer, comparerChecksInside>(original, restored2D, comparer); 
} 
 
template <typename T, typename TComparer = std::equal_to<T>, bool comparerChecksInside = false> 
void TestBinSaverSerialization(const T& original, const TComparer& comparer = TComparer()) { 
    TestBinSaverSerializationToBuffer<T, TComparer, comparerChecksInside>(original, comparer); 
    TestBinSaverSerializationToVector<T, TComparer, comparerChecksInside>(original, comparer); 
}