2008-01-11 13:32:00 +00:00
|
|
|
#include "array_fun.h"
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2009-04-16 13:43:23 +00:00
|
|
|
using namespace aria2::expr;
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
2008-01-11 13:32:00 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
class array_funTest : public CppUnit::TestFixture {
|
2008-01-11 13:32:00 +00:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(array_funTest);
|
|
|
|
CPPUNIT_TEST(testArray_negate);
|
|
|
|
CPPUNIT_TEST(testArray_and);
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_TEST(testArrayLength);
|
2009-02-12 13:44:34 +00:00
|
|
|
CPPUNIT_TEST(testArrayWrapper);
|
2008-01-11 13:32:00 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
2009-02-08 11:27:22 +00:00
|
|
|
|
2008-01-11 13:32:00 +00:00
|
|
|
public:
|
|
|
|
void testBit_negate();
|
|
|
|
void testBit_and();
|
|
|
|
void testArray_negate();
|
|
|
|
void testArray_and();
|
2008-02-08 15:53:45 +00:00
|
|
|
void testArrayLength();
|
2009-02-12 13:44:34 +00:00
|
|
|
void testArrayWrapper();
|
2009-02-08 11:27:22 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
struct X {
|
2009-02-08 11:27:22 +00:00
|
|
|
int m;
|
|
|
|
};
|
2008-01-11 13:32:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(array_funTest);
|
|
|
|
|
|
|
|
void array_funTest::testArray_negate()
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
unsigned char a[] = {0xaa, 0x55};
|
2009-04-25 17:04:32 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0x55, (~array(a))[0]);
|
2009-04-16 13:43:23 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0xaa, (~array((unsigned char*)a))[1]);
|
2008-01-11 13:32:00 +00:00
|
|
|
|
2009-04-25 17:04:32 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0xaa, (~~array(a))[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0x55, (~~array(a))[1]);
|
2008-01-11 13:32:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void array_funTest::testArray_and()
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
unsigned char a1[] = {0xaa, 0x55};
|
|
|
|
unsigned char a2[] = {0x1a, 0x25};
|
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0x0a, (array(a1) & array(a2))[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0x05, (array(a1) & array(a2))[1]);
|
2008-01-11 13:32:00 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0xa0, (array(a1) & ~array(a2))[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0x50, (array(a1) & ~array(a2))[1]);
|
2008-01-11 13:32:00 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0xa0, (~array(a2) & array(a1))[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0x50, (~array(a2) & array(a1))[1]);
|
2008-01-11 13:32:00 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0x45, (~array(a1) & ~array(a2))[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL((unsigned char)0x8a, (~array(a1) & ~array(a2))[1]);
|
2008-01-11 13:32:00 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
void array_funTest::testArrayLength()
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
int64_t ia[] = {1, 2, 3, 4, 5};
|
2014-08-29 14:37:31 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)5, arraySize(ia));
|
2012-02-29 15:02:29 +00:00
|
|
|
// This causes compile error under clang and gcc v3.4.3 opensolaris
|
|
|
|
// 5.11
|
|
|
|
// int64_t zeroLengthArray[] = {};
|
2014-08-29 14:37:31 +00:00
|
|
|
// CPPUNIT_ASSERT_EQUAL((size_t)0, arraySize(zeroLengthArray));
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
void arrayPtrCast(struct array_funTest::X* x) {}
|
|
|
|
} // namespace
|
2009-02-12 13:10:38 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
void arrayPtrConstCast(const struct array_funTest::X* x) {}
|
|
|
|
} // namespace
|
2009-02-12 13:10:38 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
void arrayWrapperConst(const array_wrapper<int, 10>& array)
|
2009-02-12 13:44:34 +00:00
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL(9, array[9]);
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2009-02-12 13:44:34 +00:00
|
|
|
|
|
|
|
void array_funTest::testArrayWrapper()
|
|
|
|
{
|
|
|
|
array_wrapper<int, 10> a1;
|
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)10, a1.size());
|
2015-12-27 09:39:47 +00:00
|
|
|
for (size_t i = 0; i < a1.size(); ++i) {
|
2009-02-12 13:44:34 +00:00
|
|
|
a1[i] = i;
|
|
|
|
}
|
|
|
|
CPPUNIT_ASSERT_EQUAL(9, a1[9]);
|
|
|
|
array_wrapper<int, 10> a2 = a1;
|
|
|
|
CPPUNIT_ASSERT_EQUAL(9, a2[9]);
|
|
|
|
|
2009-02-12 15:39:07 +00:00
|
|
|
arrayWrapperConst(a2);
|
|
|
|
|
2009-02-12 13:44:34 +00:00
|
|
|
array_wrapper<struct X, 10> x1;
|
|
|
|
arrayPtrCast(x1);
|
|
|
|
arrayPtrConstCast(x1);
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|