2 * Unit test suite for file functions
4 * Copyright 2002 Bill Currie
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "wine/test.h"
32 static void test_fdopen( void )
34 static char buffer[] = {0,1,2,3,4,5,6,7,8,9};
38 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
39 write (fd, buffer, sizeof (buffer));
42 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
43 lseek (fd, 5, SEEK_SET);
44 file = fdopen (fd, "rb");
45 ok (fread (buffer, 1, sizeof (buffer), file) == 5, "read wrong byte count\n");
46 ok (memcmp (buffer, buffer + 5, 5) == 0, "read wrong bytes\n");
48 unlink ("fdopen.tst");
51 static void test_fileops( void )
53 static char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
59 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
60 write (fd, outbuffer, sizeof (outbuffer));
63 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
64 file = fdopen (fd, "rb");
65 ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
66 ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n");
67 ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
68 ok(feof(file) !=0,"feof doesn't signal EOF\n");
70 ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n");
71 ok(lstrlenA(buffer) == strlen(outbuffer) -1,"fgets didn't read right size\n");
72 ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
73 ok(strlen(buffer) == 1,"fgets dropped chars\n");
74 ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n");
76 fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
77 file = fdopen (fd, "rt"); /* open in TEXT mode */
78 ok(fgetws(wbuffer,sizeof(wbuffer),file) !=0,"fgetws failed unexpected\n");
79 ok(fgetws(wbuffer,sizeof(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
80 ok(feof(file) !=0,"feof doesn't signal EOF\n");
82 ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
83 ok(lstrlenW(wbuffer) == (strlen(outbuffer) -1),"fgetws didn't read right size\n");
84 ok(fgetws(wbuffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
85 ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
87 unlink ("fdopen.tst");
90 static WCHAR* AtoW( char* p )
93 DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
94 buffer = malloc( len * sizeof(WCHAR) );
95 MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
99 static void test_fgetwc( void )
105 const char mytext[]= "This is test_fgetwc\n";
106 WCHAR wtextW[LLEN+1];
107 WCHAR *mytextW = NULL, *aptr, *wptr;
108 BOOL diff_found = FALSE;
111 tempf=_tempnam(".","wne");
112 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
113 fputs(mytext,tempfh);
115 tempfh = fopen(tempf,"rt");
116 fgetws(wtextW,LLEN,tempfh);
117 mytextW = AtoW ((char*)mytext);
121 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
123 diff_found |= (*aptr != *wptr);
125 ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
126 if(mytextW) free (mytextW);
131 static void test_file_put_get( void )
135 const char mytext[]= "This is a test_file_put_get\n";
136 const char dostext[]= "This is a test_file_put_get\r\n";
138 WCHAR wtextW[LLEN+1];
139 WCHAR *mytextW = NULL, *aptr, *wptr;
140 BOOL diff_found = FALSE;
143 tempf=_tempnam(".","wne");
144 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
145 fputs(mytext,tempfh);
147 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
148 fgets(btext,LLEN,tempfh);
149 ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
150 ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
152 tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
153 fputs(dostext,tempfh);
155 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
156 fgets(btext,LLEN,tempfh);
157 ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
159 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
160 fgets(btext,LLEN,tempfh);
161 ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
164 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
165 fgetws(wtextW,LLEN,tempfh);
166 mytextW = AtoW ((char*)mytext);
170 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
172 diff_found |= (*aptr != *wptr);
174 ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
175 if(mytextW) free (mytextW);
179 static void test_file_write_read( void )
183 const char mytext[]= "This is test_file_write_read\nsecond line\n";
184 const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
187 tempf=_tempnam(".","wne");
188 ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
189 _S_IREAD | _S_IWRITE)) != -1,
190 "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
191 ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
192 "_write _O_TEXT bad return value\n");
194 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
195 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
196 "_read _O_BINARY got bad length\n");
197 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
198 "problems with _O_TEXT _write / _O_BINARY _read\n");
199 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
201 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
202 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
203 "_read _O_TEXT got bad length\n");
204 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
205 "problems with _O_TEXT _write / _read\n");
207 ok(unlink(tempf) !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
209 tempf=_tempnam(".","wne");
210 ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0)) != -1,
211 "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
212 ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
213 "_write _O_BINARY bad return value\n");
215 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
216 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
217 "_read _O_BINARY got bad length\n");
218 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
219 "problems with _O_BINARY _write / _read\n");
220 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
222 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
223 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
224 "_read _O_TEXT got bad length\n");
225 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
226 "problems with _O_BINARY _write / _O_TEXT _read\n");
229 ok(_chmod (tempf, _S_IREAD | _S_IWRITE) == 0,
230 "Can't chmod '%s' to read-write: %d\n", tempf, errno);
231 ok(unlink(tempf) !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
234 static void test_tmpnam( void )
236 char name[MAX_PATH] = "abc";
240 ok(res != NULL, "tmpnam returned NULL\n");
241 ok(res[0] == '\\', "first character is not a backslash\n");
242 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
243 ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
246 ok(res != NULL, "tmpnam returned NULL\n");
247 ok(res == name, "supplied buffer was not used\n");
248 ok(res[0] == '\\', "first character is not a backslash\n");
249 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
250 ok(res[strlen(res)-1] != '.', "second call - last character is not a dot\n");
261 test_file_write_read();