#include "ValueBase.h" #include #include #include #include "Exception.h" #include "util.h" namespace aria2 { class ValueBaseTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(ValueBaseTest); CPPUNIT_TEST(testString); CPPUNIT_TEST(testDict); CPPUNIT_TEST(testDictIter); CPPUNIT_TEST(testList); CPPUNIT_TEST(testListIter); CPPUNIT_TEST(testDowncast); CPPUNIT_TEST_SUITE_END(); public: void setUp() {} void tearDown() {} void testString(); void testDict(); void testDictIter(); void testList(); void testListIter(); void testDowncast(); }; CPPUNIT_TEST_SUITE_REGISTRATION(ValueBaseTest); void ValueBaseTest::testString() { String s(std::string("aria2")); CPPUNIT_ASSERT_EQUAL(std::string("aria2"), s.s()); unsigned char dataWithNull[] = { 0xf0, '\0', 0x0f }; String sWithNull(dataWithNull, sizeof(dataWithNull)); CPPUNIT_ASSERT(memcmp(dataWithNull, sWithNull.s().c_str(), sizeof(dataWithNull)) == 0); String zero(""); CPPUNIT_ASSERT_EQUAL(std::string(""), zero.s()); String z; CPPUNIT_ASSERT_EQUAL(std::string(""), z.s()); const unsigned char uc[] = { 0x08, 0x19, 0x2a, 0x3b }; String data(uc, sizeof(uc)); CPPUNIT_ASSERT_EQUAL(util::toHex(uc, sizeof(uc)), util::toHex(data.uc(), data.s().size())); } void ValueBaseTest::testDowncast() { Integer integer(100); const Integer* x = downcast(&integer); CPPUNIT_ASSERT(x); CPPUNIT_ASSERT_EQUAL(static_cast(100), x->i()); CPPUNIT_ASSERT(!downcast(&integer)); std::shared_ptr si(new Integer(101)); const Integer* x2 = downcast(si); CPPUNIT_ASSERT_EQUAL(static_cast(101), x2->i()); String str("foo"); const String* x3 = downcast(&str); CPPUNIT_ASSERT_EQUAL(static_cast("foo"), x3->s()); List list; const List* x4 = downcast(&list); CPPUNIT_ASSERT(x4); Dict dict; const Dict* x5 = downcast(&dict); CPPUNIT_ASSERT(x5); } void ValueBaseTest::testDict() { Dict dict; CPPUNIT_ASSERT(dict.empty()); dict.put("ki", Integer::g(7)); dict.put("ks", String::g("abc")); CPPUNIT_ASSERT_EQUAL(static_cast(2), dict.size()); CPPUNIT_ASSERT(dict.containsKey("ki")); CPPUNIT_ASSERT_EQUAL(static_cast(7), downcast(dict["ki"])->i()); CPPUNIT_ASSERT(dict.containsKey("ks")); CPPUNIT_ASSERT_EQUAL(std::string("abc"), downcast(dict["ks"])->s()); CPPUNIT_ASSERT(!dict["kn"]); // This does not adds kn key CPPUNIT_ASSERT_EQUAL(static_cast(2), dict.size()); auto& ref = dict; ref["kn2"]; // This doesn't add kn2 key. CPPUNIT_ASSERT_EQUAL(static_cast(2), ref.size()); CPPUNIT_ASSERT(!ref.containsKey("kn2")); dict.removeKey("ks"); CPPUNIT_ASSERT_EQUAL(static_cast(1), dict.size()); CPPUNIT_ASSERT(!dict.containsKey("ks")); auto ki = dict.popValue("ki"); CPPUNIT_ASSERT_EQUAL(Integer::ValueType{7}, downcast(ki)->i()); CPPUNIT_ASSERT(dict.empty()); CPPUNIT_ASSERT(!dict.containsKey("ki")); } void ValueBaseTest::testDictIter() { Dict dict; dict.put("alpha2", String::g("alpha2")); dict.put("charlie", String::g("charlie")); dict.put("bravo", String::g("bravo")); dict.put("alpha", String::g("alpha")); Dict::ValueType::iterator i = dict.begin(); CPPUNIT_ASSERT_EQUAL(std::string("alpha"), (*i++).first); CPPUNIT_ASSERT_EQUAL(std::string("alpha2"), (*i++).first); CPPUNIT_ASSERT_EQUAL(std::string("bravo"), (*i++).first); CPPUNIT_ASSERT_EQUAL(std::string("charlie"), (*i++).first); CPPUNIT_ASSERT(dict.end() == i); const Dict& ref = dict; Dict::ValueType::const_iterator ci = ref.begin(); CPPUNIT_ASSERT_EQUAL(std::string("alpha"), (*ci++).first); std::advance(ci, 3); CPPUNIT_ASSERT(ref.end() == ci); } void ValueBaseTest::testList() { List list; CPPUNIT_ASSERT(list.empty()); list << Integer::g(7) << String::g("aria2"); CPPUNIT_ASSERT_EQUAL(static_cast(2), list.size()); CPPUNIT_ASSERT_EQUAL(static_cast(7), downcast(list[0])->i()); CPPUNIT_ASSERT_EQUAL(static_cast("aria2"), downcast(list[1])->s()); const List& ref = list; CPPUNIT_ASSERT_EQUAL(static_cast(7), downcast(ref[0])->i()); CPPUNIT_ASSERT_EQUAL(static_cast("aria2"), downcast(ref[1])->s()); } void ValueBaseTest::testListIter() { List list; list << String::g("alpha2") << String::g("charlie") << String::g("bravo") << String::g("alpha"); List::ValueType::iterator i = list.begin(); CPPUNIT_ASSERT_EQUAL(static_cast("alpha2"), downcast(*i++)->s()); CPPUNIT_ASSERT_EQUAL(static_cast("charlie"), downcast(*i++)->s()); CPPUNIT_ASSERT_EQUAL(static_cast("bravo"), downcast(*i++)->s()); CPPUNIT_ASSERT_EQUAL(static_cast("alpha"), downcast(*i++)->s()); CPPUNIT_ASSERT(list.end() == i); const List& ref = list; List::ValueType::const_iterator ci = ref.begin(); CPPUNIT_ASSERT_EQUAL(static_cast("alpha2"), downcast(*ci++)->s()); std::advance(ci, 3); CPPUNIT_ASSERT(ref.end() == ci); } } // namespace aria2