From 51de05346f0cb202f6ba80d5e9dfa996a97bae4e Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Wed, 4 Dec 2019 20:47:45 -0800 Subject: Updated core & tools. Fixed https://github.com/DiligentGraphics/DiligentEngine/issues/67, fixed https://github.com/DiligentGraphics/DiligentEngine/issues/49 --- Tests/TestApp/src/AllocatorTest.cpp | 83 ---- Tests/TestApp/src/MathLibTest.cpp | 732 ------------------------------ Tests/TestApp/src/RingBufferTest.cpp | 297 ------------ Tests/TestApp/src/SmartPointerTest.cpp | 797 --------------------------------- Tests/TestApp/src/TestApp.cpp | 1 - 5 files changed, 1910 deletions(-) delete mode 100644 Tests/TestApp/src/AllocatorTest.cpp delete mode 100644 Tests/TestApp/src/MathLibTest.cpp delete mode 100644 Tests/TestApp/src/RingBufferTest.cpp delete mode 100644 Tests/TestApp/src/SmartPointerTest.cpp (limited to 'Tests/TestApp/src') diff --git a/Tests/TestApp/src/AllocatorTest.cpp b/Tests/TestApp/src/AllocatorTest.cpp deleted file mode 100644 index b575fe8..0000000 --- a/Tests/TestApp/src/AllocatorTest.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* Copyright 2019 Diligent Graphics LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -// EngineSandbox.cpp : Defines the entry point for the application. -// - -#include "pch.h" -#include "AllocatorTest.h" -#include "Errors.h" -#include "DefaultRawMemoryAllocator.h" -#include "FixedBlockMemoryAllocator.h" - -using namespace Diligent; - -AllocatorTest TheTest; - -AllocatorTest::AllocatorTest() : - UnitTestBase("Allocator test") -{ - const Uint32 AllocSize = 32; - const Uint32 NumAllocationsPerPage = 16; - FixedBlockMemoryAllocator TestAllocator(DefaultRawMemoryAllocator::GetAllocator(), AllocSize, NumAllocationsPerPage); - void* Allocations[NumAllocationsPerPage][2]={}; - for(int p=0; p < 2; ++p) - { - for (int a = 1; a < NumAllocationsPerPage; ++a) - { - for (int i = 0; i < a; ++i) - Allocations[i][p] = TestAllocator.Allocate(AllocSize, "Fixed block allocator test", __FILE__, __LINE__); - - for (int i = a-1; i >= 0; --i) - TestAllocator.Free(Allocations[i][p]); - - for (int i = 0; i < a; ++i) - { - auto *NewAlloc = TestAllocator.Allocate(AllocSize, "Fixed block allocator test", __FILE__, __LINE__); - VERIFY_EXPR(Allocations[i][p] == NewAlloc); - } - - for (int i = a-1; i >= 0; --i) - TestAllocator.Free(Allocations[i][p]); - } - for (int i = 0; i < NumAllocationsPerPage; ++i) - Allocations[i][p] = TestAllocator.Allocate(AllocSize, "Fixed block allocator test", __FILE__, __LINE__); - } - - for(int p=0; p < 2; ++p) - for (int i = 0; i < NumAllocationsPerPage; ++i) - TestAllocator.Free( Allocations[i][p] ); - - for(int p=0; p < 2; ++p) - for (int i = 0; i < NumAllocationsPerPage; ++i) - Allocations[i][p] = TestAllocator.Allocate(AllocSize, "Fixed block allocator test", __FILE__, __LINE__); - - for(int p=0; p < 2; ++p) - for (int s = 0; s < 5; ++s) - for (int i = s; i < NumAllocationsPerPage; i+=5) - TestAllocator.Free( Allocations[i][p] ); - - // Double free - //TestAllocator.Free( Allocations[0][0] ); - SetStatus(TestResult::Succeeded); -} diff --git a/Tests/TestApp/src/MathLibTest.cpp b/Tests/TestApp/src/MathLibTest.cpp deleted file mode 100644 index c3f888a..0000000 --- a/Tests/TestApp/src/MathLibTest.cpp +++ /dev/null @@ -1,732 +0,0 @@ -/* Copyright 2019 Diligent Graphics LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -// EngineSandbox.cpp : Defines the entry point for the application. -// - -#include "pch.h" -#include "BasicMath.h" -#include "AdvancedMath.h" -#include "DebugUtilities.h" -#include "UnitTestBase.h" - -using namespace Diligent; - -class MathLibTest : public UnitTestBase -{ -public: - MathLibTest() : - UnitTestBase("Mathlib test") - { - // Ctor - { - float2 f2( 1, 2 ); - VERIFY_EXPR( f2.x == 1 && f2.y == 2 ); - VERIFY_EXPR( f2.x == f2[0] && f2.y == f2[1] ); - (void)f2; - } - - { - float3 f3( 1, 2, 3 ); - VERIFY_EXPR( f3.x == 1 && f3.y == 2 && f3.z == 3 ); - VERIFY_EXPR( f3.x == f3[0] && f3.y == f3[1] && f3.z == f3[2] ); - (void)f3; - } - - { - float4 f4( 1, 2, 3, 4 ); - VERIFY_EXPR( f4.x == 1 && f4.y == 2 && f4.z == 3 && f4.w == 4 ); - VERIFY_EXPR( f4.x == f4[0] && f4.y == f4[1] && f4.z == f4[2] && f4.w == f4[3] ); - (void)f4; - } - - - // a - b - { - auto v = float2( 5, 3 ) - float2( 1, 2 ); - VERIFY_EXPR( v.x == 4 && v.y == 1 ); - (void)v; - } - - { - auto v = float3( 5, 3, 20 ) - float3( 1, 2, 10 ); - VERIFY_EXPR( v.x == 4 && v.y == 1 && v.z == 10); - (void)v; - } - - { - auto v = float4( 5, 3, 20, 200 ) - float4( 1, 2, 10, 100 ); - VERIFY_EXPR( v.x == 4 && v.y == 1 && v.z == 10 && v.w == 100); - (void)v; - } - - // a -= b - { - auto v = float2( 5, 3 ); - v -= float2( 1, 2 ); - VERIFY_EXPR( v.x == 4 && v.y == 1 ); - (void)v; - } - - { - auto v = float3( 5, 3, 20 ); - v -= float3( 1, 2, 10 ); - VERIFY_EXPR( v.x == 4 && v.y == 1 && v.z == 10); - (void)v; - } - - { - auto v = float4( 5, 3, 20, 200 ); - v -= float4( 1, 2, 10, 100 ); - VERIFY_EXPR( v.x == 4 && v.y == 1 && v.z == 10 && v.w == 100); - (void)v; - } - - // -a - { - auto v = -float2( 1, 2 ); - VERIFY_EXPR( v.x == -1 && v.y == -2 ); - (void)v; - } - - { - auto v = -float3( 1, 2, 3 ); - VERIFY_EXPR( v.x == -1 && v.y == -2 && v.z == -3 ); - (void)v; - } - - { - auto v = -float4( 1, 2, 3, 4 ); - VERIFY_EXPR( v.x == -1 && v.y == -2 && v.z == -3 && v.w == -4 ); - (void)v; - } - - - // a + b - { - auto v = float2( 5, 3 ) + float2( 1, 2 ); - VERIFY_EXPR( v.x == 6 && v.y == 5 ); - (void)v; - } - - { - auto v = float3( 5, 3, 20 ) + float3( 1, 2, 10 ); - VERIFY_EXPR( v.x == 6 && v.y == 5 && v.z == 30); - (void)v; - } - - { - auto v = float4( 5, 3, 20, 200 ) + float4( 1, 2, 10, 100 ); - VERIFY_EXPR( v.x == 6 && v.y == 5 && v.z == 30 && v.w == 300); - (void)v; - } - - // a += b - { - auto v = float2( 5, 3 ); - v += float2( 1, 2 ); - VERIFY_EXPR( v.x == 6 && v.y == 5 ); - (void)v; - } - - { - auto v = float3( 5, 3, 20 ); - v+=float3( 1, 2, 10 ); - VERIFY_EXPR( v.x == 6 && v.y == 5 && v.z == 30); - (void)v; - } - - { - auto v = float4( 5, 3, 20, 200 ); - v+=float4( 1, 2, 10, 100 ); - VERIFY_EXPR( v.x == 6 && v.y == 5 && v.z == 30 && v.w == 300); - (void)v; - } - - // a * b - { - auto v = float2( 5, 3 ) * float2( 1, 2 ); - VERIFY_EXPR( v.x == 5 && v.y == 6 ); - (void)v; - } - - { - auto v = float3( 5, 3, 20 ) * float3( 1, 2, 3 ); - VERIFY_EXPR( v.x == 5 && v.y == 6 && v.z == 60); - (void)v; - } - - { - auto v = float4( 5, 3, 20, 200 ) * float4( 1, 2, 3, 4 ); - VERIFY_EXPR( v.x == 5 && v.y == 6 && v.z == 60 && v.w == 800); - (void)v; - } - - // a *= b - { - auto v = float2( 5, 3 ); - v*=float2( 1, 2 ); - VERIFY_EXPR( v.x == 5 && v.y == 6 ); - (void)v; - } - - { - auto v = float3( 5, 3, 20 ); - v*=float3( 1, 2, 3 ); - VERIFY_EXPR( v.x == 5 && v.y == 6 && v.z == 60); - (void)v; - } - - { - auto v = float4( 5, 3, 20, 200 ); - v*=float4( 1, 2, 3, 4 ); - VERIFY_EXPR( v.x == 5 && v.y == 6 && v.z == 60 && v.w == 800); - (void)v; - } - - // a * s - { - auto v = float2( 5, 3 )*2; - VERIFY_EXPR( v.x == 10 && v.y == 6 ); - (void)v; - } - - { - auto v = float3( 5, 3, 20 )*2; - VERIFY_EXPR( v.x == 10 && v.y == 6 && v.z == 40); - (void)v; - } - - { - auto v = float4( 5, 3, 20, 200 ) * 2; - VERIFY_EXPR( v.x == 10 && v.y == 6 && v.z == 40 && v.w == 400); - (void)v; - } - - // a *= s - { - auto v = float2( 5, 3 ); - v*=2; - VERIFY_EXPR( v.x == 10 && v.y == 6 ); - (void)v; - } - - { - auto v = float3( 5, 3, 20 ); - v*=2; - VERIFY_EXPR( v.x == 10 && v.y == 6 && v.z == 40); - (void)v; - } - - { - auto v = float4( 5, 3, 20, 200 ); - v*=2; - VERIFY_EXPR( v.x == 10 && v.y == 6 && v.z == 40 && v.w == 400); - (void)v; - } - - // s * a - { - auto v = 2.f * float2( 5, 3 ); - VERIFY_EXPR( v.x == 10 && v.y == 6 ); - (void)v; - } - - { - auto v = 2.f * float3( 5, 3, 20 ); - VERIFY_EXPR( v.x == 10 && v.y == 6 && v.z == 40); - (void)v; - } - - { - auto v = 2.f * float4( 5, 3, 20, 200 ); - VERIFY_EXPR( v.x == 10 && v.y == 6 && v.z == 40 && v.w == 400); - (void)v; - } - - // a / s - { - auto v = float2( 10, 6 )/2; - VERIFY_EXPR( v.x == 5 && v.y == 3 ); - (void)v; - } - - { - auto v = float3( 10, 6, 40 )/2; - VERIFY_EXPR( v.x == 5 && v.y == 3 && v.z == 20); - (void)v; - } - - { - auto v = float4( 10, 6, 40, 400 ) / 2; - VERIFY_EXPR( v.x == 5 && v.y == 3 && v.z == 20 && v.w == 200); - (void)v; - } - - - // a / b - { - auto v = float2( 6, 4 ) / float2( 1, 2 ); - VERIFY_EXPR( v.x == 6 && v.y == 2 ); - (void)v; - } - - { - auto v = float3( 6, 3, 20 ) / float3( 3, 1, 5 ); - VERIFY_EXPR( v.x == 2 && v.y == 3 && v.z == 4); - (void)v; - } - - { - auto v = float4( 6, 3, 20, 200 ) / float4( 3, 1, 5, 40 ); - VERIFY_EXPR( v.x == 2 && v.y == 3 && v.z == 4 && v.w == 5); - (void)v; - } - - // a /= b - { - auto v = float2( 6, 4 ); - v/=float2( 1, 2 ); - VERIFY_EXPR( v.x == 6 && v.y == 2 ); - (void)v; - } - - { - auto v = float3( 6, 3, 20 ); - v/=float3( 3, 1, 5 ); - VERIFY_EXPR( v.x == 2 && v.y == 3 && v.z == 4); - (void)v; - } - - { - auto v = float4( 6, 3, 20, 200 ); - v/=float4( 3, 1, 5, 40 ); - VERIFY_EXPR( v.x == 2 && v.y == 3 && v.z == 4 && v.w == 5); - (void)v; - } - - // a /= s - { - auto v = float2( 6, 4 ); - v/=2; - VERIFY_EXPR( v.x == 3 && v.y == 2 ); - (void)v; - } - - { - auto v = float3( 4, 6, 20 ); - v/=2; - VERIFY_EXPR( v.x == 2 && v.y == 3 && v.z == 10); - (void)v; - } - - { - auto v = float4( 4, 6, 20, 200 ); - v/=2; - VERIFY_EXPR( v.x == 2 && v.y == 3 && v.z == 10 && v.w == 100); - (void)v; - } - - // max - { - auto v = std::max( float2( 6, 4 ), float2( 1, 40 ) ); - VERIFY_EXPR( v.x == 6 && v.y == 40 ); - (void)v; - } - - { - auto v = std::max( float3( 4, 6, 20 ), float3( 40, 3, 23 ) ); - VERIFY_EXPR( v.x == 40 && v.y == 6 && v.z == 23); - (void)v; - } - - { - auto v = std::max( float4( 4, 6, 20, 100 ), float4( 40, 3, 23, 50 ) ); - VERIFY_EXPR( v.x == 40 && v.y == 6 && v.z == 23 && v.w == 100); - (void)v; - } - - // min - { - auto v = std::min( float2( 6, 4 ), float2( 1, 40 ) ); - VERIFY_EXPR( v.x == 1 && v.y == 4 ); - (void)v; - } - - { - auto v = std::min( float3( 4, 6, 20 ), float3( 40, 3, 23 ) ); - VERIFY_EXPR( v.x == 4 && v.y == 3 && v.z == 20); - (void)v; - } - - { - auto v = std::min( float4( 4, 6, 20, 100 ), float4( 40, 3, 23, 50 ) ); - VERIFY_EXPR( v.x == 4 && v.y == 3 && v.z == 20 && v.w == 50); - (void)v; - } - - - // a == b - { - VERIFY_EXPR( float2(1,2) == float2(1,2) ); - VERIFY_EXPR( float3(1,2,3) == float3(1,2,3) ); - VERIFY_EXPR( float4(1,2,3,4) == float4(1,2,3,4) ); - } - - // a != b - { - VERIFY_EXPR( float2(1,2) != float2(1,9) && float2(9,2) != float2(1,2) ); - VERIFY_EXPR( float3(1,2,3) != float3(9,2,3) && float3(1,2,3) != float3(1,9,3) && float3(1,2,3) != float3(1,2,9) ); - VERIFY_EXPR( float4(1,2,3,4) != float4(9,2,3,4) && float4(1,2,3,4) != float4(1,9,3,4) && float4(1,2,3,4) != float4(1,2,9,4) && float4(1,2,3,4) != float4(1,2,3,9) ); - } - - // a < b - { - VERIFY_EXPR( float2(1,5) < float2(3,5) == float2(1,0) ); - VERIFY_EXPR( float2(3,1) < float2(3,4) == float2(0,1) ); - VERIFY_EXPR( float3(1,5,10) < float3(3,5,20) == float3(1,0,1) ); - VERIFY_EXPR( float3(3,1,2) < float3(3,4,2) == float3(0,1,0) ); - VERIFY_EXPR( float4(1,4,10,50) < float4(3,4,20, 50) == float4(1,0,1,0) ); - VERIFY_EXPR( float4(3,1,2,30) < float4(3,4,2, 70) == float4(0,1,0,1) ); - } - - // a <= b - { - VERIFY_EXPR( float2(1,5) <= float2(1,4) == float2(1,0) ); - VERIFY_EXPR( float2(5,2) <= float2(3,2) == float2(0,1) ); - VERIFY_EXPR( float3(3,5,10) <= float3(3,4,10) == float3(1,0,1) ); - VERIFY_EXPR( float3(5,4,2) <= float3(3,4,0) == float3(0,1,0) ); - VERIFY_EXPR( float4(3,5,20,100) <= float4(3,4,20, 50) == float4(1,0,1,0) ); - VERIFY_EXPR( float4(5,4,2,70) <= float4(3,4,0, 70) == float4(0,1,0,1) ); - } - - // a >= b - { - VERIFY_EXPR( float2(1,5) >= float2(3,5) == float2(0,1) ); - VERIFY_EXPR( float2(3,1) >= float2(3,4) == float2(1,0) ); - VERIFY_EXPR( float3(1,5,10) >= float3(3,5,20) == float3(0,1,0) ); - VERIFY_EXPR( float3(3,1,2) >= float3(3,4,2) == float3(1,0,1) ); - VERIFY_EXPR( float4(1,4,10,50) >= float4(3,4,20, 50) == float4(0,1,0,1) ); - VERIFY_EXPR( float4(3,1,2,30) >= float4(3,4,2, 70) == float4(1,0,1,0) ); - } - - // a > b - { - VERIFY_EXPR( float2(1,5) > float2(1,4) == float2(0,1) ); - VERIFY_EXPR( float2(5,2) > float2(3,2) == float2(1,0) ); - VERIFY_EXPR( float3(3,5,10) > float3(3,4,10) == float3(0,1,0) ); - VERIFY_EXPR( float3(5,4,2) > float3(3,4,0) == float3(1,0,1) ); - VERIFY_EXPR( float4(3,5,20,100) > float4(3,4,20, 50) == float4(0,1,0,1) ); - VERIFY_EXPR( float4(5,4,2,70) > float4(3,4,0, 70) == float4(1,0,1,0) ); - } - - // abs - { - VERIFY_EXPR( abs(float2(-1,-5)) == float2( 1, 5) ); - VERIFY_EXPR( abs(float2( 1, 5)) == float2( 1, 5) ); - - VERIFY_EXPR( abs(float3(-1,-5, -10)) == float3( 1, 5, 10) ); - VERIFY_EXPR( abs(float3( 1, 5, 10)) == float3( 1, 5, 10) ); - - VERIFY_EXPR( abs(float4(-1,-5, -10, -100)) == float4( 1, 5, 10, 100) ); - VERIFY_EXPR( abs(float4( 1, 5, 10, 100)) == float4( 1, 5, 10, 100) ); - } - - // clamp - { - VERIFY_EXPR( clamp( -1, 1, 10) == 1 ); - VERIFY_EXPR( clamp( 11, 1, 10) == 10 ); - VERIFY_EXPR( clamp( 9, 1, 10) == 9 ); - - VERIFY_EXPR( clamp(float2(-10,-11), float2(1,2), float2(10,11)) == float2(1, 2) ); - VERIFY_EXPR( clamp(float2( 11, 12), float2(1,2), float2(10,11)) == float2(10,11) ); - VERIFY_EXPR( clamp(float2( 9, 8), float2(1,2), float2(10,11)) == float2(9, 8) ); - - VERIFY_EXPR( clamp(float3(-10, -11, -12), float3(1,2,3), float3(10,11,12)) == float3( 1, 2, 3)); - VERIFY_EXPR( clamp(float3( 11, 12, 13), float3(1,2,3), float3(10,11,12)) == float3(10,11,12)); - VERIFY_EXPR( clamp(float3( 9, 8, 7), float3(1,2,3), float3(10,11,12)) == float3( 9, 8, 7)); - - VERIFY_EXPR( clamp(float4(-10, -11, -12, -13), float4(1,2,3,4), float4(10,11,12,13)) == float4( 1, 2, 3, 4)); - VERIFY_EXPR( clamp(float4( 11, 12, 13, 14), float4(1,2,3,4), float4(10,11,12,13)) == float4(10,11,12,13)); - VERIFY_EXPR( clamp(float4( 9, 8, 7, 6), float4(1,2,3,4), float4(10,11,12,13)) == float4( 9, 8, 7, 6)); - } - - // dot - { - VERIFY_EXPR( dot( float2( 1, 2 ), float2( 1, 2 ) ) == 5 ); - VERIFY_EXPR( dot( float3( 1, 2, 3 ), float3( 1, 2, 3 ) ) == 14 ); - VERIFY_EXPR( dot( float4( 1, 2, 3, 4 ), float4( 1, 2, 3, 4 ) ) == 30 ); - } - - // length - { - auto l = length( float2(3,4) ); - VERIFY_EXPR( l >= 5.f - 1e-6f && l <= 5.f + 1e+6f ); - (void)l; - } - - // Matrix 3x3 - { - float2x2 m1(1, 2, - 5, 6); - float2x2 m2(1, 2, - 5, 6); - VERIFY_EXPR(m1._11 == 1 && m1._12 == 2 && - m1._21 == 5 && m1._22 == 6); - VERIFY_EXPR(m1[0][0] == 1 && m1[0][1] == 2 && - m1[1][0] == 5 && m1[1][1] == 6); - - VERIFY_EXPR(m1 == m2); - auto t = m1.Transpose().Transpose(); - VERIFY_EXPR(t == m1); - (void)t; - } - - // Matrix 3x3 - { - float3x3 m1( 1, 2, 3, - 5, 6, 7, - 9, 10, 11), - m2( 1, 2, 3, - 5, 6, 7, - 9, 10, 11); - VERIFY_EXPR(m1._11 == 1 && m1._12 == 2 && m1._13 == 3 && - m1._21 == 5 && m1._22 == 6 && m1._23 == 7 && - m1._31 == 9 && m1._32 == 10 && m1._33 == 11 ); - VERIFY_EXPR(m1[0][0] == 1 && m1[0][1] == 2 && m1[0][2] == 3 && - m1[1][0] == 5 && m1[1][1] == 6 && m1[1][2] == 7 && - m1[2][0] == 9 && m1[2][1] == 10 && m1[2][2] == 11 ); - - VERIFY_EXPR( m1 == m2 ); - auto t = m1.Transpose().Transpose(); - VERIFY_EXPR( t == m1 ); - (void)t; - } - - // Matrix 4x4 - { - float4x4 m1( 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16), - m2( 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16) ; - VERIFY_EXPR(m1._11 == 1 && m1._12 == 2 && m1._13 == 3 && m1._14 == 4 && - m1._21 == 5 && m1._22 == 6 && m1._23 == 7 && m1._24 == 8 && - m1._31 == 9 && m1._32 == 10 && m1._33 == 11 && m1._34 == 12 && - m1._41 == 13 && m1._42 == 14 && m1._43 == 15 && m1._44 == 16 ); - VERIFY_EXPR(m1[0][0] == 1 && m1[0][1] == 2 && m1[0][2] == 3 && m1[0][3] == 4 && - m1[1][0] == 5 && m1[1][1] == 6 && m1[1][2] == 7 && m1[1][3] == 8 && - m1[2][0] == 9 && m1[2][1] == 10 && m1[2][2] == 11 && m1[2][3] == 12 && - m1[3][0] == 13 && m1[3][1] == 14 && m1[3][2] == 15 && m1[3][3] == 16 ); - - VERIFY_EXPR( m1 == m2 ); - auto t = m1.Transpose().Transpose(); - VERIFY_EXPR( t == m1 ); - (void)t; - } - - // Inverse - { - float4x4 m( 7, 8, 3, 6, - 5, 1, 4, 9, - 5, 11, 7, 2, - 13, 4, 19, 8); - auto inv = m.Inverse(); - auto identity = m * inv; - for( int j = 0; j < 4; ++j) - for( int i = 0; i < 4; ++i ) - { - float ref = i == j ? 1.f : 0.f; - auto val = identity[i][j]; - VERIFY_EXPR( fabs( val - ref ) < 1e-6f ); - (void)ref;(void)val; - } - } - - // Determinant - { - float4x4 m1( 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16); - auto det = m1.Determinant(); - VERIFY_EXPR( det == 0 ); - (void)det; - } - - { - std::hash()(float2(1.0, 2.0)); - std::hash()(float3(1.0, 2.0, 3.0)); - std::hash()(float4(1.0, 2.0, 3.0, 5.0)); - float4x4 m1( 1, 2, 3, 4, - 5, 6, 7, 8, - 9, 10, 11, 12, - 13, 14, 15, 16); - std::hash()(m1); - - float3x3 m2( 1, 2, 3, - 5, 6, 7, - 9, 10, 11); - std::hash()(m2); - - float2x2 m3(1, 2, - 5, 6); - std::hash()(m3); - } - - // Test ortho projection matrix - { - { - float4x4 OrthoProj = float4x4::Ortho(2.f, 4.f, -4.f, 12.f, false); - auto c0 = float3(-1.f, -2.f, -4.f) * OrthoProj; - auto c1 = float3(+1.f, +2.f, +12.f) * OrthoProj; - VERIFY_EXPR(c0 == float3(-1, -1, 0) && c1 == float3(+1,+1,+1) ); - (void)c0;(void)c1; - } - - { - float4x4 OrthoProj = float4x4::Ortho(2.f, 4.f, -4.f, 12.f, true); - auto c0 = float3(-1.f, -2.f, -4.f) * OrthoProj; - auto c1 = float3(+1.f, +2.f, +12.f) * OrthoProj; - VERIFY_EXPR(c0 == float3(-1, -1, -1) && c1 == float3(+1, +1, +1)); - (void)c0;(void)c1; - } - - { - float4x4 OrthoProj = float4x4::OrthoOffCenter(-2.f, 6.f, -4.f, +12.f, -6.f, 10.f, false); - auto c0 = float3(-2.f, -4.f, -6.f) * OrthoProj; - auto c1 = float3(+6.f, +12.f, +10.f) * OrthoProj; - VERIFY_EXPR(c0 == float3(-1, -1, 0) && c1 == float3(+1, +1, +1)); - (void)c0;(void)c1; - } - - { - float4x4 OrthoProj = float4x4::OrthoOffCenter(-2.f, 6.f, -4.f, +12.f, -6.f, 10.f, true); - auto c0 = float3(-2.f, -4.f, -6.f) * OrthoProj; - auto c1 = float3(+6.f, +12.f, +10.f) * OrthoProj; - VERIFY_EXPR(c0 == float3(-1, -1, -1) && c1 == float3(+1, +1, +1)); - (void)c0;(void)c1; - } - } - - { - Plane3D plane = {}; - std::hash()(plane); - - ViewFrustum frustum = {}; - std::hash()(frustum); - - ViewFrustumExt frustm_ext = {}; - std::hash()(frustm_ext); - } - - { - float4 vec4(1,2,3,4); - float3 vec3 = vec4; - VERIFY_EXPR(vec3== float3(1, 2, 3)); - (void)vec3; - } - - { - double data[] = {1,2,3,4, - 5,6,7,8, - 9,10,11,12, - 13,14,15,16}; - VERIFY_EXPR(float2::MakeVector(data) == float2(1, 2)); - VERIFY_EXPR(float3::MakeVector(data) == float3(1, 2, 3)); - VERIFY_EXPR(float4::MakeVector(data) == float4(1, 2, 3, 4)); - VERIFY_EXPR(Quaternion::MakeQuaternion(data) == Quaternion(1,2,3,4)); - VERIFY_EXPR(float4x4::MakeMatrix(data) == float4x4(1,2,3,4, - 5,6,7,8, - 9,10,11,12, - 13,14,15,16)); - VERIFY_EXPR(float3x3::MakeMatrix(data) == float3x3(1,2,3, - 4,5,6, - 7,8,9)); - VERIFY_EXPR(float2x2::MakeMatrix(data) == float2x2(1,2, - 3,4)); - (void)data; - } - - { - { - float2x2 m1(1,2, - 3,4); - float2x2 m2(5,6, - 7,8); - auto m = m1; - m *= m2; - VERIFY_EXPR(m == m1*m2); - } - { - float3x3 m1(1,2,3, - 4,5,6, - 7,8,9); - float3x3 m2(10,11,12, - 13,14,15, - 16,17,18); - auto m = m1; - m *= m2; - VERIFY_EXPR(m == m1*m2); - } - { - float4x4 m1(1,2,3,4, - 5,6,7,8, - 9,10,11,12, - 13,14,15,16); - float4x4 m2(17,18,19,20, - 21,22,23,24, - 25,26,27,28, - 29,30,31,32); - auto m = m1; - m *= m2; - VERIFY_EXPR(m == m1*m2); - } - } - - { - VERIFY_EXPR(float2(1,2).Recast() == Vector2(1,2)); - VERIFY_EXPR(float3(1,2,3).Recast() == Vector3(1,2,3)); - VERIFY_EXPR(float4(1,2,3,4).Recast() == Vector4(1,2,3,4)); - } - - { - VERIFY_EXPR(std::floor(float2(0.1f, 1.2f)) == float2(0,1)); - VERIFY_EXPR(std::floor(float3(0.1f, 1.2f, 2.3f)) == float3(0,1,2)); - VERIFY_EXPR(std::floor(float4(0.1f, 1.2f, 2.3f, 3.4f)) == float4(0,1,2,3)); - VERIFY_EXPR(std::ceil(float2(0.1f, 1.2f)) == float2(1,2)); - VERIFY_EXPR(std::ceil(float3(0.1f, 1.2f, 2.3f)) == float3(1,2,3)); - VERIFY_EXPR(std::ceil(float4(0.1f, 1.2f, 2.3f, 3.4f)) == float4(1,2,3,4)); - } - - HermiteSpline(float3(1,2,3), float3(4,5,6), float3(7,8,9), float3(10, 11, 12), 0.1f); - HermiteSpline(double3(1,2,3), double3(4,5,6), double3(7,8,9), double3(10, 11, 12), 0.1); - - SetStatus(TestResult::Succeeded); - } -}; - -static MathLibTest g_TheMathLibTest; diff --git a/Tests/TestApp/src/RingBufferTest.cpp b/Tests/TestApp/src/RingBufferTest.cpp deleted file mode 100644 index 14c942e..0000000 --- a/Tests/TestApp/src/RingBufferTest.cpp +++ /dev/null @@ -1,297 +0,0 @@ -/* Copyright 2019 Diligent Graphics LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -// EngineSandbox.cpp : Defines the entry point for the application. -// - -#include "pch.h" -#include "RingBuffer.h" -#include "DefaultRawMemoryAllocator.h" -#include "DebugUtilities.h" -#include "UnitTestBase.h" - -using namespace Diligent; - -class RingBufferTest : public UnitTestBase -{ -public: - RingBufferTest(); -}; - -static RingBufferTest TheRingBufferTest; - -RingBufferTest::RingBufferTest() : - UnitTestBase("Ring buffer test") -{ - auto &Allocator = DefaultRawMemoryAllocator::GetAllocator(); - { - RingBuffer RB(1023, Allocator); - - auto Offset = RB.Allocate(120, 16); - // - // O h - // | | | - // 0 128 - VERIFY_EXPR(Offset == 0); - - - Offset = RB.Allocate(10, 1); - // - // t O h - // | | | | - // 0 128 138 - VERIFY_EXPR(Offset == 128); - - Offset = RB.Allocate(10, 32); - // - // t O h - // | | | | - // 0 128 138 160 192 - VERIFY_EXPR(Offset == 160); - - Offset = RB.Allocate(17, 1); - // - // t O h - // | | | | - // 0 128 138 160 192 209 - VERIFY_EXPR(Offset == 192); - - Offset = RB.Allocate(65, 64); - // - // t O h - // | | | | - // 0 128 138 160 192 209 256 384 - VERIFY_EXPR(Offset == 256); - - RB.FinishCurrentFrame(1); - // - // t h1 - // | | | - // 0 384 - - - Offset = RB.Allocate(100, 256); - // - // t h1 O h - // | | | | | - // 0 384 512 768 - VERIFY_EXPR(Offset == 512); - - - Offset = RB.Allocate(127, 1); - // - // t h1 O h - // | | | | | - // 0 384 512 768 895 = 1023-128 - VERIFY_EXPR(Offset == 768); - - Offset = RB.Allocate(128, 2); - VERIFY_EXPR(Offset == RingBuffer::InvalidOffset); - - Offset = RB.Allocate(128, 1); - // - // t h1 O h - // | | | | - // 0 384 512 768 895 1023 - VERIFY_EXPR(Offset == 895); - - RB.FinishCurrentFrame(2); - // - // t h1 h2,h - // | | | - // 0 384 1023 - VERIFY_EXPR(RB.IsFull()); - - Offset = RB.Allocate(1, 1); - VERIFY_EXPR(Offset == RingBuffer::InvalidOffset); - - RingBuffer RB1(std::move(RB)); - VERIFY_EXPR(RB.IsEmpty()); - RB1.ReleaseCompletedFrames(1); - // - // t h2 - // | | | - // 0 384 1023 - - RB1.ReleaseCompletedFrames(2); - // - // h,t - // | | - // 0 1023 - VERIFY_EXPR(RB1.IsEmpty()); - VERIFY_EXPR(RB1.GetUsedSize() == 0); - - Offset = RB1.Allocate(256,1); - // - // O h t - // | | | - // 0 256 1023 - VERIFY_EXPR(Offset == 0); - - - Offset = RB1.Allocate(256, 16); - RB1.ReleaseCompletedFrames(0); - // - // O h t - // | | | | - // 0 256 512 1023 - VERIFY_EXPR(Offset == 256); - RB1.FinishCurrentFrame(2); - RB1.FinishCurrentFrame(3); // ignored - // - // h2 t - // | | | - // 0 512 1023 - RB1.ReleaseCompletedFrames(2); - // - // h,t - // | | - // 0 1023 - - RB1.ReleaseCompletedFrames(3); - - VERIFY_EXPR(RB1.GetUsedSize() == 0); - VERIFY_EXPR(RB1.IsEmpty()); - - Offset = RB1.Allocate(512,1); - // - // O h - // | | | - // 0 512 1023 - VERIFY_EXPR(Offset == 0); - - RB1.FinishCurrentFrame(4); - RB1.FinishCurrentFrame(5); - // - // t h4 - // | | | - // 0 512 1023 - - Offset = RB1.Allocate(129,1); - // - // t h4,O h - // | | | | - // 0 512 641 1023 - VERIFY_EXPR(Offset == 512); - - RB1.ReleaseCompletedFrames(4); - // - // t h - // | | | | - // 0 512 641 1023 - - - Offset = RB1.Allocate(128,128); - // - // t O h - // | | | | | - // 0 512 641 768 896 1023 - VERIFY_EXPR(Offset == 768); - - Offset = RB1.Allocate(513,64); - VERIFY_EXPR(Offset == RingBuffer::InvalidOffset); - - Offset = RB1.Allocate(513,1); - VERIFY_EXPR(Offset == RingBuffer::InvalidOffset); - - Offset = RB1.Allocate(255,1); - // - // O h t - // | | | | - // 0 255 512 1023 - VERIFY_EXPR(Offset == 0); - - RB1.FinishCurrentFrame(6); - // - // O h,h6 t - // | | | | - // 0 255 512 1023 - - - Offset = RB1.Allocate(256, 2); - // - // h6 O t,h - // | | | | | - // 0 255 256 512 1023 - VERIFY_EXPR(Offset == 256); - - VERIFY_EXPR(RB1.IsFull()); - Offset = RB1.Allocate(1,1); - VERIFY_EXPR(Offset == RingBuffer::InvalidOffset); - RB1.ReleaseCompletedFrames(6); - // - // t h - // | | | | - // 0 255 512 1023 - - Offset = RB1.Allocate(511, 1); - // - // t O h - // | | | | - // 0 255 512 1023 - VERIFY_EXPR(Offset == 512); - - Offset = RB1.Allocate(191,1); - // - // O h t - // | | | | - // 0 191 255 1023 - VERIFY_EXPR(Offset == 0); - - Offset = RB1.Allocate(64,2); - VERIFY_EXPR(Offset == RingBuffer::InvalidOffset); - - Offset = RB1.Allocate(64,1); - // - // O t,h - // | | | | - // 0 191 255 1023 - VERIFY_EXPR(Offset == 191); - - Offset = RB1.Allocate(1,1); - VERIFY_EXPR(Offset == RingBuffer::InvalidOffset); - - RB1.FinishCurrentFrame(7); - RB1.ReleaseCompletedFrames(7); - } - - { - RingBuffer RB(1024, Allocator); - auto offset = RB.Allocate(512, 1); - RB.FinishCurrentFrame(0); - RB.FinishCurrentFrame(1); - RB.ReleaseCompletedFrames(1); - RB.FinishCurrentFrame(2); - RB.FinishCurrentFrame(3); - offset = RB.Allocate(512, 1); - RB.FinishCurrentFrame(4); - RB.ReleaseCompletedFrames(2); - RB.ReleaseCompletedFrames(3); - RB.ReleaseCompletedFrames(4); - offset = RB.Allocate(512, 1); - RB.FinishCurrentFrame(5); - RB.ReleaseCompletedFrames(5); - } - - SetStatus(TestResult::Succeeded); -} diff --git a/Tests/TestApp/src/SmartPointerTest.cpp b/Tests/TestApp/src/SmartPointerTest.cpp deleted file mode 100644 index 850ca99..0000000 --- a/Tests/TestApp/src/SmartPointerTest.cpp +++ /dev/null @@ -1,797 +0,0 @@ -/* Copyright 2019 Diligent Graphics LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. - * - * In no event and under no legal theory, whether in tort (including negligence), - * contract, or otherwise, unless required by applicable law (such as deliberate - * and grossly negligent acts) or agreed to in writing, shall any Contributor be - * liable for any damages, including any direct, indirect, special, incidental, - * or consequential damages of any character arising as a result of this License or - * out of the use or inability to use the software (including but not limited to damages - * for loss of goodwill, work stoppage, computer failure or malfunction, or any and - * all other commercial damages or losses), even if such Contributor has been advised - * of the possibility of such damages. - */ - -// EngineSandbox.cpp : Defines the entry point for the application. -// - -#include "pch.h" -#include -#include "SmartPointerTest.h" -#include "Errors.h" -#include "DefaultRawMemoryAllocator.h" - -using namespace Diligent; - -template -Type* MakeNewObj() -{ - return MakeNewRCObj().operator()(); -} - -void SmartPointerTest::CreateObject(Object **ppObj) -{ - *ppObj = MakeNewObj(); - (*ppObj)->AddRef(); -} - -void SmartPointerTest::WaitForThreadStart(int VarId) -{ - std::unique_lock lk(m_Mtx); - m_CondVar.wait(lk, [&]{return m_bThreadStart[VarId] || m_bStopThreads;}); -} - -void SmartPointerTest::StartThreadsAndWait(int VarId, size_t NumThreads) -{ - { - std::unique_lock lk(m_Mtx); - m_bThreadStart[VarId] = true; - m_NumThreadsCompleted = 0; - } - m_CondVar.notify_all(); - - while((size_t)m_NumThreadsCompleted < NumThreads) - std::this_thread::yield(); - m_bThreadStart[VarId] = false; - VERIFY_EXPR(m_NumThreadsCompleted == NumThreads); -} - -void SmartPointerTest::WorkerThreadFunc(SmartPointerTest *This, size_t ThreadNum) -{ - while(!This->m_bStopThreads) - { - for(int i =0; i < NumThreadInterations; ++i) - { - // Wait until main() sends data - This->WaitForThreadStart(0); - if(This->m_bStopThreads) - { - This->m_NumThreadsCompleted++; - return; - } - - { - auto *pObject = This->m_pSharedObject; - for(int j=0; j < 100; ++j) - { - //LOG_INFO_MESSAGE("t",std::this_thread::get_id(), ": AddRef" ); - pObject->m_Value++; - pObject->AddRef(); - } - This->m_NumThreadsCompleted++; - - This->WaitForThreadStart(1); - for(int j=0; j < 100; ++j) - { - //LOG_INFO_MESSAGE("t",std::this_thread::get_id(), ": Release" ); - pObject->m_Value--; - pObject->Release(); - } - This->m_NumThreadsCompleted++; - } - - { - This->WaitForThreadStart(0); - auto *pObject = This->m_pSharedObject; - auto *pRefCounters = pObject->GetReferenceCounters(); - if (ThreadNum % 3 == 0) - { - pObject->m_Value++; - pObject->AddRef(); - } - else - pRefCounters->AddWeakRef(); - This->m_NumThreadsCompleted++; - - This->WaitForThreadStart(1); - if (ThreadNum % 3 == 0) - { - pObject->m_Value--; - pObject->Release(); - } - else - pRefCounters->ReleaseWeakRef(); - This->m_NumThreadsCompleted++; - } - - { - // Test interferences of ReleaseStrongRef() and GetObject() - - // Goal: catch scenario when GetObject() runs between - // AtomicDecrement() and acquiring the lock in ReleaseStrongRef(): - - // m_lNumStrongReferences == 1 - // - - // Scenario I - // - // Thread 1 | Thread 2 | Thread 3 - // | | - // | | - // | | - // | 1. Acquire the lock | - // | 2. Increment m_lNumStrongReferences | - // 1. Decrement m_lNumStrongReferences | 3. Read StrongRefCnt > 1 | - // 2. Test RefCount!=0 | 4. Return the reference to object | - // 3. DO NOT destroy the object | | - // 4. Wait for the lock | | - - - // Scenario I - // - // Thread 1 | Thread 2 | Thread 3 - // | | - // | | - // 1. Decrement m_lNumStrongReferences | | - // | 1. Acquire the lock | - // 2. Test RefCount==0 | 2. Increment m_lNumStrongReferences | - // 3. Start destroying the object | 3. Read StrongRefCnt == 1 | - // 4. Wait for the lock | 4. DO NOT create the object | - // | 5. Decrement m_lNumStrongReferences | - // | | 1. Acquire the lock - // | | 2. Increment m_lNumStrongReferences - // | | 3. Read StrongRefCnt == 1 - // | | 4. DO NOT create the object - // | | 5. Decrement m_lNumStrongReferences - // 5. Acquire the lock | - // 6. DESTROY the object | - - This->WaitForThreadStart(0); - auto *pObject = This->m_pSharedObject; - RefCntWeakPtr weakPtr(pObject); - RefCntAutoPtr strongPtr, strongPtr2; - if (ThreadNum < 2) - { - strongPtr = pObject; - strongPtr->m_Value++; - } - else - weakPtr = WeakPtr(pObject); - This->m_NumThreadsCompleted++; - - This->WaitForThreadStart(1); - if (ThreadNum == 0) - { - strongPtr->m_Value--; - strongPtr.Release(); - } - else - { - strongPtr2 = weakPtr.Lock(); - if(strongPtr2) - strongPtr2->m_Value++; - weakPtr.Release(); - } - This->m_NumThreadsCompleted++; - } - - - { - This->WaitForThreadStart(0); - auto *pObject = This->m_pSharedObject; - RefCntWeakPtr weakPtr; - RefCntAutoPtr strongPtr; - if (ThreadNum % 4 == 0) - { - strongPtr = pObject; - strongPtr->m_Value++; - } - else - weakPtr = WeakPtr(pObject); - This->m_NumThreadsCompleted++; - - This->WaitForThreadStart(1); - if (ThreadNum % 4 == 0) - { - strongPtr->m_Value--; - strongPtr.Release(); - } - else - { - auto Ptr = weakPtr.Lock(); - if(Ptr) - Ptr->m_Value++; - Ptr.Release(); - } - This->m_NumThreadsCompleted++; - } - } - } -} - -void SmartPointerTest::StartConcurrencyTest() -{ - auto numCores = std::thread::hardware_concurrency(); - m_Threads.resize(numCores); - for(auto &t : m_Threads) - t = std::thread(WorkerThreadFunc, this, &t-m_Threads.data()); -} - -SmartPointerTest::~SmartPointerTest() -{ - m_bStopThreads = true; - StartThreadsAndWait(0, m_Threads.size()); - - for(auto &t : m_Threads) - t.join(); - - auto NumIterations = NumThreadInterations; - std::stringstream infoss; - infoss << "Performed " << m_NumTestsPerformed << " concurrency tests with " << NumIterations << " iterations on " << m_Threads.size() << " threads"; - SetStatus(TestResult::Succeeded, infoss.str().c_str()); -} - -void SmartPointerTest::RunConcurrencyTest() -{ - for(int i=0;i(); - - StartThreadsAndWait(0, m_Threads.size()); - - StartThreadsAndWait(1, m_Threads.size()); - - m_pSharedObject = MakeNewObj(); - - StartThreadsAndWait(0, m_Threads.size()); - - StartThreadsAndWait(1, m_Threads.size()); - - m_pSharedObject = MakeNewObj(); - - StartThreadsAndWait(0, m_Threads.size()); - - StartThreadsAndWait(1, m_Threads.size()); - - m_pSharedObject = MakeNewObj(); - - StartThreadsAndWait(0, m_Threads.size()); - - StartThreadsAndWait(1, m_Threads.size()); - } - ++m_NumTestsPerformed; -} - - -SmartPointerTest::SmartPointerTest() : - UnitTestBase("Smart pointer test"), - m_pSharedObject(nullptr), - m_NumThreadsCompleted(0) -{ - // Test constructors of RefCntAutoPtr - { - SmartPtr SP0; - SmartPtr SP1(nullptr); - auto *pRawPtr = MakeNewObj(); - SmartPtr SP2(pRawPtr); - SmartPtr SP2_1(pRawPtr); - - SmartPtr SP3(SP0); - SmartPtr SP4(SP2); - SmartPtr SP5(std::move(SP3)); - VERIFY_EXPR(!SP3); - SmartPtr SP6(std::move(SP4)); - VERIFY_EXPR(!SP4); - - RefCntAutoPtr DerivedSP(MakeNewObj()); - SmartPtr SP7(DerivedSP); - SmartPtr SP8(std::move(DerivedSP)); - VERIFY_EXPR(!DerivedSP); - } - - // Test Attach/Detach - { - { - SmartPtr SP0; - auto *pRawPtr = MakeNewObj(); - SP0.Attach(nullptr); - SP0.Attach(pRawPtr); - pRawPtr->AddRef(); - } - - { - SmartPtr SP0; - auto *pRawPtr = MakeNewObj(); - pRawPtr->AddRef(); - SP0.Attach(pRawPtr); - SP0.Attach(nullptr); - } - - { - SmartPtr SP0(MakeNewObj()); - auto *pRawPtr = MakeNewObj(); - SP0.Attach(pRawPtr); - pRawPtr->AddRef(); - } - - { - SmartPtr SP0(MakeNewObj()); - auto *pRawPtr = MakeNewObj(); - pRawPtr->AddRef(); - SP0.Attach(pRawPtr); - auto *pRawPtr2 = SP0.Detach(); - pRawPtr2->Release(); - - auto *pRawPtr3 = SmartPtr().Detach(); - (void)pRawPtr3; - auto *pRawPtr4 = SmartPtr(MakeNewObj()).Detach(); - (void)pRawPtr4; - pRawPtr4->Release(); - } - } - - // Test operator = - { - SmartPtr SP0; - auto pRawPtr1 = MakeNewObj(); - SmartPtr SP1(pRawPtr1); - SmartPtr SP2(pRawPtr1); - SP0 = SP0; - SP0 = std::move(SP0); - SP0 = nullptr; - assert(SP0 == nullptr); - - SP1 = pRawPtr1; - SP1 = SP1; - SP1 = std::move(SP1); - assert(SP1 == pRawPtr1); - - SP1 = SP2; - SP1 = std::move(SP2); - assert(SP1 == pRawPtr1); - - auto pRawPtr2 = MakeNewObj(); - SmartPtr SP3(pRawPtr2); - - SP0 = pRawPtr2; - SmartPtr SP4; - SP4 = SP3; - SmartPtr SP5; - SP5 = std::move(SP4); - VERIFY_EXPR(!SP4); - - SP1 = pRawPtr2; - SP1 = nullptr; - SP1 = std::move(SP5); - VERIFY_EXPR(!SP5); - - RefCntAutoPtr DerivedSP(MakeNewObj()); - SP1 = DerivedSP; - SP2 = std::move(DerivedSP); - VERIFY_EXPR(!DerivedSP); - } - - // Test logical operators - { - auto pRawPtr1 = MakeNewObj(); - auto pRawPtr2 = MakeNewObj(); - SmartPtr SP0, SP1(pRawPtr1), SP2(pRawPtr1), SP3(pRawPtr2); - assert( !SP0 ); - bool b1 = SP0.operator bool(); - assert( !b1 ); (void)b1; - if(SP0) - assert( false ); - - assert( !(!SP1) ); - assert( SP1 ); - assert( SP0 != SP1 ); - assert( SP0 == SP0 ); - assert( SP1 == SP1 ); - assert( SP1 == SP2 ); - assert( SP1 != SP3 ); - assert( SP0 < SP3 ); - assert( (SP1 < SP3) == (pRawPtr1()), SP2, SP3, SP4(MakeNewObj()); - auto *pRawPtr = MakeNewObj(); - pRawPtr->AddRef(); - - *static_cast(&SP0) = pRawPtr; - SP0.Detach(); - *&SP2 = pRawPtr; - SP2.Detach(); - - CreateObject(&SP3); - - CreateObject(&SP1); - *static_cast(&SP4) = pRawPtr; - - { - SmartPtr SP5(MakeNewObj()); - auto pDblPtr = &SP5; - *pDblPtr = MakeNewObj(); - (*pDblPtr)->AddRef(); - auto pDblPtr2 = &SP5; - CreateObject(pDblPtr2); - } - - SmartPtr SP6(MakeNewObj()); - // This will not work: - // Object **pDblPtr3 = &SP6; - // *pDblPtr3 = new Object; - } - - // Test constructors of RefCntWeakPtr - { - SmartPtr SP0, SP1(MakeNewObj()); - WeakPtr WP0; - WeakPtr WP1(WP0); - WeakPtr WP2(SP0); - WeakPtr WP3(SP1); - WeakPtr WP4(WP3); - WeakPtr WP5(std::move(WP0)); - WeakPtr WP6(std::move(WP4)); - - auto *pRawPtr = MakeNewObj(); - pRawPtr->AddRef(); - WeakPtr WP7(pRawPtr); - pRawPtr->Release(); - } - - // Test operator = - { - auto *pRawPtr = MakeNewObj(); - SmartPtr SP0, SP1(pRawPtr); - WeakPtr WP0, WP1(SP1), WP2(SP1); - WP0 = WP0; - WP0 = std::move(WP0); - WP1 = WP1; - WP1 = std::move(WP1); - WP1 = WP2; - WP1 = std::move(WP2); - WP1 = pRawPtr; - WP0 = pRawPtr; - WP0.Release(); - WP0 = WP2; - - WP1 = WP0; - WP0 = SP1; - WP2 = std::move(WP1); - } - - // Test logical operators - { - SmartPtr SP0, SP1(MakeNewObj()); - WeakPtr WP0, WP1(SP0), WP2(SP1), WP3(SP1); - assert( WP0 == WP1 ); - assert( WP0 != WP2 ); - assert( WP2 == WP3 ); - SP1.Release(); - assert( WP2 == WP3 ); - } - - // Test Lock() - { - SmartPtr SP0, SP1(MakeNewObj()); - WeakPtr WP0, WP1(SP0), WP2(SP1); - WeakPtr WP3(WP2); - auto L1 = WP0.Lock(); - assert( !L1 ); - L1 = WP1.Lock(); - assert( !L1 ); - L1 = WP2.Lock(); - assert( L1 ); - L1 = WP3.Lock(); - assert( L1 ); - auto pRawPtr = SP1.Detach(); - L1.Release(); - - L1 = WP3.Lock(); - assert( L1 ); - L1.Release(); - - pRawPtr->Release(); - - L1 = WP3.Lock(); - assert( !L1 ); - } - - { - class OwnerTest : public RefCountedObject - { - public: - OwnerTest(IReferenceCounters *pRefCounters) : - RefCountedObject(pRefCounters), - Obj( NEW_RC_OBJ( DefaultRawMemoryAllocator::GetAllocator(), "Test object", Object, this)()) - { - } - - virtual void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface ){} - ~OwnerTest() - { - Obj->~Object(); - DefaultRawMemoryAllocator::GetAllocator().Free(Obj); - } - private: - Object *Obj; - }; - - OwnerTest *pOwnerObject = MakeNewObj(); - pOwnerObject->AddRef(); - pOwnerObject->Release(); - } - - { - class SelfRefTest : public RefCountedObject - { - public: - SelfRefTest(IReferenceCounters *pRefCounters) : - RefCountedObject(pRefCounters), - wpSelf( this ) - {} - - virtual void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface ){} - private: - RefCntWeakPtr wpSelf; - }; - - SelfRefTest *pSelfRefTest = MakeNewObj(); - pSelfRefTest->AddRef(); - pSelfRefTest->Release(); - } - - { - class ExceptionTest1 : public RefCountedObject - { - public: - ExceptionTest1(IReferenceCounters *pRefCounters) : - RefCountedObject(pRefCounters), - wpSelf(this) - { - throw std::runtime_error("test exception"); - } - - virtual void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface ){} - private: - RefCntWeakPtr wpSelf; - }; - - try - { - auto *pExceptionTest = MakeNewObj(); - (void)pExceptionTest; - } - catch(std::runtime_error &) - { - - } - } - - { - class ExceptionTest2 : public RefCountedObject - { - public: - ExceptionTest2(IReferenceCounters *pRefCounters) : - RefCountedObject(pRefCounters), - wpSelf(this) - { - throw std::runtime_error("test exception"); - } - - virtual void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface ){} - private: - RefCntWeakPtr wpSelf; - }; - - try - { - auto *pExceptionTest = NEW_RC_OBJ( DefaultRawMemoryAllocator::GetAllocator(), "Test object", ExceptionTest2)(); - (void)pExceptionTest; - } - catch(std::runtime_error &) - { - - } - } - - { - class ExceptionTest3 : public RefCountedObject - { - public: - ExceptionTest3(IReferenceCounters *pRefCounters) : - RefCountedObject(pRefCounters), - m_Member(*this) - { - } - - class Subclass - { - public: - Subclass(ExceptionTest3 &parent) : - wpSelf(&parent) - { - throw std::runtime_error("test exception"); - } - private: - RefCntWeakPtr wpSelf; - }; - virtual void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface ){} - private: - Subclass m_Member; - }; - - try - { - auto *pExceptionTest = NEW_RC_OBJ( DefaultRawMemoryAllocator::GetAllocator(), "Test object", ExceptionTest3)(); - (void)pExceptionTest; - } - catch(std::runtime_error &) - { - - } - } - - { - class OwnerObject : public RefCountedObject - { - public: - OwnerObject(IReferenceCounters *pRefCounters) : - RefCountedObject(pRefCounters) - {} - - void CreateMember() - { - try - { - m_pMember = NEW_RC_OBJ( DefaultRawMemoryAllocator::GetAllocator(), "Test object", ExceptionTest4, this)(*this); - } - catch (...) - { - - } - } - virtual void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface ){} - - class ExceptionTest4 : public RefCountedObject - { - public: - ExceptionTest4(IReferenceCounters *pRefCounters, OwnerObject &owner) : - RefCountedObject(pRefCounters), - m_Member(owner, *this) - { - } - - class Subclass - { - public: - Subclass(OwnerObject &owner, ExceptionTest4 &parent) : - wpParent(&parent), - wpOwner(&owner) - { - throw std::runtime_error("test exception"); - } - private: - RefCntWeakPtr wpParent; - RefCntWeakPtr wpOwner; - }; - virtual void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface ){} - private: - Subclass m_Member; - }; - - RefCntAutoPtr m_pMember; - }; - - RefCntAutoPtr pOwner( NEW_RC_OBJ( DefaultRawMemoryAllocator::GetAllocator(), "Test object", OwnerObject)() ); - pOwner->CreateMember(); - } - - - { - class OwnerObject : public RefCountedObject - { - public: - OwnerObject(IReferenceCounters *pRefCounters) : - RefCountedObject(pRefCounters) - { - m_pMember = NEW_RC_OBJ( DefaultRawMemoryAllocator::GetAllocator(), "Test object", ExceptionTest4, this)(*this); - } - - virtual void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface ){} - - class ExceptionTest4 : public RefCountedObject - { - public: - ExceptionTest4(IReferenceCounters *pRefCounters, OwnerObject &owner) : - RefCountedObject(pRefCounters), - m_Member(owner, *this) - { - } - - class Subclass - { - public: - Subclass(OwnerObject &owner, ExceptionTest4 &parent) : - wpParent(&parent), - wpOwner(&owner) - { - throw std::runtime_error("test exception"); - } - private: - RefCntWeakPtr wpParent; - RefCntWeakPtr wpOwner; - }; - virtual void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface ){} - private: - Subclass m_Member; - }; - - RefCntAutoPtr m_pMember; - }; - - try - { - RefCntAutoPtr pOwner( NEW_RC_OBJ( DefaultRawMemoryAllocator::GetAllocator(), "Test object", OwnerObject)() ); - } - catch (...) - { - - } - } - - { - class TestObject : public RefCountedObject - { - public: - TestObject(IReferenceCounters *pRefCounters) : - RefCountedObject(pRefCounters) - { - } - - virtual void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface )override final{} - - inline virtual Atomics::Long Release()override final - { - return RefCountedObject::Release( - [&]() - { - ppWeakPtr->Release(); - } - ); - } - RefCntWeakPtr* ppWeakPtr = nullptr; - }; - - RefCntAutoPtr pObj( NEW_RC_OBJ( DefaultRawMemoryAllocator::GetAllocator(), "Test object", TestObject)() ); - RefCntWeakPtr pWeakPtr(pObj); - - pObj->ppWeakPtr = &pWeakPtr; - pObj.Release(); - } - - StartConcurrencyTest(); - RunConcurrencyTest(); -} - diff --git a/Tests/TestApp/src/TestApp.cpp b/Tests/TestApp/src/TestApp.cpp index ea8dc0f..f01ba31 100644 --- a/Tests/TestApp/src/TestApp.cpp +++ b/Tests/TestApp/src/TestApp.cpp @@ -666,7 +666,6 @@ void TestApp::WindowResize(int width, int height) void TestApp::Update(double CurrTime, double ElapsedTime) { - m_SmartPointerTest.RunConcurrencyTest(); m_CurrTime = CurrTime; } -- cgit v1.2.3