File: fixed_size_string.cpp

package info (click to toggle)
fastcdr 2.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,412 kB
  • sloc: cpp: 38,058; ansic: 80; xml: 22; makefile: 19; sh: 16
file content (298 lines) | stat: -rw-r--r-- 9,524 bytes parent folder | download
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <fastcdr/cdr/fixed_size_string.hpp>
#include <gtest/gtest.h>

using namespace eprosima::fastcdr;

constexpr size_t MAX_CHARS = 255;
constexpr size_t OTHER_MAX_CHARS = 127;

class FixedSizeStringTests : public ::testing::Test
{
public:

    char const* pattern0 = "foo/bar/baz";
    char const* long_pattern =
            "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
            "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
            "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
            "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
            "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
            "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
            "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"
            "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";

    size_t pattern0_len = strlen(pattern0);
};

TEST_F(FixedSizeStringTests, default_constructor)
{
    std::string std_s;
    fixed_string<MAX_CHARS> fixed_s;

    ASSERT_EQ(fixed_s.size(), 0u);
    ASSERT_EQ(fixed_s, "");
    ASSERT_EQ(fixed_s, std_s);
}

TEST_F(FixedSizeStringTests, construct_with_empty_c_string)
{
    std::string std_s;
    fixed_string<MAX_CHARS> fixed_s("");

    ASSERT_EQ(fixed_s.size(), 0u);
    ASSERT_EQ(fixed_s, "");
    ASSERT_EQ(fixed_s, std_s);
}

TEST_F(FixedSizeStringTests, construct_with_empty_std_string)
{
    std::string std_s;
    fixed_string<MAX_CHARS> fixed_s(std_s);

    ASSERT_EQ(fixed_s.size(), 0u);
    ASSERT_EQ(fixed_s, "");
    ASSERT_EQ(fixed_s, std_s);
}

TEST_F(FixedSizeStringTests, construct_with_c_string)
{
    fixed_string<MAX_CHARS> fixed_s(pattern0);

    ASSERT_EQ(fixed_s.size(), pattern0_len);
    ASSERT_EQ(fixed_s, pattern0);
}

TEST_F(FixedSizeStringTests, construct_with_std_string)
{
    std::string std_string(pattern0);
    fixed_string<MAX_CHARS> fixed_s(std_string);

    ASSERT_EQ(fixed_s.size(), pattern0_len);
    ASSERT_EQ(fixed_s, std_string);
}

TEST_F(FixedSizeStringTests, construct_with_long_c_string)
{
    fixed_string<MAX_CHARS> fixed_s(long_pattern);

    ASSERT_EQ(fixed_s.size(), MAX_CHARS);
    ASSERT_EQ(fixed_s, long_pattern);
}

TEST_F(FixedSizeStringTests, construct_with_long_std_string)
{
    std::string std_string(long_pattern);
    fixed_string<MAX_CHARS> fixed_s(std_string);

    ASSERT_EQ(fixed_s.size(), MAX_CHARS);
    ASSERT_EQ(fixed_s, std_string);
}

