summaryrefslogtreecommitdiff
path: root/Runtime/Profiler/MemoryProfilerTests.cpp
blob: 4c02f54138e36a19011a47adff69da8721a33982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "UnityPrefix.h"
#include "MemoryProfiler.h"

#if ENABLE_UNIT_TESTS

#include "External/UnitTest++/src/UnitTest++.h"

#if ENABLE_PROFILER
#include "Runtime/Profiler/MemoryProfiler.h"

SUITE (MemoryProfilerTests)
{
	struct MemoryProfilerFixture
	{
		MemoryProfilerFixture()
		{
			// string allocates length + 1 rounded up to mod 16 
			stringlength = 32;
#if UNITY_OSX
			// on osx, there is allocated room for refcounting as well
			stringlength += 2;
#endif
		}
		~MemoryProfilerFixture()
		{
		}
		int stringlength;
	};

	struct VectorOfStrings
	{
		UNITY_VECTOR(kMemDefault, UnityStr) vec;
	};

	struct StructWithStrings
	{
		UnityStr str1;
		UnityStr str2;
	};

	TEST_FIXTURE(MemoryProfilerFixture, GetRelatedMemorySize_StringVector_MatchesExpectedSize)
	{
		VectorOfStrings* vec = UNITY_NEW_AS_ROOT(VectorOfStrings, kMemDefault, "", "");
		vec->vec.reserve(10);
		{
			UnityStr str("HelloWorld HelloWorld");
			SET_ALLOC_OWNER(vec);
			vec->vec.push_back(str);
		}
		
		CHECK_EQUAL(GetMemoryProfiler()->GetRelatedMemorySize(vec), sizeof(UnityStr)*10 + sizeof(UNITY_VECTOR(kMemDefault, UnityStr)) + stringlength);
		UNITY_DELETE(vec, kMemDefault);
	}

	TEST_FIXTURE(MemoryProfilerFixture, GetRelatedMemorySize_StringLosingOwner_MatchesExpectedSize)
	{
		StructWithStrings* strings;
		{
			VectorOfStrings* vec = UNITY_NEW_AS_ROOT(VectorOfStrings,kMemDefault, "", "");	
			SET_ALLOC_OWNER(vec);
			strings = UNITY_NEW(StructWithStrings, kMemDefault);
			strings->str1 = "HelloWorld HelloWorld";
			strings->str2 = "HelloWorld HelloWorld";
			CHECK_EQUAL(GetMemoryProfiler()->GetRelatedMemorySize(vec), sizeof(UNITY_VECTOR(kMemDefault, UnityStr)) + 2 * stringlength + sizeof(StructWithStrings) );
			UNITY_DELETE(vec, kMemDefault);
		}
		UNITY_DELETE(strings, kMemDefault);
	}

}

#endif
#endif