/* Copyright (C) 2009 Matthias Kretz This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) version 3. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef ENABLE_ARRAY_BOUNDS_CHECKING #define ENABLE_ARRAY_BOUNDS_CHECKING #endif #include "unittest.h" #include void test1DimArray() { AliHLTResizableArray a(10); for (int i = 0; i < 10; ++i) { a[i] = 2; } for (int i = 0; i < 10; ++i) { VERIFY(a[i] == 2); } EXPECT_ASSERT_FAILURE(a[10] = 2); EXPECT_ASSERT_FAILURE(a[-1] = 2); a.Resize(2); for (int i = 0; i < 2; ++i) { a[i] = 2; } for (int i = 0; i < 2; ++i) { VERIFY(a[i] == 2); } EXPECT_ASSERT_FAILURE(a[2] = 2); EXPECT_ASSERT_FAILURE(a[-1] = 2); } void test2DimArray() { AliHLTResizableArray a(3, 3); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { a(i, j) = 1; } } for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { VERIFY(a(i, j) == 1); } } EXPECT_ASSERT_FAILURE(a(2, 3) = 2); EXPECT_ASSERT_FAILURE(a(3, 0) = 2); EXPECT_ASSERT_FAILURE(a(-1, 0) = 2); EXPECT_ASSERT_FAILURE(a(0, -1) = 2); a.Resize(2, 2); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { a(i, j) = 1; } } for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { VERIFY(a(i, j) == 1); } } EXPECT_ASSERT_FAILURE(a(1, 2) = 2); EXPECT_ASSERT_FAILURE(a(2, 0) = 2); EXPECT_ASSERT_FAILURE(a(-1, 0) = 2); EXPECT_ASSERT_FAILURE(a(0, -1) = 2); } void test2DimArraySubArray() { AliHLTResizableArray a(3, 3); for (int i = 0; i < 3; ++i) { AliHLTArray b = a[i]; for (int j = 0; j < 3; ++j) { b[j] = i + (j << 4); VERIFY(&b[j] == &a(i, j)); } } for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { VERIFY(a(i, j) == (i + (j << 4))); VERIFY(a(i, j) == a[i][j]); } } } void testArrayArithmetic() { AliHLTResizableArray a(10); for (int i = 0; i < 10; ++i) { *(a + i) = i; VERIFY(a[i] == i); } AliHLTArray b = a + 10; VERIFY(b[-1] == a[9]); EXPECT_ASSERT_FAILURE(b[0]); } void test1DimFixedArray() { AliHLTFixedArray > a; for (int i = 0; i < 10; ++i) { a[i] = 2; } for (int i = 0; i < 10; ++i) { VERIFY(a[i] == 2); } EXPECT_ASSERT_FAILURE(a[10] = 2); EXPECT_ASSERT_FAILURE(a[-1] = 2); } void test2DimFixedArray() { AliHLTFixedArray > a; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { a(i, j) = 1; } } for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { VERIFY(a(i, j) == 1); } } EXPECT_ASSERT_FAILURE(a(2, 3) = 2); EXPECT_ASSERT_FAILURE(a(3, 0) = 2); EXPECT_ASSERT_FAILURE(a(-1, 0) = 2); EXPECT_ASSERT_FAILURE(a(0, -1) = 2); } void test2DimFixedArraySubArray() { AliHLTFixedArray > a; for (int i = 0; i < 3; ++i) { AliHLTArray b = a[i]; for (int j = 0; j < 3; ++j) { b[j] = i + (j << 4); VERIFY(&b[j] == &a(i, j)); } } for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { VERIFY(a(i, j) == (i + (j << 4))); VERIFY(a(i, j) == a[i][j]); } } } void testFixedArrayArithmetic() { AliHLTFixedArray > a; for (int i = 0; i < 10; ++i) { *(a + i) = i; VERIFY(a[i] == i); } AliHLTArray b = a + 10; VERIFY(b[-1] == a[9]); EXPECT_ASSERT_FAILURE(b[0]); } void testArrayAlignments() { { unsigned char i = 0; AliHLTResizableArray b( 10 ); VERIFY( ( reinterpret_cast( &b[i] ) & ( 8 - 1 ) ) == 0 ); AliHLTFixedArray, 8> a; VERIFY( ( reinterpret_cast( &a[i] ) & ( 8 - 1 ) ) == 0 ); } { unsigned char i = 0; AliHLTResizableArray b( 10 ); VERIFY( ( reinterpret_cast( &b[i] ) & ( 16 - 1 ) ) == 0 ); AliHLTFixedArray, 16> a; VERIFY( ( reinterpret_cast( &a[i] ) & ( 16 - 1 ) ) == 0 ); } { unsigned char i = 0; AliHLTResizableArray b( 10 ); VERIFY( ( reinterpret_cast( &b[i] ) & ( 32 - 1 ) ) == 0 ); AliHLTFixedArray, 32> a; VERIFY( ( reinterpret_cast( &a[i] ) & ( 32 - 1 ) ) == 0 ); } { unsigned char i = 0; AliHLTResizableArray b( 10 ); VERIFY( ( reinterpret_cast( &b[i] ) & ( 64 - 1 ) ) == 0 ); AliHLTFixedArray, 64> a; VERIFY( ( reinterpret_cast( &a[i] ) & ( 64 - 1 ) ) == 0 ); } { unsigned char i = 0; AliHLTResizableArray b( 10 ); VERIFY( ( reinterpret_cast( &b[i] ) & ( 128 - 1 ) ) == 0 ); AliHLTFixedArray, 128> a; VERIFY( ( reinterpret_cast( &a[i] ) & ( 128 - 1 ) ) == 0 ); } } template void testCacheLineAlignment() { //std::cout << sizeof( T ) << " - " << sizeof( AliHLTInternal::CacheLineSizeHelper ) << std::endl; AliHLTFixedArray, AliHLTFullyCacheLineAligned> a; AliHLTResizableArray b( 20 ); for ( int i = 0; i < 20; ++i ) { VERIFY( ( reinterpret_cast( &a[i] ) & ( 64 - 1 ) ) == 0 ); VERIFY( ( reinterpret_cast( &b[i] ) & ( 64 - 1 ) ) == 0 ); } } struct CtorDtorCounter { static int count; CtorDtorCounter() { ++count; } ~CtorDtorCounter() { --count; } }; int CtorDtorCounter::count = 0; void testCtorDtor() { CtorDtorCounter::count = 0; { AliHLTResizableArray a( 200 ); COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); { AliHLTResizableArray a( 20, 10 ); COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); { AliHLTResizableArray a( 5, 10, 4 ); COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); { AliHLTResizableArray a( 200 ); COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); { AliHLTResizableArray a( 20, 10 ); COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); { AliHLTResizableArray a( 5, 10, 4 ); COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); } void testCtorDtorFixed() { CtorDtorCounter::count = 0; { AliHLTFixedArray > a; COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); { AliHLTFixedArray > a; COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); { AliHLTFixedArray > a; COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); { AliHLTFixedArray, 16> a; COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); { AliHLTFixedArray, 16> a; COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); { AliHLTFixedArray, 16> a; COMPARE( CtorDtorCounter::count, 200 ); } COMPARE( CtorDtorCounter::count, 0 ); } template struct StructOfSize { char mem[Size]; }; int main() { // ResizableArray runTest(test1DimArray); runTest(test2DimArray); runTest(test2DimArraySubArray); runTest(testArrayArithmetic); // FixedArray runTest(test1DimFixedArray); runTest(test2DimFixedArray); runTest(test2DimFixedArraySubArray); runTest(testFixedArrayArithmetic); runTest(testCtorDtor); runTest(testCtorDtorFixed); // Alignments runTest(testArrayAlignments); runTest(testCacheLineAlignment); runTest(testCacheLineAlignment); runTest(testCacheLineAlignment >); runTest(testCacheLineAlignment >); runTest(testCacheLineAlignment >); runTest(testCacheLineAlignment >); runTest(testCacheLineAlignment >); runTest(testCacheLineAlignment >); runTest(testCacheLineAlignment >); runTest(testCacheLineAlignment >); runTest(testCacheLineAlignment >); runTest(testCacheLineAlignment >); return 0; }