Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/strings_ut/strings_ut.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #ifdef __cplusplus
5 #include <cstdlib>
6 #include <cstddef>
7 #else
8 #include <stdlib.h>
9 #include <stddef.h>
10 #endif
11
12 void* my_gballoc_malloc(size_t size)
13 {
14 return malloc(size);
15 }
16
17 void* my_gballoc_realloc(void* ptr, size_t size)
18 {
19 return realloc(ptr, size);
20 }
21
22 void my_gballoc_free(void* ptr)
23 {
24 free(ptr);
25 }
26
27 #include "azure_macro_utils/macro_utils.h"
28 #include "testrunnerswitcher.h"
29 #include "umock_c/umock_c.h"
30 #include "umock_c/umock_c_negative_tests.h"
31 #include "umock_c/umocktypes_charptr.h"
32
33 #define ENABLE_MOCKS
34
35 #include "azure_c_shared_utility/gballoc.h"
36
37 #undef ENABLE_MOCKS
38
39 #include "azure_c_shared_utility/strings.h"
40
41 static const char TEST_STRING_VALUE []= "DataValueTest";
42 static const char INITIAL_STRING_VALUE []= "Initial_";
43 static const char MULTIPLE_TEST_STRING_VALUE[] = "DataValueTestDataValueTest";
44 static const char* COMBINED_STRING_VALUE = "Initial_DataValueTest";
45 static const char* QUOTED_TEST_STRING_VALUE = "\"DataValueTest\"";
46 static const char* FORMAT_STRING = "test_format_%s";
47 static const char* FORMAT_INTEGER = "test_format_%d";
48 static const char* FORMAT_STRING_RESULT = "test_format_DataValueTest";
49 static const char* FORMAT_INTEGER_RESULT = "test_format_1234";
50 static const char* INIT_FORMAT_STRING_RESULT = "Initial_test_format_DataValueTest";
51 static const char* INIT_FORMAT_INTEGER_RESULT = "Initial_test_format_1234";
52 static const char* EMPTY_STRING = "";
53 static const char* MODIFIED_STRING_VALUE = "Initial*";
54 static const char* MODIFIED_STRING_VALUE2 = "*nitial_";
55
56 #define NUMBER_OF_CHAR_TOCOPY 8
57 #define TEST_INTEGER_VALUE 1234
58
59 static TEST_MUTEX_HANDLE g_testByTest;
60
61 static const struct JSONEncoding {
62 const char* source;
63 const char* expectedJSON;
64 } JSONtests[]=
65 {
66 { "", "\"\"" }, /*empty string*/
67 { "a", "\"a\"" }, /*a => "a"*/
68 { "aaslkdjhfalksjh", "\"aaslkdjhfalksjh\"" }, /*aaslkdjhfalksjh => "aaslkdjhfalksjh"*/
69 { "\x01", "\"\\u0001\"" },
70 { "\x1F", "\"\\u001F\"" },
71 { "\x1F", "\"\\u001F\"" },
72 { "\"", "\"\\\"\""},
73 { "\\", "\"\\\\\"" },
74 { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F some text\"\\a/a",
75 "\"\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\u0008\\u0009\\u000A\\u000B\\u000C\\u000D\\u000E\\u000F\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001A\\u001B\\u001C\\u001D\\u001E\\u001F some text\\\"\\\\a\\/a\"" },
76
77 };
78
79 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
80
81 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
82 {
83 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
84 }
85
86 BEGIN_TEST_SUITE(strings_unittests)
87
88 TEST_SUITE_INITIALIZE(setsBufferTempSize)
89 {
90 g_testByTest = TEST_MUTEX_CREATE();
91 ASSERT_IS_NOT_NULL(g_testByTest);
92
93 umock_c_init(on_umock_c_error);
94
95 REGISTER_UMOCK_ALIAS_TYPE(STRING_HANDLE, void*);
96 ASSERT_ARE_EQUAL(int, 0, umocktypes_charptr_register_types() );
97
98 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
99 REGISTER_GLOBAL_MOCK_FAIL_RETURN(gballoc_malloc, NULL);
100
101 REGISTER_GLOBAL_MOCK_HOOK(gballoc_realloc, my_gballoc_realloc);
102 REGISTER_GLOBAL_MOCK_FAIL_RETURN(gballoc_realloc, NULL);
103
104 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
105 }
106
107 TEST_SUITE_CLEANUP(TestClassCleanup)
108 {
109 umock_c_deinit();
110
111 TEST_MUTEX_DESTROY(g_testByTest);
112 }
113
114 TEST_FUNCTION_INITIALIZE(a)
115 {
116 if (TEST_MUTEX_ACQUIRE(g_testByTest))
117 {
118 ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
119 }
120
121 umock_c_reset_all_calls();
122 }
123
124 TEST_FUNCTION_CLEANUP(cleans)
125 {
126 TEST_MUTEX_RELEASE(g_testByTest);
127 }
128
129 /* STRING_Tests BEGIN */
130 /* Tests_SRS_STRING_07_001: [STRING_new shall allocate a new STRING_HANDLE pointing to an empty string.] */
131 TEST_FUNCTION(STRING_new_Succeed)
132 {
133 ///arrange
134 STRING_HANDLE g_hString;
135
136 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
137 .IgnoreArgument(1);
138 STRICT_EXPECTED_CALL(gballoc_malloc(1));
139
140 ///act
141 g_hString = STRING_new();
142
143 ///assert
144 ASSERT_IS_NOT_NULL(g_hString);
145 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
146
147 ///cleanup
148 STRING_delete(g_hString);
149 }
150
151 TEST_FUNCTION(STRING_new_fail)
152 {
153 //arrange
154 STRING_HANDLE str_handle;
155 size_t count;
156 size_t index;
157 int negativeTestsInitResult = umock_c_negative_tests_init();
158 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
159
160 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
161 .IgnoreArgument(1);
162 STRICT_EXPECTED_CALL(gballoc_malloc(1));
163
164 umock_c_negative_tests_snapshot();
165
166 //act
167 count = umock_c_negative_tests_call_count();
168 for (index = 0; index < count; index++)
169 {
170 char tmp_msg[128];
171
172 umock_c_negative_tests_reset();
173 umock_c_negative_tests_fail_call(index);
174
175 str_handle = STRING_new();
176
177 sprintf(tmp_msg, "STRING_new failure in test %lu/%lu", (unsigned long)index+1, (unsigned long)count);
178
179 //assert
180 ASSERT_IS_NULL(str_handle, tmp_msg);
181 }
182
183 //cleanup
184 umock_c_negative_tests_deinit();
185 }
186
187 /* Tests_SRS_STRING_07_007: [STRING_new_with_memory shall return a NULL STRING_HANDLE if the supplied char* is empty.] */
188 TEST_FUNCTION(STRING_new_With_Memory_NULL_Memory_Fail)
189 {
190 ///arrange
191 STRING_HANDLE g_hString;
192
193 ///act
194 g_hString = STRING_new_with_memory(NULL);
195
196 ///assert
197 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
198 ASSERT_IS_NULL(g_hString);
199 }
200
201 /* Tests_SRS_STRING_07_006: [STRING_new_with_memory shall return a STRING_HANDLE by using the supplied char* memory.] */
202 TEST_FUNCTION(STRING_new_With_Memory_Succeed)
203 {
204 ///arrange
205 STRING_HANDLE g_hString;
206 size_t nLen = strlen(TEST_STRING_VALUE) + 1;
207 char* szTestString = (char*)malloc(nLen);
208 strncpy(szTestString, TEST_STRING_VALUE, nLen);
209 umock_c_reset_all_calls();
210
211 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
212 .IgnoreArgument(1);
213
214 ///act
215 g_hString = STRING_new_with_memory(szTestString);
216
217 ///assert
218 ASSERT_ARE_EQUAL(char_ptr, TEST_STRING_VALUE, STRING_c_str(g_hString) );
219 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
220
221 ///cleanup
222 STRING_delete(g_hString);
223 }
224
225 /* Tests_SRS_STRING_07_003: [STRING_construct shall allocate a new string with the value of the specified const char*.] */
226 TEST_FUNCTION(STRING_construct_Succeed)
227 {
228 ///arrange
229 STRING_HANDLE g_hString;
230
231 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
232 .IgnoreArgument(1);
233 STRICT_EXPECTED_CALL(gballoc_malloc(strlen(TEST_STRING_VALUE) + 1));
234
235 ///act
236 g_hString = STRING_construct(TEST_STRING_VALUE);
237
238 ///assert
239 ASSERT_ARE_EQUAL(char_ptr, TEST_STRING_VALUE, STRING_c_str(g_hString) );
240 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
241
242 ///cleanup
243 STRING_delete(g_hString);
244 }
245
246 /* Tests_SRS_STRING_07_003: [STRING_construct shall allocate a new string with the value of the specified const char*.] */
247 TEST_FUNCTION(STRING_construct_Fail)
248 {
249 //arrange
250 STRING_HANDLE str_handle;
251 size_t count;
252 size_t index;
253 int negativeTestsInitResult = umock_c_negative_tests_init();
254 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
255
256 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
257 .IgnoreArgument(1);
258 STRICT_EXPECTED_CALL(gballoc_malloc(strlen(TEST_STRING_VALUE) + 1));
259
260 umock_c_negative_tests_snapshot();
261
262 //act
263 count = umock_c_negative_tests_call_count();
264 for (index = 0; index < count; index++)
265 {
266 char tmp_msg[128];
267
268 umock_c_negative_tests_reset();
269 umock_c_negative_tests_fail_call(index);
270
271 str_handle = STRING_construct(TEST_STRING_VALUE);
272
273 sprintf(tmp_msg, "STRING_construct failure in test %lu/%lu", (unsigned long)index+1, (unsigned long)count);
274
275 //assert
276 ASSERT_IS_NULL(str_handle, tmp_msg);
277 }
278
279 //cleanup
280 umock_c_negative_tests_deinit();
281 }
282
283 /* Tests_SRS_STRING_07_005: [If the supplied const char* is NULL STRING_construct shall return a NULL value.] */
284 TEST_FUNCTION(STRING_construct_With_NULL_HANDLE_Fail)
285 {
286 ///arrange
287 STRING_HANDLE g_hString;
288
289 ///act
290 g_hString = STRING_construct(NULL);
291
292 ///assert
293 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
294 ASSERT_IS_NULL(g_hString);
295 }
296
297 /* Tests_SRS_STRING_07_008: [STRING_new_quoted shall return a valid STRING_HANDLE Copying the supplied const char* value surrounded by quotes.] */
298 TEST_FUNCTION(STRING_new_quoted_Succeed)
299 {
300 ///arrange
301 STRING_HANDLE g_hString;
302
303 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
304 .IgnoreArgument(1);
305 STRICT_EXPECTED_CALL(gballoc_malloc(2 + strlen(TEST_STRING_VALUE) + 1));
306
307 ///act
308 g_hString = STRING_new_quoted(TEST_STRING_VALUE);
309
310 ///assert
311 ASSERT_ARE_EQUAL(char_ptr, QUOTED_TEST_STRING_VALUE, STRING_c_str(g_hString) );
312 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
313
314 ///cleanup
315 STRING_delete(g_hString);
316 }
317
318 /* Tests_SRS_STRING_07_009: [STRING_new_quoted shall return a NULL STRING_HANDLE if the supplied const char* is NULL.] */
319 TEST_FUNCTION(STRING_new_quoted_NULL_Fail)
320 {
321 ///arrange
322 STRING_HANDLE g_hString;
323
324 ///act
325 g_hString = STRING_new_quoted(NULL);
326
327 ///assert
328 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
329 ASSERT_IS_NULL(g_hString);
330 }
331
332 /* Tests_SRS_STRING_07_039: [If the parameter format is NULL then STRING_construct_sprintf shall return NULL.] */
333 TEST_FUNCTION(STRING_construct_sprintf_format_NULL_Fail)
334 {
335 ///arrange
336 STRING_HANDLE g_hString;
337 int value = 123;
338
339 ///act
340 g_hString = STRING_construct_sprintf(NULL, value);
341
342 ///assert
343 ASSERT_IS_NULL(g_hString);
344 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
345 }
346
347 /* Tests_SRS_STRING_07_045: [STRING_construct_sprintf shall allocate a new string with the value of the specified printf formated const char. ] */
348 /* Tests_SRS_STRING_07_041: [STRING_construct_sprintf shall determine the size of the resulting string and allocate the necessary memory.] */
349 TEST_FUNCTION(STRING_construct_sprintf_Succeed)
350 {
351 ///arrange
352 STRING_HANDLE str_handle;
353
354 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
355 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
356
357 ///act
358 str_handle = STRING_construct_sprintf(FORMAT_STRING, TEST_STRING_VALUE);
359
360 ///assert
361 ASSERT_IS_NOT_NULL(str_handle);
362 ASSERT_ARE_EQUAL(char_ptr, FORMAT_STRING_RESULT, STRING_c_str(str_handle) );
363 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
364
365 ///cleanup
366 STRING_delete(str_handle);
367 }
368
369 /* Tests_SRS_STRING_07_045: [STRING_construct_sprintf shall allocate a new string with the value of the specified printf formated const char. ] */
370 /* Tests_SRS_STRING_07_041: [STRING_construct_sprintf shall determine the size of the resulting string and allocate the necessary memory.] */
371 TEST_FUNCTION(STRING_construct_sprintf_Empty_String_Succeed)
372 {
373 ///arrange
374 STRING_HANDLE str_handle;
375
376 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
377 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
378
379 ///act
380 str_handle = STRING_construct_sprintf(EMPTY_STRING);
381
382 ///assert
383 ASSERT_IS_NOT_NULL(str_handle);
384 ASSERT_ARE_EQUAL(size_t, STRING_length(str_handle), 0);
385 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
386
387 ///cleanup
388 STRING_delete(str_handle);
389 }
390
391 /* Tests_SRS_STRING_07_040: [If any error is encountered STRING_construct_sprintf shall return NULL.] */
392 TEST_FUNCTION(STRING_construct_sprintf_fail)
393 {
394 //arrange
395 STRING_HANDLE str_handle;
396 size_t count;
397 size_t index;
398 int negativeTestsInitResult = umock_c_negative_tests_init();
399 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
400
401 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
402 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
403
404 umock_c_negative_tests_snapshot();
405
406 //act
407 count = umock_c_negative_tests_call_count();
408 for (index = 0; index < count; index++)
409 {
410 char tmp_msg[128];
411
412 umock_c_negative_tests_reset();
413 umock_c_negative_tests_fail_call(index);
414
415 str_handle = STRING_construct_sprintf(FORMAT_STRING, TEST_STRING_VALUE);
416
417 sprintf(tmp_msg, "STRING_construct_sprintf failure in test %lu/%lu", (unsigned long)index+1, (unsigned long)count);
418
419 //assert
420 ASSERT_IS_NULL(str_handle, tmp_msg);
421 }
422
423 //cleanup
424 umock_c_negative_tests_deinit();
425 }
426
427 /* Tests_ */
428 TEST_FUNCTION(STRING_Concat_Succeed)
429 {
430 ///arrange
431 int nResult;
432 STRING_HANDLE g_hString;
433 g_hString = STRING_construct(INITIAL_STRING_VALUE);
434 umock_c_reset_all_calls();
435
436 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, strlen(INITIAL_STRING_VALUE) + strlen(TEST_STRING_VALUE) + 1))
437 .IgnoreArgument(1);
438
439 ///act
440 nResult = STRING_concat(g_hString, TEST_STRING_VALUE);
441
442 ///assert
443 ASSERT_ARE_EQUAL(char_ptr, COMBINED_STRING_VALUE, STRING_c_str(g_hString) );
444 ASSERT_ARE_EQUAL(int, nResult, 0);
445 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
446
447 ///cleanup
448 STRING_delete(g_hString);
449 }
450
451 /* Tests_SRS_STRING_07_013: [STRING_concat shall return a nonzero number if the STRING_HANDLE and const char* is NULL.] */
452 TEST_FUNCTION(STRING_Concat_HANDLE_NULL_Fail)
453 {
454 ///arrange
455 int nResult;
456 STRING_HANDLE g_hString;
457 g_hString = STRING_construct(INITIAL_STRING_VALUE);
458 umock_c_reset_all_calls();
459
460 ///act
461 nResult = STRING_concat(NULL, TEST_STRING_VALUE);
462
463 ///assert
464 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
465 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
466
467 ///cleanup
468 STRING_delete(g_hString);
469
470 }
471
472 /* Tests_SRS_STRING_07_013: [STRING_concat shall return a nonzero number if the STRING_HANDLE and const char* is NULL.] */
473 TEST_FUNCTION(STRING_Concat_CharPtr_NULL_Fail)
474 {
475 ///arrange
476 int nResult;
477 STRING_HANDLE g_hString;
478 g_hString = STRING_construct(INITIAL_STRING_VALUE);
479 umock_c_reset_all_calls();
480
481 ///act
482 nResult = STRING_concat(g_hString, NULL);
483
484 ///assert
485 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
486 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
487
488 ///cleanup
489 STRING_delete(g_hString);
490 }
491
492 /* Tests_SRS_STRING_07_013: [STRING_concat shall return a nonzero number if the STRING_HANDLE and const char* is NULL.] */
493 TEST_FUNCTION(STRING_Concat_HANDLE_and_CharPtr_NULL_Fail)
494 {
495 ///arrange
496 ///act
497
498 int nResult = STRING_concat(NULL, TEST_STRING_VALUE);
499
500 ///assert
501 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
502
503 ///cleanup
504 }
505
506 /* Tests_SRS_STRING_07_013: [STRING_concat shall return a nonzero number if the STRING_HANDLE and const char* is NULL.] */
507 TEST_FUNCTION(STRING_Concat_Copy_Multiple_Succeed)
508 {
509 ///arrange
510 STRING_HANDLE g_hString;
511 g_hString = STRING_new();
512 STRING_copy(g_hString, TEST_STRING_VALUE);
513 umock_c_reset_all_calls();
514
515 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, strlen(TEST_STRING_VALUE) + strlen(TEST_STRING_VALUE)+1))
516 .IgnoreArgument(1)
517 .IgnoreArgument(2);
518
519 ///act
520 STRING_concat(g_hString, TEST_STRING_VALUE);
521
522 ///assert
523 ASSERT_ARE_EQUAL(char_ptr, MULTIPLE_TEST_STRING_VALUE, STRING_c_str(g_hString) );
524 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
525
526 ///cleanup
527 STRING_delete(g_hString);
528 }
529
530 /* Tests_SRS_STRING_07_034: [String_Concat_with_STRING shall concatenate a given STRING_HANDLE variable with a source STRING_HANDLE.] */
531 TEST_FUNCTION(STRING_Concat_With_STRING_SUCCEED)
532 {
533 ///arrange
534 int nResult;
535 STRING_HANDLE g_hString = STRING_construct(INITIAL_STRING_VALUE);
536 STRING_HANDLE hAppend = STRING_construct(TEST_STRING_VALUE);
537 umock_c_reset_all_calls();
538
539 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, strlen(INITIAL_STRING_VALUE) + strlen(TEST_STRING_VALUE) + 1))
540 .IgnoreArgument(1);
541
542 ///act
543 nResult = STRING_concat_with_STRING(g_hString, hAppend);
544
545 ///assert
546 ASSERT_ARE_EQUAL(char_ptr, COMBINED_STRING_VALUE, STRING_c_str(g_hString) );
547 ASSERT_ARE_EQUAL(int, nResult, 0);
548 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
549
550 // Clean up
551 STRING_delete(hAppend);
552 STRING_delete(g_hString);
553 }
554
555 /* Tests_SRS_STRING_07_035: [String_Concat_with_STRING shall return a nonzero number if an error is encountered.] */
556 TEST_FUNCTION(STRING_Concat_With_STRING_HANDLE_NULL_Fail)
557 {
558 ///arrange
559 int nResult;
560 STRING_HANDLE hAppend = STRING_construct(TEST_STRING_VALUE);
561 umock_c_reset_all_calls();
562
563 ///act
564 nResult = STRING_concat_with_STRING(NULL, hAppend);
565
566 ///assert
567 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
568 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
569
570 // Clean up
571 STRING_delete(hAppend);
572 }
573
574 /* Tests_SRS_STRING_07_035: [String_Concat_with_STRING shall return a nonzero number if an error is encountered.] */
575 TEST_FUNCTION(STRING_Concat_With_STRING_Append_HANDLE_NULL_Fail)
576 {
577 ///arrange
578 STRING_HANDLE g_hString;
579 int nResult;
580 g_hString = STRING_construct(INITIAL_STRING_VALUE);
581 umock_c_reset_all_calls();
582
583 ///act
584 nResult = STRING_concat_with_STRING(g_hString, NULL);
585
586 ///assert
587 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
588 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
589
590 ///cleanup
591 STRING_delete(g_hString);
592 }
593
594 /* Tests_SRS_STRING_07_035: [String_Concat_with_STRING shall return a nonzero number if an error is encountered.] */
595 TEST_FUNCTION(STRING_Concat_With_STRING_All_HANDLE_NULL_Fail)
596 {
597 ///arrange
598
599 ///act
600 int nResult = STRING_concat_with_STRING(NULL, NULL);
601
602 ///assert
603 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
604 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
605 }
606
607 /* Tests_SRS_STRING_07_016: [STRING_copy shall copy the const char* into the supplied STRING_HANDLE.] */
608 TEST_FUNCTION(STRING_Copy_Succeed)
609 {
610 ///arrange
611 STRING_HANDLE g_hString;
612 int nResult;
613 g_hString = STRING_construct(INITIAL_STRING_VALUE);
614 umock_c_reset_all_calls();
615
616 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, strlen(TEST_STRING_VALUE) + 1))
617 .IgnoreArgument(1);
618
619 ///act
620 nResult = STRING_copy(g_hString, TEST_STRING_VALUE);
621
622 ///assert
623 ASSERT_ARE_EQUAL(char_ptr, TEST_STRING_VALUE, STRING_c_str(g_hString) );
624 ASSERT_ARE_EQUAL(int, nResult, 0);
625 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
626
627 ///cleanup
628 STRING_delete(g_hString);
629 }
630
631 /* Tests_SRS_STRING_07_017: [STRING_copy shall return a nonzero value if any of the supplied parameters are NULL.] */
632 TEST_FUNCTION(STRING_Copy_NULL_Fail)
633 {
634 ///arrange
635 int nResult;
636 STRING_HANDLE g_hString;
637 g_hString = STRING_construct(INITIAL_STRING_VALUE);
638 umock_c_reset_all_calls();
639
640 ///act
641 nResult = STRING_copy(g_hString, NULL);
642
643 ///assert
644 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
645 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
646
647 ///cleanup
648 STRING_delete(g_hString);
649 }
650
651 /* Tests_SRS_STRING_07_018: [STRING_copy_n shall copy the number of characters defined in size_t.] */
652 TEST_FUNCTION(STRING_Copy_n_Succeed)
653 {
654 ///arrange
655 int nResult;
656 STRING_HANDLE g_hString;
657 g_hString = STRING_construct(INITIAL_STRING_VALUE);
658 umock_c_reset_all_calls();
659
660 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, NUMBER_OF_CHAR_TOCOPY + 1))
661 .IgnoreArgument(1);
662
663 ///act
664 nResult = STRING_copy_n(g_hString, COMBINED_STRING_VALUE, NUMBER_OF_CHAR_TOCOPY);
665
666 ///assert
667 ASSERT_ARE_EQUAL(char_ptr, INITIAL_STRING_VALUE, STRING_c_str(g_hString) );
668 ASSERT_ARE_EQUAL(int, nResult, 0);
669 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
670
671 ///cleanup
672 STRING_delete(g_hString);
673 }
674
675 /* Tests_SRS_STRING_07_019: [STRING_copy_n shall return a nonzero value if STRING_HANDLE or const char* is NULL.] */
676 TEST_FUNCTION(STRING_Copy_n_With_HANDLE_NULL_Fail)
677 {
678 ///arrange
679
680 ///act
681 int nResult = STRING_copy_n(NULL, COMBINED_STRING_VALUE, NUMBER_OF_CHAR_TOCOPY);
682
683 ///assert
684 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
685 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
686 }
687
688 /* Tests_SRS_STRING_07_019: [STRING_copy_n shall return a nonzero value if STRING_HANDLE or const char* is NULL.] */
689 TEST_FUNCTION(STRING_Copy_n_With_CONST_CHAR_NULL_Fail)
690 {
691 ///arrange
692 int nResult;
693 STRING_HANDLE g_hString;
694 g_hString = STRING_construct(INITIAL_STRING_VALUE);
695 umock_c_reset_all_calls();
696
697 ///act
698 nResult = STRING_copy_n(g_hString, NULL, NUMBER_OF_CHAR_TOCOPY);
699
700 ///assert
701 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
702 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
703
704 ///cleanup
705 STRING_delete(g_hString);
706 }
707
708 /* Tests_SRS_STRING_07_018: [STRING_copy_n shall copy the number of characters defined in size_t.] */
709 TEST_FUNCTION(STRING_Copy_n_With_Size_0_Succeed)
710 {
711 ///arrange
712 int nResult;
713 STRING_HANDLE g_hString;
714 g_hString = STRING_construct(INITIAL_STRING_VALUE);
715 umock_c_reset_all_calls();
716
717 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, 1))
718 .IgnoreArgument(1);
719
720 ///act
721 nResult = STRING_copy_n(g_hString, COMBINED_STRING_VALUE, 0);
722
723 ///assert
724 ASSERT_ARE_EQUAL(char_ptr, EMPTY_STRING, STRING_c_str(g_hString) );
725 ASSERT_ARE_EQUAL(int, nResult, 0);
726 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
727
728 ///cleanup
729 STRING_delete(g_hString);
730 }
731
732 /* Tests_SRS_STRING_07_014: [STRING_quote shall "quote" the supplied STRING_HANDLE and return 0 on success.] */
733 TEST_FUNCTION(STRING_quote_Succeed)
734 {
735 ///arrange
736 int nResult;
737 STRING_HANDLE g_hString;
738 g_hString = STRING_construct(TEST_STRING_VALUE);
739 umock_c_reset_all_calls();
740
741 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, 2 + strlen(TEST_STRING_VALUE) + 1))
742 .IgnoreArgument(1);
743
744 ///act
745 nResult = STRING_quote(g_hString);
746
747 ///assert
748 ASSERT_ARE_EQUAL(char_ptr, QUOTED_TEST_STRING_VALUE, STRING_c_str(g_hString) );
749 ASSERT_ARE_EQUAL(int, nResult, 0);
750 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
751
752 ///cleanup
753 STRING_delete(g_hString);
754 }
755
756 TEST_FUNCTION(STRING_quote_fail)
757 {
758 ///arrange
759 STRING_HANDLE str_handle;
760 size_t count;
761 size_t index;
762 int negativeTestsInitResult = umock_c_negative_tests_init();
763 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
764
765 str_handle = STRING_construct(TEST_STRING_VALUE);
766 umock_c_reset_all_calls();
767
768 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, 2 + strlen(TEST_STRING_VALUE) + 1))
769 .IgnoreArgument(1);
770
771 umock_c_negative_tests_snapshot();
772
773 ///act
774 count = umock_c_negative_tests_call_count();
775 for (index = 0; index < count; index++)
776 {
777 char tmp_msg[128];
778 int nResult;
779
780 umock_c_negative_tests_reset();
781 umock_c_negative_tests_fail_call(index);
782
783 nResult = STRING_quote(str_handle);
784
785 sprintf(tmp_msg, "STRING_quote failure in test %lu/%lu", (unsigned long)index+1, (unsigned long)count);
786
787 //assert
788 ASSERT_ARE_NOT_EQUAL(int, 0, nResult, tmp_msg);
789 }
790
791 ///cleanup
792 STRING_delete(str_handle);
793 umock_c_negative_tests_deinit();
794 }
795
796 /* Tests_SRS_STRING_07_015: [STRING_quote shall return a nonzero value if any of the supplied parameters are NULL.] */
797 TEST_FUNCTION(STRING_quote_NULL_HANDLE_Fail)
798 {
799 ///arrange
800
801 ///act
802 int nResult = STRING_quote(NULL);
803
804 ///assert
805 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
806 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
807 }
808
809 /* Tests_SRS_STRING_07_021: [STRING_c_str shall return NULL if the STRING_HANDLE is NULL.] */
810 TEST_FUNCTION(STRING_c_str_NULL_HANDLE_Fail)
811 {
812 ///arrange
813
814 ///act
815 const char* s = STRING_c_str(NULL);
816
817 ///assert
818 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
819 ASSERT_IS_NULL(s);
820 }
821
822 /* Tests_SRS_STRING_07_020: [STRING_c_str shall return the const char* associated with the given STRING_HANDLE.] */
823 TEST_FUNCTION(STRING_c_str_Success)
824 {
825 ///arrange
826 const char* s;
827 STRING_HANDLE g_hString;
828
829 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
830 .IgnoreArgument(1);
831 STRICT_EXPECTED_CALL(gballoc_malloc(strlen(TEST_STRING_VALUE) + 1));
832
833 ///act
834 g_hString = STRING_construct(TEST_STRING_VALUE);
835 s = STRING_c_str(g_hString);
836
837 ///assert
838 ASSERT_ARE_EQUAL(char_ptr, s, TEST_STRING_VALUE);
839 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
840
841 ///cleanup
842 STRING_delete(g_hString);
843 }
844
845 /* Tests_SRS_STRING_07_022: [STRING_empty shall revert the STRING_HANDLE to an empty state.] */
846 TEST_FUNCTION(STRING_empty_Succeed)
847 {
848 ///arrange
849 STRING_HANDLE g_hString;
850 int nResult;
851 g_hString = STRING_construct(TEST_STRING_VALUE);
852 umock_c_reset_all_calls();
853
854 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, 1))
855 .IgnoreArgument(1);
856
857 ///act
858 nResult = STRING_empty(g_hString);
859
860 ///assert
861 ASSERT_ARE_EQUAL(int, nResult, 0);
862 ASSERT_ARE_EQUAL(char_ptr, EMPTY_STRING, STRING_c_str(g_hString) );
863 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
864
865 ///cleanup
866 STRING_delete(g_hString);
867 }
868
869 /* Tests_SRS_STRING_07_023: [STRING_empty shall return a nonzero value if the STRING_HANDLE is NULL.] */
870 TEST_FUNCTION(STRING_empty_NULL_HANDLE_Fail)
871 {
872 ///arrange
873
874 ///act
875 int nResult = STRING_empty(NULL);
876
877 ///assert
878 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
879 ASSERT_ARE_NOT_EQUAL(int, nResult, 0);
880 }
881
882 /* Tests_SRS_STRING_07_011: [STRING_delete will not attempt to free anything with a NULL STRING_HANDLE.] */
883 TEST_FUNCTION(STRING_delete_NULL_Succeed)
884 {
885 ///arrange
886
887 ///act
888 STRING_delete(NULL);
889
890 ///assert
891 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
892 }
893
894 /* Tests_SRS_STRING_07_011: [STRING_delete will not attempt to free anything with a NULL STRING_HANDLE.] */
895 TEST_FUNCTION(STRING_delete_Succeed)
896 {
897 ///arrange
898 STRING_HANDLE g_hString;
899 g_hString = STRING_new();
900 umock_c_reset_all_calls();
901
902 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
903 .IgnoreArgument(1);
904 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
905 .IgnoreArgument(1);
906
907 ///act
908 STRING_delete(g_hString);
909
910 ///assert
911 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
912 }
913
914 TEST_FUNCTION(STRING_length_Succeed)
915 {
916 ///arrange
917 STRING_HANDLE g_hString;
918 size_t nResult;
919 g_hString = STRING_construct(TEST_STRING_VALUE);
920 umock_c_reset_all_calls();
921
922 ///act
923 nResult = STRING_length(g_hString);
924
925 ///assert
926 ASSERT_ARE_EQUAL(size_t, nResult, strlen(TEST_STRING_VALUE) );
927 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
928
929 ///cleanup
930 STRING_delete(g_hString);
931 }
932
933 TEST_FUNCTION(STRING_length_NULL_HANDLE_Fail)
934 {
935 ///arrange
936
937 ///act
938 size_t nResult = STRING_length(NULL);
939
940 ///assert
941 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
942 ASSERT_ARE_EQUAL(size_t, nResult, 0);
943 }
944
945 /*Tests_SRS_STRING_02_002: [If parameter handle is NULL then STRING_clone shall return NULL.]*/
946 TEST_FUNCTION(STRING_clone_NULL_HANDLE_return_NULL)
947 {
948 ///arrange
949
950 ///act
951 STRING_HANDLE result = STRING_clone(NULL);
952
953 ///assert
954 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
955 ASSERT_IS_NULL( result);
956 }
957
958 /*Tests_SRS_STRING_02_001: [STRING_clone shall produce a new string having the same content as the handle string.]*/
959 TEST_FUNCTION(STRING_clone_succeeds)
960 {
961 ///arrange
962 STRING_HANDLE result;
963 STRING_HANDLE hSource = STRING_construct("aa");
964 umock_c_reset_all_calls();
965
966 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
967 .IgnoreArgument(1);
968 STRICT_EXPECTED_CALL(gballoc_malloc(sizeof("aa")));
969
970 ///act
971 result = STRING_clone(hSource);
972
973 ///assert
974 ASSERT_ARE_NOT_EQUAL(void_ptr, NULL, result);
975 ASSERT_ARE_NOT_EQUAL(void_ptr, STRING_c_str(hSource), STRING_c_str(result));
976 ASSERT_ARE_EQUAL (char_ptr, STRING_c_str(hSource), STRING_c_str(result));
977 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
978
979 ///cleanup
980 STRING_delete(hSource);
981 STRING_delete(result);
982 }
983
984 /*Tests_SRS_STRING_02_002: [If parameter handle is NULL then STRING_clone shall return NULL.]*/
985 TEST_FUNCTION(STRING_clone_fail)
986 {
987 //arrange
988 STRING_HANDLE str_handle;
989 size_t count;
990 size_t index;
991 int negativeTestsInitResult = umock_c_negative_tests_init();
992 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
993
994 str_handle = STRING_construct("aa");
995 umock_c_reset_all_calls();
996
997 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
998 .IgnoreArgument(1);
999 STRICT_EXPECTED_CALL(gballoc_malloc(sizeof("aa")));
1000
1001 umock_c_negative_tests_snapshot();
1002
1003 //act
1004 count = umock_c_negative_tests_call_count();
1005 for (index = 0; index < count; index++)
1006 {
1007 STRING_HANDLE str_result;
1008 char tmp_msg[128];
1009
1010 umock_c_negative_tests_reset();
1011 umock_c_negative_tests_fail_call(index);
1012
1013 str_result = STRING_clone(str_handle);
1014
1015 sprintf(tmp_msg, "STRING_clone failure in test %lu/%lu", (unsigned long)index+1, (unsigned long)count);
1016
1017 //assert
1018 ASSERT_IS_NULL(str_result, tmp_msg);
1019 }
1020
1021 //cleanup
1022 umock_c_negative_tests_deinit();
1023 STRING_delete(str_handle);
1024 }
1025
1026 /*Tests_SRS_STRING_02_008: [If psz is NULL then STRING_construct_n shall return NULL*/
1027 TEST_FUNCTION(STRING_construct_n_with_NULL_argument_fails)
1028 {
1029 ///arrange
1030
1031 ///act
1032 STRING_HANDLE result = STRING_construct_n(NULL, 3);
1033
1034 ///assert
1035 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1036 ASSERT_IS_NULL(result);
1037 }
1038
1039 /*Tests_SRS_STRING_02_009: [If n is bigger than the size of the string psz, then STRING_construct_n shall return NULL.] */
1040 TEST_FUNCTION(STRING_construct_n_with_too_big_size_fails)
1041 {
1042 ///arrange
1043
1044 ///act
1045 STRING_HANDLE result = STRING_construct_n("a", 2);
1046
1047 ///assert
1048 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1049 ASSERT_IS_NULL(result);
1050 }
1051
1052 /*Tests_SRS_STRING_02_007: [STRING_construct_n shall construct a STRING_HANDLE from first "n" characters of the string pointed to by psz parameter.] */
1053 TEST_FUNCTION(STRING_construct_n_succeeds_with_2_char)
1054 {
1055 ///arrange
1056 STRING_HANDLE result;
1057
1058 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1059 .IgnoreArgument(1);
1060 STRICT_EXPECTED_CALL(gballoc_malloc(3))
1061 .IgnoreArgument(1);
1062
1063 ///act
1064 result = STRING_construct_n("qq", 2);
1065
1066 ///assert
1067 ASSERT_IS_NOT_NULL(result);
1068 ASSERT_ARE_EQUAL(char_ptr, "qq", STRING_c_str(result));
1069 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1070
1071 ///cleanup
1072 STRING_delete(result);
1073 }
1074
1075 /*Tests_SRS_STRING_02_007: [STRING_construct_n shall construct a STRING_HANDLE from first "n" characters of the string pointed to by psz parameter.] */
1076 TEST_FUNCTION(STRING_construct_n_succeeds_with_3_char_out_of_five)
1077 {
1078 ///arrange
1079 STRING_HANDLE result;
1080 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1081 .IgnoreArgument(1);
1082 STRICT_EXPECTED_CALL(gballoc_malloc(4))
1083 .IgnoreArgument(1);
1084
1085 ///act
1086 result = STRING_construct_n("12345", 3);
1087
1088 ///assert
1089 ASSERT_IS_NOT_NULL(result);
1090 ASSERT_ARE_EQUAL(char_ptr, "123", STRING_c_str(result));
1091 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1092
1093 ///cleanup
1094 STRING_delete(result);
1095 }
1096
1097 TEST_FUNCTION(STRING_construct_n_fail)
1098 {
1099 //arrange
1100 size_t count;
1101 size_t index;
1102 int negativeTestsInitResult = umock_c_negative_tests_init();
1103 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
1104
1105 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1106 .IgnoreArgument(1);
1107 STRICT_EXPECTED_CALL(gballoc_malloc(3))
1108 .IgnoreArgument(1);
1109
1110 umock_c_negative_tests_snapshot();
1111
1112 //act
1113 count = umock_c_negative_tests_call_count();
1114 for (index = 0; index < count; index++)
1115 {
1116 STRING_HANDLE result;
1117 char tmp_msg[128];
1118
1119 umock_c_negative_tests_reset();
1120 umock_c_negative_tests_fail_call(index);
1121
1122 result = STRING_construct_n("qq", 2);
1123
1124 sprintf(tmp_msg, "STRING_construct_n failure in test %lu/%lu", (unsigned long)index+1, (unsigned long)count);
1125
1126 //assert
1127 ASSERT_IS_NULL(result, tmp_msg);
1128 }
1129
1130 //cleanup
1131 umock_c_negative_tests_deinit();
1132 }
1133
1134 /* Tests_SRS_STRING_07_036: [If h1 is NULL and h2 is nonNULL then STRING_compare shall return 1.] */
1135 TEST_FUNCTION(STRING_compare_s1_NULL)
1136 {
1137 ///arrange
1138 int result;
1139 STRING_HANDLE h2 = STRING_construct("bb");
1140 umock_c_reset_all_calls();
1141
1142 ///act
1143 result = STRING_compare(NULL, h2);
1144
1145 ///assert
1146 ASSERT_ARE_EQUAL(int, 1, result);
1147 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1148
1149 ///cleanup
1150 STRING_delete(h2);
1151 }
1152
1153 /* Tests_SRS_STRING_07_037: [If h2 is NULL and h1 is nonNULL then STRING_compare shall return -1.] */
1154 TEST_FUNCTION(STRING_compare_s2_NULL)
1155 {
1156 ///arrange
1157 int result;
1158 STRING_HANDLE h1 = STRING_construct("aa");
1159 umock_c_reset_all_calls();
1160
1161 ///act
1162 result = STRING_compare(h1, NULL);
1163
1164 ///assert
1165 ASSERT_ARE_EQUAL(int, -1, result);
1166 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1167
1168 ///cleanup
1169 STRING_delete(h1);
1170 }
1171
1172 /* Tests_SRS_STRING_07_035: [If h1 and h2 are both NULL then STRING_compare shall return 0.] */
1173 TEST_FUNCTION(STRING_compare_s1_s2_NULL)
1174 {
1175 ///arrange
1176
1177 ///act
1178 int result = STRING_compare(NULL, NULL);
1179
1180 ///assert
1181 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1182 ASSERT_ARE_EQUAL(int, 0, result);
1183
1184 ///cleanup
1185 }
1186
1187 /* Tests_SRS_STRING_07_034: [STRING_compare returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string s2.] */
1188 TEST_FUNCTION(STRING_compare_s1_first_SUCCEED)
1189 {
1190 ///arrange
1191 int result;
1192 STRING_HANDLE h1 = STRING_construct("aa");
1193 STRING_HANDLE h2 = STRING_construct("bb");
1194 umock_c_reset_all_calls();
1195
1196 ///act
1197 result = STRING_compare(h1, h2);
1198
1199 ///assert
1200 ASSERT_ARE_EQUAL(int, -1, result);
1201 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1202
1203 ///cleanup
1204 STRING_delete(h1);
1205 STRING_delete(h2);
1206 }
1207
1208 /* Tests_SRS_STRING_07_034: [STRING_compare returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string s2.] */
1209 TEST_FUNCTION(STRING_compare_s2_first_SUCCEED)
1210 {
1211 ///arrange
1212 int result;
1213 STRING_HANDLE h1 = STRING_construct("aa");
1214 STRING_HANDLE h2 = STRING_construct("bb");
1215 umock_c_reset_all_calls();
1216
1217 ///act
1218 result = STRING_compare(h2, h1);
1219
1220 ///assert
1221 ASSERT_ARE_EQUAL(int, 1, result);
1222 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1223
1224 ///cleanup
1225 STRING_delete(h1);
1226 STRING_delete(h2);
1227 }
1228
1229 /* Tests_SRS_STRING_07_034: [STRING_compare returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string s2.] */
1230 /* Tests_SRS_STRING_07_038: [STRING_compare shall compare the char s variable using the strcmp function.] */
1231 TEST_FUNCTION(STRING_compare_Equal_SUCCEED)
1232 {
1233 ///arrange
1234 int result;
1235 STRING_HANDLE h1 = STRING_construct("a1234");
1236 STRING_HANDLE h2 = STRING_construct("a1234");
1237 umock_c_reset_all_calls();
1238
1239 ///act
1240 result = STRING_compare(h1, h2);
1241
1242 ///assert
1243 ASSERT_ARE_EQUAL(int, 0, result);
1244 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1245
1246 ///cleanup
1247 STRING_delete(h1);
1248 STRING_delete(h2);
1249 }
1250
1251 /*Tests_SRS_STRING_02_011: [If source is NULL then STRING_new_JSON shall return NULL.] */
1252 TEST_FUNCTION(STRING_new_JSON_with_NULL_input_returns_NULL)
1253 {
1254 ///arrange
1255
1256 ///act
1257 STRING_HANDLE result = STRING_new_JSON(NULL);
1258
1259 ///assert
1260 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1261 ASSERT_IS_NULL(result);
1262
1263 ///cleanup
1264 }
1265
1266 /*Tests_SRS_STRING_02_012: [The string shall begin with the quote character.] */
1267 /*Tests_SRS_STRING_02_013: [The string shall copy the characters of source "as they are" (until the '\0' character) with the following exceptions:] */
1268 /*Tests_SRS_STRING_02_014: [If any character has the value outside [1...127] then STRING_new_JSON shall fail and return NULL.] */
1269 /*Tests_SRS_STRING_02_016: [If the character is " (quote) then it shall be repsented as \".] */
1270 /*Tests_SRS_STRING_02_017: [If the character is \ (backslash) then it shall represented as \\.]*/
1271 /*Tests_SRS_STRING_02_018: [If the character is / (slash) then it shall be represented as \/.] */
1272 /*Tests_SRS_STRING_02_019: [If the character code is less than 0x20 then it shall be represented as \\u00xx, where xx is the hex representation of the character code.]*/
1273 /*Tests_SRS_STRING_02_020: [The string shall end with " (quote).] */
1274 TEST_FUNCTION(STRING_new_JSON_succeeds)
1275 {
1276 size_t i;
1277 for (i = 0; i < sizeof(JSONtests) / sizeof(JSONtests[0]); i++)
1278 {
1279 ///arrange
1280 STRING_HANDLE result;
1281 umock_c_reset_all_calls();
1282
1283 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1284 .IgnoreArgument(1);
1285 STRICT_EXPECTED_CALL(gballoc_malloc(strlen(JSONtests[i].expectedJSON) + 1));
1286
1287 ///act
1288 result = STRING_new_JSON(JSONtests[i].source);
1289
1290 ///assert
1291 ASSERT_ARE_EQUAL(char_ptr, JSONtests[i].expectedJSON, STRING_c_str(result));
1292 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1293
1294 ///cleanup
1295 STRING_delete(result);
1296 }
1297 }
1298
1299 /*Tests_SRS_STRING_02_021: [If the complete JSON representation cannot be produced, then STRING_new_JSON shall fail and return NULL.] */
1300 TEST_FUNCTION(STRING_new_JSON_fails)
1301 {
1302 //arrange
1303 size_t count;
1304 size_t index;
1305 int negativeTestsInitResult = umock_c_negative_tests_init();
1306 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
1307
1308 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
1309 STRICT_EXPECTED_CALL(gballoc_malloc(strlen("ab") + 2+1));
1310
1311 umock_c_negative_tests_snapshot();
1312
1313 //act
1314 count = umock_c_negative_tests_call_count();
1315 for (index = 0; index < count; index++)
1316 {
1317 char tmp_msg[128];
1318 STRING_HANDLE result;
1319
1320 umock_c_negative_tests_reset();
1321 umock_c_negative_tests_fail_call(index);
1322
1323 result = STRING_new_JSON("ab");
1324
1325 sprintf(tmp_msg, "STRING_new_JSON failure in test %lu/%lu", (unsigned long)index+1, (unsigned long)count);
1326
1327 //assert
1328 ASSERT_IS_NULL(result, tmp_msg);
1329 }
1330
1331 //cleanup
1332 umock_c_negative_tests_deinit();
1333 }
1334
1335 /*Tests_SRS_STRING_02_014: [If any character has the value outside [1...127] then STRING_new_JSON shall fail and return NULL.] */
1336 TEST_FUNCTION(STRING_new_JSON_when_character_not_ASCII_fails)
1337 {
1338 ///arrange
1339
1340 ///act
1341 STRING_HANDLE result = STRING_new_JSON("a\xFF");
1342
1343 ///assert
1344 ASSERT_IS_NULL(result);
1345 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1346
1347 ///cleanup
1348 }
1349
1350 /*Tests_SRS_STRING_02_022: [ If source is NULL and size > 0 then STRING_from_BUFFER shall fail and return NULL. ]*/
1351 TEST_FUNCTION(STRING_from_byte_array_with_NULL_array_and_size_not_zero_fails)
1352 {
1353 ///arrange
1354
1355 ///act
1356 STRING_HANDLE result = STRING_from_byte_array(NULL, 1);
1357
1358 ///assert
1359 ASSERT_IS_NULL(result);
1360 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1361
1362 ///cleanup
1363 }
1364
1365 /*Tests_SRS_STRING_02_023: [ Otherwise, STRING_from_BUFFER shall build a string that has the same content (byte-by-byte) as source and return a non-NULL handle. ]*/
1366 TEST_FUNCTION(STRING_from_byte_array_succeeds)
1367 {
1368 ///arrange
1369 STRING_HANDLE result;
1370 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1371 .IgnoreArgument_size();
1372
1373 STRICT_EXPECTED_CALL(gballoc_malloc(1 + 1));
1374
1375 ///act
1376 result = STRING_from_byte_array((const unsigned char*)"a", 1);
1377
1378 ///assert
1379 ASSERT_IS_NOT_NULL(result);
1380 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1381 ASSERT_ARE_EQUAL(char_ptr, "a", STRING_c_str(result));
1382
1383 ///cleanup
1384 STRING_delete(result);
1385 }
1386
1387 /*Tests_SRS_STRING_02_023: [ Otherwise, STRING_from_BUFFER shall build a string that has the same content (byte-by-byte) as source and return a non-NULL handle. ]*/
1388 TEST_FUNCTION(STRING_from_byte_array_succeeds_2)
1389 {
1390 ///arrange
1391 STRING_HANDLE result;
1392 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1393 .IgnoreArgument_size();
1394
1395 STRICT_EXPECTED_CALL(gballoc_malloc(1 + 0));
1396
1397 ///act
1398 result = STRING_from_byte_array(NULL, 0);
1399
1400 ///assert
1401 ASSERT_IS_NOT_NULL(result);
1402 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1403 ASSERT_ARE_EQUAL(char_ptr, "", STRING_c_str(result));
1404
1405 ///cleanup
1406 STRING_delete(result);
1407 }
1408
1409 /*Tests_SRS_STRING_02_024: [ If building the string fails, then STRING_from_BUFFER shall fail and return NULL. ]*/
1410 TEST_FUNCTION(STRING_from_byte_array_fails_1)
1411 {
1412 ///arrange
1413 STRING_HANDLE result;
1414 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1415 .IgnoreArgument_size();
1416
1417 STRICT_EXPECTED_CALL(gballoc_malloc(1 + 1))
1418 .SetReturn(NULL);
1419
1420 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1421 .IgnoreArgument_ptr();
1422
1423 ///act
1424 result = STRING_from_byte_array((const unsigned char*)"a", 1);
1425
1426 ///assert
1427 ASSERT_IS_NULL(result);
1428 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1429
1430 ///cleanup
1431 }
1432
1433 /*Tests_SRS_STRING_02_024: [ If building the string fails, then STRING_from_BUFFER shall fail and return NULL. ]*/
1434 TEST_FUNCTION(STRING_from_byte_array_fails_2)
1435 {
1436 ///arrange
1437 STRING_HANDLE result;
1438 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1439 .IgnoreArgument_size()
1440 .SetReturn(NULL);
1441
1442 ///act
1443 result = STRING_from_byte_array((const unsigned char*)"a", 1);
1444
1445 ///assert
1446 ASSERT_IS_NULL(result);
1447 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1448
1449 ///cleanup
1450 }
1451
1452 /* Tests_SRS_STRING_07_042: [if the parameters s1 or format are NULL then STRING_sprintf shall return non zero value.] */
1453 TEST_FUNCTION(STRING_sprintf_string_handle_null_fail)
1454 {
1455 ///arrange
1456
1457 ///act
1458 int str_result = STRING_sprintf(NULL, FORMAT_STRING, TEST_STRING_VALUE);
1459
1460 ///assert
1461 ASSERT_ARE_NOT_EQUAL(int, str_result, 0);
1462
1463 ///cleanup
1464 }
1465
1466 /* Tests_SRS_STRING_07_042: [if the parameters s1 or format are NULL then STRING_sprintf shall return non zero value.] */
1467 TEST_FUNCTION(STRING_sprintf_format_NULL_fail)
1468 {
1469 ///arrange
1470 int str_result;
1471 STRING_HANDLE str_handle = STRING_new();
1472 ASSERT_IS_NOT_NULL(str_handle);
1473
1474 umock_c_reset_all_calls();
1475
1476 ///act
1477 str_result = STRING_sprintf(str_handle, NULL, TEST_STRING_VALUE);
1478
1479 ///assert
1480 ASSERT_ARE_NOT_EQUAL(int, str_result, 0);
1481
1482 ///cleanup
1483 STRING_delete(str_handle);
1484 }
1485
1486 /* Tests_SRS_STRING_07_044: [On success STRING_sprintf shall return 0.] */
1487 TEST_FUNCTION(STRING_sprintf_format_succeed)
1488 {
1489 ///arrange
1490 int str_result;
1491 STRING_HANDLE str_handle = STRING_construct(INITIAL_STRING_VALUE);
1492 ASSERT_IS_NOT_NULL(str_handle);
1493
1494 umock_c_reset_all_calls();
1495
1496 EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
1497
1498 ///act
1499 str_result = STRING_sprintf(str_handle, FORMAT_STRING, TEST_STRING_VALUE);
1500
1501 ///assert
1502 ASSERT_ARE_EQUAL(int, str_result, 0);
1503 ASSERT_ARE_EQUAL(char_ptr, INIT_FORMAT_STRING_RESULT, STRING_c_str(str_handle));
1504 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1505
1506 ///cleanup
1507 STRING_delete(str_handle);
1508 }
1509
1510 /* Tests_SRS_STRING_07_044: [On success STRING_sprintf shall return 0.] */
1511 TEST_FUNCTION(STRING_sprintf_format_Empty_String_succeed)
1512 {
1513 ///arrange
1514 int str_result;
1515 STRING_HANDLE str_handle = STRING_construct(INITIAL_STRING_VALUE);
1516 ASSERT_IS_NOT_NULL(str_handle);
1517
1518 umock_c_reset_all_calls();
1519
1520 ///act
1521 str_result = STRING_sprintf(str_handle, EMPTY_STRING);
1522
1523 ///assert
1524 ASSERT_ARE_EQUAL(int, str_result, 0);
1525 ASSERT_ARE_EQUAL(char_ptr, STRING_c_str(str_handle), INITIAL_STRING_VALUE);
1526 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1527
1528 ///cleanup
1529 STRING_delete(str_handle);
1530 }
1531
1532 /* Tests_SRS_STRING_07_043: [If any error is encountered STRING_sprintf shall return a non zero value.] */
1533 TEST_FUNCTION(STRING_sprintf_format_fail)
1534 {
1535 ///arrange
1536 size_t count;
1537 size_t index;
1538 int negativeTestsInitResult = umock_c_negative_tests_init();
1539 STRING_HANDLE str_handle;
1540 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
1541
1542 str_handle = STRING_construct(INITIAL_STRING_VALUE);
1543 ASSERT_IS_NOT_NULL(str_handle);
1544
1545 umock_c_reset_all_calls();
1546
1547 EXPECTED_CALL(gballoc_realloc(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
1548
1549 umock_c_negative_tests_snapshot();
1550
1551 //act
1552 count = umock_c_negative_tests_call_count();
1553 for (index = 0; index < count; index++)
1554 {
1555 char tmp_msg[128];
1556 int str_result;
1557
1558 umock_c_negative_tests_reset();
1559 umock_c_negative_tests_fail_call(index);
1560
1561 str_result = STRING_sprintf(str_handle, FORMAT_STRING, TEST_STRING_VALUE);
1562
1563 sprintf(tmp_msg, "STRING_sprintf failure in test %lu/%lu", (unsigned long)index+1, (unsigned long)count);
1564
1565 ///assert
1566 ASSERT_ARE_NOT_EQUAL(int, str_result, 0);
1567 }
1568
1569 ///cleanup
1570 umock_c_negative_tests_deinit();
1571 STRING_delete(str_handle);
1572 }
1573
1574 /* Tests_SRS_STRING_07_046: [ If handle is NULL STRING_replace shall return a non-zero value. ] */
1575 TEST_FUNCTION(STRING_replace_handle_NULL_fail)
1576 {
1577 //arrange
1578 int str_result;
1579
1580 //act
1581 str_result = STRING_replace(NULL, '_', '*');
1582
1583 //assert
1584 ASSERT_ARE_NOT_EQUAL(int, 0, str_result);
1585 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1586
1587 //cleanup
1588 }
1589
1590 /* Tests_SRS_STRING_07_047: [ STRING_replace shall replace all instances of target with replace. ] */
1591 /* Tests_SRS_STRING_07_049: [ On success STRING_replace shall return zero. ] */
1592 TEST_FUNCTION(STRING_replace_empty_string_success)
1593 {
1594 //arrange
1595 int str_result;
1596 STRING_HANDLE str_handle = STRING_construct(EMPTY_STRING);
1597 ASSERT_IS_NOT_NULL(str_handle);
1598 umock_c_reset_all_calls();
1599
1600 //act
1601 str_result = STRING_replace(str_handle, '_', '*');
1602
1603 //assert
1604 ASSERT_ARE_EQUAL(int, 0, str_result);
1605 ASSERT_ARE_EQUAL(char_ptr, EMPTY_STRING, STRING_c_str(str_handle));
1606 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1607
1608 //cleanup
1609 STRING_delete(str_handle);
1610 }
1611
1612 /* Tests_SRS_STRING_07_047: [ STRING_replace shall replace all instances of target with replace. ] */
1613 /* Tests_SRS_STRING_07_049: [ On success STRING_replace shall return zero. ] */
1614 TEST_FUNCTION(STRING_replace_value_not_found_success)
1615 {
1616 //arrange
1617 int str_result;
1618 STRING_HANDLE str_handle = STRING_construct(TEST_STRING_VALUE);
1619 ASSERT_IS_NOT_NULL(str_handle);
1620 umock_c_reset_all_calls();
1621
1622 //act
1623 str_result = STRING_replace(str_handle, '_', '*');
1624
1625 //assert
1626 ASSERT_ARE_EQUAL(int, 0, str_result);
1627 ASSERT_ARE_EQUAL(char_ptr, TEST_STRING_VALUE, STRING_c_str(str_handle));
1628 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1629
1630 //cleanup
1631 STRING_delete(str_handle);
1632 }
1633
1634 /* Tests_SRS_STRING_07_047: [ STRING_replace shall replace all instances of target with replace. ] */
1635 /* Tests_SRS_STRING_07_049: [ On success STRING_replace shall return zero. ] */
1636 TEST_FUNCTION(STRING_replace_succeed)
1637 {
1638 //arrange
1639 int str_result;
1640 STRING_HANDLE str_handle = STRING_construct(INITIAL_STRING_VALUE);
1641 ASSERT_IS_NOT_NULL(str_handle);
1642 umock_c_reset_all_calls();
1643
1644 //act
1645 str_result = STRING_replace(str_handle, '_', '*');
1646
1647 //assert
1648 ASSERT_ARE_EQUAL(int, 0, str_result);
1649 ASSERT_ARE_EQUAL(char_ptr, MODIFIED_STRING_VALUE, STRING_c_str(str_handle));
1650 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1651
1652 //cleanup
1653 STRING_delete(str_handle);
1654 }
1655
1656 /* Tests_SRS_STRING_07_048: [ If target and replace are equal STRING_replace, shall do nothing shall return zero. ] */
1657 TEST_FUNCTION(STRING_replace_same_string_succeed)
1658 {
1659 //arrange
1660 int str_result;
1661 STRING_HANDLE str_handle = STRING_construct(INITIAL_STRING_VALUE);
1662 ASSERT_IS_NOT_NULL(str_handle);
1663 umock_c_reset_all_calls();
1664
1665 //act
1666 str_result = STRING_replace(str_handle, '_', '_');
1667
1668 //assert
1669 ASSERT_ARE_EQUAL(int, 0, str_result);
1670 ASSERT_ARE_EQUAL(char_ptr, INITIAL_STRING_VALUE, STRING_c_str(str_handle));
1671 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1672
1673 //cleanup
1674 STRING_delete(str_handle);
1675 }
1676
1677 /* Tests_SRS_STRING_07_047: [ STRING_replace shall replace all instances of target with replace. ] */
1678 /* Tests_SRS_STRING_07_049: [ On success STRING_replace shall return zero. ] */
1679 TEST_FUNCTION(STRING_replace_find_first_succeed)
1680 {
1681 //arrange
1682 int str_result;
1683 STRING_HANDLE str_handle = STRING_construct(INITIAL_STRING_VALUE);
1684 ASSERT_IS_NOT_NULL(str_handle);
1685 umock_c_reset_all_calls();
1686
1687 //act
1688 str_result = STRING_replace(str_handle, 'I', '*');
1689
1690 //assert
1691 ASSERT_ARE_EQUAL(int, 0, str_result);
1692 ASSERT_ARE_EQUAL(char_ptr, MODIFIED_STRING_VALUE2, STRING_c_str(str_handle));
1693 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1694
1695 //cleanup
1696 STRING_delete(str_handle);
1697 }
1698
1699 END_TEST_SUITE(strings_unittests)