aria2/test/OptionTest.cc
Tatsuhiro Tsujikawa 699f04d0b8 Disable --deferred-input when --save-session is used together
With --deferred-input=true, aria2 only reads input file to fill active
download slots, while keeping input file open.  Meanwhile,
--save-session saves all download info inside memory, but this does
not take into account of unread item in input file.  This will lead to
lose input data in saved session file.  Also current BufferedFile
implementation used to read/write input/output file take a lock on
Windows.  This effectively prevents session serializer from writing
session data to the same file which is still kept open because of
--deferred-input.  See GH-493
2015-11-28 13:02:21 +09:00

150 lines
3.7 KiB
C++

#include "Option.h"
#include <string>
#include <cppunit/extensions/HelperMacros.h>
#include "prefs.h"
namespace aria2 {
class OptionTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(OptionTest);
CPPUNIT_TEST(testPutAndGet);
CPPUNIT_TEST(testPutAndGetAsInt);
CPPUNIT_TEST(testPutAndGetAsDouble);
CPPUNIT_TEST(testDefined);
CPPUNIT_TEST(testBlank);
CPPUNIT_TEST(testMerge);
CPPUNIT_TEST(testParent);
CPPUNIT_TEST(testRemove);
CPPUNIT_TEST_SUITE_END();
private:
public:
void setUp() {
}
void testPutAndGet();
void testPutAndGetAsInt();
void testPutAndGetAsDouble();
void testDefined();
void testBlank();
void testMerge();
void testParent();
void testRemove();
};
CPPUNIT_TEST_SUITE_REGISTRATION( OptionTest );
void OptionTest::testPutAndGet() {
Option op;
op.put(PREF_TIMEOUT, "value");
CPPUNIT_ASSERT(op.defined(PREF_TIMEOUT));
CPPUNIT_ASSERT_EQUAL(std::string("value"), op.get(PREF_TIMEOUT));
}
void OptionTest::testPutAndGetAsInt() {
Option op;
op.put(PREF_TIMEOUT, "1000");
CPPUNIT_ASSERT(op.defined(PREF_TIMEOUT));
CPPUNIT_ASSERT_EQUAL((int32_t)1000, op.getAsInt(PREF_TIMEOUT));
}
void OptionTest::testPutAndGetAsDouble() {
Option op;
op.put(PREF_TIMEOUT, "10.0");
CPPUNIT_ASSERT_EQUAL(10.0, op.getAsDouble(PREF_TIMEOUT));
}
void OptionTest::testDefined()
{
Option op;
op.put(PREF_TIMEOUT, "v");
op.put(PREF_DIR, "");
CPPUNIT_ASSERT(op.defined(PREF_TIMEOUT));
CPPUNIT_ASSERT(op.defined(PREF_DIR));
CPPUNIT_ASSERT(!op.defined(PREF_DAEMON));
}
void OptionTest::testBlank()
{
Option op;
op.put(PREF_TIMEOUT, "v");
op.put(PREF_DIR, "");
CPPUNIT_ASSERT(!op.blank(PREF_TIMEOUT));
CPPUNIT_ASSERT(op.blank(PREF_DIR));
CPPUNIT_ASSERT(op.blank(PREF_DAEMON));
}
void OptionTest::testMerge()
{
Option src;
src.put(PREF_TIMEOUT, "100");
src.put(PREF_DAEMON, "true");
Option dest;
dest.put(PREF_DAEMON, "false");
dest.put(PREF_DIR, "foo");
dest.merge(src);
CPPUNIT_ASSERT_EQUAL(100, dest.getAsInt(PREF_TIMEOUT));
CPPUNIT_ASSERT(dest.getAsBool(PREF_DAEMON));
CPPUNIT_ASSERT_EQUAL(std::string("foo"), dest.get(PREF_DIR));
CPPUNIT_ASSERT(!dest.defined(PREF_OUT));
}
void OptionTest::testParent()
{
Option child;
std::shared_ptr<Option> parent(new Option());
parent->put(PREF_TIMEOUT, "100");
child.put(PREF_DIR, "foo");
CPPUNIT_ASSERT(!child.defined(PREF_TIMEOUT));
CPPUNIT_ASSERT(!child.definedLocal(PREF_TIMEOUT));
child.setParent(parent);
CPPUNIT_ASSERT(child.defined(PREF_TIMEOUT));
CPPUNIT_ASSERT_EQUAL(std::string("100"), child.get(PREF_TIMEOUT));
CPPUNIT_ASSERT_EQUAL((int32_t)100, child.getAsInt(PREF_TIMEOUT));
CPPUNIT_ASSERT(!child.definedLocal(PREF_TIMEOUT));
// blank
CPPUNIT_ASSERT(!child.blank(PREF_DIR));
child.put(PREF_DIR, "");
CPPUNIT_ASSERT(child.blank(PREF_DIR));
CPPUNIT_ASSERT(!child.blank(PREF_TIMEOUT));
// override
child.put(PREF_TIMEOUT, "200");
CPPUNIT_ASSERT(child.defined(PREF_TIMEOUT));
CPPUNIT_ASSERT(child.definedLocal(PREF_TIMEOUT));
CPPUNIT_ASSERT_EQUAL(std::string("200"), child.get(PREF_TIMEOUT));
child.removeLocal(PREF_TIMEOUT);
CPPUNIT_ASSERT(child.defined(PREF_TIMEOUT));
CPPUNIT_ASSERT(!child.definedLocal(PREF_TIMEOUT));
}
void OptionTest::testRemove()
{
Option child;
auto parent = std::make_shared<Option>();
child.setParent(parent);
child.put(PREF_DIR, "foo");
child.put(PREF_TIMEOUT, "200");
parent->put(PREF_DIR, "bar");
parent->put(PREF_TIMEOUT, "400");
child.remove(PREF_DIR);
CPPUNIT_ASSERT(!child.defined(PREF_DIR));
child.removeLocal(PREF_TIMEOUT);
CPPUNIT_ASSERT(!child.definedLocal(PREF_TIMEOUT));
CPPUNIT_ASSERT(child.defined(PREF_TIMEOUT));
CPPUNIT_ASSERT(parent->defined(PREF_TIMEOUT));
}
} // namespace aria2