Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/crtabstractions_ut/crt_abstractions_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 <climits>
7 #include <cfloat>
8 #include <cmath>
9 #else
10 #include <stdlib.h>
11 #include <limits.h>
12 #include <float.h>
13 #include <math.h>
14 #endif
15
16 #include "azure_macro_utils/macro_utils.h"
17 #include "testrunnerswitcher.h"
18
19 // VS 2008 does not have INFINITY and all the nice goodies...
20 #if defined (TIZENRT)
21 #define DEFINE_INFINITY 1
22 #else
23
24 #if defined _MSC_VER
25 #if _MSC_VER <= 1500
26 #define DEFINE_INFINITY 1
27 #endif
28 #endif
29 #endif
30
31 #if defined DEFINE_INFINITY
32
33 #pragma warning(disable:4756 4056) // warning C4756: overflow in constant arithmetic
34
35 // These defines are missing in math.h for WEC2013 SDK
36 #ifndef _HUGE_ENUF
37 #define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow
38 #endif
39
40 #define INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF))
41 #define HUGE_VALF ((float)INFINITY)
42 #define HUGE_VALL ((long double)INFINITY)
43 #define NAN ((float)(INFINITY * 0.0F))
44
45 #define isnan _isnan
46
47 #endif
48
49 void* real_malloc(size_t size)
50 {
51 return malloc(size);
52 }
53
54 void* real_realloc(void* ptr, size_t size)
55 {
56 return realloc(ptr, size);
57 }
58
59 void real_free(void* ptr)
60 {
61 free(ptr);
62 }
63
64 #define ENABLE_MOCKS
65 #include "azure_c_shared_utility/gballoc.h"
66 #undef ENABLE_MOCKS
67
68 #include "azure_c_shared_utility/crt_abstractions.h"
69 #include <errno.h>
70
71 #if defined _MSC_VER
72 #include "crtdbg.h"
73 static _invalid_parameter_handler oldInvalidParameterHandler;
74 static int oldReportType;
75
76 static void my_invalid_parameter_handler(
77 const wchar_t * expression,
78 const wchar_t * function,
79 const wchar_t * file,
80 unsigned int line,
81 uintptr_t pReserved
82 )
83 {
84 (void)expression;
85 (void)function;
86 (void)file;
87 (void)line;
88 (void)pReserved;
89 /*do nothing*/
90 }
91 /* The below defines are because on Windows platform, the secure version of the CRT functions will invoke WATSON if no invalid parameter handler provided by the user */
92 #define HOOK_INVALID_PARAMETER_HANDLER() {oldInvalidParameterHandler = _set_invalid_parameter_handler(my_invalid_parameter_handler);oldReportType=_CrtSetReportMode(_CRT_ASSERT, 0);}
93 #define UNHOOK_INVALID_PARAMETER_HANDLER() {(void)_CrtSetReportMode(_CRT_ASSERT, oldReportType); (void)_set_invalid_parameter_handler(oldInvalidParameterHandler);}
94
95 #else /* _MSC_VER */
96 #define HOOK_INVALID_PARAMETER_HANDLER() do{}while(0)
97 #define UNHOOK_INVALID_PARAMETER_HANDLER() do{}while(0)
98 #endif /* _MSC_VER */
99
100 static const unsigned int interestingUnsignedIntNumbersToBeConverted[] =
101 {
102 0,
103 1,
104 2,
105 3,
106 4,
107 5,
108 6,
109 7,
110 8,
111 9,
112 10,
113 21,
114 32,
115 43,
116 54,
117 65,
118 76,
119 87,
120 98,
121 123,
122 1234,
123 12341,
124 UINT_MAX / 2,
125 UINT_MAX -1,
126 UINT_MAX,
127 42,
128 0x42
129 };
130
131 #ifndef SIZE_MAX
132 #define SIZE_MAX ((size_t)~(size_t)0)
133 #endif
134
135 static const size_t interestingSize_tNumbersToBeConverted[] =
136 {
137 0,
138 1,
139 2,
140 3,
141 4,
142 5,
143 6,
144 7,
145 8,
146 9,
147 10,
148 21,
149 32,
150 43,
151 54,
152 65,
153 76,
154 87,
155 98,
156 123,
157 1234,
158 12341,
159 SIZE_MAX / 2,
160 SIZE_MAX -1,
161 SIZE_MAX,
162 42,
163 0x42
164 };
165
166 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
167
168 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
169 {
170 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
171 }
172
173 BEGIN_TEST_SUITE(CRTAbstractions_UnitTests)
174
175 TEST_SUITE_INITIALIZE(a)
176 {
177 umock_c_init(on_umock_c_error);
178
179 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, real_malloc);
180 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, real_free);
181 REGISTER_GLOBAL_MOCK_HOOK(gballoc_realloc, real_realloc);
182 }
183
184 TEST_SUITE_CLEANUP(b)
185 {
186 umock_c_deinit();
187 }
188
189 /* strcat_s */
190
191 // Tests_SRS_CRT_ABSTRACTIONS_99_008: [strcat_s shall append the src to dst and terminates the resulting string with a null character.]
192 // Tests_SRS_CRT_ABSTRACTIONS_99_009: [The initial character of src shall overwrite the terminating null character of dst.]
193 // Tests_SRS_CRT_ABSTRACTIONS_99_003: [strcat_s shall return Zero upon success.]
194 TEST_FUNCTION(strcat_s_Appends_Source_To_Destination)
195 {
196 // arrange
197 char dstString[128] = "Destination";
198 size_t dstSizeInBytes = sizeof(dstString);
199 char srcString[] = "Source";
200 int result;
201
202 // act
203 result = strcat_s(dstString, dstSizeInBytes, srcString);
204
205 // assert
206 ASSERT_ARE_EQUAL(char_ptr, "DestinationSource", dstString);
207 ASSERT_ARE_EQUAL(int, 0, result);
208 }
209
210 TEST_FUNCTION(strcat_s_Appends_Empty_Source_To_Destination)
211 {
212 // arrange
213 char dstString[128] = "Destination";
214 size_t dstSizeInBytes = sizeof(dstString);
215 char srcString[] = "";
216 int result;
217
218 // act
219 result = strcat_s(dstString, dstSizeInBytes, srcString);
220
221 // assert
222 ASSERT_ARE_EQUAL(char_ptr, "Destination", dstString);
223 ASSERT_ARE_EQUAL(int, 0, result);
224 }
225
226 TEST_FUNCTION(strcat_s_Appends_Source_To_Empty_Destination)
227 {
228 // arrange
229 char dstString[128] = "";
230 size_t dstSizeInBytes = sizeof(dstString);
231 char srcString[] = "Source";
232 int result;
233
234 // act
235 result = strcat_s(dstString, dstSizeInBytes, srcString);
236
237 // assert
238 ASSERT_ARE_EQUAL(char_ptr, "Source", dstString);
239 ASSERT_ARE_EQUAL(int, 0, result);
240 }
241
242 TEST_FUNCTION(strcat_s_Appends_Empty_Source_To_Empty_Destination)
243 {
244 // arrange
245 char dstString[128] = "";
246 size_t dstSizeInBytes = sizeof(dstString);
247 char srcString[] = "";
248 int result;
249
250 // act
251 result = strcat_s(dstString, dstSizeInBytes, srcString);
252
253 // assert
254 ASSERT_ARE_EQUAL(char_ptr, "", dstString);
255 ASSERT_ARE_EQUAL(int, 0, result);
256 }
257
258 // Tests_SRS_CRT_ABSTRACTIONS_99_004: [If dst is NULL or unterminated, the error code returned shall be EINVAL & dst shall not be modified.]
259 TEST_FUNCTION(strcat_s_With_NULL_Destination_Fails)
260 {
261 // arrange
262 char* dstString = NULL;
263 size_t sizeInBytes = sizeof(dstString);
264 char srcString[] = "Source";
265 int result;
266
267 // act
268 HOOK_INVALID_PARAMETER_HANDLER();
269 #ifdef _MSC_VER
270 #pragma warning(suppress: 6387) /* This is test code, explictly calling with NULL argument */
271 #endif
272 result = strcat_s(dstString, sizeInBytes, srcString);
273 UNHOOK_INVALID_PARAMETER_HANDLER();
274
275 // assert
276 ASSERT_IS_NULL(dstString);
277 ASSERT_ARE_EQUAL(int, EINVAL, result);
278 }
279
280 TEST_FUNCTION(strcat_s_With_Unterminated_Destination_Fails)
281 {
282 // arrange
283 size_t i;
284 char dstString[128];
285 size_t dstSizeInBytes = sizeof(dstString);
286 char srcString[] = "Source";
287 int result;
288 for (i = 0; i < dstSizeInBytes; i++)
289 {
290 dstString[i] = 'z';
291 }
292
293 // act
294 HOOK_INVALID_PARAMETER_HANDLER();
295 result = strcat_s(dstString, dstSizeInBytes, srcString);
296 UNHOOK_INVALID_PARAMETER_HANDLER();
297
298 // assert
299 #ifndef _MSC_VER /* MSDN claims that content of destination buffer is not modified, but that is not true. Filing bug. */
300 for (size_t i = 0; i < dstSizeInBytes; i++)
301 {
302 ASSERT_ARE_EQUAL(char, 'z', dstString[i]);
303 }
304 #endif
305 ASSERT_ARE_EQUAL(int, EINVAL, result);
306 }
307
308 // Tests_SRS_CRT_ABSTRACTIONS_99_005: [If src is NULL, the error code returned shall be EINVAL and dst[0] shall be set to 0.]
309 TEST_FUNCTION(strcat_s_With_NULL_Source_Fails)
310 {
311 // arrange
312 char dstString[128] = "Source";
313 size_t dstSizeInBytes = sizeof(dstString);
314 char* srcString = NULL;
315 int result;
316
317 // act
318 HOOK_INVALID_PARAMETER_HANDLER();
319 #ifdef _MSC_VER
320 #pragma warning(suppress: 6387) /* This is test code, explictly calling with NULL argument */
321 #endif
322 result = strcat_s(dstString, dstSizeInBytes, srcString);
323 UNHOOK_INVALID_PARAMETER_HANDLER();
324
325 // assert
326 ASSERT_ARE_EQUAL(char,'\0', dstString[0]);
327 ASSERT_ARE_EQUAL(int, EINVAL, result);
328 }
329
330 // Tests_SRS_CRT_ABSTRACTIONS_99_006: [If the dstSizeInBytes is 0 or smaller than the required size for dst & src, the error code returned shall be ERANGE & dst[0] set to 0.]
331 TEST_FUNCTION(strcat_s_With_dstSizeInBytes_Equals_Zero_Fails)
332 {
333 // arrange
334 char dstString[128] = "Destination";
335 size_t dstSizeInBytes = sizeof(dstString);
336 char srcString[] = "Source";
337 int result;
338
339 // act
340 dstSizeInBytes = 0;
341 HOOK_INVALID_PARAMETER_HANDLER();
342 result = strcat_s(dstString, dstSizeInBytes, srcString);
343 UNHOOK_INVALID_PARAMETER_HANDLER();
344
345 // assert
346 #ifdef _MSC_VER /*MSDN Claims that destination buffer would be set to empty & ERANGE is the error, but not the case. Filing bug.*/
347 ASSERT_ARE_EQUAL(char_ptr, "Destination", dstString);
348 ASSERT_ARE_EQUAL(int, EINVAL, result);
349 #else
350 ASSERT_ARE_EQUAL(char, '\0', dstString[0]);
351 ASSERT_ARE_EQUAL(int, ERANGE, result);
352 #endif
353 }
354
355 TEST_FUNCTION(strcat_s_With_dstSizeInBytes_Smaller_Than_dst_and_src_Fails)
356 {
357 // arrange
358 char dstString[128] = "Destination";
359 size_t dstSizeInBytes = sizeof(dstString);
360 char srcString[] = "Source";
361 int result;
362
363 // act
364 HOOK_INVALID_PARAMETER_HANDLER();
365 dstSizeInBytes = strlen(dstString) + (strlen(srcString) - 3);
366 result = strcat_s(dstString, dstSizeInBytes, srcString);
367 UNHOOK_INVALID_PARAMETER_HANDLER();
368
369 // assert
370 ASSERT_ARE_EQUAL(char,'\0', dstString[0]);
371 ASSERT_ARE_EQUAL(int, ERANGE, result);
372 }
373
374 /* strcpy_s */
375
376 // Tests_SRS_CRT_ABSTRACTIONS_99_016: [strcpy_s shall copy the contents in the address of src, including the terminating null character, to the location that's specified by dst.]
377 // Tests_SRS_CRT_ABSTRACTIONS_99_011 : [strcpy_s shall return Zero upon success]
378 TEST_FUNCTION(strcpy_s_copies_Source_into_Destination)
379 {
380 // arrange
381 char dstString[128] = "Destination";
382 size_t dstSizeInBytes = sizeof(dstString);
383 char srcString[] = "Source";
384 int result = 0;
385
386 // act
387 result = strcpy_s(dstString, dstSizeInBytes, srcString);
388
389 // assert
390 ASSERT_ARE_EQUAL(char_ptr, "Source", dstString);
391 ASSERT_ARE_EQUAL(int, 0, result);
392 }
393
394 TEST_FUNCTION(strcpy_s_copies_Empty_Source_into_Destination)
395 {
396 // arrange
397 char dstString[128] = "Destination";
398 size_t dstSizeInBytes = sizeof(dstString);
399 char srcString[] = "";
400 int result = 0;
401
402 // act
403 result = strcpy_s(dstString, dstSizeInBytes, srcString);
404
405 // assert
406 ASSERT_ARE_EQUAL(char_ptr, "", dstString);
407 ASSERT_ARE_EQUAL(int, 0, result);
408 }
409
410 TEST_FUNCTION(strcpy_s_copies_Source_into_Empty_Destination)
411 {
412 // arrange
413 char dstString[128] = "";
414 size_t dstSizeInBytes = sizeof(dstString);
415 char srcString[] = "Source";
416 int result = 0;
417
418 // act
419 result = strcpy_s(dstString, dstSizeInBytes, srcString);
420
421 // assert
422 ASSERT_ARE_EQUAL(char_ptr, "Source", dstString);
423 ASSERT_ARE_EQUAL(int, 0, result);
424 }
425
426 TEST_FUNCTION(strcpy_s_copies_Empty_Source_into_Empty_Destination)
427 {
428 // arrange
429 char dstString[128] = "";
430 size_t dstSizeInBytes = sizeof(dstString);
431 char srcString[] = "";
432 int result = 0;
433
434 // act
435 result = strcpy_s(dstString, dstSizeInBytes, srcString);
436
437 // assert
438 ASSERT_ARE_EQUAL(char_ptr, "", dstString);
439 ASSERT_ARE_EQUAL(int, 0, result);
440 }
441
442 // Tests_SRS_CRT_ABSTRACTIONS_99_012 : [If dst is NULL, the error code returned shall be EINVAL & dst shall not be modified.]
443 TEST_FUNCTION(strcpy_s_With_NULL_Destination_Fails)
444 {
445 // arrange
446 char* dstString = NULL;
447 size_t dstSizeInBytes = sizeof(dstString);
448 char srcString[] = "Source";
449 int result = 0;
450
451 // act
452 HOOK_INVALID_PARAMETER_HANDLER();
453 #ifdef _MSC_VER
454 #pragma warning(suppress: 6387) /* This is test code, explictly calling with NULL argument */
455 #endif
456 result = strcpy_s(dstString, dstSizeInBytes, srcString);
457 UNHOOK_INVALID_PARAMETER_HANDLER();
458
459 // assert
460 ASSERT_ARE_EQUAL(char_ptr, NULL, dstString);
461 ASSERT_ARE_EQUAL(int, EINVAL, result);
462 }
463
464 // Tests_SRS_CRT_ABSTRACTIONS_99_013 : [If src is NULL, the error code returned shall be EINVAL and dst[0] shall be set to 0.]
465 TEST_FUNCTION(strcpy_s_With_NULL_Source_Fails)
466 {
467 // arrange
468 char dstString[128] = "Destination";
469 size_t dstSizeInBytes = sizeof(dstString);
470 char* srcString = NULL;
471 int result = 0;
472
473 // act
474 HOOK_INVALID_PARAMETER_HANDLER();
475 #ifdef _MSC_VER
476 #pragma warning(suppress: 6387) /* This is test code, explictly calling with NULL argument */
477 #endif
478 result = strcpy_s(dstString, dstSizeInBytes, srcString);
479 UNHOOK_INVALID_PARAMETER_HANDLER();
480
481 // assert
482 ASSERT_ARE_EQUAL(char,'\0', dstString[0]);
483 ASSERT_ARE_EQUAL(int, EINVAL, result);
484 }
485
486 // Tests_SRS_CRT_ABSTRACTIONS_99_014 : [If the dstSizeInBytes is 0 or smaller than the required size for the src string, the error code returned shall be ERANGE & dst[0] set to 0.]
487 TEST_FUNCTION(strcpy_s_With_dstSizeInBytes_Equals_Zero_Fails)
488 {
489 // arrange
490 char dstString[128] = "Destination";
491 size_t dstSizeInBytes = sizeof(dstString);
492 char srcString[] = "Source";
493 int result;
494
495 // act
496 dstSizeInBytes = 0;
497 HOOK_INVALID_PARAMETER_HANDLER();
498 result = strcpy_s(dstString, dstSizeInBytes, srcString);
499 UNHOOK_INVALID_PARAMETER_HANDLER();
500
501 // assert
502 #ifdef _MSC_VER /*MSDN Claims that destination buffer would be set to empty & ERANGE is the error, but not the case. Filing bug.*/
503 ASSERT_ARE_EQUAL(char_ptr, "Destination", dstString);
504 ASSERT_ARE_EQUAL(int, EINVAL, result);
505 #else
506 ASSERT_ARE_EQUAL(char, '\0', dstString[0]);
507 ASSERT_ARE_EQUAL(int, ERANGE, result);
508 #endif
509 }
510
511 TEST_FUNCTION(strcpy_s_With_dstSizeInBytes_Smaller_Than_source_Fails)
512 {
513 // arrange
514 char dstString[128] = "Destination";
515 size_t dstSizeInBytes = sizeof(dstString);
516 char srcString[] = "Source";
517 int result;
518
519 // act
520 HOOK_INVALID_PARAMETER_HANDLER();
521 dstSizeInBytes = sizeof(srcString) - 2;
522 result = strcpy_s(dstString, dstSizeInBytes, srcString);
523 UNHOOK_INVALID_PARAMETER_HANDLER();
524
525 // assert
526 ASSERT_ARE_EQUAL(char,'\0', dstString[0]);
527 ASSERT_ARE_EQUAL(int, ERANGE, result);
528 }
529
530 /* strncpy_s */
531
532 // Tests_SRS_CRT_ABSTRACTIONS_99_025 : [strncpy_s shall copy the first N characters of src to dst, where N is the lesser of MaxCount and the length of src.]
533 // Tests_SRS_CRT_ABSTRACTIONS_99_041 : [If those N characters will fit within dst(whose size is given as dstSizeInBytes) and still leave room for a null terminator, then those characters shall be copied and a terminating null is appended; otherwise, strDest[0] is set to the null character and ERANGE error code returned per requirement below.]
534 // Tests_SRS_CRT_ABSTRACTIONS_99_018: [strncpy_s shall return Zero upon success]
535 TEST_FUNCTION(strncpy_s_copies_N_chars_of_source_to_destination_where_maxCount_equals_source_Length)
536 {
537 // arrange
538 char dstString[] = "Destination";
539 size_t dstSizeInBytes = sizeof(dstString);
540 char srcString[] = "Source";
541 size_t maxCount = sizeof(srcString);
542 int result;
543
544 // act
545 result = strncpy_s(dstString, dstSizeInBytes, srcString, maxCount);
546
547 // assert
548 ASSERT_ARE_EQUAL(char_ptr, "Source", dstString);
549 ASSERT_ARE_EQUAL(int, 0, result);
550 }
551
552 TEST_FUNCTION(strncpy_s_copies_N_chars_of_source_to_destination_where_maxCount_is_larger_than_Source_Length)
553 {
554 // arrange
555 char dstString[] = "Destination";
556 size_t dstSizeInBytes = sizeof(dstString);
557 char srcString[] = "Source";
558 size_t maxCount = sizeof(srcString);
559 int result;
560
561 // act
562 result = strncpy_s(dstString, dstSizeInBytes, srcString, maxCount+5);
563
564 // assert
565 ASSERT_ARE_EQUAL(char_ptr, "Source", dstString);
566 ASSERT_ARE_EQUAL(int, 0, result);
567 }
568
569 TEST_FUNCTION(strncpy_s_copies_N_chars_of_source_to_destination_where_maxCount_is_less_than_source_length)
570 {
571 // arrange
572 char dstString[] = "Destination";
573 size_t dstSizeInBytes = sizeof(dstString);
574 char srcString[] = "Source";
575 size_t maxCount = sizeof(srcString);
576 int result;
577
578 // act
579 result = strncpy_s(dstString, dstSizeInBytes, srcString, maxCount - 3);
580
581 // assert
582 ASSERT_ARE_EQUAL(char_ptr, "Sour", dstString);
583 ASSERT_ARE_EQUAL(int, 0, result);
584 }
585
586 // Tests_SRS_CRT_ABSTRACTIONS_99_026 : [If MaxCount is _TRUNCATE(defined as - 1), then as much of src as will fit into dst shall be copied while still leaving room for the terminating null to be appended.]
587 TEST_FUNCTION(strncpy_s_with_maxCount_set_to_TRUNCATE_and_destination_fits_source)
588 {
589 // arrange
590 char dstString[] = "Destination";
591 size_t dstSizeInBytes = sizeof(dstString);
592 char srcString[] = "Source";
593 size_t maxCount = sizeof(srcString);
594 int result;
595
596 // act
597 maxCount = _TRUNCATE;
598 result = strncpy_s(dstString, dstSizeInBytes, srcString, maxCount);
599
600 // assert
601 ASSERT_ARE_EQUAL(char_ptr, "Source", dstString);
602 ASSERT_ARE_EQUAL(int, 0, result);
603 }
604
605 // Tests_SRS_CRT_ABSTRACTIONS_99_026 : [If MaxCount is _TRUNCATE(defined as - 1), then as much of src as will fit into dst shall be copied while still leaving room for the terminating null to be appended.]
606 // Tests_SRS_CRT_ABSTRACTIONS_99_019: [If truncation occurred as a result of the copy, the error code returned shall be STRUNCATE .]
607 TEST_FUNCTION(strncpy_s_with_maxCount_set_to_TRUNCATE_and_destination_is_smaller_than_source)
608 {
609 // arrange
610 char dstString[] = "Dest";
611 size_t dstSizeInBytes = sizeof(dstString);
612 char srcString[] = "Source";
613 size_t maxCount = sizeof(srcString);
614 int result;
615
616 // act
617 maxCount = _TRUNCATE;
618 result = strncpy_s(dstString, dstSizeInBytes, srcString, maxCount);
619
620 // assert
621 ASSERT_ARE_EQUAL(char_ptr, "Sour", dstString);
622 ASSERT_ARE_EQUAL(int, STRUNCATE, result);
623 }
624
625 // Tests_SRS_CRT_ABSTRACTIONS_99_020 : [If dst is NULL, the error code returned shall be EINVAL and dst shall not be modified.]
626 TEST_FUNCTION(strncpy_s_fails_with_destination_set_to_NULL)
627 {
628 // arrange
629 char* dstString = NULL;
630 size_t dstSizeInBytes = sizeof(dstString);
631 char srcString[] = "Source";
632 size_t maxCount = sizeof(srcString);
633 int result;
634
635 // act
636 HOOK_INVALID_PARAMETER_HANDLER();
637 #ifdef _MSC_VER
638 #pragma warning(suppress: 6387) /* This is test code, explictly calling with NULL argument */
639 #endif
640 result = strncpy_s(dstString, dstSizeInBytes, srcString, maxCount);
641 UNHOOK_INVALID_PARAMETER_HANDLER();
642
643 // assert
644 ASSERT_IS_NULL(dstString);
645 ASSERT_ARE_EQUAL(int, EINVAL, result);
646 }
647
648 // Tests_SRS_CRT_ABSTRACTIONS_99_021: [If src is NULL, the error code returned shall be EINVAL and dst[0] shall be set to 0.]
649 TEST_FUNCTION(strncpy_s_fails_with_source_set_to_NULL)
650 {
651 // arrange
652 char dstString[] = "Destination";
653 size_t dstSizeInBytes = sizeof(dstString);
654 char* srcString = NULL;
655 size_t maxCount = sizeof(srcString);
656 int result;
657
658 // act
659 HOOK_INVALID_PARAMETER_HANDLER();
660 #ifdef _MSC_VER
661 #pragma warning(suppress: 6387) /* This is test code, explictly calling with NULL argument */
662 #endif
663 result = strncpy_s(dstString, dstSizeInBytes, srcString, maxCount);
664 UNHOOK_INVALID_PARAMETER_HANDLER();
665
666 // assert
667 ASSERT_ARE_EQUAL(char,'\0', dstString[0]);
668 ASSERT_ARE_EQUAL(int, EINVAL, result);
669 }
670
671 // Tests_SRS_CRT_ABSTRACTIONS_99_022: [If the dstSizeInBytes is 0, the error code returned shall be EINVAL and dst shall not be modified.]
672 TEST_FUNCTION(strncpy_s_fails_with_dstSizeInBytes_set_to_Zero)
673 {
674 // arrange
675 char dstString[] = "Destination";
676 size_t dstSizeInBytes = sizeof(dstString);
677 char srcString[] = "Source";
678 size_t maxCount = sizeof(srcString);
679 int result;
680
681 // act
682 HOOK_INVALID_PARAMETER_HANDLER();
683 dstSizeInBytes = 0;
684 result = strncpy_s(dstString, dstSizeInBytes, srcString, maxCount);
685 UNHOOK_INVALID_PARAMETER_HANDLER();
686
687 // assert
688 ASSERT_ARE_EQUAL(char_ptr, "Destination", dstString);
689 ASSERT_ARE_EQUAL(int, EINVAL, result);
690 }
691
692 // Tests_SRS_CRT_ABSTRACTIONS_99_023 : [If dst is not NULL & dstSizeInBytes is smaller than the required size for the src string, the error code returned shall be ERANGE and dst[0] shall be set to 0.]
693 TEST_FUNCTION(strncpy_s_dstSizeInBytes_is_smaller_than_the_required_size_for_source)
694 {
695 // arrange
696 char dstString[] = "Dest";
697 size_t dstSizeInBytes = sizeof(dstString);
698 char srcString[] = "Source";
699 size_t maxCount = sizeof(srcString);
700 int result;
701
702 // act
703 HOOK_INVALID_PARAMETER_HANDLER();
704 result = strncpy_s(dstString, dstSizeInBytes, srcString, maxCount);
705 UNHOOK_INVALID_PARAMETER_HANDLER();
706
707 // assert
708 ASSERT_ARE_EQUAL(char,'\0', dstString[0]);
709 ASSERT_ARE_EQUAL(int, ERANGE, result);
710 }
711
712 /* sprintf_s */
713
714 // Tests_SRS_CRT_ABSTRACTIONS_99_029: [The sprintf_s function shall format and store series of characters and values in dst.Each argument(if any) is converted and output according to the corresponding Format Specification in the format variable.]
715 // Tests_SRS_CRT_ABSTRACTIONS_99_031: [A null character is appended after the last character written.]
716 // Tests_SRS_CRT_ABSTRACTIONS_99_027: [sprintf_s shall return the number of characters stored in dst upon success. This number shall not include the terminating null character.]
717 TEST_FUNCTION(sprintf_s_formats_and_stores_chars_and_values_in_destination)
718 {
719 // arrange
720 char dstString[1024];
721 size_t dstSizeInBytes = sizeof(dstString);
722 char expected_string[] = "sprintf_s: 123, hello, Z, 1.5";
723 int expected_string_size = (int)(sizeof(expected_string));
724 int result;
725
726 // act
727 result = sprintf_s(dstString, dstSizeInBytes, "sprintf_s: %d, %s, %c, %3.1f", 123, "hello", 'Z', 1.5f);
728
729 // assert
730 ASSERT_ARE_EQUAL(char_ptr, expected_string, dstString);
731 ASSERT_ARE_EQUAL(int, expected_string_size -1, result);
732 }
733
734 // Tests_SRS_CRT_ABSTRACTIONS_99_028: [If dst or format is a null pointer, sprintf_s shall return -1.]
735 TEST_FUNCTION(sprintf_s_fails_with_dst_set_to_null)
736 {
737 // arrange
738 char* dstString = NULL;
739 size_t dstSizeInBytes = sizeof(dstString);
740 int result;
741
742 // act
743 HOOK_INVALID_PARAMETER_HANDLER();
744 #ifdef _MSC_VER
745 #pragma warning(suppress: 6387) /* This is test code, explictly calling with NULL argument */
746 #endif
747 result = sprintf_s(dstString, dstSizeInBytes, "sprintf_s: %d, %s, %c, %3.1f", 123, "hello", 'Z', 1.5f);
748 UNHOOK_INVALID_PARAMETER_HANDLER();
749
750 // assert
751 ASSERT_ARE_EQUAL(int, -1, result);
752 ASSERT_ARE_EQUAL(int, EINVAL, errno);
753 }
754
755 TEST_FUNCTION(sprintf_s_fails_with_format_set_to_null)
756 {
757 // arrange
758 char dstString[1024];
759 size_t dstSizeInBytes = sizeof(dstString);
760 int result;
761
762 // act
763 HOOK_INVALID_PARAMETER_HANDLER();
764 #ifdef _MSC_VER
765 #pragma warning(suppress: 6387) /* This is test code, explictly calling with NULL argument */
766 #endif
767 result = sprintf_s(dstString, dstSizeInBytes, NULL);
768 UNHOOK_INVALID_PARAMETER_HANDLER();
769
770 // assert
771 ASSERT_ARE_EQUAL(int, -1, result);
772 ASSERT_ARE_EQUAL(int, EINVAL, errno);
773 }
774
775 // Tests_SRS_CRT_ABSTRACTIONS_99_034 : [If the dst buffer is too small for the text being printed, then dst is set to an empty string and the function shall return -1.]
776 TEST_FUNCTION(sprintf_s_fails_with_dst_too_small)
777 {
778 // arrange
779 char dstString[5];
780 size_t dstSizeInBytes = sizeof(dstString);
781 int result;
782
783 // act
784 HOOK_INVALID_PARAMETER_HANDLER();
785 result = sprintf_s(dstString, dstSizeInBytes, "sprintf_s: %d, %s, %c, %3.1f", 123, "hello", 'Z', 1.5f);
786 UNHOOK_INVALID_PARAMETER_HANDLER();
787
788 // assert
789 ASSERT_ARE_EQUAL(char_ptr, "", dstString);
790 ASSERT_ARE_EQUAL(int, -1, result);
791 }
792
793 TEST_FUNCTION(sprintf_s_fails_with_dst_buffer_size_not_fitting_null_char)
794 {
795 // arrange
796 char dstString[5];
797 size_t dstSizeInBytes = sizeof(dstString);
798 int result;
799
800 // act
801 HOOK_INVALID_PARAMETER_HANDLER();
802 result = sprintf_s(dstString, dstSizeInBytes, "12345");
803 UNHOOK_INVALID_PARAMETER_HANDLER();
804
805 // assert
806 ASSERT_ARE_EQUAL(char_ptr, "", dstString);
807 ASSERT_ARE_EQUAL(int, -1, result);
808 }
809
810 /* strtoull_s */
811
812 /*Tests_SRS_CRT_ABSTRACTIONS_21_014: [If the correct value is outside the range, the strtoull_s returns the value ULLONG_MAX, and errno will receive the value ERANGE.]*/
813 TEST_FUNCTION(strtoull_s_decimal_base_max_ull_64bit_success)
814 {
815 // arrange
816 const char* subjectStr = "18446744073709551615";
817 char* endptr;
818 int base = 10;
819 uint64_t result;
820
821 uint64_t expectedResult = ULLONG_MAX;
822 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
823
824 // act
825 result = strtoull_s(subjectStr, &endptr, base);
826
827 // assert
828 ASSERT_ARE_EQUAL(int, 0, errno);
829 ASSERT_ARE_EQUAL(char_ptr, "18446744073709551615", subjectStr);
830 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
831 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
832 }
833
834 /*Tests_SRS_CRT_ABSTRACTIONS_21_014: [If the correct value is outside the range, the strtoull_s returns the value ULLONG_MAX, and errno will receive the value ERANGE.]*/
835 TEST_FUNCTION(strtoull_s_hexadecimal_base_max_ull_128bit_success)
836 {
837 // arrange
838 const char* subjectStr = "0xffffffffffffffffffffffffffffffff";
839 char* endptr;
840 int base = 16;
841 unsigned long long result;
842 size_t ull_size = sizeof(unsigned long long);
843
844 unsigned long long expectedResult = ULLONG_MAX;
845
846 // act
847 result = strtoull_s(subjectStr, &endptr, base);
848
849 // assert
850 ASSERT_ARE_EQUAL(int, ERANGE, errno);
851 if (ull_size == 128)
852 {
853 uint64_t upperResult = (uint64_t)((result >> 63) / 2);
854 uint64_t lowerResult = (uint64_t)(result & ULLONG_MAX);
855 ASSERT_ARE_EQUAL(uint64_t, expectedResult, upperResult);
856 ASSERT_ARE_EQUAL(uint64_t, expectedResult, lowerResult);
857 }
858 else
859 {
860 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
861 }
862 }
863
864 /*Tests_SRS_CRT_ABSTRACTIONS_21_038: [If the subject sequence starts with a negative sign, the strtoull_s will convert it to the posive representation of the negative value.]*/
865 TEST_FUNCTION(strtoull_s_negative_nanber_decimal_base_ull_success)
866 {
867 // arrange
868 const char* subjectStr = "-5";
869 char* endptr;
870 int base = 10;
871 uint64_t result;
872
873 uint64_t expectedResult = (uint64_t)(-5);
874 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
875
876 // act
877 result = strtoull_s(subjectStr, &endptr, base);
878
879 // assert
880 ASSERT_ARE_EQUAL(int, 0, errno);
881 ASSERT_ARE_EQUAL(char_ptr, "-5", subjectStr);
882 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
883 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
884 }
885
886 /*Tests_SRS_CRT_ABSTRACTIONS_21_014: [If the correct value is outside the range, the strtoull_s returns the value ULLONG_MAX, and errno will receive the value ERANGE.]*/
887 TEST_FUNCTION(strtoull_s_decimal_base_overflow_max_ull_fail)
888 {
889 // arrange
890 const char* subjectStr = "18446744073709551616";
891 char* endptr;
892 int base = 10;
893 uint64_t result;
894
895 uint64_t expectedResult = ULLONG_MAX;
896 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
897
898 // act
899 result = strtoull_s(subjectStr, &endptr, base);
900
901 // assert
902 ASSERT_ARE_EQUAL(int, ERANGE, errno);
903 ASSERT_ARE_EQUAL(char_ptr, "18446744073709551616", subjectStr);
904 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
905 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
906 }
907
908 /*Tests_SRS_CRT_ABSTRACTIONS_21_014: [If the correct value is outside the range, the strtoull_s returns the value ULLONG_MAX, and errno will receive the value ERANGE.]*/
909 TEST_FUNCTION(strtoull_s_hexadecimal_base_overflow_max_ull_fail)
910 {
911 // arrange
912 const char* subjectStr = "0xFFFFFFFFFFFFFFFFF";
913 char* endptr;
914 int base = 16;
915 uint64_t result;
916
917 uint64_t expectedResult = ULLONG_MAX;
918 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
919
920 // act
921 result = strtoull_s(subjectStr, &endptr, base);
922
923 // assert
924 ASSERT_ARE_EQUAL(int, ERANGE, errno);
925 ASSERT_ARE_EQUAL(char_ptr, "0xFFFFFFFFFFFFFFFFF", subjectStr);
926 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
927 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
928 }
929
930 /*Tests_SRS_CRT_ABSTRACTIONS_21_001: [The strtoull_s must convert the initial portion of the string pointed to by nptr to uint64_t int representation.]*/
931 /*Tests_SRS_CRT_ABSTRACTIONS_21_002: [The strtoull_s must resembling an integer represented in some radix determined by the value of base.]*/
932 /*Tests_SRS_CRT_ABSTRACTIONS_21_003: [The strtoull_s must return the integer that represents the value in the initial part of the string. If any.]*/
933 TEST_FUNCTION(strtoull_s_decimal_base_success)
934 {
935 // arrange
936 const char* subjectStr = "123456";
937 char* endptr;
938 int base = 10;
939 uint64_t result;
940
941 uint64_t expectedResult = 123456ULL;
942 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
943
944 // act
945 result = strtoull_s(subjectStr, &endptr, base);
946
947 // assert
948 ASSERT_ARE_EQUAL(char_ptr, "123456", subjectStr);
949 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
950 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
951 }
952
953 /*Tests_SRS_CRT_ABSTRACTIONS_21_011: [The valid sequence starts after the first non-white-space character, followed by an optional positive or negative sign, a number or a letter(depending of the base).]*/
954 /*Tests_SRS_CRT_ABSTRACTIONS_21_010: [The white-space must be one of the characters ' ', '\f', '\n', '\r', '\t', '\v'.]*/
955 TEST_FUNCTION(strtoull_s_decimal_base_with_spaces_success)
956 {
957 // arrange
958 const char* subjectStr = " 123456";
959 char* endptr;
960 int base = 10;
961 uint64_t result;
962
963 uint64_t expectedResult = 123456ULL;
964 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
965
966 // act
967 result = strtoull_s(subjectStr, &endptr, base);
968
969 // assert
970 ASSERT_ARE_EQUAL(char_ptr, " 123456", subjectStr);
971 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
972 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
973 }
974
975 /*Tests_SRS_CRT_ABSTRACTIONS_21_010: [The white-space must be one of the characters ' ', '\f', '\n', '\r', '\t', '\v'.]*/
976 TEST_FUNCTION(strtoull_s_decimal_base_with_tab_success)
977 {
978 // arrange
979 const char* subjectStr = " \t 123456";
980 char* endptr;
981 int base = 10;
982 uint64_t result;
983
984 uint64_t expectedResult = 123456ULL;
985 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
986
987 // act
988 result = strtoull_s(subjectStr, &endptr, base);
989
990 // assert
991 ASSERT_ARE_EQUAL(char_ptr, " \t 123456", subjectStr);
992 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
993 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
994 }
995
996 /*Tests_SRS_CRT_ABSTRACTIONS_21_011: [The valid sequence starts after the first non-white-space character, followed by an optional positive or negative sign, a number or a letter(depending of the base).]*/
997 TEST_FUNCTION(strtoull_s_decimal_base_with_plus_signal_success)
998 {
999 // arrange
1000 const char* subjectStr = " +123456";
1001 char* endptr;
1002 int base = 10;
1003 uint64_t result;
1004
1005 uint64_t expectedResult = 123456ULL;
1006 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1007
1008 // act
1009 result = strtoull_s(subjectStr, &endptr, base);
1010
1011 // assert
1012 ASSERT_ARE_EQUAL(char_ptr, " +123456", subjectStr);
1013 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1014 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1015 }
1016
1017 /*Tests_SRS_CRT_ABSTRACTIONS_21_038: [If the subject sequence starts with a negative sign, the strtoull_s will convert it to the posive representation of the negative value.]*/
1018 TEST_FUNCTION(strtoull_s_decimal_base_with_minus_sign_fail)
1019 {
1020 // arrange
1021 const char* subjectStr = " -123456";
1022 char* endptr;
1023 int base = 10;
1024 uint64_t result;
1025
1026 uint64_t expectedResult = (uint64_t)(-123456);
1027 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1028
1029 // act
1030 result = strtoull_s(subjectStr, &endptr, base);
1031
1032 // assert
1033 ASSERT_ARE_EQUAL(char_ptr, " -123456", subjectStr);
1034 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1035 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1036 }
1037
1038 /*Tests_SRS_CRT_ABSTRACTIONS_21_004: [The strtoull_s must return in endptr a final string of one or more unrecognized characters, including the terminating null character of the input string.]*/
1039 TEST_FUNCTION(strtoull_s_decimal_base_follow_by_spaces_success)
1040 {
1041 // arrange
1042 const char* subjectStr = "123456 ";
1043 char* endptr;
1044 int base = 10;
1045 uint64_t result;
1046
1047 uint64_t expectedResult = 123456ULL;
1048 char* expectedEndptr = (char*)subjectStr + 6;
1049
1050 // act
1051 result = strtoull_s(subjectStr, &endptr, base);
1052
1053 // assert
1054 ASSERT_ARE_EQUAL(char_ptr, "123456 ", subjectStr);
1055 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1056 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1057 }
1058
1059 /*Tests_SRS_CRT_ABSTRACTIONS_21_004: [The strtoull_s must return in endptr a final string of one or more unrecognized characters, including the terminating null character of the input string.]*/
1060 TEST_FUNCTION(strtoull_s_decimal_base_follow_by_spaces_and_number_success)
1061 {
1062 // arrange
1063 const char* subjectStr = "123456 789";
1064 char* endptr;
1065 int base = 10;
1066 uint64_t result;
1067
1068 uint64_t expectedResult = 123456ULL;
1069 char* expectedEndptr = (char*)subjectStr + 6;
1070
1071 // act
1072 result = strtoull_s(subjectStr, &endptr, base);
1073
1074 // assert
1075 ASSERT_ARE_EQUAL(char_ptr, "123456 789", subjectStr);
1076 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1077 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1078 }
1079
1080 /*Tests_SRS_CRT_ABSTRACTIONS_21_004: [The strtoull_s must return in endptr a final string of one or more unrecognized characters, including the terminating null character of the input string.]*/
1081 TEST_FUNCTION(strtoull_s_decimal_base_follow_by_percent_success)
1082 {
1083 // arrange
1084 const char* subjectStr = "123456%%";
1085 char* endptr;
1086 int base = 10;
1087 uint64_t result;
1088
1089 uint64_t expectedResult = 123456ULL;
1090 char* expectedEndptr = (char*)subjectStr + 6;
1091
1092 // act
1093 result = strtoull_s(subjectStr, &endptr, base);
1094
1095 // assert
1096 ASSERT_ARE_EQUAL(char_ptr, "123456%%", subjectStr);
1097 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1098 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1099 }
1100
1101 /*Tests_SRS_CRT_ABSTRACTIONS_21_004: [The strtoull_s must return in endptr a final string of one or more unrecognized characters, including the terminating null character of the input string.]*/
1102 TEST_FUNCTION(strtoull_s_decimal_base_follow_by_string_success)
1103 {
1104 // arrange
1105 const char* subjectStr = "123456abc";
1106 char* endptr;
1107 int base = 10;
1108 uint64_t result;
1109
1110 uint64_t expectedResult = 123456ULL;
1111 char* expectedEndptr = (char*)subjectStr + 6;
1112
1113 // act
1114 result = strtoull_s(subjectStr, &endptr, base);
1115
1116 // assert
1117 ASSERT_ARE_EQUAL(char_ptr, "123456abc", subjectStr);
1118 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1119 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1120 }
1121
1122 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1123 TEST_FUNCTION(strtoull_s_hexadecimal_base_uppercase_success)
1124 {
1125 // arrange
1126 const char* subjectStr = "1E240";
1127 char* endptr;
1128 int base = 16;
1129 uint64_t result;
1130
1131 uint64_t expectedResult = 123456ULL;
1132 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1133
1134 // act
1135 result = strtoull_s(subjectStr, &endptr, base);
1136
1137 // assert
1138 ASSERT_ARE_EQUAL(char_ptr, "1E240", subjectStr);
1139 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1140 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1141 }
1142
1143 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1144 TEST_FUNCTION(strtoull_s_hexadecimal_base_lowercase_success)
1145 {
1146 // arrange
1147 const char* subjectStr = "1e240";
1148 char* endptr;
1149 int base = 16;
1150 uint64_t result;
1151
1152 uint64_t expectedResult = 123456ULL;
1153 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1154
1155 // act
1156 result = strtoull_s(subjectStr, &endptr, base);
1157
1158 // assert
1159 ASSERT_ARE_EQUAL(char_ptr, "1e240", subjectStr);
1160 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1161 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1162 }
1163
1164 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1165 /*Tests_SRS_CRT_ABSTRACTIONS_21_008: [If the base is 0 and '0x' or '0X' precedes the number, strtoull_s must convert to a hexadecimal (base 16).]*/
1166 TEST_FUNCTION(strtoull_s_0x_hexadecimal_base_uppercase_success)
1167 {
1168 // arrange
1169 const char* subjectStr = "0X1e240";
1170 char* endptr;
1171 int base = 16;
1172 uint64_t result;
1173
1174 uint64_t expectedResult = 123456ULL;
1175 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1176
1177 // act
1178 result = strtoull_s(subjectStr, &endptr, base);
1179
1180 // assert
1181 ASSERT_ARE_EQUAL(char_ptr, "0X1e240", subjectStr);
1182 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1183 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1184 }
1185
1186 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1187 /*Tests_SRS_CRT_ABSTRACTIONS_21_008: [If the base is 0 and '0x' or '0X' precedes the number, strtoull_s must convert to a hexadecimal (base 16).]*/
1188 TEST_FUNCTION(strtoull_s_0x_hexadecimal_base_lowercase_success)
1189 {
1190 // arrange
1191 const char* subjectStr = "0x1e240";
1192 char* endptr;
1193 int base = 16;
1194 uint64_t result;
1195
1196 uint64_t expectedResult = 123456ULL;
1197 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1198
1199 // act
1200 result = strtoull_s(subjectStr, &endptr, base);
1201
1202 // assert
1203 ASSERT_ARE_EQUAL(char_ptr, "0x1e240", subjectStr);
1204 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1205 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1206 }
1207
1208 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1209 TEST_FUNCTION(strtoull_s_0x_hexadecimal_base_out_of_base_range_character_fail)
1210 {
1211 // arrange
1212 const char* subjectStr = "0xje240";
1213 char* endptr;
1214 int base = 16;
1215 uint64_t result;
1216
1217 uint64_t expectedResult = 0ULL;
1218 char* expectedEndptr = (char*)subjectStr;
1219
1220 // act
1221 result = strtoull_s(subjectStr, &endptr, base);
1222
1223 // assert
1224 ASSERT_ARE_EQUAL(char_ptr, "0xje240", subjectStr);
1225 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1226 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1227 }
1228
1229 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1230 /*Tests_SRS_CRT_ABSTRACTIONS_21_008: [If the base is 0 and '0x' or '0X' precedes the number, strtoull_s must convert to a hexadecimal (base 16).]*/
1231 /*Tests_SRS_CRT_ABSTRACTIONS_21_009: [If the base is 0 and '0' precedes the number, strtoull_s must convert to an octal (base 8).]*/
1232 TEST_FUNCTION(strtoull_s_0x_hexadecimal_with_base_8_character_success)
1233 {
1234 // arrange
1235 const char* subjectStr = "0x1e240";
1236 char* endptr;
1237 int base = 8;
1238 uint64_t result;
1239
1240 uint64_t expectedResult = 0ULL;
1241 char* expectedEndptr = (char*)subjectStr + 1;
1242
1243 // act
1244 result = strtoull_s(subjectStr, &endptr, base);
1245
1246 // assert
1247 ASSERT_ARE_EQUAL(char_ptr, "0x1e240", subjectStr);
1248 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1249 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1250 }
1251
1252 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1253 /*Tests_SRS_CRT_ABSTRACTIONS_21_008: [If the base is 0 and '0x' or '0X' precedes the number, strtoull_s must convert to a hexadecimal (base 16).]*/
1254 TEST_FUNCTION(strtoull_s_0_base_with_0x_hexadecimal_success)
1255 {
1256 // arrange
1257 const char* subjectStr = "0x1e240";
1258 char* endptr;
1259 int base = 0;
1260 uint64_t result;
1261
1262 uint64_t expectedResult = 123456ULL;
1263 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1264
1265 // act
1266 result = strtoull_s(subjectStr, &endptr, base);
1267
1268 // assert
1269 ASSERT_ARE_EQUAL(char_ptr, "0x1e240", subjectStr);
1270 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1271 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1272 }
1273
1274 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1275 TEST_FUNCTION(strtoull_s_octal_base_success)
1276 {
1277 // arrange
1278 const char* subjectStr = "361100";
1279 char* endptr;
1280 int base = 8;
1281 uint64_t result;
1282
1283 uint64_t expectedResult = 123456ULL;
1284 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1285
1286 // act
1287 result = strtoull_s(subjectStr, &endptr, base);
1288
1289 // assert
1290 ASSERT_ARE_EQUAL(char_ptr, "361100", subjectStr);
1291 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1292 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1293 }
1294
1295 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1296 TEST_FUNCTION(strtoull_s_binary_base_success)
1297 {
1298 // arrange
1299 const char* subjectStr = "11110001001000000";
1300 char* endptr;
1301 int base = 2;
1302 uint64_t result;
1303
1304 uint64_t expectedResult = 123456ULL;
1305 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1306
1307 // act
1308 result = strtoull_s(subjectStr, &endptr, base);
1309
1310 // assert
1311 ASSERT_ARE_EQUAL(char_ptr, "11110001001000000", subjectStr);
1312 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1313 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1314 }
1315
1316 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1317 TEST_FUNCTION(strtoull_s_36_base_success)
1318 {
1319 // arrange
1320 const char* subjectStr = "hello";
1321 char* endptr;
1322 int base = 36;
1323 uint64_t result;
1324
1325 uint64_t expectedResult = (uint64_t)((17*1679616)+(14*46656)+(21*1296)+(21*36)+(24));
1326 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1327
1328 // act
1329 result = strtoull_s(subjectStr, &endptr, base);
1330
1331 // assert
1332 ASSERT_ARE_EQUAL(char_ptr, "hello", subjectStr);
1333 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1334 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1335 }
1336
1337 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1338 TEST_FUNCTION(strtoull_s_36_base_looks_like_hexadecimal_with_0x_success)
1339 {
1340 // arrange
1341 const char* subjectStr = "0x1";
1342 char* endptr;
1343 int base = 36;
1344 uint64_t result;
1345
1346 uint64_t expectedResult = (uint64_t)((33 * 36) + (1));
1347 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1348
1349 // act
1350 result = strtoull_s(subjectStr, &endptr, base);
1351
1352 // assert
1353 ASSERT_ARE_EQUAL(char_ptr, "0x1", subjectStr);
1354 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1355 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1356 }
1357
1358 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1359 /*Tests_SRS_CRT_ABSTRACTIONS_21_009: [If the base is 0 and '0' precedes the number, strtoull_s must convert to an octal (base 8).]*/
1360 TEST_FUNCTION(strtoull_s_0_base_with_actal_success)
1361 {
1362 // arrange
1363 const char* subjectStr = "0361100";
1364 char* endptr;
1365 int base = 0;
1366 uint64_t result;
1367
1368 uint64_t expectedResult = 123456ULL;
1369 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1370
1371 // act
1372 result = strtoull_s(subjectStr, &endptr, base);
1373
1374 // assert
1375 ASSERT_ARE_EQUAL(char_ptr, "0361100", subjectStr);
1376 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1377 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1378 }
1379
1380 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1381 TEST_FUNCTION(strtoull_s_base_out_of_the_range_underflow_fail)
1382 {
1383 // arrange
1384 const char* subjectStr = "10";
1385 char* endptr;
1386 int base = 1;
1387 uint64_t result;
1388
1389 uint64_t expectedResult = 0ULL;
1390 char* expectedEndptr = (char*)subjectStr;
1391
1392 // act
1393 result = strtoull_s(subjectStr, &endptr, base);
1394
1395 // assert
1396 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1397 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1398 }
1399
1400 /*Tests_SRS_CRT_ABSTRACTIONS_21_005: [The strtoull_s must convert number using base 2 to 36.]*/
1401 TEST_FUNCTION(strtoull_s_base_out_of_the_range_overflow_fail)
1402 {
1403 // arrange
1404 const char* subjectStr = "10";
1405 char* endptr;
1406 int base = 37;
1407 uint64_t result;
1408
1409 uint64_t expectedResult = 0ULL;
1410 char* expectedEndptr = (char*)subjectStr;
1411
1412 // act
1413 result = strtoull_s(subjectStr, &endptr, base);
1414
1415 // assert
1416 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1417 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1418 }
1419
1420 /*Tests_SRS_CRT_ABSTRACTIONS_21_013: [If no conversion could be performed, the strtoull_s returns the value 0L.]*/
1421 TEST_FUNCTION(strtoull_s_invalid_string_blahblah_fail)
1422 {
1423 // arrange
1424 const char* subjectStr = "blahblah";
1425 char* endptr;
1426 int base = 10;
1427 uint64_t result;
1428
1429 uint64_t expectedResult = 0ULL;
1430 char* expectedEndptr = (char*)subjectStr;
1431
1432 // act
1433 result = strtoull_s(subjectStr, &endptr, base);
1434
1435 // assert
1436 ASSERT_ARE_EQUAL(char_ptr, "blahblah", subjectStr);
1437 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1438 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1439 }
1440
1441 /*Tests_SRS_CRT_ABSTRACTIONS_21_013: [If no conversion could be performed, the strtoull_s returns the value 0L.]*/
1442 TEST_FUNCTION(strtoull_s_empty_string_fail)
1443 {
1444 // arrange
1445 const char* subjectStr = "";
1446 char* endptr;
1447 int base = 10;
1448 uint64_t result;
1449
1450 uint64_t expectedResult = 0ULL;
1451 char* expectedEndptr = (char*)subjectStr;
1452
1453 // act
1454 result = strtoull_s(subjectStr, &endptr, base);
1455
1456 // assert
1457 ASSERT_ARE_EQUAL(char_ptr, "", subjectStr);
1458 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1459 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1460 }
1461
1462 /*Tests_SRS_CRT_ABSTRACTIONS_21_013: [If no conversion could be performed, the strtoull_s returns the value 0L.]*/
1463 /*Tests_SRS_CRT_ABSTRACTIONS_21_035: [If the nptr is NULL, the strtoull_s must not perform any conversion and must returns 0L; endptr must receive NULL, provided that endptr is not a NULL pointer.]*/
1464 TEST_FUNCTION(strtoull_s_null_ptr_to_string_fail)
1465 {
1466 // arrange
1467 const char* subjectStr = NULL;
1468 char* endptr;
1469 int base = 10;
1470 uint64_t result;
1471
1472 uint64_t expectedResult = 0ULL;
1473 char* expectedEndptr = (char*)subjectStr;
1474
1475 // act
1476 result = strtoull_s(subjectStr, &endptr, base);
1477
1478 // assert
1479 ASSERT_ARE_EQUAL(uint64_t, expectedResult, result);
1480 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1481 }
1482
1483 /* strtof_s */
1484
1485 /*Tests_SRS_CRT_ABSTRACTIONS_21_015: [The strtof_s must convert the initial portion of the string pointed to by nptr to float representation.]*/
1486 /*Tests_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
1487 /*Tests_SRS_CRT_ABSTRACTIONS_21_019: [The valid sequence for strtof_s starts after the first non-white - space character, followed by an optional positive or negative sign, a number, 'INF', or 'NAN' (ignoring case).]*/
1488 TEST_FUNCTION(strtof_s_exponential_number_success)
1489 {
1490 // arrange
1491 const char* subjectStr = "1.0e5";
1492 char* endptr;
1493 float result;
1494
1495 float expectedResult = 1.0e5;
1496 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1497
1498 // act
1499 result = strtof_s(subjectStr, &endptr);
1500
1501 // assert
1502 ASSERT_ARE_EQUAL(char_ptr, "1.0e5", subjectStr);
1503 ASSERT_ARE_EQUAL(float, expectedResult, result);
1504 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1505 }
1506
1507 /*Tests_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
1508 TEST_FUNCTION(strtof_s_uppercase_exponential_number_success)
1509 {
1510 // arrange
1511 const char* subjectStr = "1.98E5";
1512 char* endptr;
1513 float result;
1514
1515 float expectedResult = 1.98E5;
1516 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1517
1518 // act
1519 result = strtof_s(subjectStr, &endptr);
1520
1521 // assert
1522 ASSERT_ARE_EQUAL(char_ptr, "1.98E5", subjectStr);
1523 ASSERT_ARE_EQUAL(float, expectedResult, result);
1524 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1525 }
1526
1527 /*Tests_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
1528 TEST_FUNCTION(strtof_s_float_without_exponential_number_success)
1529 {
1530 // arrange
1531 const char* subjectStr = "1234.5678910";
1532 char* endptr;
1533 float result;
1534
1535 float expectedResult = 1234.5678910f;
1536 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1537
1538 // act
1539 result = strtof_s(subjectStr, &endptr);
1540
1541 // assert
1542 ASSERT_ARE_EQUAL(char_ptr, "1234.5678910", subjectStr);
1543 ASSERT_ARE_EQUAL(float, expectedResult, result);
1544 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1545 }
1546
1547 /*Tests_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
1548 TEST_FUNCTION(strtof_s_integer_number_success)
1549 {
1550 // arrange
1551 const char* subjectStr = "12345678910";
1552 char* endptr;
1553 float result;
1554
1555 float expectedResult = 12345678910.0f;
1556 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1557
1558 // act
1559 result = strtof_s(subjectStr, &endptr);
1560
1561 // assert
1562 ASSERT_ARE_EQUAL(char_ptr, "12345678910", subjectStr);
1563 ASSERT_ARE_EQUAL(float, expectedResult, result);
1564 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1565 }
1566
1567 /*Tests_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
1568 TEST_FUNCTION(strtof_s_only_fraction_number_success)
1569 {
1570 // arrange
1571 const char* subjectStr = "0.12345678910";
1572 char* endptr;
1573 float result;
1574
1575 float expectedResult = 0.12345678910f;
1576 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1577
1578 // act
1579 result = strtof_s(subjectStr, &endptr);
1580
1581 // assert
1582 ASSERT_ARE_EQUAL(char_ptr, "0.12345678910", subjectStr);
1583 ASSERT_ARE_EQUAL(float, expectedResult, result);
1584 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1585 }
1586
1587 /*Tests_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
1588 TEST_FUNCTION(strtof_s_0_with_exponential_number_success)
1589 {
1590 // arrange
1591 const char* subjectStr = "0.0e10";
1592 char* endptr;
1593 float result;
1594
1595 float expectedResult = 0.0f;
1596 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1597
1598 // act
1599 result = strtof_s(subjectStr, &endptr);
1600
1601 // assert
1602 ASSERT_ARE_EQUAL(char_ptr, "0.0e10", subjectStr);
1603 ASSERT_ARE_EQUAL(float, expectedResult, result);
1604 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1605 }
1606
1607 /*Tests_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
1608 TEST_FUNCTION(strtof_s_float_positive_number_success)
1609 {
1610 // arrange
1611 const char* subjectStr = "42.42";
1612 char* endptr;
1613 float result;
1614
1615 float expectedResult = 42.42f;
1616 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1617
1618 // act
1619 result = strtof_s(subjectStr, &endptr);
1620
1621 // assert
1622 ASSERT_ARE_EQUAL(char_ptr, "42.42", subjectStr);
1623 ASSERT_ARE_EQUAL(float, expectedResult, result);
1624 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1625 }
1626
1627 /*Tests_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
1628 TEST_FUNCTION(strtof_s_float_negative_number_success)
1629 {
1630 // arrange
1631 const char* subjectStr = "-42.42";
1632 char* endptr;
1633 float result;
1634
1635 float expectedResult = -42.42f;
1636 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1637
1638 // act
1639 result = strtof_s(subjectStr, &endptr);
1640
1641 // assert
1642 ASSERT_ARE_EQUAL(char_ptr, "-42.42", subjectStr);
1643 ASSERT_ARE_EQUAL(float, expectedResult, result);
1644 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1645 }
1646
1647 /*Tests_SRS_CRT_ABSTRACTIONS_21_018: [The white-space for strtof_s must be one of the characters ' ', '\f', '\n', '\r', '\t', '\v'.]*/
1648 TEST_FUNCTION(strtof_s_exponential_number_with_spaces_before_the_number_success)
1649 {
1650 // arrange
1651 const char* subjectStr = "\r\n1.0e5";
1652 char* endptr;
1653 float result;
1654
1655 float expectedResult = 1.0e5;
1656 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1657
1658 // act
1659 result = strtof_s(subjectStr, &endptr);
1660
1661 // assert
1662 ASSERT_ARE_EQUAL(char_ptr, "\r\n1.0e5", subjectStr);
1663 ASSERT_ARE_EQUAL(float, expectedResult, result);
1664 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1665 }
1666
1667 /*Tests_SRS_CRT_ABSTRACTIONS_21_018: [The white-space for strtof_s must be one of the characters ' ', '\f', '\n', '\r', '\t', '\v'.]*/
1668 /*Tests_SRS_CRT_ABSTRACTIONS_21_017: [The strtof_s must return in endptr a final string of one or more unrecognized characters, including the terminating null character of the input string.]*/
1669 TEST_FUNCTION(strtof_s_exponential_number_with_characters_after_the_number_success)
1670 {
1671 // arrange
1672 const char* subjectStr = "1.0e5 123";
1673 char* endptr;
1674 float result;
1675
1676 float expectedResult = 1.0e5;
1677 char* expectedEndptr = (char*)subjectStr + 5;
1678
1679 // act
1680 result = strtof_s(subjectStr, &endptr);
1681
1682 // assert
1683 ASSERT_ARE_EQUAL(char_ptr, "1.0e5 123", subjectStr);
1684 ASSERT_ARE_EQUAL(float, expectedResult, result);
1685 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1686 }
1687
1688 /*Tests_SRS_CRT_ABSTRACTIONS_21_016: [The strtof_s must return the float that represents the value in the initial part of the string. If any.]*/
1689 TEST_FUNCTION(strtof_s_min_positive_value_success)
1690 {
1691 // arrange
1692 const char* subjectStr = "1.175494351e-38";
1693 char* endptr;
1694 float result;
1695
1696 float expectedResult = FLT_MIN;
1697 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1698
1699 // act
1700 result = strtof_s(subjectStr, &endptr);
1701
1702 // assert
1703 ASSERT_ARE_EQUAL(int, 0, errno);
1704 ASSERT_ARE_EQUAL(char_ptr, "1.175494351e-38", subjectStr);
1705 ASSERT_ARE_EQUAL(float, expectedResult, result);
1706 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1707 }
1708
1709 /*Tests_SRS_CRT_ABSTRACTIONS_21_022: [If the correct value is outside the range, the strtof_s returns the value plus or minus HUGE_VALF, and errno will receive the value ERANGE.]*/
1710 TEST_FUNCTION(strtof_s_min_negative_value_success)
1711 {
1712 // arrange
1713 const char* subjectStr = "-3.402823466e+38";
1714 char* endptr;
1715 float result;
1716
1717 float expectedResult = -3.402823466e+38f;
1718 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1719
1720 // act
1721 result = strtof_s(subjectStr, &endptr);
1722
1723 // assert
1724 ASSERT_ARE_EQUAL(int, 0, errno);
1725 ASSERT_ARE_EQUAL(char_ptr, "-3.402823466e+38", subjectStr);
1726 ASSERT_ARE_EQUAL(float, expectedResult, result);
1727 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1728 }
1729
1730 /*Tests_SRS_CRT_ABSTRACTIONS_21_022: [If the correct value is outside the range, the strtof_s returns the value plus or minus HUGE_VALF, and errno will receive the value ERANGE.]*/
1731 TEST_FUNCTION(strtof_s_overflow_max_positive_value_fail)
1732 {
1733 // arrange
1734 const char* subjectStr = "3.402823467e+38";
1735 char* endptr;
1736 float result;
1737
1738 float expectedResult = HUGE_VALF;
1739 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1740
1741 // act
1742 result = strtof_s(subjectStr, &endptr);
1743
1744 // assert
1745 ASSERT_ARE_EQUAL(int, ERANGE, errno);
1746 ASSERT_ARE_EQUAL(char_ptr, "3.402823467e+38", subjectStr);
1747 ASSERT_ARE_EQUAL(float, expectedResult, result);
1748 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1749 }
1750
1751 /*Tests_SRS_CRT_ABSTRACTIONS_21_022: [If the correct value is outside the range, the strtof_s returns the value plus or minus HUGE_VALF, and errno will receive the value ERANGE.]*/
1752 TEST_FUNCTION(strtof_s_overflow_in_the_integer_part_value_fail)
1753 {
1754 // arrange
1755 const char* subjectStr = "18446744073709551616";
1756 char* endptr;
1757 float result;
1758
1759 float expectedResult = HUGE_VALF;
1760 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1761
1762 // act
1763 result = strtof_s(subjectStr, &endptr);
1764
1765 // assert
1766 ASSERT_ARE_EQUAL(int, ERANGE, errno);
1767 ASSERT_ARE_EQUAL(char_ptr, "18446744073709551616", subjectStr);
1768 ASSERT_ARE_EQUAL(float, expectedResult, result);
1769 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1770 }
1771
1772 /*Tests_SRS_CRT_ABSTRACTIONS_21_022: [If the correct value is outside the range, the strtof_s returns the value plus or minus HUGE_VALF, and errno will receive the value ERANGE.]*/
1773 TEST_FUNCTION(strtof_s_big_integer_part_value_success)
1774 {
1775 // arrange
1776 const char* subjectStr = "184467440737095516";
1777 char* endptr;
1778 float result;
1779
1780 float expectedResult = 184467440737095516.0f;
1781 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1782
1783 // act
1784 result = strtof_s(subjectStr, &endptr);
1785
1786 // assert
1787 ASSERT_ARE_EQUAL(int, 0, errno);
1788 ASSERT_ARE_EQUAL(char_ptr, "184467440737095516", subjectStr);
1789 ASSERT_ARE_EQUAL(float, expectedResult, result);
1790 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1791 }
1792
1793 /*Tests_SRS_CRT_ABSTRACTIONS_21_022: [If the correct value is outside the range, the strtof_s returns the value plus or minus HUGE_VALF, and errno will receive the value ERANGE.]*/
1794 TEST_FUNCTION(strtof_s_exponential_number_overflow_max_positive_value_fail)
1795 {
1796 // arrange
1797 const char* subjectStr = "1.0e39";
1798 char* endptr;
1799 float result;
1800
1801 float expectedResult = HUGE_VALF;
1802 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1803
1804 // act
1805 result = strtof_s(subjectStr, &endptr);
1806
1807 // assert
1808 ASSERT_ARE_EQUAL(int, ERANGE, errno);
1809 ASSERT_ARE_EQUAL(char_ptr, "1.0e39", subjectStr);
1810 ASSERT_ARE_EQUAL(float, expectedResult, result);
1811 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1812 }
1813
1814 /*Tests_SRS_CRT_ABSTRACTIONS_21_023: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtof_s must return the INFINITY value for float.]*/
1815 TEST_FUNCTION(strtof_s_short_infinity_uppercase_success)
1816 {
1817 // arrange
1818 const char* subjectStr = "INF";
1819 char* endptr;
1820 float result;
1821
1822 float expectedResult = INFINITY;
1823 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1824
1825 // act
1826 result = strtof_s(subjectStr, &endptr);
1827
1828 // assert
1829 ASSERT_ARE_EQUAL(char_ptr, "INF", subjectStr);
1830 ASSERT_ARE_EQUAL(float, expectedResult, result);
1831 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1832 }
1833
1834 /*Tests_SRS_CRT_ABSTRACTIONS_21_023: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtof_s must return the INFINITY value for float.]*/
1835 TEST_FUNCTION(strtof_s_short_negative_infinity_uppercase_success)
1836 {
1837 // arrange
1838 const char* subjectStr = "-INF";
1839 char* endptr;
1840 float result;
1841
1842 float expectedResult = -INFINITY;
1843 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1844
1845 // act
1846 result = strtof_s(subjectStr, &endptr);
1847
1848 // assert
1849 ASSERT_ARE_EQUAL(char_ptr, "-INF", subjectStr);
1850 ASSERT_ARE_EQUAL(float, expectedResult, result);
1851 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1852 }
1853
1854 /*Tests_SRS_CRT_ABSTRACTIONS_21_023: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtof_s must return the INFINITY value for float.]*/
1855 TEST_FUNCTION(strtof_s_long_infinity_uppercase_success)
1856 {
1857 // arrange
1858 const char* subjectStr = "INFINITY";
1859 char* endptr;
1860 float result;
1861
1862 float expectedResult = INFINITY;
1863 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1864
1865 // act
1866 result = strtof_s(subjectStr, &endptr);
1867
1868 // assert
1869 ASSERT_ARE_EQUAL(char_ptr, "INFINITY", subjectStr);
1870 ASSERT_ARE_EQUAL(float, expectedResult, result);
1871 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1872 }
1873
1874 /*Tests_SRS_CRT_ABSTRACTIONS_21_023: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtof_s must return the INFINITY value for float.]*/
1875 TEST_FUNCTION(strtof_s_long_infinity_mixedcase_success)
1876 {
1877 // arrange
1878 const char* subjectStr = "InFINiTY";
1879 char* endptr;
1880 float result;
1881
1882 float expectedResult = INFINITY;
1883 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1884
1885 // act
1886 result = strtof_s(subjectStr, &endptr);
1887
1888 // assert
1889 ASSERT_ARE_EQUAL(char_ptr, "InFINiTY", subjectStr);
1890 ASSERT_ARE_EQUAL(float, expectedResult, result);
1891 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1892 }
1893
1894 /*Tests_SRS_CRT_ABSTRACTIONS_21_024: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtof_s must return 0.0f and points endptr to the first character after the 'NAN' sequence.]*/
1895 TEST_FUNCTION(strtof_s_short_nan_uppercase_success)
1896 {
1897 // arrange
1898 const char* subjectStr = "NAN";
1899 char* endptr;
1900 float result;
1901
1902 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1903
1904 // act
1905 result = strtof_s(subjectStr, &endptr);
1906
1907 // assert
1908 ASSERT_ARE_EQUAL(char_ptr, "NAN", subjectStr);
1909 ASSERT_IS_TRUE(isnan(result));
1910 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1911 }
1912
1913 /*Tests_SRS_CRT_ABSTRACTIONS_21_024: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtof_s must return 0.0f and points endptr to the first character after the 'NAN' sequence.]*/
1914 TEST_FUNCTION(strtof_s_long_nan_uppercase_success)
1915 {
1916 // arrange
1917 const char* subjectStr = "NAN(1234)";
1918 char* endptr;
1919 float result;
1920
1921 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1922
1923 // act
1924 result = strtof_s(subjectStr, &endptr);
1925
1926 // assert
1927 ASSERT_ARE_EQUAL(char_ptr, "NAN(1234)", subjectStr);
1928 ASSERT_IS_TRUE(isnan(result));
1929 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1930 }
1931
1932 /*Tests_SRS_CRT_ABSTRACTIONS_21_024: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtof_s must return 0.0f and points endptr to the first character after the 'NAN' sequence.]*/
1933 TEST_FUNCTION(strtof_s_long_nan_mixedcase_success)
1934 {
1935 // arrange
1936 const char* subjectStr = "NaN(1234)";
1937 char* endptr;
1938 float result;
1939
1940 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
1941
1942 // act
1943 result = strtof_s(subjectStr, &endptr);
1944
1945 // assert
1946 ASSERT_ARE_EQUAL(char_ptr, "NaN(1234)", subjectStr);
1947 ASSERT_IS_TRUE(isnan(result));
1948 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1949 }
1950
1951 /*Tests_SRS_CRT_ABSTRACTIONS_21_024: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtof_s must return 0.0f and points endptr to the first character after the 'NAN' sequence.]*/
1952 TEST_FUNCTION(strtof_s_long_nan_withou_close_parenthesis_fail)
1953 {
1954 // arrange
1955 const char* subjectStr = "NaN(1234";
1956 char* endptr;
1957 float result;
1958
1959 float expectedResult = 0.0f;
1960 char* expectedEndptr = (char*)subjectStr;
1961
1962 // act
1963 result = strtof_s(subjectStr, &endptr);
1964
1965 // assert
1966 ASSERT_ARE_EQUAL(char_ptr, "NaN(1234", subjectStr);
1967 ASSERT_ARE_EQUAL(float, expectedResult, result);
1968 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1969 }
1970
1971 /*Tests_SRS_CRT_ABSTRACTIONS_21_020: [If the subject sequence is empty or does not have the expected form, the strtof_s must not perform any conversion and must returns 0.0f; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
1972 /*Tests_SRS_CRT_ABSTRACTIONS_21_021: [If no conversion could be performed, the strtof_s returns the value 0.0f.]*/
1973 TEST_FUNCTION(strtof_s_empty_string_success)
1974 {
1975 // arrange
1976 const char* subjectStr = "";
1977 char* endptr;
1978 float result;
1979
1980 float expectedResult = 0.0f;
1981 char* expectedEndptr = (char*)subjectStr;
1982
1983 // act
1984 result = strtof_s(subjectStr, &endptr);
1985
1986 // assert
1987 ASSERT_ARE_EQUAL(char_ptr, "", subjectStr);
1988 ASSERT_ARE_EQUAL(float, expectedResult, result);
1989 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
1990 }
1991
1992 /*Tests_SRS_CRT_ABSTRACTIONS_21_036: [**If the nptr is NULL, the strtof_s must not perform any conversion and must returns 0.0f; endptr must receive NULL, provided that endptr is not a NULL pointer.]*/
1993 /*Tests_SRS_CRT_ABSTRACTIONS_21_021: [If no conversion could be performed, the strtof_s returns the value 0.0f.]*/
1994 TEST_FUNCTION(strtof_s_string_to_null_pointer_success)
1995 {
1996 // arrange
1997 const char* subjectStr = NULL;
1998 char* endptr;
1999 float result;
2000
2001 float expectedResult = 0.0f;
2002 char* expectedEndptr = NULL;
2003
2004 // act
2005 result = strtof_s(subjectStr, &endptr);
2006
2007 // assert
2008 ASSERT_ARE_EQUAL(char_ptr, NULL, subjectStr);
2009 ASSERT_ARE_EQUAL(float, expectedResult, result);
2010 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2011 }
2012
2013 /*Tests_SRS_CRT_ABSTRACTIONS_21_020: [If the subject sequence is empty or does not have the expected form, the strtof_s must not perform any conversion and must returns 0.0f; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
2014 TEST_FUNCTION(strtof_s_valid_conversion_with_return_string_null_pointer_success)
2015 {
2016 // arrange
2017 const char* subjectStr = "1.0e5";
2018 float result;
2019
2020 float expectedResult = 1.0e5;
2021
2022 // act
2023 result = strtof_s(subjectStr, NULL);
2024
2025 // assert
2026 ASSERT_ARE_EQUAL(char_ptr, "1.0e5", subjectStr);
2027 ASSERT_ARE_EQUAL(float, expectedResult, result);
2028 }
2029
2030 /*Tests_SRS_CRT_ABSTRACTIONS_21_020: [If the subject sequence is empty or does not have the expected form, the strtof_s must not perform any conversion and must returns 0.0f; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
2031 /*Tests_SRS_CRT_ABSTRACTIONS_21_021: [If no conversion could be performed, the strtof_s returns the value 0.0f.]*/
2032 TEST_FUNCTION(strtof_s_invalid_conversion_with_return_string_null_pointer_success)
2033 {
2034 // arrange
2035 const char* subjectStr = "blahblah";
2036 float result;
2037
2038 float expectedResult = 0.0f;
2039
2040 // act
2041 result = strtof_s(subjectStr, NULL);
2042
2043 // assert
2044 ASSERT_ARE_EQUAL(char_ptr, "blahblah", subjectStr);
2045 ASSERT_ARE_EQUAL(float, expectedResult, result);
2046 }
2047
2048 /* strtold_s */
2049
2050 /*Tests_SRS_CRT_ABSTRACTIONS_21_025: [The strtold_s must convert the initial portion of the string pointed to by nptr to long double representation.]*/
2051 /*Tests_SRS_CRT_ABSTRACTIONS_21_026: [The strtold_s must return the long double that represents the value in the initial part of the string. If any.]*/
2052 /*Tests_SRS_CRT_ABSTRACTIONS_21_029: [The valid sequence for strtold_s starts after the first non-white - space character, followed by an optional positive or negative sign, a number, 'INF', or 'NAN' (ignoring case).]*/
2053 TEST_FUNCTION(strtold_s_exponential_number_success)
2054 {
2055 // arrange
2056 const char* subjectStr = "1.0e5";
2057 char* endptr;
2058 long double result;
2059
2060 double expectedResult = 1.0e5;
2061 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
2062
2063 // act
2064 result = strtold_s(subjectStr, &endptr);
2065
2066 // assert
2067 ASSERT_ARE_EQUAL(char_ptr, "1.0e5", subjectStr);
2068 ASSERT_IS_TRUE(expectedResult == result);
2069 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2070 }
2071
2072 /*Tests_SRS_CRT_ABSTRACTIONS_21_028: [The white-space for strtold_s must be one of the characters ' ', '\f', '\n', '\r', '\t', '\v'.]*/
2073 /*Tests_SRS_CRT_ABSTRACTIONS_21_027: [The strtold_s must return in endptr a final string of one or more unrecognized characters, including the terminating null character of the input string.]*/
2074 TEST_FUNCTION(strtold_s_exponential_number_with_characters_after_the_number_success)
2075 {
2076 // arrange
2077 const char* subjectStr = "1.0e5 123";
2078 char* endptr;
2079 long double result;
2080
2081 long double expectedResult = 1.0e5;
2082 char* expectedEndptr = (char*)subjectStr + 5;
2083
2084 // act
2085 result = strtold_s(subjectStr, &endptr);
2086
2087 // assert
2088 ASSERT_ARE_EQUAL(char_ptr, "1.0e5 123", subjectStr);
2089 ASSERT_IS_TRUE(expectedResult == result);
2090 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2091 }
2092
2093
2094 /*Tests_SRS_CRT_ABSTRACTIONS_21_026: [The strtold_s must return the long double that represents the value in the initial part of the string. If any.]*/
2095 TEST_FUNCTION(strtold_s_min_positive_value_success)
2096 {
2097 // arrange
2098 const char* subjectStr = "2.225073858507201e-308";
2099 char* endptr;
2100 long double result;
2101 long double significant;
2102
2103 long double maxExpectedSignificant = 2.225073858507202;
2104 long double minExpectedSignificant = 2.225073858507200;
2105 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
2106
2107 // act
2108 result = strtold_s(subjectStr, &endptr);
2109 significant = result*1e308;
2110
2111 // assert
2112 ASSERT_ARE_EQUAL(int, 0, errno);
2113 ASSERT_ARE_EQUAL(char_ptr, "2.225073858507201e-308", subjectStr);
2114 ASSERT_IS_TRUE((significant >= minExpectedSignificant) && (significant <= maxExpectedSignificant));
2115 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2116 }
2117
2118 /*Tests_SRS_CRT_ABSTRACTIONS_21_032: [If the correct value is outside the range, the strtold_s returns the value plus or minus HUGE_VALL, and errno will receive the value ERANGE.]*/
2119 TEST_FUNCTION(strtold_s_max_positive_exponential_number_success)
2120 {
2121 // arrange
2122 const char* subjectStr = "1.797693134862315e+308";
2123 char* endptr;
2124 long double result;
2125 long double significant;
2126
2127 long double maxExpectedSignificant = 1.797693134862316;
2128 long double minExpectedSignificant = 1.797693134862314;
2129 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
2130
2131 // act
2132 result = strtold_s(subjectStr, &endptr);
2133 significant = result*1e-308;
2134
2135 // assert
2136 ASSERT_ARE_EQUAL(int, 0, errno);
2137 ASSERT_ARE_EQUAL(char_ptr, "1.797693134862315e+308", subjectStr);
2138 ASSERT_IS_TRUE((significant >= minExpectedSignificant) && (significant <= maxExpectedSignificant));
2139 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2140 }
2141
2142 /*Tests_SRS_CRT_ABSTRACTIONS_21_032: [If the correct value is outside the range, the strtold_s returns the value plus or minus HUGE_VALL, and errno will receive the value ERANGE.]*/
2143 TEST_FUNCTION(strtold_s_min_negative_value_success)
2144 {
2145 // arrange
2146 const char* subjectStr = "-1.797693134862315e+308";
2147 char* endptr;
2148 long double result;
2149 long double significant;
2150
2151 long double maxExpectedSignificant = -1.797693134862314;
2152 long double minExpectedSignificant = -1.797693134862316;
2153 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
2154
2155 // act
2156 result = strtold_s(subjectStr, &endptr);
2157 significant = result*1e-308;
2158
2159 // assert
2160 ASSERT_ARE_EQUAL(int, 0, errno);
2161 ASSERT_ARE_EQUAL(char_ptr, "-1.797693134862315e+308", subjectStr);
2162 ASSERT_IS_TRUE((significant >= minExpectedSignificant) && (significant <= maxExpectedSignificant));
2163 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2164 }
2165
2166 /*Tests_SRS_CRT_ABSTRACTIONS_21_032: [If the correct value is outside the range, the strtold_s returns the value plus or minus HUGE_VALL, and errno will receive the value ERANGE.]*/
2167 TEST_FUNCTION(strtold_s_overflow_max_positive_value_fail)
2168 {
2169 // arrange
2170 const char* subjectStr = "1.8e+308";
2171 char* endptr;
2172 long double result;
2173
2174 long double expectedResult = HUGE_VALL;
2175 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
2176
2177 // act
2178 result = strtold_s(subjectStr, &endptr);
2179
2180 // assert
2181 ASSERT_ARE_EQUAL(int, ERANGE, errno);
2182 ASSERT_ARE_EQUAL(char_ptr, "1.8e+308", subjectStr);
2183 ASSERT_IS_TRUE(expectedResult == result);
2184 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2185 }
2186
2187 /*Tests_SRS_CRT_ABSTRACTIONS_21_032: [If the correct value is outside the range, the strtold_s returns the value plus or minus HUGE_VALL, and errno will receive the value ERANGE.]*/
2188 TEST_FUNCTION(strtold_s_exponential_number_overflow_max_positive_value_fail)
2189 {
2190 // arrange
2191 const char* subjectStr = "1.0e309";
2192 char* endptr;
2193 long double result;
2194
2195 long double expectedResult = HUGE_VALF;
2196 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
2197
2198 // act
2199 result = strtold_s(subjectStr, &endptr);
2200
2201 // assert
2202 ASSERT_ARE_EQUAL(int, ERANGE, errno);
2203 ASSERT_ARE_EQUAL(char_ptr, "1.0e309", subjectStr);
2204 ASSERT_IS_TRUE(expectedResult == result);
2205 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2206 }
2207
2208 /*Tests_SRS_CRT_ABSTRACTIONS_21_033: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtold_s must return the INFINITY value for long double.]*/
2209 TEST_FUNCTION(strtold_s_short_infinity_uppercase_success)
2210 {
2211 // arrange
2212 const char* subjectStr = "INF";
2213 char* endptr;
2214 long double result;
2215
2216 long double expectedResult = INFINITY;
2217 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
2218
2219 // act
2220 result = strtold_s(subjectStr, &endptr);
2221
2222 // assert
2223 ASSERT_ARE_EQUAL(char_ptr, "INF", subjectStr);
2224 ASSERT_IS_TRUE(expectedResult == result);
2225 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2226 }
2227
2228 /*Tests_SRS_CRT_ABSTRACTIONS_21_033: [If the string is 'INF' of 'INFINITY' (ignoring case), the strtold_s must return the INFINITY value for long double.]*/
2229 TEST_FUNCTION(strtold_s_short_negative_infinity_uppercase_success)
2230 {
2231 // arrange
2232 const char* subjectStr = "-INF";
2233 char* endptr;
2234 long double result;
2235
2236 long double expectedResult = -INFINITY;
2237 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
2238
2239 // act
2240 result = strtold_s(subjectStr, &endptr);
2241
2242 // assert
2243 ASSERT_ARE_EQUAL(char_ptr, "-INF", subjectStr);
2244 ASSERT_IS_TRUE(expectedResult == result);
2245 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2246 }
2247
2248 /*Tests_SRS_CRT_ABSTRACTIONS_21_034: [If the string is 'NAN' or 'NAN(...)' (ignoring case), the strtold_s must return 0.0 and points endptr to the first character after the 'NAN' sequence.]*/
2249 TEST_FUNCTION(strtold_s_long_nan_mixedcase_success)
2250 {
2251 // arrange
2252 const char* subjectStr = "NaN(1234)";
2253 char* endptr;
2254 long double result;
2255
2256 char* expectedEndptr = (char*)subjectStr + strlen(subjectStr);
2257
2258 // act
2259 result = strtold_s(subjectStr, &endptr);
2260
2261 // assert
2262 ASSERT_ARE_EQUAL(char_ptr, "NaN(1234)", subjectStr);
2263 ASSERT_IS_TRUE(isnan((double)result));
2264 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2265 }
2266
2267 /*Tests_SRS_CRT_ABSTRACTIONS_21_030: [If the subject sequence is empty or does not have the expected form, the strtold_s must not perform any conversion and must returns 0.0; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
2268 /*Tests_SRS_CRT_ABSTRACTIONS_21_031: [If no conversion could be performed, the strtold_s returns the value 0.0.]*/
2269 TEST_FUNCTION(strtold_s_empty_string_success)
2270 {
2271 // arrange
2272 const char* subjectStr = "";
2273 char* endptr;
2274 long double result;
2275
2276 long double expectedResult = 0.0;
2277 char* expectedEndptr = (char*)subjectStr;
2278
2279 // act
2280 result = strtold_s(subjectStr, &endptr);
2281
2282 // assert
2283 ASSERT_ARE_EQUAL(char_ptr, "", subjectStr);
2284 ASSERT_IS_TRUE(expectedResult == result);
2285 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2286 }
2287
2288 /*Tests_SRS_CRT_ABSTRACTIONS_21_030: [If the subject sequence is empty or does not have the expected form, the strtold_s must not perform any conversion and must returns 0.0; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
2289 /*Tests_SRS_CRT_ABSTRACTIONS_21_031: [If no conversion could be performed, the strtold_s returns the value 0.0.]*/
2290 /*Tests_SRS_CRT_ABSTRACTIONS_21_037: [If the nptr is NULL, the strtold_s must not perform any conversion and must returns 0.0; endptr must receive NULL, provided that endptr is not a NULL pointer.]*/
2291 TEST_FUNCTION(strtold_s_string_to_null_pointer_success)
2292 {
2293 // arrange
2294 const char* subjectStr = NULL;
2295 char* endptr;
2296 long double result;
2297
2298 long double expectedResult = 0.0;
2299 char* expectedEndptr = NULL;
2300
2301 // act
2302 result = strtold_s(subjectStr, &endptr);
2303
2304 // assert
2305 ASSERT_ARE_EQUAL(char_ptr, NULL, subjectStr);
2306 ASSERT_IS_TRUE(expectedResult == result);
2307 ASSERT_ARE_EQUAL(void_ptr, expectedEndptr, endptr);
2308 }
2309
2310 /*Tests_SRS_CRT_ABSTRACTIONS_21_030: [If the subject sequence is empty or does not have the expected form, the strtold_s must not perform any conversion and must returns 0.0; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
2311 TEST_FUNCTION(strtold_s_valid_conversion_with_return_string_null_pointer_success)
2312 {
2313 // arrange
2314 const char* subjectStr = "1.0e5";
2315 long double result;
2316
2317 long double expectedResult = 1.0e5;
2318
2319 // act
2320 result = strtold_s(subjectStr, NULL);
2321
2322 // assert
2323 ASSERT_ARE_EQUAL(char_ptr, "1.0e5", subjectStr);
2324 ASSERT_IS_TRUE(expectedResult == result);
2325 }
2326
2327 /*Tests_SRS_CRT_ABSTRACTIONS_21_030: [If the subject sequence is empty or does not have the expected form, the strtold_s must not perform any conversion and must returns 0.0; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.]*/
2328 /*Tests_SRS_CRT_ABSTRACTIONS_21_031: [If no conversion could be performed, the strtold_s returns the value 0.0.]*/
2329 TEST_FUNCTION(strtold_s_invalid_conversion_with_return_string_null_pointer_success)
2330 {
2331 // arrange
2332 const char* subjectStr = "blahblah";
2333 long double result;
2334
2335 long double expectedResult = 0.0;
2336
2337 // act
2338 result = strtold_s(subjectStr, NULL);
2339
2340 // assert
2341 ASSERT_ARE_EQUAL(char_ptr, "blahblah", subjectStr);
2342 ASSERT_IS_TRUE(expectedResult == result);
2343 }
2344
2345 /* mallocAndStrcpy_s */
2346
2347 // Tests_SRS_CRT_ABSTRACTIONS_99_038 : [mallocAndstrcpy_s shall allocate memory for destination buffer to fit the string in the source parameter.]
2348 // Tests_SRS_CRT_ABSTRACTIONS_99_039 : [mallocAndstrcpy_s shall copy the contents in the address source, including the terminating null character into location specified by the destination pointer after the memory allocation.]
2349 // Tests_SRS_CRT_ABSTRACTIONS_99_035: [mallocAndstrcpy_s shall return Zero upon success]
2350 TEST_FUNCTION(mallocAndStrcpy_s_copies_source_string_into_allocated_memory)
2351 {
2352 // arrange
2353 char* destString = NULL;
2354 char srcString[] = "Source";
2355 int result;
2356
2357 // act
2358 result = mallocAndStrcpy_s(&destString, srcString);
2359
2360 // assert
2361 ASSERT_ARE_EQUAL(char_ptr, destString, srcString);
2362 ASSERT_ARE_EQUAL(int, 0, result);
2363
2364 ///cleanup
2365 free(destString);
2366 }
2367
2368 // Tests_SRS_CRT_ABSTRACTIONS_99_036: [destination parameter or source parameter is NULL, the error code returned shall be EINVAL and destination shall not be modified.]
2369 TEST_FUNCTION(mallocAndStrcpy_s_fails_with_destination_pointer_set_to_null)
2370 {
2371 // arrange
2372 char** destPointer = NULL;
2373 char srcString[] = "Source";
2374 int result;
2375
2376 // act
2377 result = mallocAndStrcpy_s(destPointer, srcString);
2378
2379 // assert
2380 ASSERT_ARE_EQUAL(int, EINVAL, result);
2381 }
2382
2383 TEST_FUNCTION(mallocAndStrcpy_s_fails_with_source_set_to_null)
2384 {
2385 // arrange
2386 char* destString = (char*)("Destination");
2387 char* srcString = NULL;
2388 int result;
2389
2390 // act
2391 result = mallocAndStrcpy_s(&destString, srcString);
2392
2393 // assert
2394 ASSERT_ARE_EQUAL(char_ptr, "Destination", destString);
2395 ASSERT_ARE_EQUAL(int, EINVAL, result);
2396 }
2397
2398 /*http://vstfrd:8080/Azure/RD/_workitems/edit/3216760*/
2399 #if 0
2400 // Tests_SRS_CRT_ABSTRACTIONS_99_037: [Upon failure to allocate memory for the destination, the function will return ENOMEM.]
2401 TEST_FUNCTION(mallocAndStrcpy_s_fails_upon_failure_to_allocate_memory)
2402 {
2403 // arrange
2404 char* destString = NULL;
2405 char* srcString = "Source";
2406 int result;
2407 mallocSize = 0;
2408
2409 // act
2410 #ifdef _CRTDBG_MAP_ALLOC
2411 HOOK_FUNCTION(_malloc_dbg, malloc_null);
2412 result = mallocAndStrcpy_s(&destString, srcString);
2413 UNHOOK_FUNCTION(_malloc_dbg, malloc_null);
2414 #else
2415 HOOK_FUNCTION(malloc, malloc_null);
2416 result = mallocAndStrcpy_s(&destString, srcString);
2417 UNHOOK_FUNCTION(malloc, malloc_null);
2418 #endif
2419
2420 // assert
2421 ASSERT_ARE_EQUAL(int, ENOMEM, result);
2422 ASSERT_ARE_EQUAL(size_t,strlen(srcString)+1, mallocSize);
2423 }
2424 #endif
2425
2426 /*Tests_SRS_CRT_ABSTRACTIONS_02_003: [If destination is NULL then unsignedIntToString shall fail.] */
2427 TEST_FUNCTION(unsignedIntToString_fails_when_destination_is_NULL)
2428 {
2429 // arrange
2430
2431 // act
2432 int result = unsignedIntToString(NULL, 100, 43);
2433
2434 // assert
2435 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2436 }
2437
2438 /*Tests_SRS_CRT_ABSTRACTIONS_02_002: [If the conversion fails for any reason (for example, insufficient buffer space), a non-zero return value shall be supplied and unsignedIntToString shall fail.] */
2439 TEST_FUNCTION(unsignedIntToString_fails_when_destination_is_not_sufficient_for_1_digit)
2440 {
2441 // arrange
2442 char destination[1000];
2443 unsigned int toBeConverted = 1;
2444 size_t destinationSize = 1;
2445
2446 ///act
2447 int result = unsignedIntToString(destination, destinationSize, toBeConverted);
2448
2449 ///assert
2450 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2451
2452 }
2453
2454 /*Tests_SRS_CRT_ABSTRACTIONS_02_002: [If the conversion fails for any reason (for example, insufficient buffer space), a non-zero return value shall be supplied and unsignedIntToString shall fail.] */
2455 TEST_FUNCTION(unsignedIntToString_fails_when_destination_is_not_sufficient_for_more_than_1_digit)
2456 {
2457 // arrange
2458 char destination[1000];
2459 unsigned int toBeConverted = 1; /*7 would not be a right starting digit*/
2460 size_t destinationSize = 1;
2461 while (toBeConverted <= (UINT_MAX / 10))
2462 {
2463 ///arrange
2464 int result;
2465 destinationSize++;
2466 toBeConverted *= 10;
2467
2468 ///act
2469 result = unsignedIntToString(destination, destinationSize, toBeConverted);
2470
2471 ///assert
2472 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2473 }
2474 }
2475
2476 /*Tests_SRS_CRT_ABSTRACTIONS_02_001: [unsignedIntToString shall convert the parameter value to its decimal representation as a string in the buffer indicated by parameter destination having the size indicated by parameter destinationSize.] */
2477 TEST_FUNCTION(unsignedIntToString_succeeds_1_digit)
2478 {
2479 // arrange
2480 char destination[1000];
2481 unsigned int toBeConverted = 2;
2482 size_t destinationSize = 2;
2483
2484 ///act
2485 int result = unsignedIntToString(destination, destinationSize, toBeConverted);
2486
2487 ///assert
2488 ASSERT_ARE_EQUAL(int, 0, result);
2489 ASSERT_ARE_EQUAL(char_ptr, "2", destination);
2490
2491 }
2492
2493 /*Tests_SRS_CRT_ABSTRACTIONS_02_001: [unsignedIntToString shall convert the parameter value to its decimal representation as a string in the buffer indicated by parameter destination having the size indicated by parameter destinationSize.] */
2494 /*Tests_SRS_CRT_ABSTRACTIONS_02_004: [If the conversion has been successfull then unsignedIntToString shall return 0.] */
2495 TEST_FUNCTION(unsignedIntToString_succeeds_for_interesting_numbers)
2496 {
2497 // arrange
2498 char destination[1000];
2499 size_t i;
2500 for (i = 0; i<sizeof(interestingUnsignedIntNumbersToBeConverted) / sizeof(interestingUnsignedIntNumbersToBeConverted[0]); i++)
2501 {
2502 ///act
2503 unsigned int valueFromString = 0;
2504 size_t pos = 0;
2505 int result = unsignedIntToString(destination, 1000, interestingUnsignedIntNumbersToBeConverted[i]);
2506
2507 ///assert
2508 ASSERT_ARE_EQUAL(int, 0, result);
2509
2510 while (destination[pos] != '\0')
2511 {
2512 if (valueFromString > (UINT_MAX / 10))
2513 {
2514 ASSERT_FAIL("string produced was too big... ");
2515 }
2516 else
2517 {
2518 valueFromString *= 10;
2519 valueFromString += (destination[pos] - '0');
2520 }
2521 pos++;
2522 }
2523 if (interestingUnsignedIntNumbersToBeConverted[i] != valueFromString)
2524 {
2525 ASSERT_FAIL("unexpected value");
2526 }
2527 }
2528 }
2529
2530 /*Tests_SRS_CRT_ABSTRACTIONS_02_001: [unsignedIntToString shall convert the parameter value to its decimal representation as a string in the buffer indicated by parameter destination having the size indicated by parameter destinationSize.] */
2531 /*Tests_SRS_CRT_ABSTRACTIONS_02_004: [If the conversion has been successfull then unsignedIntToString shall return 0.] */
2532 TEST_FUNCTION(unsignedIntToString_succeeds_for_space_just_about_right)
2533 {
2534 // arrange
2535 char destination[1000];
2536 unsigned int toBeConverted = 1; /*7 would not be a right starting digit*/
2537 size_t destinationSize = 2;
2538 while (toBeConverted <= (UINT_MAX / 10))
2539 {
2540 ///arrange
2541 int result;
2542 unsigned int valueFromString = 0;
2543 size_t pos = 0;
2544 destinationSize++;
2545 toBeConverted *= 10;
2546
2547 ///act
2548 result = unsignedIntToString(destination, destinationSize, toBeConverted);
2549
2550 ///assert
2551 ASSERT_ARE_EQUAL(int, 0, result);
2552
2553 while (destination[pos] != '\0')
2554 {
2555 if (valueFromString > (UINT_MAX / 10))
2556 {
2557 ASSERT_FAIL("string produced was too big... ");
2558 }
2559 else
2560 {
2561 valueFromString *= 10;
2562 valueFromString += (destination[pos] - '0');
2563 }
2564 pos++;
2565 }
2566 if (toBeConverted != valueFromString)
2567 {
2568 ASSERT_FAIL("unexpected value");
2569 }
2570 }
2571 }
2572
2573
2574 /*Tests_SRS_CRT_ABSTRACTIONS_02_007: [If destination is NULL then size_tToString shall fail.] */
2575 TEST_FUNCTION(size_tToString_fails_when_destination_is_NULL)
2576 {
2577 // arrange
2578
2579 // act
2580 int result = size_tToString(NULL, 100, 43);
2581
2582 // assert
2583 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2584 }
2585
2586 /*Tests_SRS_CRT_ABSTRACTIONS_02_006: [If the conversion fails for any reason (for example, insufficient buffer space), a non-zero return value shall be supplied and size_tToString shall fail.] */
2587 TEST_FUNCTION(size_tToString_fails_when_destination_is_not_sufficient_for_1_digit)
2588 {
2589 // arrange
2590 char destination[1000];
2591 size_t toBeConverted = 1;
2592 size_t destinationSize = 1;
2593
2594 ///act
2595 int result = size_tToString(destination, destinationSize, toBeConverted);
2596
2597 ///assert
2598 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2599
2600 }
2601
2602 /*Tests_SRS_CRT_ABSTRACTIONS_02_006: [If the conversion fails for any reason (for example, insufficient buffer space), a non-zero return value shall be supplied and size_tToString shall fail.] */
2603 TEST_FUNCTION(size_tToString_fails_when_destination_is_not_sufficient_for_more_than_1_digit)
2604 {
2605 // arrange
2606 char destination[1000];
2607 size_t toBeConverted = 1; /*7 would not be a right starting digit*/
2608 size_t destinationSize = 1;
2609 while (toBeConverted <= (UINT_MAX / 10))
2610 {
2611 ///arrange
2612 int result;
2613 destinationSize++;
2614 toBeConverted *= 10;
2615
2616 ///act
2617 result = size_tToString(destination, destinationSize, toBeConverted);
2618
2619 ///assert
2620 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2621 }
2622 }
2623
2624 /*Tests_SRS_CRT_ABSTRACTIONS_02_001: [size_tToString shall convert the parameter value to its decimal representation as a string in the buffer indicated by parameter destination having the size indicated by parameter destinationSize.] */
2625 TEST_FUNCTION(size_tToString_succeeds_1_digit)
2626 {
2627 // arrange
2628 char destination[1000];
2629 size_t toBeConverted = 2;
2630 size_t destinationSize = 2;
2631
2632 ///act
2633 int result = size_tToString(destination, destinationSize, toBeConverted);
2634
2635 ///assert
2636 ASSERT_ARE_EQUAL(int, 0, result);
2637 ASSERT_ARE_EQUAL(char_ptr, "2", destination);
2638
2639 }
2640
2641 /*Tests_SRS_CRT_ABSTRACTIONS_02_001: [size_tToString shall convert the parameter value to its decimal representation as a string in the buffer indicated by parameter destination having the size indicated by parameter destinationSize.] */
2642 /*Tests_SRS_CRT_ABSTRACTIONS_02_004: [If the conversion has been successfull then size_tToString shall return 0.] */
2643 TEST_FUNCTION(size_tToString_succeeds_for_interesting_numbers)
2644 {
2645 // arrange
2646 char destination[1000];
2647 size_t i;
2648 for (i = 0; i<sizeof(interestingSize_tNumbersToBeConverted) / sizeof(interestingSize_tNumbersToBeConverted[0]); i++)
2649 {
2650 ///act
2651 size_t valueFromString = 0;
2652 size_t pos = 0;
2653 int result = size_tToString(destination, 1000, interestingSize_tNumbersToBeConverted[i]);
2654
2655 ///assert
2656 ASSERT_ARE_EQUAL(int, 0, result);
2657
2658 while (destination[pos] != '\0')
2659 {
2660 if (valueFromString > (SIZE_MAX / 10))
2661 {
2662 ASSERT_FAIL("string produced was too big... ");
2663 }
2664 else
2665 {
2666 valueFromString *= 10;
2667 valueFromString += (destination[pos] - '0');
2668 }
2669 pos++;
2670 }
2671 if (interestingSize_tNumbersToBeConverted[i] != valueFromString)
2672 {
2673 ASSERT_FAIL("unexpected value");
2674 }
2675 }
2676 }
2677
2678 /*Tests_SRS_CRT_ABSTRACTIONS_02_001: [size_tToString shall convert the parameter value to its decimal representation as a string in the buffer indicated by parameter destination having the size indicated by parameter destinationSize.] */
2679 /*Tests_SRS_CRT_ABSTRACTIONS_02_004: [If the conversion has been successfull then size_tToString shall return 0.] */
2680 TEST_FUNCTION(size_tToString_succeeds_for_space_just_about_right)
2681 {
2682 // arrange
2683 char destination[1000];
2684 size_t toBeConverted = 1; /*7 would not be a right starting digit*/
2685 size_t destinationSize = 2;
2686 while (toBeConverted <= (SIZE_MAX / 10))
2687 {
2688 ///arrange
2689 int result;
2690 size_t valueFromString = 0;
2691 size_t pos = 0;
2692 destinationSize++;
2693 toBeConverted *= 10;
2694
2695 ///act
2696 result = size_tToString(destination, destinationSize, toBeConverted);
2697
2698 ///assert
2699 ASSERT_ARE_EQUAL(int, 0, result);
2700
2701 while (destination[pos] != '\0')
2702 {
2703 if (valueFromString > (SIZE_MAX / 10))
2704 {
2705 ASSERT_FAIL("string produced was too big... ");
2706 }
2707 else
2708 {
2709 valueFromString *= 10;
2710 valueFromString += (destination[pos] - '0');
2711 }
2712 pos++;
2713 }
2714 if (toBeConverted != valueFromString)
2715 {
2716 ASSERT_FAIL("unexpected value");
2717 }
2718 }
2719 }
2720
2721 END_TEST_SUITE(CRTAbstractions_UnitTests)