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: test5.c (fprintf) |
8 | ** |
9 | ** Purpose: Tests the count specifier (%n). |
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 | |
22 | static void DoTest(char *formatstr, int param, char *checkstr) |
23 | { |
24 | FILE *fp; |
25 | char buf[256] = { 0 }; |
26 | int n = -1; |
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, &n)) < 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 (n != param) |
49 | { |
50 | Fail("ERROR: Expected count parameter to resolve to %d, got %X\n" , |
51 | param, n); |
52 | } |
53 | |
54 | if (memcmp(buf, checkstr, strlen(buf) + 1) != 0) |
55 | { |
56 | Fail("ERROR: Expected \"%s\" got \"%s\".\n" , checkstr, buf); |
57 | } |
58 | |
59 | |
60 | if ((fclose( fp )) != 0) |
61 | |
62 | { |
63 | |
64 | Fail("ERROR: fclose failed to close \"testfile.txt\"\n" ); |
65 | |
66 | } |
67 | } |
68 | |
69 | static void DoShortTest(char *formatstr, int param, char *checkstr) |
70 | { |
71 | FILE *fp; |
72 | char buf[256] = { 0 }; |
73 | short int n = -1; |
74 | |
75 | if ((fp = fopen("testfile.txt" , "w+" )) == NULL ) |
76 | { |
77 | Fail("ERROR: fopen failed to create testfile\n" ); |
78 | } |
79 | |
80 | if ((fprintf(fp, formatstr, &n)) < 0) |
81 | { |
82 | Fail("ERROR: fprintf failed\n" ); |
83 | } |
84 | |
85 | if ((fseek(fp, 0, SEEK_SET)) != 0) |
86 | { |
87 | Fail("ERROR: fseek failed\n" ); |
88 | } |
89 | |
90 | if ((fgets(buf, 100, fp)) == NULL) |
91 | { |
92 | Fail("ERROR: fseek failed\n" ); |
93 | } |
94 | |
95 | if (n != param) |
96 | { |
97 | Fail("ERROR: Expected count parameter to resolve to %d, got %X\n" , |
98 | param, n); |
99 | } |
100 | |
101 | if (memcmp(buf, checkstr, strlen(buf) + 1) != 0) |
102 | { |
103 | Fail("ERROR: Expected \"%s\" got \"%s\".\n" , checkstr, buf); |
104 | } |
105 | |
106 | if ((fclose( fp )) != 0) |
107 | { |
108 | Fail("ERROR: fclose failed to close \"testfile.txt\"\n" ); |
109 | } |
110 | } |
111 | |
112 | int __cdecl main(int argc, char *argv[]) |
113 | { |
114 | if (PAL_Initialize(argc, argv) != 0) |
115 | return(FAIL); |
116 | |
117 | DoTest("foo %n bar" , 4, "foo bar" ); |
118 | DoTest("foo %#n bar" , 4, "foo bar" ); |
119 | DoTest("foo % n bar" , 4, "foo bar" ); |
120 | DoTest("foo %+n bar" , 4, "foo bar" ); |
121 | DoTest("foo %-n bar" , 4, "foo bar" ); |
122 | DoTest("foo %0n bar" , 4, "foo bar" ); |
123 | DoShortTest("foo %hn bar" , 4, "foo bar" ); |
124 | DoTest("foo %ln bar" , 4, "foo bar" ); |
125 | DoTest("foo %Ln bar" , 4, "foo bar" ); |
126 | DoTest("foo %I64n bar" , 4, "foo bar" ); |
127 | DoTest("foo %20.3n bar" , 4, "foo bar" ); |
128 | |
129 | PAL_Terminate(); |
130 | return PASS; |
131 | } |
132 | |