Line data Source code
1 : /////////////////////////////////////////////////////////////////////////// 2 : // Spetabaru - Berenger Bramas MPCDF - 2017 3 : // Under LGPL Licence, please you must read the LICENCE file. 4 : /////////////////////////////////////////////////////////////////////////// 5 : 6 : #include "UTester.hpp" 7 : 8 : #include "Data/SpDataAccessMode.hpp" 9 : #include "Utils/SpUtils.hpp" 10 : 11 : #include "Task/SpTask.hpp" 12 : #include "Legacy/SpRuntime.hpp" 13 : 14 : #include "Utils/SpBufferDataView.hpp" 15 : #include "Utils/SpHeapBuffer.hpp" 16 : 17 : class TestClassWithCounter{ 18 : static std::atomic<int> NbCreated; 19 : static std::atomic<int> NbDeleted; 20 : 21 : public: 22 2 : TestClassWithCounter(){ 23 2 : NbCreated += 1; 24 2 : } 25 : 26 : TestClassWithCounter(const TestClassWithCounter&) = delete; 27 : TestClassWithCounter(TestClassWithCounter&&) = delete; 28 : 29 : TestClassWithCounter& operator=(TestClassWithCounter&&) = delete; 30 : TestClassWithCounter& operator=(const TestClassWithCounter&) = delete; 31 : 32 2 : ~TestClassWithCounter(){ 33 2 : NbDeleted += 1; 34 2 : } 35 : 36 4 : static int GetNbCreated(){ 37 4 : return NbCreated; 38 : } 39 : 40 4 : static int GetNbDeleted(){ 41 4 : return NbDeleted; 42 : } 43 : }; 44 : 45 : std::atomic<int> TestClassWithCounter::NbCreated(0); 46 : std::atomic<int> TestClassWithCounter::NbDeleted(0); 47 : 48 : 49 : class HeapBufferTest : public UTester< HeapBufferTest > { 50 : using Parent = UTester< HeapBufferTest >; 51 : 52 1 : void TestBasic(){ 53 1 : const int NumThreads = SpUtils::DefaultNumThreads(); 54 1 : SpRuntime runtime(NumThreads); 55 : 56 : { 57 2 : SpHeapBuffer<TestClassWithCounter> heapBuffer(10); 58 : 59 1 : auto testBuffer = heapBuffer.getNewBuffer(); 60 : 61 1 : TestClassWithCounter* objectPtr = nullptr; 62 : 63 1 : runtime.task(SpWrite(testBuffer.getDataDep()), 64 1 : [&objectPtr](SpDataBuffer<TestClassWithCounter> testObject){ 65 1 : objectPtr = &(*testObject); 66 2 : }); 67 1 : runtime.task(SpWrite(testBuffer.getDataDep()), 68 2 : [this,&objectPtr](SpDataBuffer<TestClassWithCounter> testObject){ 69 1 : UASSERTETRUE(objectPtr == &(*testObject)); 70 3 : }); 71 : 72 1 : runtime.waitAllTasks(); 73 : 74 1 : runtime.stopAllThreads(); 75 : 76 1 : UASSERTETRUE(TestClassWithCounter::GetNbCreated() != TestClassWithCounter::GetNbDeleted()); 77 : } 78 : 79 1 : UASSERTETRUE(TestClassWithCounter::GetNbCreated() == TestClassWithCounter::GetNbDeleted()); 80 1 : } 81 : 82 1 : void TestSameBuffer(){ 83 1 : const int NumThreads = SpUtils::DefaultNumThreads(); 84 1 : SpRuntime runtime(NumThreads); 85 : 86 : { 87 1 : SpHeapBuffer<TestClassWithCounter> heapBuffer(10); 88 : 89 : 90 1 : TestClassWithCounter* objectPtr = nullptr; 91 : 92 1 : int sequentialTaskFlow = 0; 93 : 94 : { 95 1 : auto testBuffer = heapBuffer.getNewBuffer(); 96 1 : runtime.task(SpWrite(sequentialTaskFlow), SpWrite(testBuffer.getDataDep()), 97 1 : [&objectPtr](int& /*sequentialTaskFlow*/, SpDataBuffer<TestClassWithCounter> testObject){ 98 1 : objectPtr = &(*testObject); 99 103 : }); 100 : } 101 : 102 101 : for(int idx = 0 ; idx < 100 ; ++idx){ 103 100 : auto testBuffer = heapBuffer.getNewBuffer(); 104 100 : runtime.task(SpWrite(sequentialTaskFlow), SpWrite(testBuffer.getDataDep()), 105 200 : [this, &objectPtr](int& /*sequentialTaskFlow*/, SpDataBuffer<TestClassWithCounter> testObject){ 106 100 : UASSERTETRUE(objectPtr == &(*testObject)); 107 300 : }); 108 : } 109 : 110 1 : runtime.waitAllTasks(); 111 : 112 1 : runtime.stopAllThreads(); 113 : 114 1 : UASSERTETRUE(TestClassWithCounter::GetNbCreated() != TestClassWithCounter::GetNbDeleted()); 115 : } 116 : 117 1 : UASSERTETRUE(TestClassWithCounter::GetNbCreated() == TestClassWithCounter::GetNbDeleted()); 118 1 : } 119 : 120 1 : void SetTests() { 121 1 : Parent::AddTest(&HeapBufferTest::TestBasic, "Basic test for vec type"); 122 1 : Parent::AddTest(&HeapBufferTest::TestSameBuffer, "Basic test for vec type"); 123 1 : } 124 : }; 125 : 126 : // You must do this 127 1 : TestClass(HeapBufferTest) 128 : 129 :