| 1 |
/*------------------------------------------------------------------ |
| 2 |
* test_strcmp_s |
| 3 |
* |
| 4 |
* |
| 5 |
*------------------------------------------------------------------ |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "test_private.h" |
| 9 |
#include "safe_str_lib.h" |
| 10 |
|
| 11 |
#define LEN ( 128 ) |
| 12 |
#define SHORT_LEN ( 5 ) |
| 13 |
|
| 14 |
static char str1[LEN]; |
| 15 |
static char str2[LEN]; |
| 16 |
|
| 17 |
int test_strcmp_s (void) |
| 18 |
{ |
| 19 |
errno_t rc; |
| 20 |
int ind; |
| 21 |
int std_ind; |
| 22 |
|
| 23 |
/*--------------------------------------------------*/ |
| 24 |
|
| 25 |
rc = strcmp_s(NULL, LEN, str2, &ind); |
| 26 |
if (rc != ESNULLP) { |
| 27 |
printf("%s %u Error rc=%d \n", |
| 28 |
__FUNCTION__, __LINE__, rc); |
| 29 |
} |
| 30 |
|
| 31 |
if (ind != 0) { |
| 32 |
printf("%s %u Error ind=%d rc=%d \n", |
| 33 |
__FUNCTION__, __LINE__, ind, rc); |
| 34 |
} |
| 35 |
|
| 36 |
/*--------------------------------------------------*/ |
| 37 |
|
| 38 |
rc = strcmp_s(str1, LEN, NULL, &ind); |
| 39 |
if (rc != ESNULLP) { |
| 40 |
printf("%s %u Error rc=%d \n", |
| 41 |
__FUNCTION__, __LINE__, rc); |
| 42 |
} |
| 43 |
|
| 44 |
if (ind != 0) { |
| 45 |
printf("%s %u Error ind=%d rc=%d \n", |
| 46 |
__FUNCTION__, __LINE__, ind, rc); |
| 47 |
} |
| 48 |
|
| 49 |
/*--------------------------------------------------*/ |
| 50 |
|
| 51 |
rc = strcmp_s(str1, LEN, str2, NULL); |
| 52 |
if (rc != ESNULLP) { |
| 53 |
printf("%s %u Error rc=%d \n", |
| 54 |
__FUNCTION__, __LINE__, rc); |
| 55 |
} |
| 56 |
|
| 57 |
/*--------------------------------------------------*/ |
| 58 |
|
| 59 |
rc = strcmp_s(str1, 0, str2, &ind); |
| 60 |
if (rc != ESZEROL) { |
| 61 |
printf("%s %u Error rc=%d \n", |
| 62 |
__FUNCTION__, __LINE__, rc); |
| 63 |
} |
| 64 |
|
| 65 |
if (ind != 0) { |
| 66 |
printf("%s %u Error ind=%d rc=%d \n", |
| 67 |
__FUNCTION__, __LINE__, ind, rc); |
| 68 |
} |
| 69 |
|
| 70 |
/*--------------------------------------------------*/ |
| 71 |
|
| 72 |
rc = strcmp_s(str1, RSIZE_MAX_STR+1, str2, &ind); |
| 73 |
if (rc != ESLEMAX) { |
| 74 |
printf("%s %u Error rc=%d \n", |
| 75 |
__FUNCTION__, __LINE__, rc); |
| 76 |
} |
| 77 |
|
| 78 |
if (ind != 0) { |
| 79 |
printf("%s %u Error ind=%d rc=%d \n", |
| 80 |
__FUNCTION__, __LINE__, ind, rc); |
| 81 |
} |
| 82 |
|
| 83 |
/*--------------------------------------------------*/ |
| 84 |
|
| 85 |
str1[0] = '\0'; |
| 86 |
str2[0] = '\0'; |
| 87 |
|
| 88 |
rc = strcmp_s(str1, LEN, str2, &ind); |
| 89 |
if (rc != EOK) { |
| 90 |
printf("%s %u Error rc=%d \n", |
| 91 |
__FUNCTION__, __LINE__, rc); |
| 92 |
} |
| 93 |
|
| 94 |
if (ind != 0) { |
| 95 |
printf("%s %u Error ind=%d rc=%d \n", |
| 96 |
__FUNCTION__, __LINE__, ind, rc); |
| 97 |
} |
| 98 |
|
| 99 |
std_ind = strcmp(str1, str2); |
| 100 |
if (ind != std_ind) { |
| 101 |
printf("%s %u ind=%d std_ind=%d rc=%d \n", |
| 102 |
__FUNCTION__, __LINE__, ind, std_ind, rc); |
| 103 |
} |
| 104 |
|
| 105 |
/*--------------------------------------------------*/ |
| 106 |
|
| 107 |
strcpy (str1, "keep it simple"); |
| 108 |
strcpy (str2, "keep it simple"); |
| 109 |
|
| 110 |
rc = strcmp_s(str1, 5, str2, &ind); |
| 111 |
if (rc != EOK) { |
| 112 |
printf("%s %u Error rc=%d \n", |
| 113 |
__FUNCTION__, __LINE__, rc); |
| 114 |
} |
| 115 |
|
| 116 |
if (ind != 0) { |
| 117 |
printf("%s %u ind=%d rc=%d \n", |
| 118 |
__FUNCTION__, __LINE__, ind, rc); |
| 119 |
} |
| 120 |
|
| 121 |
/*--------------------------------------------------*/ |
| 122 |
|
| 123 |
/* K - k == -32 */ |
| 124 |
strcpy (str1, "Keep it simple"); |
| 125 |
strcpy (str2, "keep it simple"); |
| 126 |
|
| 127 |
rc = strcmp_s(str1, LEN, str2, &ind); |
| 128 |
if (rc != EOK) { |
| 129 |
printf("%s %u Error rc=%d \n", |
| 130 |
__FUNCTION__, __LINE__, rc); |
| 131 |
} |
| 132 |
|
| 133 |
if (ind != (-32)) { |
| 134 |
printf("%s %u Error ind=%d rc=%d \n", |
| 135 |
__FUNCTION__, __LINE__, ind, rc); |
| 136 |
} |
| 137 |
|
| 138 |
std_ind = strcmp(str1, str2); |
| 139 |
if (ind != std_ind) { |
| 140 |
printf("%s %u ind=%d std_ind=%d rc=%d \n", |
| 141 |
__FUNCTION__, __LINE__, ind, std_ind, rc); |
| 142 |
} |
| 143 |
|
| 144 |
/*--------------------------------------------------*/ |
| 145 |
|
| 146 |
/* p - P == 32 */ |
| 147 |
strcpy (str1, "keep it simple"); |
| 148 |
strcpy (str2, "keeP it simple"); |
| 149 |
|
| 150 |
rc = strcmp_s(str1, LEN, str2, &ind); |
| 151 |
if (rc != EOK) { |
| 152 |
printf("%s %u Error rc=%d \n", |
| 153 |
__FUNCTION__, __LINE__, rc); |
| 154 |
} |
| 155 |
|
| 156 |
if (ind != 32) { |
| 157 |
printf("%s %u Error ind=%d rc=%d \n", |
| 158 |
__FUNCTION__, __LINE__, ind, rc); |
| 159 |
} |
| 160 |
|
| 161 |
std_ind = strcmp(str1, str2); |
| 162 |
if (ind != std_ind) { |
| 163 |
printf("%s %u ind=%d std_ind=%d rc=%d \n", |
| 164 |
__FUNCTION__, __LINE__, ind, std_ind, rc); |
| 165 |
} |
| 166 |
|
| 167 |
/*--------------------------------------------------*/ |
| 168 |
|
| 169 |
strcpy (str1, "keep it simple"); |
| 170 |
|
| 171 |
rc = strcmp_s(str1, LEN, str1, &ind); |
| 172 |
if (rc != EOK) { |
| 173 |
printf("%s %u Error rc=%d \n", |
| 174 |
__FUNCTION__, __LINE__, rc); |
| 175 |
} |
| 176 |
|
| 177 |
if (ind != 0) { |
| 178 |
printf("%s %u Error ind=%d rc=%d \n", |
| 179 |
__FUNCTION__, __LINE__, ind, rc); |
| 180 |
} |
| 181 |
|
| 182 |
/* be sure the results are the same as strcmp */ |
| 183 |
std_ind = strcmp(str1, str1); |
| 184 |
if (ind != std_ind) { |
| 185 |
printf("%s %u ind=%d std_ind=%d rc=%d \n", |
| 186 |
__FUNCTION__, __LINE__, ind, std_ind, rc); |
| 187 |
} |
| 188 |
|
| 189 |
/*--------------------------------------------------*/ |
| 190 |
|
| 191 |
strcpy (str1, "keep it simplified"); |
| 192 |
strcpy (str2, "keep it simple"); |
| 193 |
|
| 194 |
rc = strcmp_s(str1, LEN, str2, &ind); |
| 195 |
if (rc != EOK) { |
| 196 |
printf("%s %u Error rc=%d \n", |
| 197 |
__FUNCTION__, __LINE__, rc); |
| 198 |
} |
| 199 |
|
| 200 |
if (ind <= 0) { |
| 201 |
printf("%s %u Error ind=%d rc=%d \n", |
| 202 |
__FUNCTION__, __LINE__, ind, rc); |
| 203 |
} |
| 204 |
|
| 205 |
/* be sure the results are the same as strcmp */ |
| 206 |
std_ind = strcmp(str1, str2); |
| 207 |
if (ind != std_ind) { |
| 208 |
printf("%s %u ind=%d std_ind=%d rc=%d \n", |
| 209 |
__FUNCTION__, __LINE__, ind, std_ind, rc); |
| 210 |
} |
| 211 |
|
| 212 |
/*--------------------------------------------------*/ |
| 213 |
|
| 214 |
strcpy (str1, "keep it simple"); |
| 215 |
strcpy (str2, "keep it simplified"); |
| 216 |
|
| 217 |
rc = strcmp_s(str1, LEN, str2, &ind); |
| 218 |
if (rc != EOK) { |
| 219 |
printf("%s %u Error rc=%d \n", |
| 220 |
__FUNCTION__, __LINE__, rc); |
| 221 |
} |
| 222 |
|
| 223 |
if (ind >= 0) { |
| 224 |
printf("%s %u Error ind=%d rc=%d \n", |
| 225 |
__FUNCTION__, __LINE__, ind, rc); |
| 226 |
} |
| 227 |
|
| 228 |
/* be sure the results are the same as strcmp */ |
| 229 |
std_ind = strcmp(str1, str2); |
| 230 |
if (ind != std_ind) { |
| 231 |
printf("%s %u ind=%d std_ind=%d rc=%d \n", |
| 232 |
__FUNCTION__, __LINE__, ind, std_ind, rc); |
| 233 |
} |
| 234 |
|
| 235 |
/*--------------------------------------------------*/ |
| 236 |
|
| 237 |
return (0); |
| 238 |
} |
| 239 |
|
| 240 |
|