filebuf_members.cc [plain text]
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ext/stdio_filebuf.h>
#include <testsuite_hooks.h>
const char name_01[] = "filebuf_members-1.tst";
const char name_02[] = "filebuf_members-1.txt";
void test_01()
{
bool test = true;
const char* name_03 = "filebuf_members-3";
std::filebuf fb_01; std::filebuf fb_02; std::filebuf fb_03;
VERIFY( !fb_01.is_open() );
VERIFY( !fb_02.is_open() );
VERIFY( !fb_03.is_open() );
fb_01.open(name_01, std::ios_base::in | std::ios_base::ate);
fb_02.open(name_02, std::ios_base::in | std::ios_base::out
| std::ios_base::trunc);
fb_02.open(name_03, std::ios_base::in | std::ios_base::out);
fb_03.open(name_03, std::ios_base::out | std::ios_base::trunc);
VERIFY( fb_01.is_open() );
VERIFY( fb_02.is_open() );
VERIFY( fb_03.is_open() );
fb_01.close();
fb_02.close();
fb_03.close();
VERIFY( !fb_01.is_open() );
VERIFY( !fb_02.is_open() );
VERIFY( !fb_03.is_open() );
}
void test_02()
{
bool test = true;
int close_num;
FILE* f2 = fopen(name_01, "r");
VERIFY( f2 != NULL );
{
__gnu_cxx::stdio_filebuf<char> fb(f2, std::ios_base::in, 512);
}
close_num = fclose(f2);
VERIFY( close_num == 0 );
FILE* f = fopen(name_01, "r");
VERIFY( f != NULL );
{
std::ifstream ifstream1(name_01);
VERIFY( ifstream1.is_open() );
std::ios_base::iostate st01 = ifstream1.rdstate();
VERIFY( st01 == std::ios_base::goodbit );
}
close_num = fclose(f);
VERIFY( close_num == 0 );
}
void test_03()
{
bool test = true;
int first_fd = ::open(name_01, O_RDONLY);
VERIFY( first_fd != -1 );
FILE* first_file = ::fdopen(first_fd, "r");
VERIFY( first_file != NULL );
__gnu_cxx::stdio_filebuf<char> fb(first_file, std::ios_base::in);
int second_fd = fb.fd();
VERIFY( first_fd == second_fd );
}
void
test_04()
{
signal(SIGPIPE, SIG_IGN);
if (0 != mkfifo("xxx", S_IRWXU))
{
std::cerr << "failed to creat fifo" << std::endl;
exit(-1);
}
int fval = fork();
if (fval == -1)
{
std::cerr << "failed to fork" << std::endl;
unlink("xxx");
exit(-1);
}
else if (fval == 0)
{
std::ifstream ifs("xxx");
sleep(1);
ifs.close();
exit(0);
}
std::ofstream ofs("xxx");
sleep(2);
ofs.put('t');
ofs.close();
if (!(ofs.rdstate() & std::ios::failbit))
{
std::cerr << "fail bit was not set!" << std::endl;
unlink("xxx");
exit(-1);
}
unlink("xxx");
}
void test_05()
{
bool test = true;
std::fstream scratch_file;
scratch_file.open("SCRATCH", std::ios::out);
scratch_file.close();
scratch_file.open("SCRATCH", std::ios::in);
if (!scratch_file)
VERIFY( false );
scratch_file.close();
}
void test_06()
{
bool test = true;
signal(SIGPIPE, SIG_IGN);
unlink("yyy");
mkfifo("yyy", S_IRWXU);
if (!fork())
{
std::filebuf fbuf;
fbuf.open("yyy", std::ios_base::in);
fbuf.sgetc();
fbuf.close();
exit(0);
}
std::filebuf fbuf;
std::filebuf* r =
fbuf.open("yyy", std::ios_base::out | std::ios_base::ate);
VERIFY( !fbuf.is_open() );
VERIFY( r == NULL );
}
int
main()
{
test_01();
test_02();
test_03();
test_04();
test_05();
test_06();
return 0;
}