| 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: swscanf.h |
| 8 | ** |
| 9 | ** Purpose: Contains common testing functions for swscanf.h |
| 10 | ** |
| 11 | ** |
| 12 | **==========================================================================*/ |
| 13 | |
| 14 | #ifndef __SWSCANF_H__ |
| 15 | #define __SWSCANF_H__ |
| 16 | |
| 17 | void DoVoidTest(WCHAR *inputstr, const WCHAR *formatstr) |
| 18 | { |
| 19 | char buf[256] = { 0 }; |
| 20 | int i; |
| 21 | int ret; |
| 22 | |
| 23 | ret = swscanf(inputstr, formatstr, buf); |
| 24 | if (ret != 0) |
| 25 | { |
| 26 | Fail("ERROR: Expected sscanf to return 0, got %d.\n" |
| 27 | "Using \"%s\" in \"%s\".\n" , ret, convertC(inputstr), |
| 28 | convertC(formatstr)); |
| 29 | } |
| 30 | |
| 31 | for (i=0; i<256; i++) |
| 32 | { |
| 33 | if (buf[i] != 0) |
| 34 | { |
| 35 | Fail("ERROR: Parameter unexpectedly modified scanning \"%s\" " |
| 36 | "using \"%s\".\n" , convertC(inputstr), |
| 37 | convertC(formatstr)); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | |
| 43 | void DoStrTest(WCHAR *inputstr, const WCHAR *formatstr, const char *checkstr) |
| 44 | { |
| 45 | char buf[256] = { 0 }; |
| 46 | int ret; |
| 47 | |
| 48 | ret = swscanf(inputstr, formatstr, buf); |
| 49 | if (ret != 1) |
| 50 | { |
| 51 | Fail("ERROR: Expected swscanf to return 1, got %d.\n" |
| 52 | "Using \"%s\" in \"%s\".\n" , ret, convertC(inputstr), |
| 53 | convertC(formatstr)); |
| 54 | } |
| 55 | |
| 56 | if (memcmp(checkstr, buf, strlen(checkstr) + 1) != 0) |
| 57 | { |
| 58 | Fail("ERROR: scanned string incorrectly from \"%s\" using \"%s\".\n" |
| 59 | "Expected \"%s\", got \"%s\".\n" , convertC(inputstr), |
| 60 | convertC(formatstr), checkstr, |
| 61 | buf); |
| 62 | } |
| 63 | |
| 64 | } |
| 65 | |
| 66 | void DoWStrTest(WCHAR *inputstr, const WCHAR *formatstr, const WCHAR *checkstr) |
| 67 | { |
| 68 | WCHAR buf[256] = { 0 }; |
| 69 | int ret; |
| 70 | |
| 71 | ret = swscanf(inputstr, formatstr, buf); |
| 72 | if (ret != 1) |
| 73 | { |
| 74 | Fail("ERROR: Expected swscanf to return 1, got %d.\n" |
| 75 | "Using \"%s\" in \"%s\".\n" , ret, convertC(inputstr), |
| 76 | convertC(formatstr)); |
| 77 | } |
| 78 | |
| 79 | if (memcmp(checkstr, buf, wcslen(checkstr)*2 + 2) != 0) |
| 80 | { |
| 81 | Fail("ERROR: scanned wide string incorrectly from \"%s\" using \"%s\".\n" |
| 82 | "Expected \"%s\", got \"%s\".\n" , convertC(inputstr), |
| 83 | convertC(formatstr), convertC(checkstr), |
| 84 | convertC(buf)); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | void DoNumTest(WCHAR *inputstr, const WCHAR *formatstr, int checknum) |
| 90 | { |
| 91 | int num = 0; |
| 92 | int ret; |
| 93 | |
| 94 | ret = swscanf(inputstr, formatstr, &num); |
| 95 | if (ret != 1) |
| 96 | { |
| 97 | Fail("ERROR: Expected swscanf to return 1, got %d.\n" |
| 98 | "Using \"%s\" in \"%s\".\n" , ret, convertC(inputstr), |
| 99 | convertC(formatstr)); |
| 100 | } |
| 101 | |
| 102 | if (checknum != num) |
| 103 | { |
| 104 | Fail("ERROR: scanned number incorrectly from \"%s\" using \"%s\".\n" |
| 105 | "Expected %d, got %d.\n" , convertC(inputstr), |
| 106 | convertC(formatstr), checknum, num); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void DoShortNumTest(WCHAR *inputstr, const WCHAR *formatstr, short checknum) |
| 111 | { |
| 112 | short num = 0; |
| 113 | int ret; |
| 114 | |
| 115 | ret = swscanf(inputstr, formatstr, &num); |
| 116 | if (ret != 1) |
| 117 | { |
| 118 | Fail("ERROR: Expected swscanf to return 1, got %d.\n" |
| 119 | "Using \"%s\" in \"%s\".\n" , ret, convertC(inputstr), |
| 120 | convertC(formatstr)); |
| 121 | } |
| 122 | |
| 123 | if (checknum != num) |
| 124 | { |
| 125 | Fail("ERROR: scanned number incorrectly from \"%s\" using \"%s\".\n" |
| 126 | "Expected %hd, got %hd.\n" , convertC(inputstr), |
| 127 | convertC(formatstr), checknum, num); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void DoI64NumTest(WCHAR *inputstr, const WCHAR *formatstr, INT64 checknum) |
| 132 | { |
| 133 | char buf[256]; |
| 134 | char check[256]; |
| 135 | INT64 num; |
| 136 | int ret; |
| 137 | |
| 138 | ret = swscanf(inputstr, formatstr, &num); |
| 139 | if (ret != 1) |
| 140 | { |
| 141 | Fail("ERROR: Expected swscanf to return 1, got %d.\n" |
| 142 | "Using \"%s\" in \"%s\".\n" , ret, convertC(inputstr), |
| 143 | convertC(formatstr)); |
| 144 | } |
| 145 | |
| 146 | if (checknum != num) |
| 147 | { |
| 148 | sprintf_s(buf, _countof(buf), "%I64d" , num); |
| 149 | sprintf_s(check, _countof(check), "%I64d" , checknum); |
| 150 | Fail("ERROR: scanned I64 number incorrectly from \"%s\" using \"%s\".\n" |
| 151 | "Expected %s, got %s.\n" , convertC(inputstr), |
| 152 | convertC(formatstr), check, buf); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void DoCharTest(WCHAR *inputstr, const WCHAR *formatstr, char* checkchars, int numchars) |
| 157 | { |
| 158 | char buf[256]; |
| 159 | int ret; |
| 160 | int i; |
| 161 | |
| 162 | for (i=0; i<256; i++) |
| 163 | buf[i] = (char)-1; |
| 164 | |
| 165 | ret = swscanf(inputstr, formatstr, buf); |
| 166 | if (ret != 1) |
| 167 | { |
| 168 | Fail("ERROR: Expected swscanf to return 1, got %d.\n" |
| 169 | "Using \"%s\" in \"%s\".\n" , ret, convertC(inputstr), |
| 170 | convertC(formatstr)); |
| 171 | } |
| 172 | |
| 173 | if (memcmp(buf, checkchars, numchars) != 0) |
| 174 | { |
| 175 | buf[numchars] = 0; |
| 176 | |
| 177 | Fail("ERROR: scanned character(s) incorrectly from \"%s\" using \"%s\".\n" |
| 178 | "Expected %s, got %s.\n" , convertC(inputstr), |
| 179 | convertC(formatstr), checkchars, buf); |
| 180 | } |
| 181 | |
| 182 | if (buf[numchars] != (char)-1) |
| 183 | { |
| 184 | Fail("ERROR: overflow occurred in scanning character(s) from \"%s\" " |
| 185 | "using \"%s\".\nExpected %d character(s)\n" , |
| 186 | convertC(inputstr), convertC(formatstr), numchars); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void DoWCharTest(WCHAR *inputstr, const WCHAR *formatstr, const WCHAR *checkchars, int numchars) |
| 191 | { |
| 192 | WCHAR buf[256]; |
| 193 | int ret; |
| 194 | int i; |
| 195 | |
| 196 | for (i=0; i<256; i++) |
| 197 | buf[i] = (WCHAR)-1; |
| 198 | |
| 199 | ret = swscanf(inputstr, formatstr, buf); |
| 200 | if (ret != 1) |
| 201 | { |
| 202 | Fail("ERROR: Expected swscanf to return 1, got %d.\n" |
| 203 | "Using \"%s\" in \"%s\".\n" , ret, convertC(inputstr), |
| 204 | convertC(formatstr)); |
| 205 | } |
| 206 | |
| 207 | if (memcmp(buf, checkchars, numchars*2) != 0) |
| 208 | { |
| 209 | buf[numchars] = 0; |
| 210 | |
| 211 | Fail("ERROR: scanned wide character(s) incorrectly from \"%s\" using \"%s\".\n" |
| 212 | "Expected %s, got %s.\n" , convertC(inputstr), |
| 213 | convertC(formatstr), convertC(checkchars), |
| 214 | convertC(buf)); |
| 215 | } |
| 216 | |
| 217 | if (buf[numchars] != (WCHAR)-1) |
| 218 | { |
| 219 | Fail("ERROR: overflow occurred in scanning wide character(s) from \"%s\" " |
| 220 | "using \"%s\".\nExpected %d character(s)\n" , |
| 221 | convertC(inputstr), convertC(formatstr), numchars); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | |
| 226 | void DoFloatTest(WCHAR *inputstr, const WCHAR *formatstr, float checkval) |
| 227 | { |
| 228 | char buf[256] = { 0 }; |
| 229 | float val; |
| 230 | int ret; |
| 231 | int i; |
| 232 | |
| 233 | for (i=0; i<256; i++) |
| 234 | buf[i] = (char)-1; |
| 235 | |
| 236 | ret = swscanf(inputstr, formatstr, buf); |
| 237 | val = *(float*)buf; |
| 238 | |
| 239 | if (ret != 1) |
| 240 | { |
| 241 | Fail("ERROR: Expected swscanf to return 1, got %d.\n" |
| 242 | "Using \"%s\" in \"%s\".\n" , ret, convertC(inputstr), |
| 243 | convertC(formatstr)); |
| 244 | } |
| 245 | |
| 246 | if (val != checkval) |
| 247 | { |
| 248 | Fail("ERROR: scanned float incorrectly from \"%s\" using \"%s\".\n" |
| 249 | "Expected \"%f\", got \"%f\".\n" , convertC(inputstr), |
| 250 | convertC(formatstr), checkval, val); |
| 251 | } |
| 252 | |
| 253 | if (buf[4] != (char)-1) |
| 254 | { |
| 255 | Fail("ERROR: overflow occurred in scanning float from \"%s\" " |
| 256 | "using \"%s\".\n" , convertC(inputstr), convertC(formatstr)); |
| 257 | |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | #endif |
| 263 | |