| 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: Tests the PAL implementation of the memchr function. |
| 10 | ** Create a string buffer, and check for a number of |
| 11 | ** characters in it. Test to ensure it returns NULL if |
| 12 | ** it can't find the character, and that the size argument |
| 13 | ** works properly. |
| 14 | ** |
| 15 | ** |
| 16 | **===================================================================*/ |
| 17 | |
| 18 | #include <palsuite.h> |
| 19 | |
| 20 | struct testCase |
| 21 | { |
| 22 | char *result; |
| 23 | char string[50]; |
| 24 | int character; |
| 25 | int length; |
| 26 | }; |
| 27 | |
| 28 | |
| 29 | int __cdecl main(int argc, char *argv[]) |
| 30 | { |
| 31 | int i = 0; |
| 32 | char *result = NULL; |
| 33 | |
| 34 | /* |
| 35 | * this structure includes several strings to be tested with |
| 36 | * memchr function and the expected results |
| 37 | */ |
| 38 | |
| 39 | struct testCase testCases[]= |
| 40 | { |
| 41 | {"st" ,"corn cup cat cream coast" ,'s',23}, |
| 42 | /* single instance of char */ |
| 43 | {"st" ,"corn cup cat cream coast" ,'s',24}, |
| 44 | /* single inst, inst< exact length */ |
| 45 | {"q" ,"corn cup cat cream coastq" ,'q',25}, |
| 46 | /* single inst at end, inst=exact length */ |
| 47 | {"q" ,"corn cup cat cream coastq" ,'q',26}, |
| 48 | /* single inst at end, inst<length, |
| 49 | length>len(string) */ |
| 50 | {"st" ,"corn cup cat cream coast" ,115,24}, |
| 51 | /* single int inst, inst<exact length */ |
| 52 | {"corn cup cat cream coast" ,"corn cup cat cream coast" ,'c',24}, |
| 53 | /* multi-inst, inst=1, exact length */ |
| 54 | {"corn cup cat cream coast" ,"corn cup cat cream coast" ,'c',1}, |
| 55 | /* multi-inst, inst = length, length=1 */ |
| 56 | {"is is a test" ,"This is a test" ,105,14}, |
| 57 | /* single int inst, exact length */ |
| 58 | {"is is a test" ,"This is a test" ,'i',14}, |
| 59 | /* double inst, exact length */ |
| 60 | {"a test" ,"This is a test" ,'a',9}, |
| 61 | /* single instance instance = length */ |
| 62 | {NULL,"This is a test" ,'b',14}, |
| 63 | /* no instance exact length */ |
| 64 | {NULL,"This is a test" ,'a',8}, |
| 65 | /* single instance - < length */ |
| 66 | {NULL,"This is a test" ,121,14}, |
| 67 | /* single instance - exact length */ |
| 68 | {" is a test of the function" ,"This is a test of the function" , |
| 69 | ' ',17} /* single inst<length, len(string)>length */ |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | /* Initialize the PAL */ |
| 74 | if ( 0 != PAL_Initialize(argc, argv)) |
| 75 | { |
| 76 | return FAIL; |
| 77 | } |
| 78 | |
| 79 | /* Loop through the testcases in the structure */ |
| 80 | for (i=0; i< sizeof(testCases)/sizeof(struct testCase); i++) |
| 81 | { |
| 82 | /* Need to type cast function in order to compare the result */ |
| 83 | result = (char *)memchr(testCases[i].string, |
| 84 | testCases[i].character,testCases[i].length); |
| 85 | |
| 86 | if (result==NULL) |
| 87 | { |
| 88 | if (testCases[i].result != NULL) |
| 89 | { |
| 90 | Fail("ERROR: Expected memcmp to return \"%s\" instead of" |
| 91 | " NULL\n" , testCases[i].result); |
| 92 | } |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | if (strcmp(result,testCases[i].result)!=0 ) |
| 97 | |
| 98 | { |
| 99 | Fail("ERROR: Expected memcmp to return \"%s\" instead of" |
| 100 | " \"%s\"\n" , testCases[i].result, result); |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | PAL_Terminate(); |
| 107 | |
| 108 | return PASS; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |