-
Notifications
You must be signed in to change notification settings - Fork 393
/
omrstrTest.cpp
1578 lines (1402 loc) · 66.7 KB
/
omrstrTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 1991, 2021 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
/*
* $RCSfile: omrstrTest.c,v $
* $Revision: 1.46 $
* $Date: 2011-12-30 14:15:00 $
*/
/**
* @file
* @ingroup PortTest
* @brief Verify port library string operations.
*
* Exercise the API for port library string operations. These functions
* can be found in the file @ref omrstr.c and in file @ref omrstrftime.c
*
* @note port library string operations are not optional in the port library table.
*
*/
#if defined(OMR_OS_WINDOWS)
#include <windows.h>
#endif /* defined(OMR_OS_WINDOWS) */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "testHelpers.hpp"
#include "omrport.h"
#include "omrstdarg.h"
#define J9STR_BUFFER_SIZE 128 /**< @internal A buffer size to play with */
#define J9STR_PRIVATE_RETURN_VALUE ((uint32_t)0xBADC0DE) /**< @internal A known return for @ref fake_omrstr_printf */
#if 0
/* some useful numbers */
#define NUM_MSECS_IN_YEAR 31536000000
#define NUM_MSECS_IN_THIRTY_DAYS 2592000000
/* 1192224606740 -> 2007/10/12 17:30:06 */
/* 1150320606740 -> 2006/06/14 17:30:06 */
/* 1160688606740 -> 2006/10/12 17:30:06 */
/* 1165872606740 -> 2006/12/11 16:30:06 */
/* 1139952606740 -> 2006/02/14 16:30:06 */
#endif
/**
* @internal
* @typdef
* Function prototype for verifying omrstr_printf calls omrstr_vprintf
*/
typedef uintptr_t (* J9STR_VPRINTF_FUNC)(struct OMRPortLibrary *portLibrary, char *buf, uintptr_t bufLen, const char *format, va_list args);
/**
* Verify helpers functions call correct functions.
*
* Override @ref omrstr.c::omrstr_vprintf "omrstr_vprintf()" and return a known value to verify any helper
* functions that should be calling this function truly are.
*
* @param[in] portLibrary The port library.
* @param[in, out] buf The string buffer to be written.
* @param[in] bufLen The size of the string buffer to be written.
* @param[in] format The format of the string.
* @param[in] args Arguments for the format string.
*
* @return J9STR_PRIVATE_RETURN_VALUE.
*/
static uintptr_t
fake_omrstr_vprintf(struct OMRPortLibrary *portLibrary, char *buf, uintptr_t bufLen, const char *format, va_list args)
{
return J9STR_PRIVATE_RETURN_VALUE;
}
/**
* @internal
* Helper function for string verification.
*
* Given a format string and it's arguments create the requested output message and
* put it in the provided buffer.
*
* @param[in] portLibrary The port library under test
* @param[in] testName The name of the test requesting this functionality
* @param[in,out] buffer buffer to write to
* @param[in] bufLength Size of buffer
* @param[in] expectedResult Expected output
* @param[in] format The format string to use
* @param[in] ... Arguments for format string
*/
static void
validate_omrstr_vprintf(struct OMRPortLibrary *portLibrary, const char *testName, char *buffer, uintptr_t bufLength, const char *expectedResult, const char *format, va_list args)
{
OMRPORT_ACCESS_FROM_OMRPORT(portLibrary);
uintptr_t rc;
rc = omrstr_vprintf(buffer, bufLength, format, args);
if (strlen(expectedResult) != rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrstr_vprintf(\"%s\") with \"%s\" returned %d, expected %d\n", format, expectedResult, rc, strlen(expectedResult));
}
if (strlen(buffer) != rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrstr_vprintf(\"%s\") with \"%s\" returned buffer with length %d, expected %d\n", format, expectedResult, strlen(buffer), rc);
}
if (strcmp(buffer, expectedResult) != 0) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrstr_vprintf(\"%s\") with \"%s\" returned \"%s\" expected \"%s\"\n", format, expectedResult, buffer, expectedResult);
}
memset(buffer, ' ', bufLength);
}
/**
* @internal
* Helper function for string verification.
*
* Given a format string and it's arguments create the requested output message and
* try to store it in a null buffer.
*
* @param[in] portLibrary The port library under test
* @param[in] testName The name of the test requesting this functionality
* @param[in,out] buffer buffer to write to
* @param[in] bufLength Size of buffer
* @param[in] expectedLength Size of buffer required for format string
* @param[in] format The format string to use
* @param[in] args Arguments for format string
*/
static void
validate_omrstr_vprintf_with_NULL(struct OMRPortLibrary *portLibrary, const char *testName, uint32_t bufLength, uint32_t expectedLength, const char *format, va_list args)
{
OMRPORT_ACCESS_FROM_OMRPORT(portLibrary);
uintptr_t rc;
/* Return the length of buffer required for the format string */
rc = omrstr_vprintf(NULL, bufLength, format, args);
if (expectedLength != rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrstr_vprintf(\"%s\") returned %d expected %d\n", format, rc, expectedLength);
}
}
/**
* @internal
* Helper function for string verification.
*
* Given a format string and it's arguments verify the following behaviour
* \args Given a larger buffer formatting is correct
* \args Given a buffer of exact size the formatting is correct
* \args Given a smaller buffer the formatting is truncated
*
* @param[in] portLibrary The port library under test
* @param[in] testName The name of the test requesting this functionality
* @param[in] format The format string
* @param[in] expectedResult The expected result
* @param[in] ... arguments for format
*/
static void
test_omrstr_vprintf(struct OMRPortLibrary *portLibrary, const char *testName, const char *format, const char *expectedResult, ...)
{
char truncatedExpectedResult[512];
char actualResult[512];
va_list args;
OMRPORT_ACCESS_FROM_OMRPORT(portLibrary);
/* Buffer larger than required */
va_start(args, expectedResult);
validate_omrstr_vprintf(OMRPORTLIB, testName, actualResult, sizeof(actualResult), expectedResult, format, args);
/* Exact size buffer (+1 for NUL)*/
validate_omrstr_vprintf(OMRPORTLIB, testName, actualResult, strlen(expectedResult) + 1, expectedResult, format, args);
/* Truncated buffer - use strlen(expectedResult) as length of buffer thus reducing the size by 1*/
if (strlen(expectedResult) > 1) {
strcpy(truncatedExpectedResult, expectedResult);
truncatedExpectedResult[strlen(expectedResult) - 1] = '\0';
validate_omrstr_vprintf(OMRPORTLIB, testName, actualResult, strlen(expectedResult), truncatedExpectedResult, format, args);
/* Truncated buffer - use strlen(expectedResult)-1 as length of buffer thus reducing the size by 2*/
if (strlen(expectedResult) > 2) {
truncatedExpectedResult[strlen(expectedResult) - 2] = '\0';
validate_omrstr_vprintf(OMRPORTLIB, testName, actualResult, strlen(expectedResult) - 1, truncatedExpectedResult, format, args);
}
}
/* Some NULL test, size of buffer does not matter */
/* NULL, 0, expect size of buffer required */
validate_omrstr_vprintf_with_NULL(OMRPORTLIB, testName, 0, (uint32_t)strlen(expectedResult) + 1, format, args);
/* NULL, -1, expect size of buffer required */
validate_omrstr_vprintf_with_NULL(OMRPORTLIB, testName, (uint32_t)-1, (uint32_t)strlen(expectedResult) + 1, format, args);
/* NULL, truncated buffer, use strlen(expectedResult) as length of buffer thus reducing the size by 1 */
validate_omrstr_vprintf_with_NULL(OMRPORTLIB, testName, (uint32_t)strlen(expectedResult), (uint32_t)strlen(expectedResult) + 1, format, args);
va_end(args);
}
/**
* @internal
* Helper function for string verification.
*
* @param[in] portLibrary The port library under test
* @param[in] testName The name of the test requesting this functionality
* @param[in] ... arguments for format string
*
* @note Pretty bogus to pass an argument for a format string you don't know about
*/
static void
test_omrstr_vprintfNulChar(struct OMRPortLibrary *portLibrary, const char *testName, ...)
{
OMRPORT_ACCESS_FROM_OMRPORT(portLibrary);
uintptr_t rc;
char actualResult[1024];
const char *format = "ab%cde";
const char *expectedResult = "ab";
va_list args;
va_start(args, testName);
rc = omrstr_vprintf(actualResult, sizeof(actualResult), format, args);
va_end(args);
if (5 != rc) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrstr_vprintf(\"%s\") returned %d expected 5\n", format, rc);
}
if (strlen(actualResult) != 2) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrstr_vprintf(\"%s\") returned %d expected 2\n", format, rc);
}
if (strcmp(actualResult, expectedResult) != 0) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrstr_vprintf(\"%s\") returned \"%s\" expected \"%s\"\n", format, expectedResult, actualResult);
}
}
/**
* @internal
* Helper function for strftime verification.
*
* @param[in] portLibrary The port library under test
* @param[in] testName The name of the test invoking this function
* @param[out] buf The formatted time string
* @param[in] bufLen The length of buf
* @param[in] format Specifies the format of the time that will be written to buf
* @param[in] timeMillis The time to format in ms
* @param[in] expectedBuf The value that is to be expected following the call to omrstr_ftime_ex
* @param[in] flags argument for omrstr_ftime_ex
*
* @return TEST_PASS if buf and expectedBuf are identical, TEST_FAIL if not
*/
static void
test_omrstr_ftime(struct OMRPortLibrary *portLibrary, const char *testName, char *buf, uintptr_t bufLen,
const char *format, int64_t timeMillis, uint32_t flags, const char *expectedBuf)
{
OMRPORT_ACCESS_FROM_OMRPORT(portLibrary);
omrstr_ftime_ex(buf, bufLen, format, timeMillis, flags);
if (0 != strncmp(buf, expectedBuf, bufLen)) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Expected \"%s\", Got \"%s\"\n", expectedBuf, buf);
}
}
/**
* @internal
* Helper function for subst_tokens verification.
*
* @param[in] portLibrary The port library under test
* @param[in] testName The name of the test invoking this function
* @param[out] buf The formatted time string
* @param[in] bufLen The length of buf
* @param[in] format Specifies the format of string written to buf
* @param[in] tokens The tokens to be used to expand the format string
* @param[in] expectedBuf The expected value following the call to omrstr_subst_tokens
* @param[in] expectedRet The return value expected from the call to omrstr_subst_tokens
*/
static void
test_omrstr_tokens(struct OMRPortLibrary *portLibrary, const char *testName, char *buf, uintptr_t bufLen, const char *format, struct J9StringTokens *tokens, const char *expectedBuf, uintptr_t expectedRet)
{
OMRPORT_ACCESS_FROM_OMRPORT(portLibrary);
uintptr_t rc;
rc = omrstr_subst_tokens(buf, bufLen, format, tokens);
if (rc != expectedRet) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Expected rc = %d, Got rc = %d\n", expectedRet, rc);
}
if (buf && (0 != strncmp(buf, expectedBuf, bufLen))) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Expected \"%s\", Got \"%s\"\n", expectedBuf, buf);
}
}
/**
* Verify port library string operations.
*
* Ensure the port library is properly setup to run string operations.
*/
TEST(PortStrTest, str_test0)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrstr_test0";
reportTestEntry(OMRPORTLIB, testName);
/* Verify that the string function pointers are non NULL */
/* Not tested, implementation dependent. No known functionality.
* Startup is private to the portlibary, it is not re-entrant safe
*/
if (NULL == OMRPORTLIB->str_startup) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->str_startup is NULL\n");
}
/* Not tested, implementation dependent. No known functionality */
if (NULL == OMRPORTLIB->str_shutdown) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->str_shutdown is NULL\n");
}
/* omrstr_test1 */
if (NULL == OMRPORTLIB->str_printf) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->str_printf is NULL\n");
}
/* omrstr_test2 */
if (NULL == OMRPORTLIB->str_vprintf) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "portLibrary->str_vprintf is NULL\n");
}
reportTestExit(OMRPORTLIB, testName);
}
/**
* Verify port library string operations.
*
* @ref omrstr.c::omrstr_printf "omrstr_printf()" is a helper function for
* omrstr.c::omrstr_vprintf "omrstr_vprintf(). It only makes sense to implement
* one in terms of the other. To verify this has indeed been done, replace
* omrstr_printf with a fake one that returns a known value. If that value is
* not returned then fail the test.
*/
TEST(PortStrTest, str_test1)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrstr_test1";
uintptr_t omrstrRC;
J9STR_VPRINTF_FUNC realVprintf;
reportTestEntry(OMRPORTLIB, testName);
/* Save the real function, put in a fake one, call it, restore old one */
realVprintf = OMRPORTLIB->str_vprintf;
OMRPORTLIB->str_vprintf = fake_omrstr_vprintf;
omrstrRC = omrstr_printf(NULL, 0, "Simple test");
OMRPORTLIB->str_vprintf = realVprintf;
if (J9STR_PRIVATE_RETURN_VALUE != omrstrRC) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrstr_printf() does not call omrstr_vprintf()\n");
}
omrstrRC = omrstr_printf(NULL, 0, "Simple test");
if ((strlen("Simple test") + 1) != omrstrRC) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "omrstr_vprintf() not restored\n");
}
reportTestExit(OMRPORTLIB, testName);
}
/**
* Verify port library string operations.
*
* Run various format strings through @ref omrstr.c::omrstr_vprintf "omrstr_vprintf()".
* Tests include basic printing of characters, strings, numbers and Unicode characters.
*/
TEST(PortStrTest, str_test2)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrstr_test2";
U_16 unicodeString[32];
reportTestEntry(OMRPORTLIB, testName);
test_omrstr_vprintf(OMRPORTLIB, testName, "Simple test", "Simple test");
test_omrstr_vprintf(OMRPORTLIB, testName, "%d", "123", 123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%d", "-123", -123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%+d", "-123", -123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%+d", "+123", 123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%+d", "+0", 0);
test_omrstr_vprintf(OMRPORTLIB, testName, "%x", "123", 0x123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5.5d", "00123", 123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5.5d", "-00123", -123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%4.5d", "00123", 123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%4.5d", "-00123", -123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5.4d", " 0123", 123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5.4d", "-0123", -123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5d", " 123", 123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5d", " -123", -123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%-5d", "123 ", 123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%-5d", "-123 ", -123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%-5.4d", "0123 ", 123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%-5.4d", "-0123", -123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%03d", "-03", -3);
/* test some simple floating point cases */
test_omrstr_vprintf(OMRPORTLIB, testName, "%-5g", "1 ", 1.0);
/* validate NUL char */
test_omrstr_vprintfNulChar(OMRPORTLIB, testName, 0);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5c", " x", 'x');
test_omrstr_vprintf(OMRPORTLIB, testName, "%5lc", " x", 'x');
test_omrstr_vprintf(OMRPORTLIB, testName, "%5lc", " \xc2\x80", 0x80);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5lc", " \xc4\xa8", 0x128);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5lc", " \xdf\xbf", 0x7ff);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5lc", " \xe0\xa0\x80", 0x800);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5lc", " \xef\x84\xa3", 0xf123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5lc", " \xef\xbf\xbf", 0xffff);
#if defined(OMR_ENV_DATA64)
test_omrstr_vprintf(OMRPORTLIB, testName, "%p", "BAADF00DDEADBEEF", (void *)0xBAADF00DDEADBEEF);
#else /* defined(OMR_ENV_DATA64) */
test_omrstr_vprintf(OMRPORTLIB, testName, "%p", "DEADBEEF", (void *)0xDEADBEEF);
#endif /* defined(OMR_ENV_DATA64) */
test_omrstr_vprintf(OMRPORTLIB, testName, "%s", "foo", "foo");
test_omrstr_vprintf(OMRPORTLIB, testName, "%.2s", "fo", "foo");
test_omrstr_vprintf(OMRPORTLIB, testName, "%5s", " foo", "foo");
test_omrstr_vprintf(OMRPORTLIB, testName, "%-5s", "foo ", "foo");
unicodeString[0] = 'f';
unicodeString[1] = 'o';
unicodeString[2] = 'o';
unicodeString[3] = '\0';
test_omrstr_vprintf(OMRPORTLIB, testName, "%ls", "foo", unicodeString);
test_omrstr_vprintf(OMRPORTLIB, testName, "%.2ls", "fo", unicodeString);
test_omrstr_vprintf(OMRPORTLIB, testName, "%5ls", " foo", unicodeString);
test_omrstr_vprintf(OMRPORTLIB, testName, "%-5ls", "foo ", unicodeString);
unicodeString[0] = 'f';
unicodeString[1] = 'o';
unicodeString[2] = 'o';
unicodeString[3] = 0x80;
unicodeString[4] = 0x800;
unicodeString[5] = 0xffff;
unicodeString[6] = 0x128;
unicodeString[7] = '\0';
test_omrstr_vprintf(OMRPORTLIB, testName, "%ls", "foo\xc2\x80\xe0\xa0\x80\xef\xbf\xbf\xc4\xa8", unicodeString);
test_omrstr_vprintf(OMRPORTLIB, testName, "%15ls", " foo\xc2\x80\xe0\xa0\x80\xef\xbf\xbf\xc4\xa8", unicodeString);
test_omrstr_vprintf(OMRPORTLIB, testName, "%4.2ls", " fo", unicodeString);
unicodeString[0] = 0x80;
unicodeString[1] = 0x800;
unicodeString[2] = 0xffff;
unicodeString[3] = 0x128;
unicodeString[4] = '\0';
test_omrstr_vprintf(OMRPORTLIB, testName, "%ls", "\xc2\x80\xe0\xa0\x80\xef\xbf\xbf\xc4\xa8", unicodeString);
test_omrstr_vprintf(OMRPORTLIB, testName, "%-12ls", "\xc2\x80\xe0\xa0\x80\xef\xbf\xbf\xc4\xa8 ", unicodeString);
test_omrstr_vprintf(OMRPORTLIB, testName, "%-8.2ls", "\xc2\x80\xe0\xa0\x80 ", unicodeString);
/* test argument re-ordering */
test_omrstr_vprintf(OMRPORTLIB, testName, "%1$d %2$d", "123 456", 123, 456);
test_omrstr_vprintf(OMRPORTLIB, testName, "%2$d %1$d", "456 123", 123, 456);
test_omrstr_vprintf(OMRPORTLIB, testName, "%*.*d", "00123", 4, 5, 123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%1$*2$.*3$d", "00123", 123, 4, 5);
test_omrstr_vprintf(OMRPORTLIB, testName, "%1$*3$.*2$d", "00123", 123, 5, 4);
test_omrstr_vprintf(OMRPORTLIB, testName, "%2$*1$.*3$d", "00123", 4, 123, 5);
test_omrstr_vprintf(OMRPORTLIB, testName, "%2$*3$.*1$d", "00123", 5, 123, 4);
test_omrstr_vprintf(OMRPORTLIB, testName, "%3$*1$.*2$d", "00123", 4, 5, 123);
test_omrstr_vprintf(OMRPORTLIB, testName, "%3$*2$.*1$d", "00123", 5, 4, 123);
reportTestExit(OMRPORTLIB, testName);
}
/**
* Verify port library string operations.
*
* Exercise @ref omrstrftime.c::omrstrftime "omrstrftime()" with various format strings and times.
* Tests include
* 1. too short a dest buffer
* 2. ime 0
* 3. a known time (February 29th 2004 01:23:45)
* 4. Tokens that are not valid for str_ftime but are set by default in omrstr_create_tokens.
* Check that these are not subsituted.
* 5. Tokens that are not valid by default anywhere.
*/
TEST(PortStrTest, str_test3)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrstr_test3";
char buf[J9STR_BUFFER_SIZE];
char expected[J9STR_BUFFER_SIZE];
uint64_t timeMillis = 0;
uintptr_t ret = 0;
reportTestEntry(OMRPORTLIB, testName);
/* First test: The epoch */
portTestEnv->log("\t This test could fail if you abut the international dateline (westside of dateline)\n");
timeMillis = 0;
strncpy(expected, "1970 01 Jan 01 XX:YY:00", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d XX:YY:%S", timeMillis, OMRSTR_FTIME_FLAG_LOCAL, expected);
strncpy(expected, "1970 01 Jan 01 00:00:00", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %H:%M:%S", timeMillis, OMRSTR_FTIME_FLAG_UTC, expected);
/* Second Test: February 29th, 2004, 12:00:00, UTC. */
timeMillis = J9CONST64(1078056000000);
strncpy(expected, "2004 02 Feb 29 00", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %S", timeMillis, OMRSTR_FTIME_FLAG_LOCAL, expected);
strncpy(expected, "2004 02 Feb 29 12:00:00", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %H:%M:%S", timeMillis, OMRSTR_FTIME_FLAG_UTC, expected);
/* Third test: Too short a buffer */
ret = omrstr_ftime(buf, 3, "%Y", timeMillis);
if (ret < 3) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Undersized buffer not detected. Expected %d, got %d\n", 5, ret);
}
/* Fourth Test: Pass in Tokens that are not valid for str_ftime but are valid in the rest of the token API.
* Use the time February 29th, 2004, 12:00:00, UTC. */
timeMillis = J9CONST64(1078056000000);
strncpy(expected, "2004 02 Feb 29 00 %pid %uid %job %home %last %seq", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %S %pid %uid %job %home %last %seq", timeMillis, OMRSTR_FTIME_FLAG_LOCAL, expected);
/* Fifth Test: Pass in Tokens that are not valid by default anywhere.
* Use the time February 29th, 2004, 12:00:00, UTC. */
timeMillis = J9CONST64(1078056000000);
strncpy(expected, "2004 02 Feb 29 00 %zzz = %zzz", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%Y %m %b %d %S %zzz = %%zzz", timeMillis, OMRSTR_FTIME_FLAG_LOCAL, expected);
/* Sixth Test: Pass in all time tokens.
* Use the time February 29th, 2004, 12:00:00, UTC. */
timeMillis = J9CONST64(1078056000000);
strncpy(expected, "04,(2004) 02,(Feb) 29 XX:00:00 %", J9STR_BUFFER_SIZE);
test_omrstr_ftime(OMRPORTLIB, testName, buf, J9STR_BUFFER_SIZE, "%y,(%Y) %m,(%b) %d XX:%M:%S %", timeMillis, OMRSTR_FTIME_FLAG_LOCAL, expected);
reportTestExit(OMRPORTLIB, testName);
}
/**
* Verify port library token operations.
*
* Exercise @ref omrstr.c::omrstr_subst_tokens "omrstr_subst_tokens" with various tokens.
* Tests include simple token tests, too short a dest buffer and token precedence
*/
TEST(PortStrTest, str_test4)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
#define TEST_BUF_LEN 1024
#define TEST_OVERFLOW_LEN 8
const char *testName = "omrstr_test4";
I_64 timeMillis;
struct J9StringTokens *tokens;
char buf[TEST_BUF_LEN];
char bufOverflow[TEST_OVERFLOW_LEN];
reportTestEntry(OMRPORTLIB, testName);
/* February 29th, 2004, 12:00:00, UTC */
timeMillis = J9CONST64(1078056000000);
portTestEnv->log("\t This test will fail if you abut the international dateline\n");
tokens = omrstr_create_tokens(timeMillis);
omrstr_set_token(tokens, "longtkn", "Long Token Value");
omrstr_set_token(tokens, "yyy", "nope nope nope");
omrstr_set_token(tokens, "yyy", "yup yup yup");
omrstr_set_token(tokens, "empty", "");
if (NULL == tokens) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Failed to create tokens\n");
} else {
/* Test 1: No tokens */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"Teststring", tokens,
"Teststring", 10);
/* Test 2: Single token */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"%Y", tokens,
"2004", 4);
/* Test 3: End with a token */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"Teststring %Y", tokens,
"Teststring 2004", 15);
/* Test 4: Start with a token */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"%y Teststring", tokens,
"04 Teststring", 13);
/* Test 5: Many tokens */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"%Y/%m/%d %S seconds %longtkn", tokens,
"2004/02/29 00 seconds Long Token Value", 38);
/* Test 6: Tokens and strings combined */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"Test1 %Y Test2-%m%d%y-%S %longtkn, %longtkn", tokens,
"Test1 2004 Test2-022904-00 Long Token Value, Long Token Value",
61);
/* Test 7: %% and end with % */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"99%% is the same as 99%", tokens,
"99% is the same as 99%", 22);
/* Test 8: Invalid tokens */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"%zzz = %%zzz", tokens,
"%zzz = %zzz", 11);
/* Test 9: Excessive % stuff */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"%Y%%%m%%%d %zzz% %%%%%%", tokens,
"2004%02%29 %zzz% %%%", 20);
/* Test 10: Simple string, buffer too short */
test_omrstr_tokens(OMRPORTLIB, testName, buf, 7, "Teststring", tokens, "Testst", 11);
/* Test 11: Single token, buffer too short */
test_omrstr_tokens(OMRPORTLIB, testName, buf, 3, "%Y", tokens, "20", 5);
/* Test 12: Test for overflow with an actual short buffer (simple string) */
test_omrstr_tokens(OMRPORTLIB, testName, bufOverflow, TEST_OVERFLOW_LEN,
"Teststring", tokens,
"Teststr", 11);
/* Test 13: Test for overflow with an actual short buffer (token) */
test_omrstr_tokens(OMRPORTLIB, testName, bufOverflow, TEST_OVERFLOW_LEN,
"Test %Y", tokens,
"Test 20", 10);
/* Test 14: Test for token precedence based on length */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"%yyy is not %yy is not %y", tokens,
"yup yup yup is not 04y is not 04", 32);
/* Test 15: Test the read only mode (should return the required buf len) */
test_omrstr_tokens(OMRPORTLIB, testName, NULL, 0,
"%yyy is not %yy is not %y", tokens,
NULL, 33); /* 33 because must include \0 */
/* Test 16: Test an empty token */
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"x%emptyx == xx", tokens,
"xx == xx", 8);
/* Test 17: All tokens that we get for free in omrstr_create_tokens (and that the user may be relying on) */
{
char expected[J9STR_BUFFER_SIZE];
const char *default_tokens[] = { " %pid", " %home", " %last", " %seq"
, " %uid"
#if defined(J9ZOS390)
, "%job"
#endif
};
uintptr_t i = 0;
uintptr_t rc;
const char *timePortionOfFormatString = "%y,(%Y) %m,(%b) %d XX:%M:%S %";
char *fullFormatString = NULL;
uintptr_t sizeOfFullFormatString = 0;
strncpy(expected, "04,(2004) 02,(Feb) 29 XX:00:00 %", J9STR_BUFFER_SIZE);
/* build the format string (tack on the default tokens) */
/* how large does it need to be? */
sizeOfFullFormatString = strlen(timePortionOfFormatString);
for (i = 0 ; i < (sizeof(default_tokens) / sizeof(char *)) ; i++) {
sizeOfFullFormatString = sizeOfFullFormatString + strlen(default_tokens[i]);
}
sizeOfFullFormatString = sizeOfFullFormatString + 1 /* null terminator */;
/* ok, now build the format string */
fullFormatString = (char *)omrmem_allocate_memory(sizeOfFullFormatString, OMRMEM_CATEGORY_PORT_LIBRARY);
strncpy(fullFormatString, timePortionOfFormatString, sizeOfFullFormatString);
for (i = 0 ; i < (sizeof(default_tokens) / sizeof(char *)) ; i++) {
strncat(fullFormatString, default_tokens[i], sizeOfFullFormatString - strlen(fullFormatString));
}
rc = omrstr_subst_tokens(buf, TEST_BUF_LEN, fullFormatString, tokens);
/* We don't know how long the returned string will be,
* but make sure that something has been substituted for each token and that the time is substituted properly .
* Do this by making sure that %<token> is not present in the output buffer*/
if (rc > TEST_BUF_LEN) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Test buffer (%i) is too small. Required size: %i\n", TEST_BUF_LEN, rc);
}
if (0 == strstr(buf, expected)) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "[%s] was not found in [%s]\n", expected, buf);
}
for (i = 0 ; i < (sizeof(default_tokens) / sizeof(char *)) ; i++) {
if (NULL != strstr(buf, default_tokens[i])) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "%s was not substituted\n", default_tokens[i]);
}
}
omrmem_free_memory(fullFormatString);
fullFormatString = NULL;
}
/* We're done, let's cleanup */
omrstr_free_tokens(tokens);
}
reportTestExit(OMRPORTLIB, testName);
#undef TEST_BUF_LEN
#undef TEST_OVERFLOW_LEN
}
/**
* Verify port library token operations.
*
* omrstr_test5 isn't really a test. Just want to do the normal use case of omrtime_current_time_millis() followed
* by omrstr_create_tokens().
*/
TEST(PortStrTest, str_test5)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
#define TEST_BUF_LEN 1024
#define TEST_OVERFLOW_LEN 8
const char *testName = "omrstr_test5";
I_64 timeMillis;
struct J9StringTokens *tokens;
char buf[TEST_BUF_LEN];
reportTestEntry(OMRPORTLIB, testName);
portTestEnv->log("\n\tThe results of omrstr_test5 are not evaluated as time conversions are specific to a time zone.\n");
portTestEnv->log("\tTherefore, this is for your viewing pleasure only.\n");
timeMillis = omrtime_current_time_millis();
portTestEnv->log("\n\tomrtime_current_time_millis returned: %lli\n", timeMillis);
portTestEnv->log("\t...creating and substituting tokens...\n");
tokens = omrstr_create_tokens(timeMillis);
(void)omrstr_subst_tokens(buf, TEST_BUF_LEN, "%Y/%m/%d %H:%M:%S", tokens);
portTestEnv->log("\tThe current time was converted to: %s\n", buf);
timeMillis = J9CONST64(1139952606740);
portTestEnv->log("\n\t using UTC timeMillis = %lli. \n\t", timeMillis);
portTestEnv->log("\t...creating and substituting tokens...\n");
tokens = omrstr_create_tokens(timeMillis);
(void)omrstr_subst_tokens(buf, TEST_BUF_LEN, "%Y/%m/%d %H:%M:%S", tokens);
portTestEnv->log("\tExpecting local time (in Ottawa, Eastern Standard Time): 2006/02/14 16:30:06 \n", timeMillis);
portTestEnv->log("\t ... converted to: %s\n", buf);
timeMillis = J9CONST64(1150320606740);
portTestEnv->log("\n\t using UTC timeMillis = %lli. \n\t ", timeMillis);
portTestEnv->log("\t...creating and substituting tokens...\n");
tokens = omrstr_create_tokens(timeMillis);
(void)omrstr_subst_tokens(buf, TEST_BUF_LEN, "%Y/%m/%d %H:%M:%S", tokens);
portTestEnv->log("\tExpecting local time (in Ottawa, Eastern Daylight Time): 2006/06/14 17:30:06 \n", timeMillis);
portTestEnv->log("\t ... converted to: %s\n", buf);
timeMillis = J9CONST64(1160688606740);
portTestEnv->log("\n\t using UTC timeMillis = %lli. \n\t ", timeMillis);
portTestEnv->log("\t...creating and substituting tokens...\n");
tokens = omrstr_create_tokens(timeMillis);
(void)omrstr_subst_tokens(buf, TEST_BUF_LEN, "%Y/%m/%d %H:%M:%S", tokens);
portTestEnv->log("\tExpecting local time (in Ottawa, Eastern Daylight Time): 2006/10/12 17:30:06 \n", timeMillis);
portTestEnv->log("\t ... converted to: %s\n", buf);
timeMillis = J9CONST64(1165872606740);
portTestEnv->log("\n\t using UTC timeMillis = %lli. \n\t", timeMillis);
portTestEnv->log("\t...creating and substituting tokens...\n");
tokens = omrstr_create_tokens(timeMillis);
(void)omrstr_subst_tokens(buf, TEST_BUF_LEN, "%Y/%m/%d %H:%M:%S", tokens);
portTestEnv->log("\tExpecting local time (in Ottawa, Eastern Standard Time): 2006/12/11 16:30:06 \n", timeMillis);
portTestEnv->log("\t ... converted to: %s\n", buf);
portTestEnv->log("\n");
reportTestExit(OMRPORTLIB, testName);
}
/**
* Verify port library token operations.
*
* Test (-)ive value of 0-12 hours and make sure we get UTC time in millis a
*/
TEST(PortStrTest, str_test6)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
#define TEST_BUF_LEN 1024
#define TEST_OVERFLOW_LEN 8
const char *testName = "omrstr_test6";
I_64 timeMillis;
struct J9StringTokens *tokens;
char buf[TEST_BUF_LEN];
reportTestEntry(OMRPORTLIB, testName);
/* anything less than 12 hours before Epoch should come back as Epoch */
timeMillis = 0 - 12 * 60 * 60 * 1000;
tokens = omrstr_create_tokens(timeMillis);
test_omrstr_tokens(OMRPORTLIB, testName, buf, TEST_BUF_LEN,
"%Y/%m/%d %H:%M:%S", tokens,
"1970/01/01 00:00:00", 19);
reportTestExit(OMRPORTLIB, testName);
}
#define TEST_BUF_LEN 1024
static const char utf8String[] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z'
};
static const uint32_t utf8StringLength = sizeof(utf8String);
static const U_16 utf16Data[] = {
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a
};
static const char *utf16String = (const char *)utf16Data;
static const uint32_t utf16StringLength = sizeof(utf16Data);
#if defined(J9ZOS390)
#pragma convlit(suspend)
#endif
static const char platformString[] = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '\0'
};
static const uint32_t platformStringLength = sizeof(platformString) - 1;
#if defined(J9ZOS390)
#pragma convlit(resume)
#endif
static void
dumpStringBytes(const char *message, const char *buffer, uint32_t length)
{
uint32_t index = 0;
fprintf(stderr, "%s", message);
for (index = 0; index < length; ++index) {
if ((index % 16) == 0) {
fprintf(stderr, "\n");
}
fprintf(stderr, "0x%x ", (unsigned char) buffer[index]);
}
fprintf(stderr, "\n");
}
static BOOLEAN
compareBytes(const char *expected, const char *actual, uint32_t length)
{
uint32_t index = 0;
for (index = 0; index < length; ++index) {
if (actual[index] != expected[index]) {
fprintf(stderr, "Error at position %d \nexpected %0x actual %0x\n", index,
(unsigned char)expected[index], (unsigned char)actual[index]);
dumpStringBytes("Expected", expected, length);
dumpStringBytes("Actual", actual, length);
return FALSE;
}
}
return TRUE;
}
/**
* Verify string platform->MUTF8 conversion, basic sanity
*/
TEST(PortStrTest, str_convPlatTo8)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrstr_convPlatToU8";
char outBuff[TEST_BUF_LEN];
int32_t originalStringLength = platformStringLength;
int32_t expectedStringLength = utf8StringLength;
int32_t convertedStringLength = 0;
reportTestEntry(OMRPORTLIB, testName);
memset(outBuff, 0, sizeof(outBuff));
convertedStringLength = omrstr_convert(J9STR_CODE_PLATFORM_RAW, J9STR_CODE_MUTF8,
platformString, originalStringLength, outBuff, sizeof(outBuff));
if (convertedStringLength != expectedStringLength) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "buffer length wrong. Expected %d actual %d\n", expectedStringLength, convertedStringLength);
}
if (!compareBytes(utf8String, outBuff, expectedStringLength)) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Converted string wrong.");
}
/* test string length */
convertedStringLength = omrstr_convert(J9STR_CODE_PLATFORM_RAW, J9STR_CODE_MUTF8,
platformString, originalStringLength, NULL, 0);
if (convertedStringLength != expectedStringLength) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "buffer length wrong. Expected %d actual %d\n", expectedStringLength, convertedStringLength);
}
reportTestExit(OMRPORTLIB, testName);
}
TEST(PortStrTest, str_convLongString)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrstr_convLongString";
char mutf8Buff[TEST_BUF_LEN];
char returnBuff[TEST_BUF_LEN];
char longPlatformString[300];
int32_t originalStringLength = sizeof(longPlatformString);
int32_t mutf8StringLength = 0;
int32_t returnStringLength = 0;
int32_t i = 0;
reportTestEntry(OMRPORTLIB, testName);
memset(mutf8Buff, 0, sizeof(mutf8Buff));
memset(returnBuff, 0, sizeof(returnBuff));
for (i = 0; i < originalStringLength; ++i) {
longPlatformString[i] = (i % 127) + 1; /* stick to ASCII */
}
/* test string length */
mutf8StringLength = omrstr_convert(J9STR_CODE_PLATFORM_RAW, J9STR_CODE_MUTF8,
longPlatformString, originalStringLength, NULL, 0);
if (mutf8StringLength < 0) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Length calculation for modified UTF8 failed, error code %d\n", mutf8StringLength);
}
/* do the actual conversion */
mutf8StringLength = omrstr_convert(J9STR_CODE_PLATFORM_RAW, J9STR_CODE_MUTF8,
longPlatformString, originalStringLength, mutf8Buff, sizeof(mutf8Buff));
if (mutf8StringLength < 0) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Conversion to modified UTF8 failed, error code %d\n", mutf8StringLength);
}
/* test string length in the other direction */
returnStringLength = omrstr_convert(J9STR_CODE_MUTF8, J9STR_CODE_PLATFORM_RAW,
mutf8Buff, mutf8StringLength, NULL, 0);
if (returnStringLength < 0) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Length calculation for platform failed, error code %d\n", mutf8StringLength);
}
/* convert back and verify that it matches the original */
returnStringLength = omrstr_convert(J9STR_CODE_MUTF8, J9STR_CODE_PLATFORM_RAW,
mutf8Buff, mutf8StringLength, returnBuff, sizeof(returnBuff));
if (returnStringLength < 0) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Conversion to platform failed, error code %d\n", mutf8StringLength);
}
if (!compareBytes(longPlatformString, returnBuff, originalStringLength)) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Converted string wrong.");
}
if (returnStringLength != originalStringLength) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "buffer length wrong. Expected %d actual %d\n", originalStringLength, returnStringLength);
}
reportTestExit(OMRPORTLIB, testName);
}
/**
* Verify string MUTF8->platform, basic sanity
*/
TEST(PortStrTest, str_convU8ToPlat)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrstr_convU8ToPlat";
char outBuff[TEST_BUF_LEN];
int32_t originalStringLength = utf8StringLength;
int32_t expectedStringLength = platformStringLength;
int32_t convertedStringLength = 0;
reportTestEntry(OMRPORTLIB, testName);
memset(outBuff, 0, sizeof(outBuff));
convertedStringLength = omrstr_convert(J9STR_CODE_MUTF8, J9STR_CODE_PLATFORM_RAW,
utf8String, originalStringLength, outBuff, sizeof(outBuff));
if (convertedStringLength != expectedStringLength) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "buffer length wrong. Expected %d actual %d\n", originalStringLength, convertedStringLength);
}
if (!compareBytes(platformString, outBuff, expectedStringLength)) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Converted string wrong.");
}
/* test string length */
convertedStringLength = omrstr_convert(J9STR_CODE_MUTF8, J9STR_CODE_PLATFORM_RAW,
utf8String, originalStringLength, NULL, 0);
if (convertedStringLength != expectedStringLength) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "buffer length wrong. Expected %d actual %d\n", originalStringLength, convertedStringLength);
}
reportTestExit(OMRPORTLIB, testName);
}
/**
* Verify string MUTF8->wide, basic sanity
*/
TEST(PortStrTest, str_convU8ToWide)
{
OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary());
const char *testName = "omrstr_convU8ToWide";
char outBuff[TEST_BUF_LEN];
int32_t originalStringLength = utf8StringLength;
int32_t expectedStringLength = utf16StringLength;
int32_t convertedStringLength = 0;
reportTestEntry(OMRPORTLIB, testName);
memset(outBuff, 0, sizeof(outBuff));
convertedStringLength = omrstr_convert(J9STR_CODE_MUTF8, J9STR_CODE_WIDE,
utf8String, originalStringLength, outBuff, sizeof(outBuff));
if (convertedStringLength != expectedStringLength) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "buffer length wrong. Expected %d actual %d\n", expectedStringLength, convertedStringLength);
}
if (!compareBytes(utf16String, outBuff, expectedStringLength)) {
outputErrorMessage(PORTTEST_ERROR_ARGS, "Converted string wrong.");
}
/* test string length */