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");
46 ok (memcmp (buffer
, buffer
+ 5, 5) == 0, "read wrong bytes");
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");
66 ok(fgets(buffer
,sizeof(buffer
),file
) !=0,"fgets failed unexpected");
67 ok(fgets(buffer
,sizeof(buffer
),file
) ==0,"fgets didn't signal EOF");
68 ok(feof(file
) !=0,"feof doesn't signal EOF");
70 ok(fgets(buffer
,strlen(outbuffer
),file
) !=0,"fgets failed unexpected");
71 ok(lstrlenA(buffer
) == strlen(outbuffer
) -1,"fgets didn't read right size");
72 ok(fgets(buffer
,sizeof(outbuffer
),file
) !=0,"fgets failed unexpected");
73 ok(strlen(buffer
) == 1,"fgets dropped chars");
74 ok(buffer
[0] == outbuffer
[strlen(outbuffer
)-1],"fgets exchanged chars");
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");
79 ok(fgetws(wbuffer
,sizeof(wbuffer
),file
) ==0,"fgetws didn't signal EOF");
80 ok(feof(file
) !=0,"feof doesn't signal EOF");
82 ok(fgetws(wbuffer
,strlen(outbuffer
),file
) !=0,"fgetws failed unexpected");
83 ok(lstrlenW(wbuffer
) == (strlen(outbuffer
) -1),"fgetws didn't read right size");
84 ok(fgetws(wbuffer
,sizeof(outbuffer
),file
) !=0,"fgets failed unexpected");
85 ok(lstrlenW(wbuffer
) == 1,"fgets dropped chars");
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");
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");
150 ok( btext
[strlen(mytext
)-1] == '\r', "CR not written");
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");
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");
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");
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
,_S_IREAD
| _S_IWRITE
)) != -1,"Can't open"); /* open in TEXT mode */
189 ok(_write(tempfd
,mytext
,strlen(mytext
)) == lstrlenA(mytext
), "_write _O_TEXT bad return value");
191 tempfd
= _open(tempf
,_O_RDONLY
|_O_BINARY
,0); /* open in BINARY mode */
192 ok(_read(tempfd
,btext
,LLEN
) == lstrlenA(dostext
), "_read _O_BINARY got bad length");
193 ok( memcmp(dostext
,btext
,strlen(dostext
)) == 0,"problems with _O_TEXT _write and _O_BINARY _write");
194 ok( btext
[strlen(dostext
)-2] == '\r', "CR not written");
196 tempfd
= _open(tempf
,_O_RDONLY
|_O_TEXT
); /* open in TEXT mode */
197 ok(_read(tempfd
,btext
,LLEN
) == lstrlenA(mytext
), "_read _O_TEXT got bad length");
198 ok( memcmp(mytext
,btext
,strlen(mytext
)) == 0,"problems with _O_TEXT _write / _write");
200 ok(unlink(tempf
) !=-1 ,"Can't unlink");
202 tempf
=_tempnam(".","wne");
203 ok((tempfd
= _open(tempf
,_O_CREAT
|_O_TRUNC
|_O_BINARY
|_O_RDWR
,0)) != -1,"Can't open %s",tempf
); /* open in BINARY mode */
204 ok(_write(tempfd
,dostext
,strlen(dostext
)) == lstrlenA(dostext
), "_write _O_TEXT bad return value");
206 tempfd
= _open(tempf
,_O_RDONLY
|_O_BINARY
,0); /* open in BINARY mode */
207 ok(_read(tempfd
,btext
,LLEN
) == lstrlenA(dostext
), "_read _O_BINARY got bad length");
208 ok( memcmp(dostext
,btext
,strlen(dostext
)) == 0,"problems with _O_TEXT _write and _O_BINARY _write");
209 ok( btext
[strlen(dostext
)-2] == '\r', "CR not written");
211 tempfd
= _open(tempf
,_O_RDONLY
|_O_TEXT
); /* open in TEXT mode */
212 ok(_read(tempfd
,btext
,LLEN
) == lstrlenA(mytext
), "_read _O_TEXT got bad length");
213 ok( memcmp(mytext
,btext
,strlen(mytext
)) == 0,"problems with _O_TEXT _write / _write");
219 static void test_tmpnam( void )
221 char name
[MAX_PATH
] = "abc";
225 ok(res
!= NULL
, "tmpnam returned NULL");
226 ok(res
[0] == '\\', "first character is not a backslash");
227 ok(strchr(res
+1, '\\') == 0, "file not in the root directory");
228 ok(res
[strlen(res
)-1] == '.', "first call - last character is not a dot");
231 ok(res
!= NULL
, "tmpnam returned NULL");
232 ok(res
== name
, "supplied buffer was not used");
233 ok(res
[0] == '\\', "first character is not a backslash");
234 ok(strchr(res
+1, '\\') == 0, "file not in the root directory");
235 ok(res
[strlen(res
)-1] != '.', "second call - last character is not a dot");
246 test_file_write_read();