Added test for tmpnam().
[wine/gsoc_dplay.git] / dlls / msvcrt / tests / file.c
blobd743053c4953bf9bc0852c8152ef24cca4271509
1 /*
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"
22 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <io.h>
28 #include <windef.h>
29 #include <winbase.h>
30 #include <winnls.h>
32 static void test_fdopen( void )
34 static char buffer[] = {0,1,2,3,4,5,6,7,8,9};
35 int fd;
36 FILE *file;
38 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
39 write (fd, buffer, sizeof (buffer));
40 close (fd);
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");
47 fclose (file);
48 unlink ("fdopen.tst");
51 static void test_fileops( void )
53 static char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
54 char buffer[256];
55 WCHAR wbuffer[256];
56 int fd;
57 FILE *file;
59 fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
60 write (fd, outbuffer, sizeof (outbuffer));
61 close (fd);
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");
69 rewind(file);
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");
75 fclose (file);
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");
81 rewind(file);
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");
86 fclose (file);
87 unlink ("fdopen.tst");
90 static WCHAR* AtoW( char* p )
92 WCHAR* buffer;
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 );
96 return buffer;
99 static void test_fgetwc( void )
101 #define LLEN 512
103 char* tempf;
104 FILE *tempfh;
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;
109 unsigned int i;
111 tempf=_tempnam(".","wne");
112 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
113 fputs(mytext,tempfh);
114 fclose(tempfh);
115 tempfh = fopen(tempf,"rt");
116 fgetws(wtextW,LLEN,tempfh);
117 mytextW = AtoW ((char*)mytext);
118 aptr = mytextW;
119 wptr = wtextW;
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);
127 fclose(tempfh);
128 unlink(tempf);
131 static void test_file_put_get( void )
133 char* tempf;
134 FILE *tempfh;
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";
137 char btext[LLEN];
138 WCHAR wtextW[LLEN+1];
139 WCHAR *mytextW = NULL, *aptr, *wptr;
140 BOOL diff_found = FALSE;
141 unsigned int i;
143 tempf=_tempnam(".","wne");
144 tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
145 fputs(mytext,tempfh);
146 fclose(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");
151 fclose(tempfh);
152 tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
153 fputs(dostext,tempfh);
154 fclose(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");
158 fclose(tempfh);
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");
163 fclose(tempfh);
164 tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
165 fgetws(wtextW,LLEN,tempfh);
166 mytextW = AtoW ((char*)mytext);
167 aptr = mytextW;
168 wptr = wtextW;
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);
176 fclose(tempfh);
177 unlink(tempf);
179 static void test_file_write_read( void )
181 char* tempf;
182 int tempfd;
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";
185 char btext[LLEN];
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");
190 _close(tempfd);
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");
195 _close(tempfd);
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");
199 _close(tempfd);
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");
205 _close(tempfd);
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");
210 _close(tempfd);
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");
214 _close(tempfd);
216 unlink(tempf);
219 static void test_tmpnam( void )
221 char name[MAX_PATH] = "abc";
222 char *res;
224 res = tmpnam(NULL);
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");
230 res = tmpnam(name);
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");
240 START_TEST(file)
242 test_fdopen();
243 test_fileops();
244 test_fgetwc();
245 test_file_put_get();
246 test_file_write_read();
247 test_tmpnam();