aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/zerocopy_output_ut.cpp
blob: 8d58a2e848b8b4ffbd5a488e99deb28a54583f25 (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
#include "zerocopy_output.h" 
 
#include <library/cpp/testing/unittest/registar.h>
 
#include <util/generic/string.h> 
 
// This version of string output stream is written here only 
// for testing IZeroCopyOutput implementation of DoWrite. 
class TSimpleStringOutput: public IZeroCopyOutput { 
public: 
    TSimpleStringOutput(TString& s) noexcept 
        : S_(s) 
    { 
    } 
 
private: 
    size_t DoNext(void** ptr) override { 
        if (S_.size() == S_.capacity()) { 
            S_.reserve(FastClp2(S_.capacity() + 1)); 
        } 
        size_t previousSize = S_.size(); 
        S_.resize(S_.capacity()); 
        *ptr = S_.begin() + previousSize; 
        return S_.size() - previousSize; 
    } 
 
    void DoUndo(size_t len) override { 
        Y_ENSURE(len <= S_.size()); 
        S_.resize(S_.size() - len); 
    } 
 
    TString& S_; 
}; 
 
Y_UNIT_TEST_SUITE(TestZerocopyOutput) { 
    Y_UNIT_TEST(Write) { 
        TString str; 
        TSimpleStringOutput output(str); 
        TString result; 
 
        for (size_t i = 0; i < 1000; ++i) { 
            result.push_back('a' + (i % 20)); 
        } 
 
        output.Write(result.begin(), result.size()); 
        UNIT_ASSERT_STRINGS_EQUAL(str, result); 
    } 
}