C++ Library containing a lot of needful things: Stack Trace, Command Line Parser, Resource Handling, Configuration Files, Unix Command Execution, Directories, Regular Expressions, Tokenizer, Function Trace, Standard Extensions.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB

#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;
}