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