VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstFile.cpp@ 4071

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

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.0 KB
Line 
1/* $Id: tstFile.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime Testcase - File I/O.
4 */
5
6/*
7 * Copyright (C) 2006-2007 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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <iprt/file.h>
23#include <iprt/err.h>
24#include <iprt/string.h>
25#include <iprt/stream.h>
26#include <iprt/runtime.h>
27
28
29int main()
30{
31 int cErrors = 0;
32 RTPrintf("tstFile: TESTING\n");
33 RTR3Init();
34
35 RTFILE File;
36 int rc = RTFileOpen(&File, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE);
37 if (RT_FAILURE(rc))
38 {
39 RTPrintf("tstFile: FATAL ERROR - Failed to open file #1. rc=%Rrc\n", rc);
40 return 1;
41 }
42
43 /* grow file beyond 2G */
44 rc = RTFileSetSize(File, _2G + _1M);
45 if (RT_FAILURE(rc))
46 {
47 RTPrintf("Failed to grow file #1 to 2.001GB. rc=%Rrc\n", rc);
48 cErrors++;
49 }
50 else
51 {
52 uint64_t cb;
53 rc = RTFileGetSize(File, &cb);
54 if (RT_FAILURE(rc))
55 {
56 RTPrintf("Failed to get file size of #1. rc=%Rrc\n", rc);
57 cErrors++;
58 }
59 else if (cb != _2G + _1M)
60 {
61 RTPrintf("RTFileGetSize return %RX64 bytes, expected %RX64.\n", cb, _2G + _1M);
62 cErrors++;
63 }
64 else
65 RTPrintf("tstFile: cb=%RX64\n", cb);
66
67 /*
68 * Try some writes at the beginning of the file.
69 */
70 uint64_t offFile = RTFileTell(File);
71 if (offFile != 0)
72 {
73 RTPrintf("RTFileTell -> %#RX64, expected 0 (#1)\n", offFile);
74 cErrors++;
75 }
76 static const char szTestBuf[] = "Sausages and bacon for breakfast again!";
77 unsigned cbWritten = 0;
78 while (cbWritten < sizeof(szTestBuf))
79 {
80 unsigned cbWrittenPart;
81 rc = RTFileWrite(File, &szTestBuf[cbWritten], sizeof(szTestBuf) - cbWritten, &cbWrittenPart);
82 if (RT_FAILURE(rc))
83 break;
84 cbWritten += cbWrittenPart;
85 }
86 if (RT_FAILURE(rc))
87 {
88 RTPrintf("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc);
89 cErrors++;
90 }
91 else
92 {
93 /* check that it was written correctly. */
94 rc = RTFileSeek(File, 0, RTFILE_SEEK_BEGIN, NULL);
95 if (RT_FAILURE(rc))
96 {
97 RTPrintf("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc);
98 cErrors++;
99 }
100 else
101 {
102 char szReadBuf[sizeof(szTestBuf)];
103 unsigned cbRead = 0;
104 while (cbRead < sizeof(szTestBuf))
105 {
106 unsigned cbReadPart;
107 rc = RTFileRead(File, &szReadBuf[cbRead], sizeof(szTestBuf) - cbRead, &cbReadPart);
108 if (RT_FAILURE(rc))
109 break;
110 cbRead += cbReadPart;
111 }
112 if (RT_FAILURE(rc))
113 {
114 RTPrintf("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc);
115 cErrors++;
116 }
117 else
118 {
119 if (!memcmp(szReadBuf, szTestBuf, sizeof(szTestBuf)))
120 RTPrintf("tstFile: head write ok\n");
121 else
122 {
123 RTPrintf("Data read from file #1 at offset 0 differs from what we wrote there.\n");
124 cErrors++;
125 }
126 }
127 }
128 }
129
130 /*
131 * Try some writes at the end of the file.
132 */
133 rc = RTFileSeek(File, _2G + _1M, RTFILE_SEEK_BEGIN, NULL);
134 if (RT_FAILURE(rc))
135 {
136 RTPrintf("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc);
137 cErrors++;
138 }
139 else
140 {
141 offFile = RTFileTell(File);
142 if (offFile != _2G + _1M)
143 {
144 RTPrintf("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M);
145 cErrors++;
146 }
147 else
148 {
149 unsigned cbWritten = 0;
150 while (cbWritten < sizeof(szTestBuf))
151 {
152 unsigned cbWrittenPart;
153 rc = RTFileWrite(File, &szTestBuf[cbWritten], sizeof(szTestBuf) - cbWritten, &cbWrittenPart);
154 if (RT_FAILURE(rc))
155 break;
156 cbWritten += cbWrittenPart;
157 }
158 if (RT_FAILURE(rc))
159 {
160 RTPrintf("Failed to write to file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
161 cErrors++;
162 }
163 else
164 {
165 rc = RTFileSeek(File, offFile, RTFILE_SEEK_BEGIN, NULL);
166 if (RT_FAILURE(rc))
167 {
168 RTPrintf("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc);
169 cErrors++;
170 }
171 else
172 {
173 char szReadBuf[sizeof(szTestBuf)];
174 unsigned cbRead = 0;
175 while (cbRead < sizeof(szTestBuf))
176 {
177 unsigned cbReadPart;
178 rc = RTFileRead(File, &szReadBuf[cbRead], sizeof(szTestBuf) - cbRead, &cbReadPart);
179 if (RT_FAILURE(rc))
180 break;
181 cbRead += cbReadPart;
182 }
183 if (RT_FAILURE(rc))
184 {
185 RTPrintf("Failed to read from file #1 at offset 2G + 1M. rc=%Rrc\n", rc);
186 cErrors++;
187 }
188 else
189 {
190 if (!memcmp(szReadBuf, szTestBuf, sizeof(szTestBuf)))
191 RTPrintf("tstFile: tail write ok\n");
192 else
193 {
194 RTPrintf("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n");
195 cErrors++;
196 }
197 }
198 }
199 }
200 }
201 }
202
203 /*
204 * Some general seeking around.
205 */
206 rc = RTFileSeek(File, _2G + 1, RTFILE_SEEK_BEGIN, NULL);
207 if (RT_FAILURE(rc))
208 {
209 RTPrintf("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc);
210 cErrors++;
211 }
212 else
213 {
214 offFile = RTFileTell(File);
215 if (offFile != _2G + 1)
216 {
217 RTPrintf("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1);
218 cErrors++;
219 }
220 }
221
222 /* seek end */
223 rc = RTFileSeek(File, 0, RTFILE_SEEK_END, NULL);
224 if (RT_FAILURE(rc))
225 {
226 RTPrintf("Failed to seek to end of file #1. rc=%Rrc\n", rc);
227 cErrors++;
228 }
229 else
230 {
231 offFile = RTFileTell(File);
232 if (offFile != _2G + _1M + sizeof(szTestBuf)) /* assuming tail write was ok. */
233 {
234 RTPrintf("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(szTestBuf));
235 cErrors++;
236 }
237 }
238
239 /* seek start */
240 rc = RTFileSeek(File, 0, RTFILE_SEEK_BEGIN, NULL);
241 if (RT_FAILURE(rc))
242 {
243 RTPrintf("Failed to seek to end of file #1. rc=%Rrc\n", rc);
244 cErrors++;
245 }
246 else
247 {
248 offFile = RTFileTell(File);
249 if (offFile != 0)
250 {
251 RTPrintf("RTFileTell -> %#llx, expected 0 (#5)\n", offFile);
252 cErrors++;
253 }
254 }
255 }
256
257
258 /*
259 * Cleanup.
260 */
261 rc = RTFileClose(File);
262 if (RT_FAILURE(rc))
263 {
264 RTPrintf("Failed to close file #1. rc=%Rrc\n", rc);
265 cErrors++;
266 }
267 rc = RTFileDelete("tstFile#1.tst");
268 if (RT_FAILURE(rc))
269 {
270 RTPrintf("Failed to delete file #1. rc=%Rrc\n", rc);
271 cErrors++;
272 }
273
274 /*
275 * Summary
276 */
277 if (cErrors == 0)
278 RTPrintf("tstFile: SUCCESS\n");
279 else
280 RTPrintf("tstFile: FAILURE - %d errors\n", cErrors);
281 return !!cErrors;
282}
283
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