File: test_bufferlist.cc

package info (click to toggle)
eckit 1.32.4-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 305,876 kB
  • sloc: cpp: 111,654; ansic: 2,826; yacc: 590; lex: 361; python: 237; sh: 202; makefile: 42
file content (134 lines) | stat: -rw-r--r-- 3,883 bytes parent folder | download | duplicates (3)
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

#include "eckit/io/BufferList.h"
#include "eckit/io/Length.h"
#include "eckit/testing/Test.h"

#include <cstring>
#include <iomanip>
#include <utility>

namespace eckit::test {

//----------------------------------------------------------------------------------------------------------------------

CASE("Test Empty") {
    BufferList bl;
    EXPECT(bl.count() == 0);
    EXPECT(bl.size() == Length(0));

    Buffer c;
    EXPECT_NO_THROW(c = bl.consolidate());
    EXPECT(c.size() == 0);
}

CASE("Test 1 Buffer Optimisation") {
    std::string s1 = "abcdefghijklmnopqrstuvwxyz";

    BufferList bl;
    Buffer b(s1);

    EXPECT(bl.count() == 0);
    bl.append(std::move(b));
    EXPECT(bl.count() == 1);

    Buffer c;
    EXPECT_NO_THROW(c = bl.consolidate());

    EXPECT(c.size() == Length(s1.size() + 1));
    EXPECT(bl.count() == 0);
    EXPECT(bl.size() == Length(0));
}


CASE("Test Buffer Aggregation") {

    std::string s1                 = "abcdefghijklmnopqrstuvwxyz";
    std::string s2                 = "The quick brown fox jumps over the lazy dog";
    std::string s3                 = "Oh no, not again!!!";
    std::string expected_combined1 = s1 + '\0' + s2;
    std::string expected_combined2 = s1 + '\0' + s2 + '\0' + s3;

    BufferList bl;
    EXPECT(bl.count() == 0);

    // n.b. +n in length comparisons for '\0' in strings

    bl.append(Buffer(s1));
    EXPECT(bl.count() == 1);
    EXPECT(bl.size() == Length(s1.size() + 1));
    bl.append(Buffer(s2));
    EXPECT(bl.count() == 2);
    EXPECT(bl.size() == Length(s1.size() + s2.size() + 2));
    EXPECT(bl.size() == Length(expected_combined1.size() + 1));

    Buffer consolidated1 = bl.consolidate();
    EXPECT(consolidated1.size() == expected_combined1.size() + 1);
    EXPECT(::memcmp(expected_combined1.c_str(), consolidated1, consolidated1.size()) == 0);

    // We cannot continue appending. Once consolidated, we clear the internal memory.
    EXPECT(bl.count() == 0);
    EXPECT(bl.size() == Length(0));

    bl.append(Buffer(s1));
    bl.append(Buffer(s2));
    bl.append(Buffer(s3));

    EXPECT(bl.count() == 3);

    EXPECT(bl.size() == Length(s1.size() + s2.size() + s3.size() + 3));
    EXPECT(bl.size() == Length(expected_combined2.size() + 1));

    Buffer consolidated2 = bl.consolidate();
    EXPECT(consolidated2.size() == expected_combined2.size() + 1);
    EXPECT(::memcmp(consolidated2, expected_combined2.c_str(), consolidated2.size()) == 0);

    // we can call consolidate multiple times, but that returns empty buffer on subsequent calls

    Buffer empty = bl.consolidate();

    EXPECT(empty.size() == Length(0));
}

CASE("Test ownership of aggregated buffers") {

    std::string s1                = "abcdefghijklmnopqrstuvwxyz";
    std::string s2                = "The quick brown fox jumps over the lazy dog";
    std::string s3                = "Oh no, not again!!!";
    std::string expected_combined = s1 + '\0' + s2 + '\0' + s3;

    Buffer consolidated{0};

    {
        BufferList bl;

        {
            Buffer b1(s1);
            Buffer b2(s2);
            Buffer b3(s3);

            bl.append(std::move(b1));
            bl.append(std::move(b2));
            bl.append(std::move(b3));

            EXPECT(bl.count() == 3);

            // Check that the Buffers really have been moved out...
            EXPECT(b1.size() == 0);
            EXPECT(b2.size() == 0);
            EXPECT(b3.size() == 0);
        }

        consolidated = bl.consolidate();
    }

    EXPECT(consolidated.size() == expected_combined.size() + 1);
    EXPECT(::memcmp(consolidated, expected_combined.c_str(), consolidated.size()) == 0);
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit::test

int main(int argc, char** argv) {
    return eckit::testing::run_tests(argc, argv);
}