TEST_F(FixedSizeStringTests, assign_operators_and_inequality)
{
    fixed_string<MAX_CHARS> fixed_s;

    std::string std_s_empty;
    std::string std_s(pattern0);
    std::string std_s_long(long_pattern);

    fixed_s = std_s_long;
    ASSERT_EQ(fixed_s.size(), MAX_CHARS);
    ASSERT_EQ(fixed_s, long_pattern);
    ASSERT_NE(fixed_s, pattern0);
    ASSERT_NE(fixed_s, "");

    fixed_s = pattern0;
    ASSERT_EQ(fixed_s.size(), pattern0_len);
    ASSERT_EQ(fixed_s, std_s);
    ASSERT_NE(fixed_s, std_s_empty);
    ASSERT_NE(fixed_s, std_s_long);

    fixed_s = std_s_empty;
    ASSERT_EQ(fixed_s.size(), 0u);
    ASSERT_EQ(fixed_s, "");
    ASSERT_NE(fixed_s, long_pattern);
    ASSERT_NE(fixed_s, pattern0);

    fixed_s = long_pattern;
    ASSERT_EQ(fixed_s.size(), MAX_CHARS);
    ASSERT_EQ(fixed_s, std_s_long);
    ASSERT_NE(fixed_s, std_s);
    ASSERT_NE(fixed_s, std_s_empty);

    fixed_s = std_s;
    ASSERT_EQ(fixed_s.size(), pattern0_len);
    ASSERT_EQ(fixed_s, pattern0);
    ASSERT_NE(fixed_s, "");
    ASSERT_NE(fixed_s, long_pattern);

    fixed_s = "";
    ASSERT_EQ(fixed_s.size(), 0u);
    ASSERT_EQ(fixed_s, std_s_empty);
    ASSERT_NE(fixed_s, std_s_long);
    ASSERT_NE(fixed_s, std_s);
}

TEST_F(FixedSizeStringTests, different_fixed_sizes)
{
    fixed_string<MAX_CHARS> s1;
    fixed_string<OTHER_MAX_CHARS> s2;

    ASSERT_EQ(s1, s2);

    s1 = long_pattern;
    ASSERT_NE(s1, s2);

    s2 = s1;
    ASSERT_EQ(s2, s1);

    s2 = pattern0;
    ASSERT_NE(s2, s1);

    s1 = s2;
    ASSERT_EQ(s1, s2);
}

TEST_F(FixedSizeStringTests, comparisons)
{
    const char* c_string_short = "test string";
    const char* c_string_a = "test string a";
    const char* c_string_b = "test string b";
    const char* c_string_c = "test string c";
    const char* c_string_large = "test string b ";
    std::string std_string_short = "test string";
    std::string std_string_a = "test string a";
    std::string std_string_b = "test string b";
    std::string std_string_c = "test string c";
    std::string std_string_large = "test string b ";
    fixed_string<MAX_CHARS> fixed_short = "test string";
    fixed_string<MAX_CHARS> fixed_a = "test string a";
    fixed_string<MAX_CHARS> fixed_b = "test string b";
    fixed_string<MAX_CHARS> fixed_c = "test string c";
    fixed_string<MAX_CHARS> fixed_large = "test string b ";

    ASSERT_NE(fixed_b, c_string_short);
    ASSERT_NE(fixed_b, c_string_a);
    ASSERT_EQ(fixed_b, c_string_b);
    ASSERT_NE(fixed_b, c_string_c);
    ASSERT_NE(fixed_b, c_string_large);

    ASSERT_LT(0, fixed_b.compare(c_string_short));
    ASSERT_LT(0, fixed_b.compare(c_string_a));
    ASSERT_EQ(0, fixed_b.compare(c_string_b));
    ASSERT_GT(0, fixed_b.compare(c_string_c));
    ASSERT_GT(0, fixed_b.compare(c_string_large));

    ASSERT_NE(fixed_b, std_string_short);
    ASSERT_NE(fixed_b, std_string_a);
    ASSERT_EQ(fixed_b, std_string_b);
    ASSERT_NE(fixed_b, std_string_c);
    ASSERT_NE(fixed_b, std_string_large);

    ASSERT_LE(0, fixed_b.compare(std_string_short));
    ASSERT_LE(0, fixed_b.compare(std_string_a));
    ASSERT_EQ(0, fixed_b.compare(std_string_b));
    ASSERT_GT(0, fixed_b.compare(std_string_c));
    ASSERT_GT(0, fixed_b.compare(std_string_large));

    ASSERT_NE(fixed_b, fixed_short);
    ASSERT_NE(fixed_b, fixed_a);
    ASSERT_EQ(fixed_b, fixed_b);
    ASSERT_NE(fixed_b, fixed_c);
    ASSERT_NE(fixed_b, fixed_large);

    ASSERT_LE(0, fixed_b.compare(fixed_short));
    ASSERT_LE(0, fixed_b.compare(fixed_a));
    ASSERT_EQ(0, fixed_b.compare(fixed_b));
    ASSERT_GT(0, fixed_b.compare(fixed_c));
    ASSERT_GT(0, fixed_b.compare(fixed_large));
}

