From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Serialize/Blobification/BlobTests.cpp | 358 ++++++++++++++++++++++++++ 1 file changed, 358 insertions(+) create mode 100644 Runtime/Serialize/Blobification/BlobTests.cpp (limited to 'Runtime/Serialize/Blobification/BlobTests.cpp') diff --git a/Runtime/Serialize/Blobification/BlobTests.cpp b/Runtime/Serialize/Blobification/BlobTests.cpp new file mode 100644 index 0000000..8b0a2d3 --- /dev/null +++ b/Runtime/Serialize/Blobification/BlobTests.cpp @@ -0,0 +1,358 @@ +#include "UnityPrefix.h" + +#if ENABLE_UNIT_TESTS && !(UNITY_LINUX && UNITY_64) +#include "External/UnitTest++/src/UnitTest++.h" +#include +#include "Runtime/Math/Vector3.h" +#include "Runtime/Math/Simd/math.h" +#include "Runtime/mecanim/defs.h" +#include "Runtime/mecanim/types.h" +#include "Runtime/Allocator/MemoryManager.h" +#include "Runtime/Serialize/SerializeTraits.h" +#include "Runtime/Serialize/Blobification/offsetptr.h" +#include "Runtime/Serialize/Blobification/BlobWrite.h" + +template TYPE* Construct() +{ + #if ENABLE_MEMORY_MANAGER + void* _where = GetMemoryManager().Allocate (sizeof(TYPE), ALIGN_OF(TYPE), MemLabelId(kMemNewDeleteId, NULL), kAllocateOptionNone, "Construct"); + #else + void* _where = malloc(sizeof(TYPE)); + #endif + return new(_where) TYPE; +} + +template TYPE* ConstructArray(size_t num) +{ + #if ENABLE_MEMORY_MANAGER + void* _where = GetMemoryManager().Allocate (sizeof(TYPE)*num, ALIGN_OF(TYPE), MemLabelId(kMemNewDeleteId, NULL), kAllocateOptionNone, "ConstructArray"); + #else + void* _where = malloc(sizeof(TYPE)*num); + #endif + return new(_where) TYPE[num]; +} + +template void Destruct(TYPE* p) +{ + #if ENABLE_MEMORY_MANAGER + GetMemoryManager().Deallocate (p, MemLabelId(kMemNewDeleteId, NULL)); + #else + free(p); + #endif +} + + +struct SampleDataA +{ + enum { + kLastIndex = 20 + }; + + int intValue1; + math::float4 float4Value; // (intentially unaligned) + Vector3f vector3; + + mecanim::uint32_t index[kLastIndex]; + + OffsetPtr nullPtr; + + OffsetPtr floatPtr; + + mecanim::uint32_t arraySize; + OffsetPtr array; + + mecanim::uint32_t emptyArraySize; + OffsetPtr emptyArray; + + int intValue2; + + DECLARE_SERIALIZE(SampleData) +}; + +struct SampleData +{ + int intValue1; // 0 + math::float4 float4Value; // 16 (intentially unaligned) + Vector3f vector3; // 32 + + OffsetPtr nullPtr; // 44 + + OffsetPtr floatPtr; // 52 + + mecanim::uint32_t arraySize; // 60 + OffsetPtr array; // 64 + + mecanim::uint32_t emptyArraySize; // 72 + OffsetPtr emptyArray; // 76 + + mecanim::uint32_t sampleDataASize; // 84 + OffsetPtr sampleDataA; // 88 + + mecanim::uint32_t sampleDataAHandleSize; // 96 + OffsetPtr > sampleDataAHandle; // 100 + + int intValue2; // 108 + + DECLARE_SERIALIZE(SampleData) +}; + + + +template inline +void SampleDataA::Transfer(TransferFunction& transfer) +{ + TRANSFER(intValue1); + TRANSFER(float4Value); + TRANSFER(vector3); + + STATIC_ARRAY_TRANSFER(mecanim::uint32_t, index, kLastIndex); + + TRANSFER(nullPtr); + TRANSFER(floatPtr); + + TRANSFER_BLOB_ONLY(arraySize); + MANUAL_ARRAY_TRANSFER2(float, array, arraySize); + + + TRANSFER_BLOB_ONLY(emptyArraySize); + MANUAL_ARRAY_TRANSFER2(math::float4, emptyArray, emptyArraySize); + + TRANSFER(intValue2); +} + + +template inline +void SampleData::Transfer(TransferFunction& transfer) +{ + TRANSFER(intValue1); + TRANSFER(float4Value); + TRANSFER(vector3); + + TRANSFER(nullPtr); + TRANSFER(floatPtr); + + TRANSFER_BLOB_ONLY(arraySize); + MANUAL_ARRAY_TRANSFER2(double, array, arraySize); + + + TRANSFER_BLOB_ONLY(emptyArraySize); + MANUAL_ARRAY_TRANSFER2(math::float4, emptyArray, emptyArraySize); + + TRANSFER_BLOB_ONLY(sampleDataASize); + MANUAL_ARRAY_TRANSFER2(SampleDataA, sampleDataA, sampleDataASize); + + TRANSFER_BLOB_ONLY(sampleDataAHandleSize); + MANUAL_ARRAY_TRANSFER2(OffsetPtr, sampleDataAHandle, sampleDataAHandleSize); + + TRANSFER(intValue2); +} + +static void SetupTestDataA (SampleDataA& sourceData) +{ + sourceData.intValue1 = 1; + sourceData.float4Value = math::float4(1, 2, 3, 4); + sourceData.vector3 = Vector3f(1,2,3); + + mecanim::uint32_t i; + for(i=0; i(sourceData.sampleDataASize); + for(int i=0;i [2]; + sourceData.sampleDataAHandle[0] = Construct(); + SetupTestDataA(*sourceData.sampleDataAHandle[0]); + sourceData.sampleDataAHandle[1] = Construct(); + SetupTestDataA(*sourceData.sampleDataAHandle[1]); +} + + +static void DeleteTestData (SampleData& sourceData) +{ + for(int i=0;i(&deserialized) % ALIGN_OF(SampleDataA) == 0 ); + CHECK (deserialized.intValue1 == 1); + CHECK (dot(deserialized.float4Value - math::float4(1, 2, 3, 4)) == math::float1::zero()); + + mecanim::uint32_t i; + for(i=0; i(&deserialized) % ALIGN_OF(SampleData) == 0); + CHECK (deserialized.intValue1 == 1); + CHECK (dot(deserialized.float4Value - math::float4(1, 2, 3, 4)) == math::float1::zero() ); + + CHECK (deserialized.nullPtr.IsNull()); + + float ptr = *deserialized.floatPtr; + CHECK (ptr == 5.5F); + + double* array = deserialized.array.Get(); + CHECK (array[0] == 6.5); + CHECK (array[1] == 7.5); + CHECK (array[2] == 8.5); + + CHECK (deserialized.emptyArray.IsNull()); + CHECK (deserialized.emptyArraySize == 0); + + CHECK (deserialized.vector3 == Vector3f(1,2,3)); + CHECK (deserialized.intValue2 == 2); + + CHECK (deserialized.sampleDataASize == 4); + for(int i=0;i (data.begin())); + + // Generate blob with reduce copy + BlobWrite::container_type dataReduced; + BlobWrite blobWriteReduce (dataReduced, kNoTransferInstructionFlags, kBuildNoTargetPlatform); + blobWriteReduce.SetReduceCopy(true); + blobWriteReduce.Transfer(sourceData, "Base"); + TestData(*reinterpret_cast (dataReduced.begin())); + + // Ensure reduced blob is actually smaller. + CHECK (dataReduced.size() < data.size()); + + // Ensure that 64 bit data is larger than non-64 bit data + BlobWrite::container_type data64; + BlobWrite blobWrite64 (data64, kNoTransferInstructionFlags, kBuildStandaloneWin64Player); + blobWrite64.Transfer(sourceData, "Base"); + + BlobWrite::container_type data32; + BlobWrite blobWrite32 (data32, kNoTransferInstructionFlags, kBuildStandaloneWinPlayer); + blobWrite32.Transfer(sourceData, "Base"); + CHECK (data64.size() > data32.size()); + + DeleteTestData (sourceData); + } + + TEST (Blobification_OffsetPtr) + { + OffsetPtr* ptrHigh = new OffsetPtr; + OffsetPtr* ptrLow = new OffsetPtr; + + size_t* ptrH = reinterpret_cast(std::numeric_limits::max()-4); + size_t* ptrL = reinterpret_cast(4); + + ptrHigh->reset(ptrH); + ptrLow->reset(ptrL); + + size_t h = reinterpret_cast(ptrHigh->Get()); + size_t l = reinterpret_cast(ptrLow->Get()); + + + CHECK (h == std::numeric_limits::max()-4); + CHECK (l == 4); + + delete ptrHigh; + delete ptrLow; + } +} + +#endif //ENABLE_UNIT_TESTS -- cgit v1.1-26-g67d0