array_funTest.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "array_fun.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. using namespace aria2::expr;
  4. namespace aria2 {
  5. class array_funTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(array_funTest);
  7. CPPUNIT_TEST(testArray_negate);
  8. CPPUNIT_TEST(testArray_and);
  9. CPPUNIT_TEST(testArrayLength);
  10. CPPUNIT_TEST(testArrayPtr);
  11. CPPUNIT_TEST(testArrayWrapper);
  12. CPPUNIT_TEST(testVbeginVend);
  13. CPPUNIT_TEST_SUITE_END();
  14. public:
  15. void testBit_negate();
  16. void testBit_and();
  17. void testArray_negate();
  18. void testArray_and();
  19. void testArrayLength();
  20. void testArrayPtr();
  21. void testArrayWrapper();
  22. void testVbeginVend();
  23. struct X{
  24. int m;
  25. };
  26. };
  27. CPPUNIT_TEST_SUITE_REGISTRATION(array_funTest);
  28. void array_funTest::testArray_negate()
  29. {
  30. unsigned char a[] = { 0xaa, 0x55 };
  31. CPPUNIT_ASSERT_EQUAL((unsigned char)0x55, (~array(a))[0]);
  32. CPPUNIT_ASSERT_EQUAL((unsigned char)0xaa, (~array((unsigned char*)a))[1]);
  33. CPPUNIT_ASSERT_EQUAL((unsigned char)0xaa, (~~array(a))[0]);
  34. CPPUNIT_ASSERT_EQUAL((unsigned char)0x55, (~~array(a))[1]);
  35. }
  36. void array_funTest::testArray_and()
  37. {
  38. unsigned char a1[] = { 0xaa, 0x55 };
  39. unsigned char a2[] = { 0x1a, 0x25 };
  40. CPPUNIT_ASSERT_EQUAL((unsigned char)0x0a, (array(a1)&array(a2))[0]);
  41. CPPUNIT_ASSERT_EQUAL((unsigned char)0x05, (array(a1)&array(a2))[1]);
  42. CPPUNIT_ASSERT_EQUAL((unsigned char)0xa0, (array(a1)&~array(a2))[0]);
  43. CPPUNIT_ASSERT_EQUAL((unsigned char)0x50, (array(a1)&~array(a2))[1]);
  44. CPPUNIT_ASSERT_EQUAL((unsigned char)0xa0, (~array(a2)&array(a1))[0]);
  45. CPPUNIT_ASSERT_EQUAL((unsigned char)0x50, (~array(a2)&array(a1))[1]);
  46. CPPUNIT_ASSERT_EQUAL((unsigned char)0x45, (~array(a1)&~array(a2))[0]);
  47. CPPUNIT_ASSERT_EQUAL((unsigned char)0x8a, (~array(a1)&~array(a2))[1]);
  48. }
  49. void array_funTest::testArrayLength()
  50. {
  51. int64_t ia[] = { 1, 2, 3, 4, 5 };
  52. int64_t zeroLengthArray[] = {};
  53. CPPUNIT_ASSERT_EQUAL((size_t)5, A2_ARRAY_LEN(ia));
  54. // This causes compile error under gcc v3.4.3 opensolaris 5.11
  55. CPPUNIT_ASSERT_EQUAL((size_t)0, A2_ARRAY_LEN(zeroLengthArray));
  56. }
  57. namespace {
  58. // Check operator[] in const context.
  59. void arrayPtrConst(const array_ptr<struct array_funTest::X>& ax)
  60. {
  61. CPPUNIT_ASSERT_EQUAL(100, ax[3].m);
  62. CPPUNIT_ASSERT_EQUAL(99, ax[2].m);
  63. }
  64. } // namespace
  65. namespace {
  66. void arrayPtrCast(struct array_funTest::X* x) {}
  67. } // namespace
  68. namespace {
  69. void arrayPtrConstCast(const struct array_funTest::X* x) {}
  70. } // namespace
  71. void array_funTest::testArrayPtr()
  72. {
  73. array_ptr<struct X> ax(new struct X[10]);
  74. ax[3].m = 100;
  75. ax[2].m = 99;
  76. CPPUNIT_ASSERT_EQUAL(100, ax[3].m);
  77. CPPUNIT_ASSERT_EQUAL(99, ax[2].m);
  78. arrayPtrConst(ax);
  79. arrayPtrCast(ax);
  80. arrayPtrConstCast(ax);
  81. }
  82. namespace {
  83. void arrayWrapperConst(const array_wrapper<int, 10>& array)
  84. {
  85. CPPUNIT_ASSERT_EQUAL(9, array[9]);
  86. }
  87. } // namespace
  88. void array_funTest::testArrayWrapper()
  89. {
  90. array_wrapper<int, 10> a1;
  91. CPPUNIT_ASSERT_EQUAL((size_t)10, a1.size());
  92. for(size_t i = 0; i < a1.size(); ++i) {
  93. a1[i] = i;
  94. }
  95. CPPUNIT_ASSERT_EQUAL(9, a1[9]);
  96. array_wrapper<int, 10> a2 = a1;
  97. CPPUNIT_ASSERT_EQUAL(9, a2[9]);
  98. arrayWrapperConst(a2);
  99. array_wrapper<struct X, 10> x1;
  100. arrayPtrCast(x1);
  101. arrayPtrConstCast(x1);
  102. }
  103. void array_funTest::testVbeginVend()
  104. {
  105. int a[] = {1,2,3};
  106. CPPUNIT_ASSERT_EQUAL(&a[0], vbegin(a));
  107. CPPUNIT_ASSERT_EQUAL(a+3, vend(a));
  108. }
  109. } // namespace aria2