| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*============================================================================ |
| 6 | ** |
| 7 | ** Source: test1.c |
| 8 | ** |
| 9 | ** Purpose: |
| 10 | ** Search for a number of tokens within strings. Check that the return values |
| 11 | ** are what is expected, and also that the strings match up with our expected |
| 12 | ** results. |
| 13 | ** |
| 14 | ** |
| 15 | **==========================================================================*/ |
| 16 | |
| 17 | #include <palsuite.h> |
| 18 | |
| 19 | int __cdecl main(int argc, char *argv[]) |
| 20 | { |
| 21 | /* foo bar baz */ |
| 22 | WCHAR str[] = {'f','o','o',' ','b','a','r',' ','b','a','z','\0'}; |
| 23 | |
| 24 | /* foo \0ar baz */ |
| 25 | WCHAR result1[] = {'f','o','o',' ','\0','a','r',' ','b','a','z','\0'}; |
| 26 | |
| 27 | /* foo \0a\0 baz */ |
| 28 | WCHAR result2[] = {'f','o','o',' ','\0','a','\0',' ','b','a','z','\0'}; |
| 29 | |
| 30 | WCHAR* tempString; |
| 31 | int len = 0; |
| 32 | WCHAR *ptr; |
| 33 | |
| 34 | if (PAL_Initialize(argc, argv)) |
| 35 | { |
| 36 | return FAIL; |
| 37 | } |
| 38 | |
| 39 | len = (wcslen(str)*sizeof(WCHAR)) + 2; |
| 40 | |
| 41 | /* Tokenize 'str'. It will hit the 'b' delimiter first. Check to see |
| 42 | that the ptr is pointing to the start of the string and do a compare |
| 43 | to ensure the tokenized string is what we expected. |
| 44 | */ |
| 45 | |
| 46 | tempString = convert("bz" ); |
| 47 | ptr = wcstok(str, tempString); |
| 48 | free(tempString); |
| 49 | |
| 50 | if (ptr != str) |
| 51 | { |
| 52 | Fail("ERROR: Expected wcstok() to return %p, got %p!\n" , str, ptr); |
| 53 | } |
| 54 | |
| 55 | if (memcmp(str, result1, len) != 0) |
| 56 | { |
| 57 | Fail("ERROR: wcstok altered the string in an unexpected fashion." ); |
| 58 | } |
| 59 | |
| 60 | /* If NULL is passed as the first parameter, wcstok will continue |
| 61 | tokenizing the same string. Test that this works properly. |
| 62 | */ |
| 63 | tempString = convert("r " ); |
| 64 | ptr = wcstok(NULL, tempString); |
| 65 | free(tempString); |
| 66 | |
| 67 | if (ptr != str + 5) |
| 68 | { |
| 69 | Fail("ERROR: Expected wcstok() to return %p, got %p!\n" , str+5, ptr); |
| 70 | } |
| 71 | |
| 72 | if (memcmp(str, result2, len) != 0) |
| 73 | { |
| 74 | Fail("ERROR: wcstok altered the string in an unexpected fashion." ); |
| 75 | } |
| 76 | |
| 77 | /* Continue onward, and search for 'X' now, which won't be found. The |
| 78 | pointer should point just after the last NULL in the string. And |
| 79 | the string itself shouldn't have changed. |
| 80 | */ |
| 81 | tempString = convert("X" ); |
| 82 | ptr = wcstok(NULL, tempString); |
| 83 | free(tempString); |
| 84 | |
| 85 | if (ptr != str + 7) |
| 86 | { |
| 87 | Fail("ERROR: Expected wcstok() to return %p, got %p!\n" , str + 7, ptr); |
| 88 | } |
| 89 | |
| 90 | if (memcmp(str, result2, len) != 0) |
| 91 | { |
| 92 | Fail("ERROR: wcstok altered the string in an unexpeced fashion.\n" ); |
| 93 | } |
| 94 | |
| 95 | /* Call wcstok again. Now the ptr should point to the end of the |
| 96 | string at NULL. And the string itself shouldn't have changed. |
| 97 | */ |
| 98 | tempString = convert("X" ); |
| 99 | ptr = wcstok(NULL, tempString); |
| 100 | free(tempString); |
| 101 | |
| 102 | if (ptr != NULL) |
| 103 | { |
| 104 | Fail("ERROR: Expected wcstok() to return %p, got %p!\n" , NULL, ptr); |
| 105 | } |
| 106 | |
| 107 | if (memcmp(str, result2, len) != 0) |
| 108 | { |
| 109 | Fail("ERROR: wcstok altered the string in an unexpeced fashion.\n" ); |
| 110 | } |
| 111 | |
| 112 | PAL_Terminate(); |
| 113 | return PASS; |
| 114 | } |
| 115 | |