1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include "GroupId.h"
#include <cppunit/extensions/HelperMacros.h>
#include "TestUtil.h"
#include "array_fun.h"
namespace aria2 {
class GroupIdTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(GroupIdTest);
CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testToNumericId);
CPPUNIT_TEST(testExpandUnique);
CPPUNIT_TEST(testToHex);
CPPUNIT_TEST(testToAbbrevHex);
CPPUNIT_TEST_SUITE_END();
public:
void setUp()
{
GroupId::clear();
}
void testCreate();
void testToNumericId();
void testExpandUnique();
void testToHex();
void testToAbbrevHex();
};
CPPUNIT_TEST_SUITE_REGISTRATION( GroupIdTest );
void GroupIdTest::testCreate()
{
std::shared_ptr<GroupId> gid = GroupId::create();
CPPUNIT_ASSERT(gid);
CPPUNIT_ASSERT(0 != gid->getNumericId());
CPPUNIT_ASSERT(!GroupId::import(gid->getNumericId()));
CPPUNIT_ASSERT(!GroupId::import(0));
}
void GroupIdTest::testToNumericId()
{
a2_gid_t gid;
std::string hex;
hex = std::string(16, '0');
CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
GroupId::toNumericId(gid, hex.c_str()));
hex = std::string(16, 'f');
CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId(gid, hex.c_str()));
CPPUNIT_ASSERT_EQUAL((a2_gid_t)UINT64_MAX, gid);
CPPUNIT_ASSERT_EQUAL(0, GroupId::toNumericId(gid, "1234567890abcdef"));
CPPUNIT_ASSERT_EQUAL((a2_gid_t)1311768467294899695LL, gid);
hex = std::string(15, 'f');
CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
GroupId::toNumericId(gid, hex.c_str()));
CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
GroupId::toNumericId(gid, ""));
CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
GroupId::toNumericId(gid, "1234567890abcdeg"));
}
void GroupIdTest::testExpandUnique()
{
a2_gid_t gid;
std::shared_ptr<GroupId> ids[] = {
GroupId::import(0xff80000000010000LL),
GroupId::import(0xff80000000020001LL),
GroupId::import(0xfff8000000030000LL)
};
for(const auto& i : ids) {
CPPUNIT_ASSERT(i);
}
CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_NOT_UNIQUE,
GroupId::expandUnique(gid, "ff8"));
CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
GroupId::expandUnique(gid, "ffg"));
CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
GroupId::expandUnique(gid,
std::string(17, 'a').c_str()));
CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_INVALID,
GroupId::expandUnique(gid, ""));
CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_NOT_UNIQUE,
GroupId::expandUnique(gid, "ff800000000"));
CPPUNIT_ASSERT_EQUAL(0, GroupId::expandUnique(gid, "ff8000000001"));
CPPUNIT_ASSERT_EQUAL(std::string("ff80000000010000"), GroupId::toHex(gid));
CPPUNIT_ASSERT_EQUAL(0, GroupId::expandUnique(gid, "ff8000000002"));
CPPUNIT_ASSERT_EQUAL(std::string("ff80000000020001"), GroupId::toHex(gid));
CPPUNIT_ASSERT_EQUAL(0, GroupId::expandUnique(gid, "fff800000003"));
CPPUNIT_ASSERT_EQUAL(std::string("fff8000000030000"), GroupId::toHex(gid));
CPPUNIT_ASSERT_EQUAL((int)GroupId::ERR_NOT_FOUND,
GroupId::expandUnique(gid, "ff80000000031"));
}
void GroupIdTest::testToHex()
{
CPPUNIT_ASSERT_EQUAL(std::string("1234567890abcdef"),
GroupId::toHex(1311768467294899695LL));
CPPUNIT_ASSERT_EQUAL(std::string("0000000000000001"),
GroupId::toHex(0000000000000000001LL));
}
void GroupIdTest::testToAbbrevHex()
{
CPPUNIT_ASSERT_EQUAL(std::string("123456"),
GroupId::toAbbrevHex(1311768467294899695LL));
CPPUNIT_ASSERT_EQUAL(std::string("000000"),
GroupId::toAbbrevHex(0000000000000000001LL));
}
} // namespace aria2
|