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