2004-08-28 16:21:25 +00:00
|
|
|
/** @file
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
$Date$
|
|
|
|
$Author$
|
|
|
|
|
|
|
|
@copy © Marc Wäckerlin
|
|
|
|
@license LGPL, see file <a href="license.html">COPYING</a>
|
|
|
|
|
|
|
|
$Log$
|
2005-04-07 20:55:21 +00:00
|
|
|
Revision 1.5 2005/04/07 20:55:21 marc
|
|
|
|
Oops, there's a make distcheck...? Now it works.
|
|
|
|
|
2004-11-25 18:27:03 +00:00
|
|
|
Revision 1.4 2004/11/25 18:27:03 marc
|
|
|
|
additional test for release and reset
|
|
|
|
|
2004-08-28 16:21:25 +00:00
|
|
|
Revision 1.3 2004/08/28 16:21:25 marc
|
|
|
|
mrw-c++-0.92 (mrw)
|
|
|
|
- new file: version.cpp
|
|
|
|
- new file header for all sources
|
|
|
|
- work around warning in mrw::auto<T>
|
|
|
|
- possibility to compile without log4cxx
|
|
|
|
- work around bugs in demangle.h and libiberty.h
|
|
|
|
- corrections in documentation
|
|
|
|
- added simple tracing mechanism
|
|
|
|
- more warnings
|
|
|
|
- small corrections in Auto<>::Free and a new test for it
|
|
|
|
- possibility to compile without stack trace
|
|
|
|
|
|
|
|
*/
|
2004-04-21 06:39:20 +00:00
|
|
|
#include <mrw/auto.hpp>
|
2005-04-07 20:55:21 +00:00
|
|
|
#include <mrw/stdext.hpp> // ifelse
|
2004-04-21 06:39:20 +00:00
|
|
|
#include <cppunit/TestFixture.h>
|
|
|
|
#include <cppunit/ui/text/TestRunner.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
#include <cppunit/extensions/TestFactoryRegistry.h>
|
|
|
|
#include <fcntl.h> // open
|
2004-08-28 16:21:25 +00:00
|
|
|
#include <string.h> // strncpy
|
2005-04-07 20:55:21 +00:00
|
|
|
#include <stdlib.h> // getenv
|
2004-04-21 06:39:20 +00:00
|
|
|
|
|
|
|
class AutoTest: public CppUnit::TestFixture {
|
|
|
|
public:
|
|
|
|
void AutoFile() {
|
|
|
|
char c(0);
|
|
|
|
int i(-1);
|
|
|
|
{
|
|
|
|
mrw::AutoFile a;
|
|
|
|
CPPUNIT_ASSERT(a==-1); // init as -1
|
2005-04-07 20:55:21 +00:00
|
|
|
i = a = open((std::string(mrw::ifelse(getenv("srcdir"), "."))
|
|
|
|
+"/test.dat").c_str(), O_RDONLY);
|
2004-04-21 06:39:20 +00:00
|
|
|
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
|
2004-08-28 16:21:25 +00:00
|
|
|
mrw::AutoFile cc(i);
|
|
|
|
CPPUNIT_ASSERT(i==b && b==cc); // ooops, two owner!
|
|
|
|
cc.release();
|
|
|
|
CPPUNIT_ASSERT(i==b && cc==-1); // it's ok now
|
2004-04-21 06:39:20 +00:00
|
|
|
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
|
|
|
|
}
|
2004-08-28 16:21:25 +00:00
|
|
|
void AutoFree() {
|
|
|
|
const char C[] = "Hello World";
|
|
|
|
mrw::Auto<char*>::Free c(malloc(sizeof(C)));
|
|
|
|
CPPUNIT_ASSERT(c);
|
|
|
|
strncpy(c, C, sizeof(C));
|
|
|
|
CPPUNIT_ASSERT(std::string(c)==C);
|
2004-11-25 18:27:03 +00:00
|
|
|
mrw::Auto<char*>::Free c2(c.release());
|
|
|
|
CPPUNIT_ASSERT(c==0 && c2!=0);
|
|
|
|
CPPUNIT_ASSERT(std::string(c2)==C);
|
|
|
|
c2.reset();
|
|
|
|
CPPUNIT_ASSERT(c2==0);
|
2004-08-28 16:21:25 +00:00
|
|
|
}
|
2004-04-21 06:39:20 +00:00
|
|
|
CPPUNIT_TEST_SUITE(AutoTest);
|
|
|
|
CPPUNIT_TEST(AutoFile);
|
2004-08-28 16:21:25 +00:00
|
|
|
CPPUNIT_TEST(AutoFree);
|
2004-04-21 06:39:20 +00:00
|
|
|
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;
|
|
|
|
}
|