45 lines
1.4 KiB
C++
45 lines
1.4 KiB
C++
![]() |
#include <mrw/auto.hpp>
|
||
|
#include <cppunit/TestFixture.h>
|
||
|
#include <cppunit/ui/text/TestRunner.h>
|
||
|
#include <cppunit/extensions/HelperMacros.h>
|
||
|
#include <cppunit/extensions/TestFactoryRegistry.h>
|
||
|
#include <fcntl.h> // open
|
||
|
|
||
|
class AutoTest: public CppUnit::TestFixture {
|
||
|
public:
|
||
|
void AutoFile() {
|
||
|
char c(0);
|
||
|
int i(-1);
|
||
|
{
|
||
|
mrw::AutoFile a;
|
||
|
CPPUNIT_ASSERT(a==-1); // init as -1
|
||
|
i = a = open("test.dat", O_RDONLY);
|
||
|
CPPUNIT_ASSERT(i==a && a>0); // file is now open
|
||
|
mrw::AutoFile b(a);
|
||
|
CPPUNIT_ASSERT(a==-1 && i==b); // b has taken ownership
|
||
|
CPPUNIT_ASSERT(read(b, &c, 1)==1 && c=='H'); // file is good
|
||
|
mrw::AutoFile c(i);
|
||
|
CPPUNIT_ASSERT(i==b && b==c); // ooops, two owner!
|
||
|
c.release();
|
||
|
CPPUNIT_ASSERT(i==b && c==-1); // it's ok now
|
||
|
b = open("test.dat", O_RDONLY);
|
||
|
//close(i);
|
||
|
CPPUNIT_ASSERT(read(i, &c, 1)==-1); // old file is closed
|
||
|
i = b.reset();
|
||
|
CPPUNIT_ASSERT(read(i, &c, 1)==-1); // new file is closed
|
||
|
i = a = open("test.dat", O_RDONLY);
|
||
|
}
|
||
|
CPPUNIT_ASSERT(read(i, &c, 1)==-1); // file is closed now
|
||
|
}
|
||
|
CPPUNIT_TEST_SUITE(AutoTest);
|
||
|
CPPUNIT_TEST(AutoFile);
|
||
|
CPPUNIT_TEST_SUITE_END();
|
||
|
};
|
||
|
CPPUNIT_TEST_SUITE_REGISTRATION(AutoTest);
|
||
|
|
||
|
int main() {
|
||
|
CppUnit::TextUi::TestRunner runner;
|
||
|
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
|
||
|
return runner.run() ? 0 : 1;
|
||
|
}
|