VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstNoCrt-1.cpp@ 7689

Last change on this file since 7689 was 7215, checked in by vboxsync, 17 years ago

msc hack.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.0 KB
Line 
1/* $Id: tstNoCrt-1.cpp 7215 2008-02-29 01:49:05Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime Testcase - Testcase for the No-CRT assembly bits.
4 */
5
6/*
7 * Copyright (C) 2008 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <iprt/nocrt/string.h>
31#include <iprt/stream.h>
32#include <iprt/initterm.h>
33#include <iprt/string.h>
34#ifdef RT_WITHOUT_NOCRT_WRAPPERS
35# error "Build error."
36#endif
37
38
39/*******************************************************************************
40* Structures and Typedefs *
41*******************************************************************************/
42#define TSTBUF_SIZE 8192
43typedef struct TSTBUF
44{
45 uint8_t abHeadFence[2048];
46 uint8_t abBuf[TSTBUF_SIZE];
47 uint8_t abTailFence[2048];
48} TSTBUF, *PTSTBUF;
49
50
51/*******************************************************************************
52* Global Variables *
53*******************************************************************************/
54static unsigned g_cErrors = 0;
55
56
57static void my_memset(void *pv, int ch, size_t cb)
58{
59 uint8_t *pb = (uint8_t *)pv;
60 while (cb--)
61 *pb++ = ch;
62}
63
64static void my_memcheck(const void *pv, int ch, size_t cb, const char *pszDesc)
65{
66 uint8_t *pb = (uint8_t *)pv;
67 while (cb--)
68 {
69 if (*pb != (uint8_t)ch)
70 {
71 g_cErrors++;
72 size_t off;
73 for (off = 1; off < cb && pb[off] != (uint8_t)ch; off++)
74 /* nandemonai */;
75 off--;
76 pb += off;
77 cb -= off;
78 if (off)
79 RTPrintf("tstNoCrt-1: %s: %p:%p - %02x instead of %02x\n",
80 pszDesc, (uintptr_t)pb - (uintptr_t)pv,
81 (uint8_t *)pb - (uint8_t *)pv + off, *pb, (uint8_t)ch);
82 else
83 RTPrintf("tstNoCrt-1: %s: %p - %02x instead of %02x\n",
84 pszDesc, (uint8_t *)pb - (uint8_t *)pv, *pb, (uint8_t)ch);
85 }
86
87 /* next*/
88 pb++;
89 }
90}
91
92
93static void TstBufInit(PTSTBUF pBuf, int ch)
94{
95 my_memset(pBuf->abHeadFence, 0x55, sizeof(pBuf->abHeadFence));
96 my_memset(pBuf->abBuf, ch, sizeof(pBuf->abBuf));
97 my_memset(pBuf->abTailFence, 0x77, sizeof(pBuf->abHeadFence));
98}
99
100
101static void TstBufCheck(PTSTBUF pBuf, const char *pszDesc)
102{
103 my_memcheck(pBuf->abHeadFence, 0x55, sizeof(pBuf->abHeadFence), pszDesc);
104 my_memcheck(pBuf->abTailFence, 0x77, sizeof(pBuf->abTailFence), pszDesc);
105}
106
107
108#if 0 /* enable this to test the testcase. */
109# undef RT_NOCRT
110# define RT_NOCRT(a) a
111# ifdef _MSC_VER
112# define mempcpy nocrt_mempcpy
113# endif
114#endif
115
116int main()
117{
118 /*
119 * Prologue.
120 */
121 RTR3Init();
122 RTPrintf("tstNoCrt-1: TESTING...\n");
123
124 /*
125 * Sanity.
126 */
127 TSTBUF Buf1;
128 TstBufInit(&Buf1, 1);
129 my_memcheck(Buf1.abBuf, 1, TSTBUF_SIZE, "sanity buf1");
130 TstBufCheck(&Buf1, "sanity buf1");
131
132 TSTBUF Buf2;
133 TstBufInit(&Buf2, 2);
134 my_memcheck(Buf2.abBuf, 2, TSTBUF_SIZE, "sanity buf2");
135 TstBufCheck(&Buf2, "sanity buf2");
136 if (g_cErrors)
137 {
138 RTPrintf("tstNoCrt-1: FAILED - fatal sanity error\n");
139 return 1;
140 }
141
142#define CHECK_PV(expect) \
143 do \
144 { \
145 if (pv != (expect)) \
146 { \
147 RTPrintf("tstNoCrt-1(%d): pv=%p expected=%p\n", __LINE__, pv, (expect)); \
148 g_cErrors++; \
149 } \
150 } while (0)
151 void *pv;
152
153#define CHECK_DIFF(op) \
154 do \
155 { \
156 if (!(iDiff op 0)) \
157 { \
158 RTPrintf("tstNoCrt-1(%d): iDiff=%d expected: %s 0\n", __LINE__, iDiff, #op); \
159 g_cErrors++; \
160 } \
161 } while (0)
162 int iDiff;
163
164 static char s_szTest1[] = "0123456789abcdef";
165 static char s_szTest2[] = "0123456789abcdef";
166 static char s_szTest3[] = "fedcba9876543210";
167
168 /*
169 * memcpy.
170 */
171 RTPrintf("tstNoCrt-1: memcpy\n");
172 TstBufInit(&Buf1, 1);
173 TstBufInit(&Buf2, 2);
174 pv = RT_NOCRT(memcpy)(Buf1.abBuf, Buf2.abBuf, TSTBUF_SIZE); CHECK_PV(Buf1.abBuf);
175 my_memcheck(Buf1.abBuf, 2, TSTBUF_SIZE, "memcpy1-dst");
176 my_memcheck(Buf2.abBuf, 2, TSTBUF_SIZE, "memcpy1-src");
177 TstBufCheck(&Buf1, "memcpy1");
178 TstBufCheck(&Buf2, "memcpy1");
179
180 TstBufInit(&Buf1, 3);
181 TstBufInit(&Buf2, 4);
182 pv = RT_NOCRT(memcpy)(Buf2.abBuf, Buf1.abBuf, TSTBUF_SIZE); CHECK_PV(Buf2.abBuf);
183 my_memcheck(Buf1.abBuf, 3, TSTBUF_SIZE, "memcpy2-dst");
184 my_memcheck(Buf2.abBuf, 3, TSTBUF_SIZE, "memcpy2-src");
185 TstBufCheck(&Buf1, "memcpy2");
186 TstBufCheck(&Buf2, "memcpy2");
187
188 TstBufInit(&Buf1, 5);
189 TstBufInit(&Buf2, 6);
190 pv = RT_NOCRT(memcpy)(Buf2.abBuf, Buf1.abBuf, 0); CHECK_PV(Buf2.abBuf);
191 my_memcheck(Buf1.abBuf, 5, TSTBUF_SIZE, "memcpy3-dst");
192 my_memcheck(Buf2.abBuf, 6, TSTBUF_SIZE, "memcpy3-src");
193 TstBufCheck(&Buf1, "memcpy3-dst");
194 TstBufCheck(&Buf2, "memcpy3-src");
195
196 for (unsigned off1 = 0; off1 <= 128; off1++)
197 {
198 for (unsigned off2 = 0; off2 <= 256; off2++)
199 {
200 char sz[32];
201 RTStrPrintf(sz, sizeof(sz), "memcpy4-%d-%d", off1, off2);
202 TstBufInit(&Buf1, 1);
203 TstBufInit(&Buf2, 2);
204 size_t cb = off2;
205 pv = RT_NOCRT(memcpy)(&Buf2.abBuf[off2], &Buf1.abBuf[off1], cb); CHECK_PV(&Buf2.abBuf[off2]);
206 my_memcheck(Buf1.abBuf, 1, TSTBUF_SIZE, sz);
207 my_memcheck(Buf2.abBuf, 2, off2, sz);
208 my_memcheck(&Buf2.abBuf[off2], 1, cb, sz);
209 my_memcheck(&Buf2.abBuf[off2 + cb], 2, TSTBUF_SIZE - cb - off2, sz);
210 TstBufCheck(&Buf1, sz);
211 TstBufCheck(&Buf2, sz);
212 }
213 }
214
215 /*
216 * mempcpy.
217 */
218 RTPrintf("tstNoCrt-1: mempcpy\n");
219 TstBufInit(&Buf1, 1);
220 TstBufInit(&Buf2, 2);
221 pv = RT_NOCRT(mempcpy)(Buf1.abBuf, Buf2.abBuf, TSTBUF_SIZE); CHECK_PV(&Buf1.abBuf[TSTBUF_SIZE]);
222 my_memcheck(Buf1.abBuf, 2, TSTBUF_SIZE, "mempcpy1-dst");
223 my_memcheck(Buf2.abBuf, 2, TSTBUF_SIZE, "mempcpy1-src");
224 TstBufCheck(&Buf1, "mempcpy1");
225 TstBufCheck(&Buf2, "mempcpy1");
226
227 TstBufInit(&Buf1, 3);
228 TstBufInit(&Buf2, 4);
229 pv = RT_NOCRT(mempcpy)(Buf2.abBuf, Buf1.abBuf, TSTBUF_SIZE); CHECK_PV(&Buf2.abBuf[TSTBUF_SIZE]);
230 my_memcheck(Buf1.abBuf, 3, TSTBUF_SIZE, "mempcpy2-dst");
231 my_memcheck(Buf2.abBuf, 3, TSTBUF_SIZE, "mempcpy2-src");
232 TstBufCheck(&Buf1, "mempcpy2");
233 TstBufCheck(&Buf2, "mempcpy2");
234
235 TstBufInit(&Buf1, 5);
236 TstBufInit(&Buf2, 6);
237 pv = RT_NOCRT(mempcpy)(Buf2.abBuf, Buf1.abBuf, 0); CHECK_PV(Buf2.abBuf);
238 my_memcheck(Buf1.abBuf, 5, TSTBUF_SIZE, "mempcpy3-dst");
239 my_memcheck(Buf2.abBuf, 6, TSTBUF_SIZE, "mempcpy3-src");
240 TstBufCheck(&Buf1, "mempcpy3-dst");
241 TstBufCheck(&Buf2, "mempcpy3-src");
242
243 for (unsigned off1 = 0; off1 <= 128; off1++)
244 {
245 for (unsigned off2 = 0; off2 <= 256; off2++)
246 {
247 char sz[32];
248 RTStrPrintf(sz, sizeof(sz), "mempcpy4-%d-%d", off1, off2);
249 TstBufInit(&Buf1, 1);
250 TstBufInit(&Buf2, 2);
251 size_t cb = off2;
252 pv = RT_NOCRT(mempcpy)(&Buf2.abBuf[off2], &Buf1.abBuf[off1], cb); CHECK_PV(&Buf2.abBuf[off2 + cb]);
253 my_memcheck(Buf1.abBuf, 1, TSTBUF_SIZE, sz);
254 my_memcheck(Buf2.abBuf, 2, off2, sz);
255 my_memcheck(&Buf2.abBuf[off2], 1, cb, sz);
256 my_memcheck(&Buf2.abBuf[off2 + cb], 2, TSTBUF_SIZE - cb - off2, sz);
257 TstBufCheck(&Buf1, sz);
258 TstBufCheck(&Buf2, sz);
259 }
260 }
261
262 /*
263 * memmove.
264 */
265 RTPrintf("tstNoCrt-1: memmove\n");
266 TstBufInit(&Buf1, 1);
267 TstBufInit(&Buf2, 2);
268 pv = RT_NOCRT(memmove)(Buf1.abBuf, Buf2.abBuf, TSTBUF_SIZE); CHECK_PV(Buf1.abBuf);
269 my_memcheck(Buf1.abBuf, 2, TSTBUF_SIZE, "memmove1-dst");
270 my_memcheck(Buf2.abBuf, 2, TSTBUF_SIZE, "memmove1-src");
271 TstBufCheck(&Buf1, "memmove1");
272 TstBufCheck(&Buf2, "memmove1");
273
274 TstBufInit(&Buf1, 3);
275 TstBufInit(&Buf2, 4);
276 pv = RT_NOCRT(memmove)(Buf2.abBuf, Buf1.abBuf, TSTBUF_SIZE); CHECK_PV(Buf2.abBuf);
277 my_memcheck(Buf1.abBuf, 3, TSTBUF_SIZE, "memmove2-dst");
278 my_memcheck(Buf2.abBuf, 3, TSTBUF_SIZE, "memmove2-src");
279 TstBufCheck(&Buf1, "memmove2");
280 TstBufCheck(&Buf2, "memmove2");
281
282 TstBufInit(&Buf1, 5);
283 TstBufInit(&Buf2, 6);
284 pv = RT_NOCRT(memmove)(Buf2.abBuf, Buf1.abBuf, 0); CHECK_PV(Buf2.abBuf);
285 my_memcheck(Buf1.abBuf, 5, TSTBUF_SIZE, "memmove3-dst");
286 my_memcheck(Buf2.abBuf, 6, TSTBUF_SIZE, "memmove3-src");
287 TstBufCheck(&Buf1, "memmove3-dst");
288 TstBufCheck(&Buf2, "memmove3-src");
289
290 for (unsigned off1 = 1; off1 <= 128; off1++)
291 {
292 for (unsigned off2 = 0; off2 <= 256; off2++)
293 {
294 /* forward */
295 char sz[32];
296 RTStrPrintf(sz, sizeof(sz), "memmove4-%d-%d", off1, off2);
297 TstBufInit(&Buf1, off1 + 1);
298 my_memset(Buf1.abBuf, off1, off1);
299 pv = RT_NOCRT(memmove)(Buf1.abBuf, &Buf1.abBuf[off2], TSTBUF_SIZE - off2); CHECK_PV(Buf1.abBuf);
300 if (off2 < off1)
301 {
302 unsigned cbLead = off1 - off2;
303 my_memcheck(Buf1.abBuf, off1, cbLead, sz);
304 my_memcheck(&Buf1.abBuf[cbLead], off1 + 1, TSTBUF_SIZE - cbLead, sz);
305 }
306 else
307 my_memcheck(Buf1.abBuf, off1 + 1, TSTBUF_SIZE, sz);
308 TstBufCheck(&Buf1, sz);
309
310 /* backward */
311 RTStrPrintf(sz, sizeof(sz), "memmove5-%d-%d", off1, off2);
312 TstBufInit(&Buf1, off1 + 1);
313 my_memset(&Buf1.abBuf[TSTBUF_SIZE - off1], off1, off1);
314 pv = RT_NOCRT(memmove)(&Buf1.abBuf[off2], Buf1.abBuf, TSTBUF_SIZE - off2); CHECK_PV(&Buf1.abBuf[off2]);
315 if (off2 < off1)
316 {
317 unsigned cbLead = off1 - off2;
318 my_memcheck(&Buf1.abBuf[TSTBUF_SIZE - cbLead], off1, cbLead, sz);
319 my_memcheck(Buf1.abBuf, off1 + 1, TSTBUF_SIZE - cbLead, sz);
320 }
321 else
322 my_memcheck(Buf1.abBuf, off1 + 1, TSTBUF_SIZE, sz);
323 TstBufCheck(&Buf1, sz);
324 /* small unaligned */
325 RTStrPrintf(sz, sizeof(sz), "memmove6-%d-%d", off1, off2);
326 TstBufInit(&Buf1, 1);
327 TstBufInit(&Buf2, 2);
328 size_t cb = off2;
329 pv = RT_NOCRT(memmove)(&Buf2.abBuf[off2], &Buf1.abBuf[off1], cb); CHECK_PV(&Buf2.abBuf[off2]);
330 my_memcheck(Buf1.abBuf, 1, TSTBUF_SIZE, sz);
331 my_memcheck(Buf2.abBuf, 2, off2, sz);
332 my_memcheck(&Buf2.abBuf[off2], 1, cb, sz);
333 my_memcheck(&Buf2.abBuf[off2 + cb], 2, TSTBUF_SIZE - cb - off2, sz);
334 TstBufCheck(&Buf1, sz);
335 TstBufCheck(&Buf2, sz);
336 }
337 }
338
339 /*
340 * memset
341 */
342 RTPrintf("tstNoCrt-1: memset\n");
343 TstBufInit(&Buf1, 1);
344 pv = RT_NOCRT(memset)(Buf1.abBuf, 0, TSTBUF_SIZE); CHECK_PV(Buf1.abBuf);
345 my_memcheck(Buf1.abBuf, 0, TSTBUF_SIZE, "memset-1");
346 TstBufCheck(&Buf1, "memset-1");
347
348 TstBufInit(&Buf1, 1);
349 pv = RT_NOCRT(memset)(Buf1.abBuf, 0xff, TSTBUF_SIZE); CHECK_PV(Buf1.abBuf);
350 my_memcheck(Buf1.abBuf, 0xff, TSTBUF_SIZE, "memset-2");
351 TstBufCheck(&Buf1, "memset-2");
352
353 TstBufInit(&Buf1, 1);
354 pv = RT_NOCRT(memset)(Buf1.abBuf, 0xff, 0); CHECK_PV(Buf1.abBuf);
355 my_memcheck(Buf1.abBuf, 1, TSTBUF_SIZE, "memset-3");
356 TstBufCheck(&Buf1, "memset-3");
357
358 for (unsigned off = 0; off < 256; off++)
359 {
360 /* move start byte by byte. */
361 char sz[32];
362 RTStrPrintf(sz, sizeof(sz), "memset4-%d", off);
363 TstBufInit(&Buf1, 0);
364 pv = RT_NOCRT(memset)(&Buf1.abBuf[off], off, TSTBUF_SIZE - off); CHECK_PV(&Buf1.abBuf[off]);
365 my_memcheck(Buf1.abBuf, 0, off, sz);
366 my_memcheck(&Buf1.abBuf[off], off, TSTBUF_SIZE - off, sz);
367 TstBufCheck(&Buf1, sz);
368
369 /* move end byte by byte. */
370 RTStrPrintf(sz, sizeof(sz), "memset5-%d", off);
371 TstBufInit(&Buf1, 0);
372 pv = RT_NOCRT(memset)(Buf1.abBuf, off, TSTBUF_SIZE - off); CHECK_PV(Buf1.abBuf);
373 my_memcheck(Buf1.abBuf, off, TSTBUF_SIZE - off, sz);
374 my_memcheck(&Buf1.abBuf[TSTBUF_SIZE - off], 0, off, sz);
375 TstBufCheck(&Buf1, sz);
376
377 /* move both start and size byte by byte. */
378 RTStrPrintf(sz, sizeof(sz), "memset6-%d", off);
379 TstBufInit(&Buf1, 0);
380 pv = RT_NOCRT(memset)(&Buf1.abBuf[off], off, off); CHECK_PV(&Buf1.abBuf[off]);
381 my_memcheck(Buf1.abBuf, 0, off, sz);
382 my_memcheck(&Buf1.abBuf[off], off, off, sz);
383 my_memcheck(&Buf1.abBuf[off * 2], 0, TSTBUF_SIZE - off * 2, sz);
384 TstBufCheck(&Buf1, sz);
385 }
386
387 /*
388 * memchr & strchr.
389 */
390 RTPrintf("tstNoCrt-1: memchr\n");
391 pv = RT_NOCRT(memchr)(&s_szTest1[0x00], 'f', sizeof(s_szTest1)); CHECK_PV(&s_szTest1[0xf]);
392 pv = RT_NOCRT(memchr)(&s_szTest1[0x0f], 'f', sizeof(s_szTest1)); CHECK_PV(&s_szTest1[0xf]);
393 pv = RT_NOCRT(memchr)(&s_szTest1[0x03], 0, sizeof(s_szTest1)); CHECK_PV(&s_szTest1[0x10]);
394 pv = RT_NOCRT(memchr)(&s_szTest1[0x10], 0, sizeof(s_szTest1)); CHECK_PV(&s_szTest1[0x10]);
395 for (unsigned i = 0; i < sizeof(s_szTest1); i++)
396 for (unsigned j = 0; j <= i; j++)
397 {
398 pv = RT_NOCRT(memchr)(&s_szTest1[j], s_szTest1[i], sizeof(s_szTest1));
399 CHECK_PV(&s_szTest1[i]);
400 }
401
402 RTPrintf("tstNoCrt-1: strchr\n");
403 pv = RT_NOCRT(strchr)(&s_szTest1[0x00], 'f'); CHECK_PV(&s_szTest1[0xf]);
404 pv = RT_NOCRT(strchr)(&s_szTest1[0x0f], 'f'); CHECK_PV(&s_szTest1[0xf]);
405 pv = RT_NOCRT(strchr)(&s_szTest1[0x03], 0); CHECK_PV(&s_szTest1[0x10]);
406 pv = RT_NOCRT(strchr)(&s_szTest1[0x10], 0); CHECK_PV(&s_szTest1[0x10]);
407 for (unsigned i = 0; i < sizeof(s_szTest1); i++)
408 for (unsigned j = 0; j <= i; j++)
409 {
410 pv = RT_NOCRT(strchr)(&s_szTest1[j], s_szTest1[i]);
411 CHECK_PV(&s_szTest1[i]);
412 }
413
414 /*
415 * Some simple memcmp/strcmp checks.
416 */
417 RTPrintf("tstNoCrt-1: memcmp\n");
418 iDiff = RT_NOCRT(memcmp)(s_szTest1, s_szTest1, sizeof(s_szTest1)); CHECK_DIFF( == );
419 iDiff = RT_NOCRT(memcmp)(s_szTest1, s_szTest2, sizeof(s_szTest1)); CHECK_DIFF( == );
420 iDiff = RT_NOCRT(memcmp)(s_szTest2, s_szTest1, sizeof(s_szTest1)); CHECK_DIFF( == );
421 iDiff = RT_NOCRT(memcmp)(s_szTest3, s_szTest3, sizeof(s_szTest1)); CHECK_DIFF( == );
422 iDiff = RT_NOCRT(memcmp)(s_szTest1, s_szTest3, sizeof(s_szTest1)); CHECK_DIFF( < );
423 iDiff = RT_NOCRT(memcmp)(s_szTest3, s_szTest1, sizeof(s_szTest1)); CHECK_DIFF( > );
424
425 RTPrintf("tstNoCrt-1: strcmp\n");
426 iDiff = RT_NOCRT(strcmp)(s_szTest1, s_szTest1); CHECK_DIFF( == );
427 iDiff = RT_NOCRT(strcmp)(s_szTest1, s_szTest2); CHECK_DIFF( == );
428 iDiff = RT_NOCRT(strcmp)(s_szTest2, s_szTest1); CHECK_DIFF( == );
429 iDiff = RT_NOCRT(strcmp)(s_szTest3, s_szTest3); CHECK_DIFF( == );
430 iDiff = RT_NOCRT(strcmp)(s_szTest1, s_szTest3); CHECK_DIFF( < );
431 iDiff = RT_NOCRT(strcmp)(s_szTest3, s_szTest1); CHECK_DIFF( > );
432
433
434 /*
435 * Summary.
436 */
437 if (!g_cErrors)
438 RTPrintf("tstNoCrt-1: SUCCESS\n");
439 else
440 RTPrintf("tstNoCrt-1: FAILURE - %d errors\n", g_cErrors);
441 return !!g_cErrors;
442}
443
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette