crypt32/tests: Add a trailing '\n' to ok() calls.
[wine] / dlls / msvcrt / tests / misc.c
1 /*
2  * Unit tests for miscellaneous msvcrt functions
3  *
4  * Copyright 2010 Andrew Nguyen
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "wine/test.h"
22 #include <errno.h>
23 #include "msvcrt.h"
24
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
29 static void init(void)
30 {
31     HMODULE hmod = GetModuleHandleA("msvcrt.dll");
32
33     prand_s = (void *)GetProcAddress(hmod, "rand_s");
34     pmemcpy_s = (void*)GetProcAddress(hmod, "memcpy_s");
35     pI10_OUTPUT = (void*)GetProcAddress(hmod, "$I10_OUTPUT");
36 }
37
38 static void test_rand_s(void)
39 {
40     int ret;
41     unsigned int rand;
42
43     if (!prand_s)
44     {
45         win_skip("rand_s is not available\n");
46         return;
47     }
48
49     errno = EBADF;
50     ret = prand_s(NULL);
51     ok(ret == EINVAL, "Expected rand_s to return EINVAL, got %d\n", ret);
52     ok(errno == EINVAL, "Expected errno to return EINVAL, got %d\n", errno);
53
54     ret = prand_s(&rand);
55     ok(ret == 0, "Expected rand_s to return 0, got %d\n", ret);
56 }
57
58 static void test_memcpy_s(void)
59 {
60     static char data[] = "data\0to\0be\0copied";
61     static char dest[32];
62     int ret;
63
64     if(!pmemcpy_s)
65     {
66         win_skip("memcpy_s is not available\n");
67         return;
68     }
69
70     errno = 0xdeadbeef;
71     ret = pmemcpy_s(NULL, 0, NULL, 0);
72     ok(ret == 0, "ret = %x\n", ret);
73     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
74
75     errno = 0xdeadbeef;
76     dest[0] = 'x';
77     ret = pmemcpy_s(dest, 10, NULL, 0);
78     ok(ret == 0, "ret = %x\n", ret);
79     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
80     ok(dest[0] == 'x', "dest[0] != \'x\'\n");
81
82     errno = 0xdeadbeef;
83     ret = pmemcpy_s(NULL, 10, data, 10);
84     ok(ret == EINVAL, "ret = %x\n", ret);
85     ok(errno == EINVAL, "errno = %x\n", errno);
86
87     errno = 0xdeadbeef;
88     dest[7] = 'x';
89     ret = pmemcpy_s(dest, 10, data, 5);
90     ok(ret == 0, "ret = %x\n", ret);
91     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
92     ok(memcmp(dest, data, 10), "All data copied\n");
93     ok(!memcmp(dest, data, 5), "First five bytes are different\n");
94
95     errno = 0xdeadbeef;
96     ret = pmemcpy_s(data, 10, data, 10);
97     ok(ret == 0, "ret = %x\n", ret);
98     ok(errno == 0xdeadbeef, "errno = %x\n", errno);
99     ok(!memcmp(dest, data, 5), "data was destroyed during overwritting\n");
100
101     errno = 0xdeadbeef;
102     dest[0] = 'x';
103     ret = pmemcpy_s(dest, 5, data, 10);
104     ok(ret == ERANGE, "ret = %x\n", ret);
105     ok(errno == ERANGE, "errno = %x\n", errno);
106     ok(dest[0] == '\0', "dest[0] != \'\\0\'\n");
107 }
108
109 typedef struct _I10_OUTPUT_data {
110     short pos;
111     char sign;
112     BYTE len;
113     char str[100];
114 } I10_OUTPUT_data;
115
116 typedef struct _I10_OUTPUT_test {
117     long double d;
118     int size;
119     int flags;
120
121     I10_OUTPUT_data out;
122     int ret;
123     const char *remain;
124 } I10_OUTPUT_test;
125
126 static const I10_OUTPUT_test I10_OUTPUT_tests[] = {
127     /* arg3 = 0 */
128     { 0.0, 10, 0, {0, ' ', 1, "0"}, 1, "" },
129     { 1.0, 10, 0, {1, ' ', 1, "1"}, 1, "000000009" },
130     { -1.0, 10, 0, {1, '-', 1, "1"}, 1, "000000009" },
131     { 1.23, 10, 0, {1, ' ', 3, "123"}, 1, "0000009" },
132     { 1e13, 10, 0, {14, ' ', 1, "1"}, 1, "000000009" },
133     { 1e30, 30, 0, {31, ' ', 21, "100000000000000001988"}, 1, "" },
134     { 1e-13, 10, 0, {-12, ' ', 1, "1"}, 1, "000000000" },
135     { 0.25, 10, 0, {0, ' ', 2, "25"}, 1, "00000000" },
136     { 1.0000001, 10, 0, {1, ' ', 8, "10000001"}, 1, "00" },
137     /* arg3 = 1 */
138     { 0.0, 10, 1, {0, ' ', 1, "0"}, 1, "" },
139     { 1.0, 10, 1, {1, ' ', 1, "1"}, 1, "0000000009" },
140     { -1.0, 10, 1, {1, '-', 1, "1"}, 1, "0000000009" },
141     { 1.23, 10, 1, {1, ' ', 3, "123"}, 1, "00000009" },
142     { 1e13, 10, 1, {14, ' ', 1, "1"}, 1, "00000000000000000009" },
143     { 1e30, 30, 1, {31, ' ', 21, "100000000000000001988"}, 1, "" },
144     { 1e-13, 10, 1, {0, ' ', 1, "0"}, 1, "" },
145     { 1e-7, 10, 1, {-6, ' ', 1, "1"}, 1, "09" },
146     { 0.25, 10, 1, {0, ' ', 2, "25"}, 1, "00000000" },
147     { 1.0000001, 10, 1, {1, ' ', 8, "10000001"}, 1, "000" },
148     /* too small buffer */
149     { 0.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
150     { 0.0, 0, 1, {0, ' ', 1, "0"}, 1, "" },
151     { 123.0, 2, 0, {3, ' ', 2, "12"}, 1, "" },
152     { 123.0, 0, 0, {0, ' ', 1, "0"}, 1, "" },
153     { 123.0, 2, 1, {3, ' ', 3, "123"}, 1, "09" },
154     { 0.99, 1, 0, {1, ' ', 1, "1"}, 1, "" },
155     { 1264567.0, 2, 0, {7, ' ', 2, "13"}, 1, "" },
156     { 1264567.0, 2, 1, {7, ' ', 7, "1264567"}, 1, "00" },
157     { 1234567891.0, 2, 1, {10, ' ', 10, "1234567891"}, 1, "09" }
158 };
159
160 static void test_I10_OUTPUT(void)
161 {
162     I10_OUTPUT_data out;
163     int i, j, ret;
164
165     if(!pI10_OUTPUT) {
166         win_skip("I10_OUTPUT not available\n");
167         return;
168     }
169
170     for(i=0; i<sizeof(I10_OUTPUT_tests)/sizeof(I10_OUTPUT_test); i++) {
171         memset(out.str, '#', sizeof(out.str));
172
173         ret = pI10_OUTPUT(I10_OUTPUT_tests[i].d, I10_OUTPUT_tests[i].size, I10_OUTPUT_tests[i].flags, &out);
174         ok(ret == I10_OUTPUT_tests[i].ret, "%d: ret = %d\n", i, ret);
175         ok(out.pos == I10_OUTPUT_tests[i].out.pos, "%d: out.pos = %hd\n", i, out.pos);
176         ok(out.sign == I10_OUTPUT_tests[i].out.sign, "%d: out.size = %c\n", i, out.sign);
177         ok(out.len == I10_OUTPUT_tests[i].out.len, "%d: out.len = %d\n", i, (int)out.len);
178         ok(!strcmp(out.str, I10_OUTPUT_tests[i].out.str), "%d: out.str = %s\n", i, out.str);
179
180         j = strlen(I10_OUTPUT_tests[i].remain);
181         if(j && I10_OUTPUT_tests[i].remain[j-1]=='9')
182             todo_wine ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
183                     "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
184         else
185             ok(!strncmp(out.str+out.len+1, I10_OUTPUT_tests[i].remain, j),
186                     "%d: &out.str[%d] = %.25s...\n", i, out.len+1, out.str+out.len+1);
187
188
189         for(j=out.len+strlen(I10_OUTPUT_tests[i].remain)+1; j<sizeof(out.str); j++)
190             if(out.str[j] != '#')
191                 ok(0, "%d: out.str[%d] = %c (expected \'#\')\n", i, j, out.str[j]);
192     }
193 }
194
195 START_TEST(misc)
196 {
197     init();
198
199     test_rand_s();
200     test_memcpy_s();
201     test_I10_OUTPUT();
202 }