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. CPPUNIT_ASSERT_EQUAL((size_t)5, A2_ARRAY_LEN(ia));
  53. // This causes compile error under clang and gcc v3.4.3 opensolaris
  54. // 5.11
  55. // int64_t zeroLengthArray[] = {};
  56. // CPPUNIT_ASSERT_EQUAL((size_t)0, A2_ARRAY_LEN(zeroLengthArray));
  57. }
  58. namespace {
  59. // Check operator[] in const context.
  60. void arrayPtrConst(const array_ptr<struct array_funTest::X>& ax)
  61. {
  62. CPPUNIT_ASSERT_EQUAL(100, ax[3].m);
  63. CPPUNIT_ASSERT_EQUAL(99, ax[2].m);
  64. }
  65. } // namespace
  66. namespace {
  67. void arrayPtrCast(struct array_funTest::X* x) {}
  68. } // namespace
  69. namespace {
  70. void arrayPtrConstCast(const struct array_funTest::X* x) {}
  71. } // namespace
  72. void array_funTest::testArrayPtr()
  73. {
  74. array_ptr<struct X> ax(new struct X[10]);
  75. ax[3].m = 100;
  76. ax[2].m = 99;
  77. CPPUNIT_ASSERT_EQUAL(100, ax[3].m);
  78. CPPUNIT_ASSERT_EQUAL(99, ax[2].m);
  79. arrayPtrConst(ax);
  80. arrayPtrCast(ax);
  81. arrayPtrConstCast(ax);
  82. }
  83. namespace {
  84. void arrayWrapperConst(const array_wrapper<int, 10>& array)
  85. {
  86. CPPUNIT_ASSERT_EQUAL(9, array[9]);
  87. }
  88. } // namespace
  89. void array_funTest::testArrayWrapper()
  90. {
  91. array_wrapper<int, 10> a1;
  92. CPPUNIT_ASSERT_EQUAL((size_t)10, a1.size());
  93. for(size_t i = 0; i < a1.size(); ++i) {
  94. a1[i] = i;
  95. }
  96. CPPUNIT_ASSERT_EQUAL(9, a1[9]);
  97. array_wrapper<int, 10> a2 = a1;
  98. CPPUNIT_ASSERT_EQUAL(9, a2[9]);
  99. arrayWrapperConst(a2);
  100. array_wrapper<struct X, 10> x1;
  101. arrayPtrCast(x1);
  102. arrayPtrConstCast(x1);
  103. }
  104. void array_funTest::testVbeginVend()
  105. {
  106. int a[] = {1,2,3};
  107. CPPUNIT_ASSERT_EQUAL(&a[0], vbegin(a));
  108. CPPUNIT_ASSERT_EQUAL(a+3, vend(a));
  109. }
  110. } // namespace aria2