📄 file.c
字号:
/*
* Unit test suite for file functions
*
* Copyright 2002 Bill Currie
* Copyright 2005 Paul Rupe
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "wine/test.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
#include <windef.h>
#include <winbase.h>
#include <winnls.h>
#include <process.h>
#include <errno.h>
static void test_fdopen( void )
{
static const char buffer[] = {0,1,2,3,4,5,6,7,8,9};
char ibuf[10];
int fd;
FILE *file;
fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
write (fd, buffer, sizeof (buffer));
close (fd);
fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
lseek (fd, 5, SEEK_SET);
file = fdopen (fd, "rb");
ok (fread (ibuf, 1, sizeof (buffer), file) == 5, "read wrong byte count\n");
ok (memcmp (ibuf, buffer + 5, 5) == 0, "read wrong bytes\n");
fclose (file);
unlink ("fdopen.tst");
}
static void test_fileops( void )
{
static const char outbuffer[] = "0,1,2,3,4,5,6,7,8,9";
char buffer[256];
WCHAR wbuffer[256];
int fd;
FILE *file;
fpos_t pos;
int i, c;
fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
write (fd, outbuffer, sizeof (outbuffer));
close (fd);
fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
file = fdopen (fd, "rb");
ok(strlen(outbuffer) == (sizeof(outbuffer)-1),"strlen/sizeof error\n");
ok(fgets(buffer,sizeof(buffer),file) !=0,"fgets failed unexpected\n");
ok(fgets(buffer,sizeof(buffer),file) ==0,"fgets didn't signal EOF\n");
ok(feof(file) !=0,"feof doesn't signal EOF\n");
rewind(file);
ok(fgets(buffer,strlen(outbuffer),file) !=0,"fgets failed unexpected\n");
ok(lstrlenA(buffer) == lstrlenA(outbuffer) -1,"fgets didn't read right size\n");
ok(fgets(buffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
ok(strlen(buffer) == 1,"fgets dropped chars\n");
ok(buffer[0] == outbuffer[strlen(outbuffer)-1],"fgets exchanged chars\n");
rewind(file);
for (i = 0, c = EOF; i < sizeof(outbuffer); i++)
{
ok((c = fgetc(file)) == outbuffer[i], "fgetc returned wrong data\n");
}
ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
ok(feof(file), "feof did not return EOF\n");
ok(ungetc(c, file) == EOF, "ungetc(EOF) did not return EOF\n");
ok(feof(file), "feof after ungetc(EOF) did not return EOF\n");
ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
c = outbuffer[sizeof(outbuffer) - 1];
ok(ungetc(c, file) == c, "ungetc did not return its input\n");
ok(!feof(file), "feof after ungetc returned EOF\n");
ok((c = fgetc(file)) != EOF, "getc after ungetc returned EOF\n");
ok(c == outbuffer[sizeof(outbuffer) - 1],
"getc did not return ungetc'd data\n");
ok(!feof(file), "feof after getc returned EOF prematurely\n");
ok((c = fgetc(file)) == EOF, "getc did not return EOF\n");
ok(feof(file), "feof after getc did not return EOF\n");
rewind(file);
ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
ok(pos == 0, "Unexpected result of fgetpos 0x%Lx\n", pos);
pos = (ULONGLONG)sizeof (outbuffer);
ok(fsetpos(file, &pos) == 0, "fsetpos failed unexpected\n");
ok(fgetpos(file,&pos) == 0, "fgetpos failed unexpected\n");
ok(pos == (ULONGLONG)sizeof (outbuffer), "Unexpected result of fgetpos 0x%Lx\n", pos);
fclose (file);
fd = open ("fdopen.tst", O_RDONLY | O_TEXT);
file = fdopen (fd, "rt"); /* open in TEXT mode */
ok(fgetws(wbuffer,sizeof(wbuffer),file) !=0,"fgetws failed unexpected\n");
ok(fgetws(wbuffer,sizeof(wbuffer),file) ==0,"fgetws didn't signal EOF\n");
ok(feof(file) !=0,"feof doesn't signal EOF\n");
rewind(file);
ok(fgetws(wbuffer,strlen(outbuffer),file) !=0,"fgetws failed unexpected\n");
ok(lstrlenW(wbuffer) == (lstrlenA(outbuffer) -1),"fgetws didn't read right size\n");
ok(fgetws(wbuffer,sizeof(outbuffer),file) !=0,"fgets failed unexpected\n");
ok(lstrlenW(wbuffer) == 1,"fgets dropped chars\n");
fclose (file);
file = fopen("fdopen.tst", "rb");
ok( file != NULL, "fopen failed\n");
/* sizeof(buffer) > content of file */
ok(fread(buffer, sizeof(buffer), 1, file) == 0, "fread test failed\n");
/* feof should be set now */
ok(feof(file), "feof after fread failed\n");
fclose (file);
unlink ("fdopen.tst");
}
static WCHAR* AtoW( char* p )
{
WCHAR* buffer;
DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
buffer = malloc( len * sizeof(WCHAR) );
MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
return buffer;
}
static void test_fgetwc( void )
{
#define LLEN 512
char* tempf;
FILE *tempfh;
static const char mytext[]= "This is test_fgetwc\n";
WCHAR wtextW[LLEN+1];
WCHAR *mytextW = NULL, *aptr, *wptr;
BOOL diff_found = FALSE;
unsigned int i;
tempf=_tempnam(".","wne");
tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
fputs(mytext,tempfh);
fclose(tempfh);
tempfh = fopen(tempf,"rt");
fgetws(wtextW,LLEN,tempfh);
mytextW = AtoW ((char*)mytext);
aptr = mytextW;
wptr = wtextW;
for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
{
diff_found |= (*aptr != *wptr);
}
ok(!(diff_found), "fgetwc difference found in TEXT mode\n");
if(mytextW) free (mytextW);
fclose(tempfh);
unlink(tempf);
}
static void test_file_put_get( void )
{
char* tempf;
FILE *tempfh;
static const char mytext[]= "This is a test_file_put_get\n";
static const char dostext[]= "This is a test_file_put_get\r\n";
char btext[LLEN];
WCHAR wtextW[LLEN+1];
WCHAR *mytextW = NULL, *aptr, *wptr;
BOOL diff_found = FALSE;
unsigned int i;
tempf=_tempnam(".","wne");
tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
fputs(mytext,tempfh);
fclose(tempfh);
tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
fgets(btext,LLEN,tempfh);
ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write\n");
ok( btext[strlen(mytext)-1] == '\r', "CR not written\n");
fclose(tempfh);
tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
fputs(dostext,tempfh);
fclose(tempfh);
tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
fgets(btext,LLEN,tempfh);
ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR\n");
fclose(tempfh);
tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
fgets(btext,LLEN,tempfh);
ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR\n");
fclose(tempfh);
tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
fgetws(wtextW,LLEN,tempfh);
mytextW = AtoW ((char*)mytext);
aptr = mytextW;
wptr = wtextW;
for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
{
diff_found |= (*aptr != *wptr);
}
ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode\n");
if(mytextW) free (mytextW);
fclose(tempfh);
unlink(tempf);
}
static void test_file_write_read( void )
{
char* tempf;
int tempfd;
static const char mytext[]= "This is test_file_write_read\nsecond line\n";
static const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
char btext[LLEN];
int ret;
tempf=_tempnam(".","wne");
tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,
_S_IREAD | _S_IWRITE);
ok( tempfd != -1,
"Can't open '%s': %d\n", tempf, errno); /* open in TEXT mode */
ok(_write(tempfd,mytext,strlen(mytext)) == lstrlenA(mytext),
"_write _O_TEXT bad return value\n");
_close(tempfd);
tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
ok(_read(tempfd,btext,LLEN) == lstrlenA(dostext),
"_read _O_BINARY got bad length\n");
ok( memcmp(dostext,btext,strlen(dostext)) == 0,
"problems with _O_TEXT _write / _O_BINARY _read\n");
ok( btext[strlen(dostext)-2] == '\r', "CR not written or read\n");
_close(tempfd);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -