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: test4.c (fprintf)
8**
9** Purpose: Tests the pointer specifier (%p).
10** This test is modeled after the fprintf series.
11**
12**
13**==========================================================================*/
14
15#include <palsuite.h>
16#include "../fprintf.h"
17
18/*
19 * Depends on memcmp, strlen, fopen, fseek and fgets.
20 */
21
22static void DoTest(char *formatstr, void* param, char* paramstr,
23 char *checkstr1, char *checkstr2)
24{
25 FILE *fp;
26 char buf[256] = { 0 };
27
28 if ((fp = fopen("testfile.txt", "w+")) == NULL )
29 {
30 Fail("ERROR: fopen failed to create testfile\n");
31 }
32
33 if ((fprintf(fp, formatstr, param)) < 0)
34 {
35 Fail("ERROR: fprintf failed\n");
36 }
37
38 if ((fseek(fp, 0, SEEK_SET)) != 0)
39 {
40 Fail("ERROR: fseek failed\n");
41 }
42
43 if ((fgets(buf, 100, fp)) == NULL)
44 {
45 Fail("ERROR: fseek failed\n");
46 }
47
48 if (memcmp(buf, checkstr1, strlen(buf) + 1) != 0 &&
49 memcmp(buf, checkstr2, strlen(buf) + 1) != 0 )
50 {
51 Fail("ERROR: failed to insert %s into \"%s\"\n"
52 "Expected \"%s\" or \"%s\" got \"%s\".\n",
53 paramstr, formatstr, checkstr1, checkstr2, buf);
54 }
55
56 if ((fclose( fp )) != 0)
57
58 {
59
60 Fail("ERROR: fclose failed to close \"testfile.txt\"\n");
61
62 }
63}
64
65
66int __cdecl main(int argc, char *argv[])
67{
68 void *ptr = (void*) 0x123456;
69 INT64 lptr = I64(0x1234567887654321);
70
71 if (PAL_Initialize(argc, argv) != 0)
72 return(FAIL);
73
74
75/*
76** Run only on 64 bit platforms
77*/
78#if defined(BIT64)
79 Trace("Testing for 64 Bit Platforms \n");
80 DoTest("%p", NULL, "NULL", "0000000000000000", "0x0");
81 DoTest("%p", ptr, "pointer to 0x123456", "0000000000123456", "0x123456");
82 DoTest("%17p", ptr, "pointer to 0x123456", " 0000000000123456", " 0x123456");
83 DoTest("%17p", ptr, "pointer to 0x123456", " 0000000000123456", "0x0123456");
84 DoTest("%-17p", ptr, "pointer to 0x123456", "0000000000123456 ", "0x123456 ");
85 DoTest("%+p", ptr, "pointer to 0x123456", "0000000000123456", "0x123456");
86 DoTest("%#p", ptr, "pointer to 0x123456", "0X0000000000123456", "0x123456");
87 DoTest("%lp", ptr, "pointer to 0x123456", "00123456", "0x123456");
88 DoTest("%hp", ptr, "pointer to 0x123456", "00003456", "0x3456");
89 DoTest("%Lp", ptr, "pointer to 0x123456", "00123456", "0x123456");
90 DoI64Test("%I64p", lptr, "pointer to 0x1234567887654321",
91 "1234567887654321", "0x1234567887654321");
92#else
93 Trace("Testing for Non 64 Bit Platforms \n");
94 DoTest("%p", NULL, "NULL", "00000000", "0x0");
95 DoTest("%p", ptr, "pointer to 0x123456", "00123456", "0x123456");
96 DoTest("%9p", ptr, "pointer to 0x123456", " 00123456", " 0x123456");
97 DoTest("%09p", ptr, "pointer to 0x123456", " 00123456", "0x0123456");
98 DoTest("%-9p", ptr, "pointer to 0x123456", "00123456 ", "0x123456 ");
99 DoTest("%+p", ptr, "pointer to 0x123456", "00123456", "0x123456");
100 DoTest("%#p", ptr, "pointer to 0x123456", "0X00123456", "0x123456");
101 DoTest("%lp", ptr, "pointer to 0x123456", "00123456", "0x123456");
102 DoTest("%hp", ptr, "pointer to 0x123456", "00003456", "0x3456");
103 DoTest("%Lp", ptr, "pointer to 0x123456", "00123456", "0x123456");
104 DoI64Test("%I64p", lptr, "pointer to 0x1234567887654321",
105 "1234567887654321", "0x1234567887654321");
106#endif
107
108 PAL_Terminate();
109 return PASS;
110}
111