File: test_split.cpp

package info (click to toggle)
ros2-rcutils 6.9.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,388 kB
  • sloc: ansic: 7,405; cpp: 7,369; python: 281; xml: 29; makefile: 4
file content (259 lines) | stat: -rw-r--r-- 9,985 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
// Copyright 2017 Open Source Robotics Foundation, Inc.
//
// 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 "gtest/gtest.h"

#include "./allocator_testing_utils.h"
#include "./time_bomb_allocator_testing_utils.h"
#include "rcutils/error_handling.h"
#include "rcutils/split.h"
#include "rcutils/types/string_array.h"

#define LOG(expected, actual) printf("Expected: %s Actual: %s\n", expected, actual);

rcutils_string_array_t test_split(const char * str, char delimiter, size_t expected_token_size)
{
  rcutils_string_array_t tokens = rcutils_get_zero_initialized_string_array();
  rcutils_ret_t ret = rcutils_split(
    str, delimiter, rcutils_get_default_allocator(), &tokens);
  EXPECT_EQ(RCUTILS_RET_OK, ret);
  fprintf(stderr, "Received %zu tokens\n", tokens.size);
  EXPECT_EQ(expected_token_size, tokens.size);
  for (size_t i = 0; i < tokens.size; ++i) {
    EXPECT_NE((size_t)0, strlen(tokens.data[i]));
  }
  return tokens;
}

rcutils_string_array_t test_split_last(
  const char * str, char delimiter, size_t expected_token_size)
{
  rcutils_string_array_t tokens = rcutils_get_zero_initialized_string_array();
  rcutils_ret_t ret = rcutils_split_last(
    str, delimiter, rcutils_get_default_allocator(), &tokens);
  EXPECT_EQ(RCUTILS_RET_OK, ret);
  EXPECT_EQ(expected_token_size, tokens.size);
  for (size_t i = 0; i < tokens.size; ++i) {
    EXPECT_NE((size_t)0, strlen(tokens.data[i]));
  }
  return tokens;
}

TEST(test_split, split) {
  rcutils_ret_t ret = RCUTILS_RET_OK;
  rcutils_string_array_t tokens_fail;
  EXPECT_EQ(
    RCUTILS_RET_INVALID_ARGUMENT,
    rcutils_split("Test", '/', rcutils_get_default_allocator(), NULL));
  rcutils_reset_error();

  // Allocating initial string_array fails
  rcutils_allocator_t time_bomb_allocator = get_time_bomb_allocator();
  set_time_bomb_allocator_calloc_count(time_bomb_allocator, 0);
  EXPECT_EQ(
    RCUTILS_RET_BAD_ALLOC,
    rcutils_split("Test", '/', time_bomb_allocator, &tokens_fail));
  rcutils_reset_error();

  // Allocating string_array->data fails
  set_time_bomb_allocator_calloc_count(time_bomb_allocator, 1);
  set_time_bomb_allocator_malloc_count(time_bomb_allocator, 0);
  EXPECT_EQ(
    RCUTILS_RET_BAD_ALLOC,
    rcutils_split("Test", '/', time_bomb_allocator, &tokens_fail));
  rcutils_reset_error();

  // Allocating string_array->data[0] fails
  set_time_bomb_allocator_calloc_count(time_bomb_allocator, 1);
  set_time_bomb_allocator_malloc_count(time_bomb_allocator, 1);
  EXPECT_EQ(
    RCUTILS_RET_BAD_ALLOC,
    rcutils_split("hello/world", '/', time_bomb_allocator, &tokens_fail));
  rcutils_reset_error();

  rcutils_string_array_t tokens0 = test_split("", '/', 0);
  ret = rcutils_string_array_fini(&tokens0);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens00 = test_split(NULL, '/', 0);
  ret = rcutils_string_array_fini(&tokens00);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens1 = test_split("hello_world", '/', 1);
  EXPECT_STREQ("hello_world", tokens1.data[0]);
  ret = rcutils_string_array_fini(&tokens1);
  ASSERT_EQ(RCUTILS_RET_OK, ret) << rcutils_get_error_string().str;

  rcutils_string_array_t tokens2 = test_split("hello/world", '/', 2);
  EXPECT_STREQ("hello", tokens2.data[0]);
  EXPECT_STREQ("world", tokens2.data[1]);
  ret = rcutils_string_array_fini(&tokens2);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens3 = test_split("/hello/world", '/', 2);
  EXPECT_STREQ("hello", tokens3.data[0]);
  EXPECT_STREQ("world", tokens3.data[1]);
  ret = rcutils_string_array_fini(&tokens3);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens4 = test_split("hello/world/", '/', 2);
  EXPECT_STREQ("hello", tokens4.data[0]);
  EXPECT_STREQ("world", tokens4.data[1]);
  ret = rcutils_string_array_fini(&tokens4);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens5 = test_split("hello//world", '/', 2);
  EXPECT_STREQ("hello", tokens5.data[0]);
  EXPECT_STREQ("world", tokens5.data[1]);
  ret = rcutils_string_array_fini(&tokens5);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens6 = test_split("/hello//world", '/', 2);
  EXPECT_STREQ("hello", tokens6.data[0]);
  EXPECT_STREQ("world", tokens6.data[1]);
  ret = rcutils_string_array_fini(&tokens6);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens7 = test_split("my/hello/world", '/', 3);
  EXPECT_STREQ("my", tokens7.data[0]);
  EXPECT_STREQ("hello", tokens7.data[1]);
  EXPECT_STREQ("world", tokens7.data[2]);
  ret = rcutils_string_array_fini(&tokens7);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens8 = test_split("/my/hello/world", '/', 3);
  EXPECT_STREQ("my", tokens8.data[0]);
  EXPECT_STREQ("hello", tokens8.data[1]);
  EXPECT_STREQ("world", tokens8.data[2]);
  ret = rcutils_string_array_fini(&tokens8);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens9 = test_split("/my/hello/world/", '/', 3);
  EXPECT_STREQ("my", tokens9.data[0]);
  EXPECT_STREQ("hello", tokens9.data[1]);
  EXPECT_STREQ("world", tokens9.data[2]);
  ret = rcutils_string_array_fini(&tokens9);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens10 = test_split("/my//hello//world//", '/', 3);
  EXPECT_STREQ("my", tokens10.data[0]);
  EXPECT_STREQ("hello", tokens10.data[1]);
  EXPECT_STREQ("world", tokens10.data[2]);
  ret = rcutils_string_array_fini(&tokens10);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens11 = test_split("///my//hello//world/////", '/', 3);
  EXPECT_STREQ("my", tokens11.data[0]);
  EXPECT_STREQ("hello", tokens11.data[1]);
  EXPECT_STREQ("world", tokens11.data[2]);
  ret = rcutils_string_array_fini(&tokens11);
  ASSERT_EQ(RCUTILS_RET_OK, ret);
}

TEST(test_split, split_last) {
  rcutils_ret_t ret = RCUTILS_RET_OK;
  rcutils_string_array_t tokens_fail;

  // Allocating string_array fails
  rcutils_allocator_t time_bomb_allocator = get_time_bomb_allocator();
  set_time_bomb_allocator_calloc_count(time_bomb_allocator, 0);
  EXPECT_EQ(
    RCUTILS_RET_BAD_ALLOC,
    rcutils_split_last("Test", '/', time_bomb_allocator, &tokens_fail));

  // Allocating string_array->data[0] fails
  set_time_bomb_allocator_malloc_count(time_bomb_allocator, 0);
  EXPECT_EQ(
    RCUTILS_RET_BAD_ALLOC,
    rcutils_split_last("Test", '/', time_bomb_allocator, &tokens_fail));

  // Allocating string_array fails, found_last != string_size
  set_time_bomb_allocator_calloc_count(time_bomb_allocator, 0);
  EXPECT_EQ(
    RCUTILS_RET_BAD_ALLOC,
    rcutils_split_last("hello/world", '/', time_bomb_allocator, &tokens_fail));

  // Allocating string_array->data[0] fails, found_last != string_size
  set_time_bomb_allocator_malloc_count(time_bomb_allocator, 0);
  EXPECT_EQ(
    RCUTILS_RET_BAD_ALLOC,
    rcutils_split_last("hello/world", '/', time_bomb_allocator, &tokens_fail));

  // Allocating string_array->data[1] fails, found_last != string_size
  set_time_bomb_allocator_malloc_count(time_bomb_allocator, 1);
  EXPECT_EQ(
    RCUTILS_RET_BAD_ALLOC,
    rcutils_split_last("hello/world", '/', time_bomb_allocator, &tokens_fail));

  rcutils_string_array_t tokens0 = test_split_last("", '/', 0);
  ret = rcutils_string_array_fini(&tokens0);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens00 = test_split_last(NULL, '/', 0);
  ret = rcutils_string_array_fini(&tokens00);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens1 = test_split_last("hello_world", '/', 1);
  LOG("hello_world", tokens1.data[0]);
  EXPECT_STREQ("hello_world", tokens1.data[0]);
  ret = rcutils_string_array_fini(&tokens1);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens2 = test_split_last("hello/world", '/', 2);
  EXPECT_STREQ("hello", tokens2.data[0]);
  LOG("hello", tokens2.data[0]);
  EXPECT_STREQ("world", tokens2.data[1]);
  LOG("world", tokens2.data[1]);
  ret = rcutils_string_array_fini(&tokens2);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens3 = test_split_last("/hello/world", '/', 2);
  EXPECT_STREQ("hello", tokens3.data[0]);
  LOG("hello", tokens3.data[0]);
  EXPECT_STREQ("world", tokens3.data[1]);
  ret = rcutils_string_array_fini(&tokens3);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens4 = test_split_last("hello/world/", '/', 2);
  EXPECT_STREQ("hello", tokens4.data[0]);
  EXPECT_STREQ("world", tokens4.data[1]);
  ret = rcutils_string_array_fini(&tokens4);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens5 = test_split_last("hello//world/", '/', 2);
  EXPECT_STREQ("hello", tokens5.data[0]);
  LOG("hello", tokens5.data[0]);
  EXPECT_STREQ("world", tokens5.data[1]);
  LOG("world", tokens5.data[1]);
  ret = rcutils_string_array_fini(&tokens5);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens6 = test_split_last("/hello//world", '/', 2);
  EXPECT_STREQ("hello", tokens6.data[0]);
  EXPECT_STREQ("world", tokens6.data[1]);
  ret = rcutils_string_array_fini(&tokens6);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens7 = test_split_last("my/hello//world", '/', 2);
  EXPECT_STREQ("my/hello", tokens7.data[0]);
  EXPECT_STREQ("world", tokens7.data[1]);
  ret = rcutils_string_array_fini(&tokens7);
  ASSERT_EQ(RCUTILS_RET_OK, ret);

  rcutils_string_array_t tokens8 = test_split_last("/my/hello//world/", '/', 2);
  EXPECT_STREQ("my/hello", tokens8.data[0]);
  EXPECT_STREQ("world", tokens8.data[1]);
  ret = rcutils_string_array_fini(&tokens8);
  ASSERT_EQ(RCUTILS_RET_OK, ret);
}