File: test_buffer_ref.cpp

package info (click to toggle)
paho.mqtt.cpp 1.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,672 kB
  • sloc: cpp: 13,068; ansic: 113; sh: 55; makefile: 22
file content (260 lines) | stat: -rw-r--r-- 6,951 bytes parent folder | download | duplicates (2)
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
// test_buffer_ref.cpp
//
// Unit tests for the buffer_ref class in the Paho MQTT C++ library.
//

/*******************************************************************************
 * Copyright (c) 2017-2020 Frank Pagliughi <fpagliughi@mindspring.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v20.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Frank Pagliughi - initial implementation and documentation
 *******************************************************************************/

#define UNIT_TESTS

#include <cstring>

#include "catch2_version.h"
#include "mqtt/buffer_ref.h"

using namespace mqtt;

static const string EMPTY_STR;

static const string STR{"Some random string"};
static const binary BIN{"\x0\x1\x2\x3\x4\x5\x6\x7"};

static const char* CSTR = "Another random string";
static const size_t CSTR_LEN = strlen(CSTR);

// ----------------------------------------------------------------------
// Test the default constructor
// ----------------------------------------------------------------------

TEST_CASE("dflt_ctor", "[collections]")
{
    string_ref sr;

    REQUIRE_FALSE(sr);
    REQUIRE(sr.empty());
}

// ----------------------------------------------------------------------
// Test the string copy constructor
// ----------------------------------------------------------------------

TEST_CASE("str_copy_ctor", "[collections]")
{
    string_ref sr(STR);
    REQUIRE(STR == sr.str());
}

// ----------------------------------------------------------------------
// Test the string move constructor
// ----------------------------------------------------------------------

TEST_CASE("str_move_ctor", "[collections]")
{
    string str(STR);
    string_ref sr(std::move(str));

    REQUIRE(STR == sr.str());
    REQUIRE(EMPTY_STR == str);
}

// ----------------------------------------------------------------------
// Test the c-string constructor
// ----------------------------------------------------------------------

TEST_CASE("cstr_ctor", "[collections]")
{
    string_ref sr(CSTR);

    REQUIRE(CSTR_LEN == strlen(sr.c_str()));
    REQUIRE(0 == strcmp(CSTR, sr.c_str()));
}

// ----------------------------------------------------------------------
// Test the pointer copy constructor
// ----------------------------------------------------------------------

TEST_CASE("ptr_copy_ctor", "[collections]")
{
    string_ptr sp(new string(STR));
    string_ref sr(sp);

    REQUIRE(STR == sr.str());
}

// ----------------------------------------------------------------------
// Test the pointer move constructor
// ----------------------------------------------------------------------

TEST_CASE("ptr_move_ctor", "[collections]")
{
    string_ptr sp(new string(STR));
    string_ref sr(std::move(sp));

    REQUIRE(STR == sr.str());
    REQUIRE_FALSE(sp);
}

// ----------------------------------------------------------------------
// Test the copy constructor
// ----------------------------------------------------------------------

TEST_CASE("copy_ctor", "[collections]")
{
    string_ref orgSR(STR);
    string_ref sr(orgSR);

    REQUIRE(STR == sr.str());
    REQUIRE(orgSR.ptr().get() == sr.ptr().get());
    REQUIRE(2 == sr.ptr().use_count());
}

// ----------------------------------------------------------------------
// Test the move constructor
// ----------------------------------------------------------------------

TEST_CASE("move_ctor", "[collections]")
{
    string_ref orgSR(STR);
    string_ref sr(std::move(orgSR));

    REQUIRE(STR == sr.str());

    REQUIRE_FALSE(orgSR);
    REQUIRE(1 == sr.ptr().use_count());
}

// ----------------------------------------------------------------------
// Test the copy assignment
// ----------------------------------------------------------------------

TEST_CASE("copy_assignment", "[collections]")
{
    string_ref sr, orgSR(STR);

    sr = orgSR;

    REQUIRE(STR == sr.str());
    REQUIRE(orgSR.ptr().get() == sr.ptr().get());
    REQUIRE(2 == sr.ptr().use_count());

    // Test for true copy
    orgSR = EMPTY_STR;
    REQUIRE(STR == sr.str());
}

// ----------------------------------------------------------------------
// Test the move assignment
// ----------------------------------------------------------------------

TEST_CASE("move_assignment", "[collections]")
{
    string_ref sr, orgSR(STR);

    sr = std::move(orgSR);

    REQUIRE(STR == sr.str());

    REQUIRE_FALSE(orgSR);
    REQUIRE(1 == sr.ptr().use_count());
}

// ----------------------------------------------------------------------
// Test the string copy assignment
// ----------------------------------------------------------------------

TEST_CASE("str_copy_assignment", "[collections]")
{
    string str(STR);
    string_ref sr;

    sr = str;
    REQUIRE(STR == sr.str());

    str = EMPTY_STR;
    REQUIRE(STR == sr.str());
}

// ----------------------------------------------------------------------
// Test the string move assignment
// ----------------------------------------------------------------------

TEST_CASE("str_move_assignment", "[collections]")
{
    string str(STR);
    string_ref sr;

    sr = std::move(str);
    REQUIRE(STR == sr.str());

    REQUIRE(EMPTY_STR == str);
    REQUIRE(1 == sr.ptr().use_count());
}

// ----------------------------------------------------------------------
// Test the c-string assignment
// ----------------------------------------------------------------------

TEST_CASE("cstr_assignment", "[collections]")
{
    string_ref sr;
    sr = CSTR;

    REQUIRE(CSTR_LEN == strlen(sr.c_str()));
    REQUIRE(0 == strcmp(CSTR, sr.c_str()));
}

// ----------------------------------------------------------------------
// Test the pointer copy assignment
// ----------------------------------------------------------------------

TEST_CASE("ptr_copy_assignment", "[collections]")
{
    string_ptr sp(new string(STR));
    string_ref sr;

    sr = sp;

    REQUIRE(STR == sr.str());
}

// ----------------------------------------------------------------------
// Test the pointer move assignment
// ----------------------------------------------------------------------

TEST_CASE("ptr_move_assignment", "[collections]")
{
    string_ptr sp(new string(STR));
    string_ref sr;

    sr = std::move(sp);

    REQUIRE(STR == sr.str());
    REQUIRE_FALSE(sp);
}

// ----------------------------------------------------------------------
// Test the reset
// ----------------------------------------------------------------------

TEST_CASE("reset", "[collections]")
{
    string_ref sr(STR);

    sr.reset();
    REQUIRE_FALSE(sr);
    REQUIRE(sr.empty());
}