TEST_F(FixedSizeStringTests, less_than_operator_fixed_string)
{
    fixed_string<MAX_CHARS> fixed_string_short = "test string";
    fixed_string<MAX_CHARS> fixed_string_long = "test string long";

    ASSERT_FALSE(fixed_string_short < fixed_string_short);
    ASSERT_FALSE(fixed_string_long < fixed_string_long);
    ASSERT_TRUE(fixed_string_short < fixed_string_long);
    ASSERT_FALSE(fixed_string_long < fixed_string_short);
}

TEST_F(FixedSizeStringTests, less_than_operator_std_string)
{
    fixed_string<MAX_CHARS> fixed_string_short = "test string";
    std::string std_string_long = "test string long";
    std::string std_string_short = "test";

    ASSERT_TRUE(fixed_string_short < std_string_long);
    ASSERT_FALSE(fixed_string_short < std_string_short);
}

TEST_F(FixedSizeStringTests, greater_than_operator_fixed_string)
{
    fixed_string<MAX_CHARS> fixed_string_short = "test string";
    fixed_string<MAX_CHARS> fixed_string_long = "test string long";

    ASSERT_FALSE(fixed_string_short > fixed_string_short);
    ASSERT_FALSE(fixed_string_long > fixed_string_long);
    ASSERT_FALSE(fixed_string_short > fixed_string_long);
    ASSERT_TRUE(fixed_string_long > fixed_string_short);
}

TEST_F(FixedSizeStringTests, greater_than_operator_std_string)
{
    fixed_string<MAX_CHARS> fixed_string_short = "test string";
    std::string std_string_long = "test string long";
    std::string std_string_short = "test";

    ASSERT_FALSE(fixed_string_short > std_string_long);
    ASSERT_TRUE(fixed_string_short > std_string_short);
}

TEST_F(FixedSizeStringTests, assign)
{
    constexpr size_t MAX_FIXED_STRING_SIZE = 10;
    fixed_string<MAX_FIXED_STRING_SIZE> fixed_s;
    std::string std_string = "test string";
    std::string max_allowed_std_string = "test strin";
    std::string allowed_std_string = "test st";

    ASSERT_TRUE(std_string.size() > MAX_FIXED_STRING_SIZE);
    ASSERT_TRUE(max_allowed_std_string.size() == MAX_FIXED_STRING_SIZE);
    ASSERT_TRUE(allowed_std_string.size() < MAX_FIXED_STRING_SIZE);

    fixed_s.assign(std_string.c_str(), std_string.size());
    ASSERT_EQ(fixed_s.size(), MAX_FIXED_STRING_SIZE);
    ASSERT_EQ(fixed_s, max_allowed_std_string);

    fixed_s.assign(max_allowed_std_string.c_str(), max_allowed_std_string.size());
    ASSERT_EQ(fixed_s.size(), MAX_FIXED_STRING_SIZE);
    ASSERT_EQ(fixed_s, max_allowed_std_string);

    fixed_s.assign(allowed_std_string.c_str(), allowed_std_string.size());
    ASSERT_EQ(fixed_s.size(), allowed_std_string.size());
    ASSERT_EQ(fixed_s, allowed_std_string);

    fixed_s.assign(nullptr, 5);
    ASSERT_EQ(fixed_s.size(), 0u);
    ASSERT_EQ(fixed_s, "");

    fixed_s.assign(nullptr, 0);
    ASSERT_EQ(fixed_s.size(), 0u);
    ASSERT_EQ(fixed_s, "");
}