Fixed definitions of TTTOOLINFOA/W_V1_SIZE and
[wine] / dlls / msvcrt / tests / file.c
1 /*
2  * Unit test suite for file functions
3  *
4  * Copyright 2002 Bill Currie
5  *
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.
10  *
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.
15  *
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
19  */
20
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>
31
32 static void test_fdopen( void )
33 {
34     static char buffer[] = {0,1,2,3,4,5,6,7,8,9};
35     int fd;
36     FILE *file;
37
38     fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
39     write (fd, buffer, sizeof (buffer));
40     close (fd);
41
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");
49 }
50
51 static WCHAR* AtoW( char* p )
52 {
53     WCHAR* buffer;
54     DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
55     buffer = malloc( len * sizeof(WCHAR) );
56     MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
57     return buffer;
58 }
59
60 static void test_fgetwc( void )
61 {
62 #define LLEN 512
63
64   char* tempf;
65   FILE *tempfh;
66   const char mytext[]= "This is test_fgetwc\n";
67   WCHAR wtextW[LLEN+1];
68   WCHAR *mytextW = NULL, *aptr, *wptr;
69   BOOL diff_found = FALSE;
70   unsigned int i;
71   
72   tempf=_tempnam(".","wne");
73   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
74   fputs(mytext,tempfh);
75   fclose(tempfh);
76   tempfh = fopen(tempf,"rt");
77   fgetws(wtextW,LLEN,tempfh);
78   mytextW = AtoW ((char*)mytext);
79   aptr = mytextW;
80   wptr = wtextW;
81
82   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
83     {
84       diff_found |= (*aptr != *wptr);
85     }
86   ok(!(diff_found), "fgetwc difference found in TEXT mode");
87   if(mytextW) free (mytextW);
88   fclose(tempfh);
89   unlink(tempf);
90 }
91   
92 static void test_file_put_get( void )
93 {
94   char* tempf;
95   FILE *tempfh;
96   const char mytext[]=  "This is a test_file_put_get\n";
97   const char dostext[]= "This is a test_file_put_get\r\n";
98   char btext[LLEN];
99   WCHAR wtextW[LLEN+1];
100   WCHAR *mytextW = NULL, *aptr, *wptr;
101   BOOL diff_found = FALSE;
102   unsigned int i;
103
104   tempf=_tempnam(".","wne");
105   tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
106   fputs(mytext,tempfh);
107   fclose(tempfh);
108   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
109   fgets(btext,LLEN,tempfh);
110   ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write");
111   ok( btext[strlen(mytext)-1] == '\r', "CR not written");
112   fclose(tempfh);
113   tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
114   fputs(dostext,tempfh);
115   fclose(tempfh);
116   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
117   fgets(btext,LLEN,tempfh);
118   ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR");
119   fclose(tempfh);
120   tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
121   fgets(btext,LLEN,tempfh);
122   ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR");
123
124   fclose(tempfh);
125   tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
126   fgetws(wtextW,LLEN,tempfh);
127   mytextW = AtoW ((char*)mytext);
128   aptr = mytextW;
129   wptr = wtextW;
130
131   for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
132     {
133       diff_found |= (*aptr != *wptr);
134     }
135   ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode");
136   if(mytextW) free (mytextW);
137   fclose(tempfh);
138   unlink(tempf);
139 }
140 static void test_file_write_read( void )
141 {
142   char* tempf;
143   int tempfd;
144   const char mytext[]=  "This is test_file_write_read\nsecond line\n";
145   const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
146   char btext[LLEN];
147
148   tempf=_tempnam(".","wne");
149   ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,_S_IREAD | _S_IWRITE)) != -1,"Can't open"); /* open in TEXT mode */
150   ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext), "_write _O_TEXT bad return value");
151   _close(tempfd);
152   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
153   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext), "_read _O_BINARY got bad length");
154   ok( memcmp(dostext,btext,strlen(dostext)) == 0,"problems with _O_TEXT _write and _O_BINARY _write");
155   ok( btext[strlen(dostext)-2] == '\r', "CR not written");
156   _close(tempfd);
157   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
158   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext), "_read _O_TEXT got bad length");
159   ok( memcmp(mytext,btext,strlen(mytext)) == 0,"problems with _O_TEXT _write / _write");
160   _close(tempfd);
161   ok(unlink(tempf) !=-1 ,"Can't unlink");
162
163   tempf=_tempnam(".","wne");
164   ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0)) != -1,"Can't open %s",tempf); /* open in BINARY mode */
165   ok(_write(tempfd,dostext,strlen(dostext)) == lstrlenA(dostext), "_write _O_TEXT bad return value");
166   _close(tempfd);
167   tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
168   ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext), "_read _O_BINARY got bad length");
169   ok( memcmp(dostext,btext,strlen(dostext)) == 0,"problems with _O_TEXT _write and _O_BINARY _write");
170   ok( btext[strlen(dostext)-2] == '\r', "CR not written");
171   _close(tempfd);
172   tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
173   ok(_read(tempfd,btext,LLEN) == lstrlenA(mytext), "_read _O_TEXT got bad length");
174   ok( memcmp(mytext,btext,strlen(mytext)) == 0,"problems with _O_TEXT _write / _write");
175   _close(tempfd);
176
177   unlink(tempf);
178 }
179
180
181 START_TEST(file)
182 {
183     test_fdopen();
184     test_fgetwc();
185     test_file_put_get();
186     test_file_write_read();
187 }