msvcrt: Implement _get_errno.
[wine.git] / dlls / msvcrt / tests / misc.c
blob21d9b5ca3f017ffcbdd865ef0778659d9e475a92
1 /*
2 * Unit tests for miscellaneous msvcrt functions
4 * Copyright 2010 Andrew Nguyen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/test.h"
22 #include <errno.h>
23 #include "msvcrt.h"
25 static int (__cdecl *prand_s)(unsigned int *);
26 static int (__cdecl *pmemcpy_s)(void *, MSVCRT_size_t, void*, MSVCRT_size_t);
27 static int (__cdecl *pI10_OUTPUT)(long double, int, int, void*);
28 static int (__cdecl *pstrerror_s)(char *, MSVCRT_size_t, int);
29 static int (__cdecl *p_get_errno)(int *);
31 static void init(void)
33 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
35 prand_s = (void *)GetProcAddress(hmod, "rand_s");
36 pmemcpy_s = (void*)GetProcAddress(hmod, "memcpy_s");
37 pI10_OUTPUT = (void*)GetProcAddress(hmod, "$I10_OUTPUT");
38 pstrerror_s = (void *)GetProcAddress(hmod, "strerror_s");
39 p_get_errno = (void *)GetProcAddress(hmod, "_get_errno");
42 static void test_rand_s(void)
44 int ret;
45 unsigned int rand;
47 if (!prand_s)
49 win_skip("rand_s is not available\n");
50 return;
53 errno = EBADF;
54 ret = prand_s(NULL);
55 ok(ret == EINVAL, "Expected rand_s to return EINVAL, got %d\n", ret);
56 ok(errno == EINVAL, "Expected errno to return EINVAL, got %d\n", errno);
58 ret = prand_s(&rand);
59 ok(ret == 0, "Expected rand_s to return 0, got %d\n", ret);
62 static void test_memcpy_s(void)
64 static char data[] = "data\0to\0be\0copied";
65 static char dest[32];
66 int ret;
68 if(!pmemcpy_s)
70 win_skip("memcpy_s is not available\n");
71 return;
74 errno = 0xdeadbeef;
75 ret = pmemcpy_s(NULL, 0, NULL, 0);
76 ok(ret == 0, "ret = %x\n", ret);
77 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
79 errno = 0xdeadbeef;
80 dest[0] = 'x';
81 ret = pmemcpy_s(dest, 10, NULL, 0);
82 ok(ret == 0, "ret = %x\n", ret);
83 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
84 ok(dest[0] == 'x', "dest[0] != \'x\'\n");
86 errno = 0xdeadbeef;
87 ret = pmemcpy_s(NULL, 10, data, 10);
88 ok(ret == EINVAL, "ret = %x\n", ret);
89 ok(errno == EINVAL, "errno = %x\n", errno);
91 errno = 0xdeadbeef;
92 dest[7] = 'x';
93 ret = pmemcpy_s(dest, 10, data, 5);
94 ok(ret == 0, "ret = %x\n", ret);
95 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
96 ok(memcmp(dest, data, 10), "All data copied\n");
97 ok(!memcmp(dest, data, 5), "First five bytes are different\n");
99 errno = 0xdeadbeef;
100 ret = pmemcpy_s(data, 10, data, 10);
101 ok(ret == 0, "ret = %x\n", ret);
102 ok(errno == 0xdeadbeef, "errno = %x\n", errno);
103 ok(!memcmp(dest, data, 5), "data was destroyed during overwriting\n");
105 errno = 0xdeadbeef;
106 dest[0] = 'x';
107 ret = pmemcpy_s(dest, 5, data, 10);
108 ok(ret == ERANGE, "ret = %x\n", ret);
109 ok(errno == ERANGE, "errno = %x\n", errno);
110 ok(dest[0] == '\0', "dest[0] != \'\\0\'\n");
113 typedef struct _I10_OUTPUT_data {
114 short pos;
115 char sign;
116 BYTE len;
117 char str[100];
118 } I10_OUTPUT_data;
120 typedef struct _I10_OUTPUT_test {
121 long double d;
122 int size;
123 int flags;
125 I10_OUTPUT_data out;
126 int ret;
127 const char *remain;
128 } I10_OUTPUT_test;
130 static const I10_OUTPUT_test I10_OUTPUT_tests[] = {
131 /* arg3 = 0 */
132 { 0.0, 10, 0, {0, ' ', 1, "0"}, 1, "" },
133 { 1.0, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
134 { -1.0, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
135 { 1.23, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
136 { 1e13, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
137 { 1e30, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
138 { 1e-13, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
139 { 0.25, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
140 { 1.0000001, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
141 /* arg3 = 1 */
142 { 0.0, 10, 1, {0, ' ', 1, "0"}, 1, "" },
143 { 1.0, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
144 { -1.0, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
145 { 1.23, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
146 { 1e13, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
147 { 1e30, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
148 { 1e-13, 10, 1, {0, ' ', 1, "0"}, 1, "" },
149 { 1e-7, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
150 { 0.25, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
151 { 1.0000001, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
152 /* too small buffer */
153 { 0.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
154 { 0.0, 0, 1, {0, ' ', 1, "0"}, 1, "" },
155 { 123.0, 2, 0, {3, ' ', 2, "12"}, 1, "" },
156 { 123.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
157 { 123.0, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
158 { 0.99, 1, 0, {1, ' ', 1, "1"}, 1, "" },
159 { 1264567.0, 2, 0, {7, ' ', 2, "13"}, 1, "" },
160 { 1264567.0, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
161 { 1234567891.0, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
164 static void test_I10_OUTPUT(void)
166 I10_OUTPUT_data out;
167 int i, j, ret;
169 if(!pI10_OUTPUT) {
170 win_skip("I10_OUTPUT not available\n");
171 return;
174 for(i=0; i<sizeof(I10_OUTPUT_tests)/sizeof(I10_OUTPUT_test); i++) {
175 memset(out.str, '#', sizeof(out.str));
177 ret = pI10_OUTPUT(I10_OUTPUT_tests[i].d, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
178 ok(ret == I10_OUTPUT_tests[i].ret, "%d: ret = %d\n", i, ret);
179 ok(out.pos == I10_OUTPUT_tests[i].out.pos, "%d: out.pos = %hd\n", i, out.pos);
180 ok(out.sign == I10_OUTPUT_tests[i].out.sign, "%d: out.size = %c\n", i, out.sign);
181 ok(out.len == I10_OUTPUT_tests[i].out.len, "%d: out.len = %d\n", i, (int)out.len);
182 ok(!strcmp(out.str, I10_OUTPUT_tests[i].out.str), "%d: out.str = %s\n", i, out.str);
184 j = strlen(I10_OUTPUT_tests[i].remain);
185 if(j && I10_OUTPUT_tests[i].remain[j-1]=='9')
186 todo_wine ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
187 "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
188 else
189 ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
190 "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
193 for(j=out.len+strlen(I10_OUTPUT_tests[i].remain)+1; j<sizeof(out.str); j++)
194 if(out.str[j] != '#')
195 ok(0, "%d: out.str[%d] = %c (expected \'#\')\n", i, j, out.str[j]);
199 static void test_strerror_s(void)
201 int ret;
202 char buf[256];
204 if (!pstrerror_s)
206 win_skip("strerror_s is not available\n");
207 return;
210 errno = EBADF;
211 ret = pstrerror_s(NULL, 0, 0);
212 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
213 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
215 errno = EBADF;
216 ret = pstrerror_s(NULL, sizeof(buf), 0);
217 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
218 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
220 memset(buf, 'X', sizeof(buf));
221 errno = EBADF;
222 ret = pstrerror_s(buf, 0, 0);
223 ok(ret == EINVAL, "Expected strerror_s to return EINVAL, got %d\n", ret);
224 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
225 ok(buf[0] == 'X', "Expected output buffer to be untouched\n");
227 memset(buf, 'X', sizeof(buf));
228 ret = pstrerror_s(buf, 1, 0);
229 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
230 ok(strlen(buf) == 0, "Expected output buffer to be null terminated\n");
232 memset(buf, 'X', sizeof(buf));
233 ret = pstrerror_s(buf, 2, 0);
234 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
235 ok(strlen(buf) == 1, "Expected output buffer to be truncated\n");
237 memset(buf, 'X', sizeof(buf));
238 ret = pstrerror_s(buf, sizeof(buf), 0);
239 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
241 memset(buf, 'X', sizeof(buf));
242 ret = pstrerror_s(buf, sizeof(buf), -1);
243 ok(ret == 0, "Expected strerror_s to return 0, got %d\n", ret);
246 static void test__get_errno(void)
248 int ret, out;
250 if (!p_get_errno)
252 win_skip("_get_errno is not available\n");
253 return;
256 errno = EBADF;
257 ret = p_get_errno(NULL);
258 ok(ret == EINVAL, "Expected _get_errno to return EINVAL, got %d\n", ret);
259 ok(errno == EBADF, "Expected errno to be EBADF, got %d\n", errno);
261 errno = EBADF;
262 out = 0xdeadbeef;
263 ret = p_get_errno(&out);
264 ok(ret == 0, "Expected _get_errno to return 0, got %d\n", ret);
265 ok(out == EBADF, "Expected output variable to be EBADF, got %d\n", out);
268 START_TEST(misc)
270 init();
272 test_rand_s();
273 test_memcpy_s();
274 test_I10_OUTPUT();
275 test_strerror_s();
276 test__get_errno();