| 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: vswprintf.h |
| 8 | ** |
| 9 | ** Purpose: Containts common testing functions for vswprintf |
| 10 | ** |
| 11 | ** |
| 12 | **==========================================================================*/ |
| 13 | |
| 14 | #ifndef __vswprintf_H__ |
| 15 | #define __vswprintf_H__ |
| 16 | |
| 17 | /* These functions leaks memory like crazy. C'est la vie. */ |
| 18 | int testvswp(wchar_t* buf, size_t buffSize, const wchar_t* format, ...) |
| 19 | { |
| 20 | int retVal = 0; |
| 21 | va_list arglist; |
| 22 | |
| 23 | va_start(arglist, format); |
| 24 | retVal = _vsnwprintf_s(buf, buffSize, _TRUNCATE, format, arglist); |
| 25 | va_end(arglist); |
| 26 | |
| 27 | return( retVal); |
| 28 | } |
| 29 | |
| 30 | void DoWStrTest(const WCHAR *formatstr, WCHAR *param, const WCHAR *checkstr) |
| 31 | { |
| 32 | WCHAR buf[256] = { 0 }; |
| 33 | |
| 34 | testvswp(buf, _countof(buf), formatstr, param); |
| 35 | |
| 36 | if (memcmp(buf, checkstr, wcslen(buf) * 2 + 2) != 0) |
| 37 | { |
| 38 | Fail("ERROR: failed to insert wide string \"%s\" into \"%s\".\n" |
| 39 | "Expected \"%s\", got \"%s\".\n" , |
| 40 | convertC(param), convertC(formatstr), |
| 41 | convertC(checkstr), convertC(buf)); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void DoStrTest(const WCHAR *formatstr, char *param, const WCHAR *checkstr) |
| 46 | { |
| 47 | WCHAR buf[256] = { 0 }; |
| 48 | |
| 49 | testvswp(buf, _countof(buf), formatstr, param); |
| 50 | |
| 51 | if (memcmp(buf, checkstr, wcslen(buf) * 2 + 2) != 0) |
| 52 | { |
| 53 | Fail("ERROR: failed to insert wide string \"%s\" into \"%s\".\n" |
| 54 | "Expected \"%s\", got \"%s\".\n" , |
| 55 | param, convertC(formatstr), convertC(checkstr), |
| 56 | convertC(buf)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void DoCharTest(const WCHAR *formatstr, char param, const WCHAR *checkstr) |
| 61 | { |
| 62 | WCHAR buf[256] = { 0 }; |
| 63 | |
| 64 | testvswp(buf, _countof(buf), formatstr, param); |
| 65 | if (memcmp(buf, checkstr, wcslen(buf)*2 + 2) != 0) |
| 66 | { |
| 67 | Fail("ERROR: failed to insert char \'%c\' (%d) into \"%s\"\n" |
| 68 | "Expected \"%s\" got \"%s\".\n" , |
| 69 | param, param, convertC(formatstr), convertC(checkstr), |
| 70 | convertC(buf)); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void DoWCharTest(const WCHAR *formatstr, WCHAR param, const WCHAR *checkstr) |
| 75 | { |
| 76 | WCHAR buf[256] = { 0 }; |
| 77 | |
| 78 | testvswp(buf, _countof(buf), formatstr, param); |
| 79 | if (memcmp(buf, checkstr, wcslen(buf)*2 + 2) != 0) |
| 80 | { |
| 81 | Fail("ERROR: failed to insert wide char \'%c\' (%d) into \"%s\"\n" |
| 82 | "Expected \"%s\" got \"%s\".\n" , |
| 83 | (char) param, param, convertC(formatstr), convertC(checkstr), |
| 84 | convertC(buf)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void DoNumTest(const WCHAR *formatstr, int value, const WCHAR *checkstr) |
| 89 | { |
| 90 | WCHAR buf[256] = { 0 }; |
| 91 | |
| 92 | testvswp(buf, _countof(buf), formatstr, value); |
| 93 | if (memcmp(buf, checkstr, wcslen(buf)* 2 + 2) != 0) |
| 94 | { |
| 95 | Fail("ERROR: failed to insert %#x into \"%s\"\n" |
| 96 | "Expected \"%s\" got \"%s\".\n" , value, convertC(formatstr), |
| 97 | convertC(checkstr), convertC(buf)); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void DoI64NumTest(const WCHAR *formatstr, INT64 value, char *valuestr, const WCHAR *checkstr) |
| 102 | { |
| 103 | WCHAR buf[256] = { 0 }; |
| 104 | |
| 105 | testvswp(buf, _countof(buf), formatstr, value); |
| 106 | if (memcmp(buf, checkstr, wcslen(buf)* 2 + 2) != 0) |
| 107 | { |
| 108 | Fail("ERROR: failed to insert %s into \"%s\"\n" |
| 109 | "Expected \"%s\" got \"%s\".\n" , valuestr, convertC(formatstr), |
| 110 | convertC(checkstr), convertC(buf)); |
| 111 | } |
| 112 | } |
| 113 | void DoDoubleTest(const WCHAR *formatstr, double value, const WCHAR *checkstr1, WCHAR |
| 114 | *checkstr2) |
| 115 | { |
| 116 | WCHAR buf[256] = { 0 }; |
| 117 | |
| 118 | testvswp(buf, _countof(buf), formatstr, value); |
| 119 | if (memcmp(buf, checkstr1, wcslen(checkstr1) + 2) != 0 && |
| 120 | memcmp(buf, checkstr2, wcslen(checkstr2) + 2) != 0) |
| 121 | { |
| 122 | Fail("ERROR: failed to insert %f into \"%s\"\n" |
| 123 | "Expected \"%s\" or \"%s\", got \"%s\".\n" , |
| 124 | value, |
| 125 | convertC(formatstr), |
| 126 | convertC(checkstr1), |
| 127 | convertC(checkstr2), |
| 128 | convertC(buf)); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #endif |
| 133 | |