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 const char buffer[] = {0,1,2,3,4,5,6,7,8,9};
39 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
40 write (fd, buffer, sizeof (buffer));
43 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
44 lseek (fd, 5, SEEK_SET);
45 file = fdopen (fd, "rb");
46 ok (fread (ibuf, 1, sizeof (buffer), file) == 5, "read wrong byte count\n");
47 ok (memcmp (ibuf, buffer + 5, 5) == 0, "read wrong bytes\n");
49 unlink ("fdopen.tst");
52 static void test_fileops( void )
54 static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
60 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
61 write (fd, outbuffer, sizeof (outbuffer));
64 fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
65 file = fdopen (fd, "rb");
66 ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
67 ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n");
68 ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
69 ok(feof(file) !=0,"feof doesn't signal EOF\n");
71 ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n");
72 ok(lstrlenA(buffer) == strlen(outbuffer) -1,"fgets didn't read right size\n");
73 ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
74 ok(strlen(buffer) == 1,"fgets dropped chars\n");
75 ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n");
77 fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
78 file = fdopen (fd, "rt"); /* open in TEXT mode */
79 ok(fgetws(wbuffer,sizeof(wbuffer),file) !=0,"fgetws failed unexpected\n");
80 ok(fgetws(wbuffer,sizeof(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
81 ok(feof(file) !=0,"feof doesn't signal EOF\n");
83 ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
84 ok(lstrlenW(wbuffer) == (strlen(outbuffer) -1),"fgetws didn't read right size\n");
85 ok(fgetws(wbuffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
86 ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
88 unlink ("fdopen.tst");
91 static WCHAR* AtoW( char* p )
94 DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
95 buffer = malloc( len * sizeof(WCHAR) );
96 MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
100 static void test_fgetwc( void )
106 static const char mytext[]= "This is test_fgetwc\n";
107 WCHAR wtextW[LLEN+1];
108 WCHAR *mytextW = NULL, *aptr, *wptr;
109 BOOL diff_found = FALSE;
112 tempf=_tempnam(".","wne");
113 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
114 fputs(mytext,tempfh);
116 tempfh = fopen(tempf,"rt");
117 fgetws(wtextW,LLEN,tempfh);
118 mytextW = AtoW ((char*)mytext);
122 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
124 diff_found |= (*aptr != *wptr);
126 ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
127 if(mytextW) free (mytextW);
132 static void test_file_put_get( void )
136 static const char mytext[]= "This is a test_file_put_get\n";
137 static const char dostext[]= "This is a test_file_put_get\r\n";
139 WCHAR wtextW[LLEN+1];
140 WCHAR *mytextW = NULL, *aptr, *wptr;
141 BOOL diff_found = FALSE;
144 tempf=_tempnam(".","wne");
145 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
146 fputs(mytext,tempfh);
148 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
149 fgets(btext,LLEN,tempfh);
150 ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
151 ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
153 tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
154 fputs(dostext,tempfh);
156 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
157 fgets(btext,LLEN,tempfh);
158 ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
160 tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
161 fgets(btext,LLEN,tempfh);
162 ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
165 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
166 fgetws(wtextW,LLEN,tempfh);
167 mytextW = AtoW ((char*)mytext);
171 for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
173 diff_found |= (*aptr != *wptr);
175 ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
176 if(mytextW) free (mytextW);
181 static void test_file_write_read( void )
185 static const char mytext[]= "This is test_file_write_read\nsecond line\n";
186 static const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
189 tempf=_tempnam(".","wne");
190 ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
191 _S_IREAD | _S_IWRITE)) != -1,
192 "Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
193 ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
194 "_write _O_TEXT bad return value\n");
196 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
197 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
198 "_read _O_BINARY got bad length\n");
199 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
200 "problems with _O_TEXT _write / _O_BINARY _read\n");
201 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
203 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
204 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
205 "_read _O_TEXT got bad length\n");
206 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
207 "problems with _O_TEXT _write / _read\n");
209 ok(unlink(tempf) !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
211 tempf=_tempnam(".","wne");
212 ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0)) != -1,
213 "Can't open '%s': %d\n", tempf, errno); /* open in BINARY mode */
214 ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext),
215 "_write _O_BINARY bad return value\n");
217 tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
218 ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
219 "_read _O_BINARY got bad length\n");
220 ok( memcmp(dostext,btext,strlen(dostext)) == 0,
221 "problems with _O_BINARY _write / _read\n");
222 ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
224 tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
225 ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext),
226 "_read _O_TEXT got bad length\n");
227 ok( memcmp(mytext,btext,strlen(mytext)) == 0,
228 "problems with _O_BINARY _write / _O_TEXT _read\n");
231 ok(_chmod (tempf, _S_IREAD | _S_IWRITE) == 0,
232 "Can't chmod '%s' to read-write: %d\n", tempf, errno);
233 ok(unlink(tempf) !=-1 ,"Can't unlink '%s': %d\n", tempf, errno);
236 static void test_tmpnam( void )
238 char name[MAX_PATH] = "abc";
242 ok(res != NULL, "tmpnam returned NULL\n");
243 ok(res[0] == '\\', "first character is not a backslash\n");
244 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
245 ok(res[strlen(res)-1] == '.', "first call - last character is not a dot\n");
248 ok(res != NULL, "tmpnam returned NULL\n");
249 ok(res == name, "supplied buffer was not used\n");
250 ok(res[0] == '\\', "first character is not a backslash\n");
251 ok(strchr(res+1, '\\') == 0, "file not in the root directory\n");
252 ok(res[strlen(res)-1] != '.', "second call - last character is not a dot\n");
263 test_file_write_read();