-
Notifications
You must be signed in to change notification settings - Fork 2
/
AddTests.cs
166 lines (143 loc) · 4.53 KB
/
AddTests.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine.Profiling;
using UnityEngine.TestTools.Constraints;
using Is = UnityEngine.TestTools.Constraints.Is;
namespace MH.GCTests
{
public class AddElement
{
const int ELEM_COUNT = 1000;
const int LOOP_COUNT = 1000; //don't know why, but if the mem allocation is too small, the profiler.GetMonoUsedSizeLong cannot detect it
List<DummyObjA> _cache = new List<DummyObjA>();
[OneTimeSetUp]
public void Init()
{
for(int i=0; i<ELEM_COUNT; ++i)
_cache.Add(new DummyObjA(i));
}
[OneTimeTearDown]
public void Fini()
{
_cache.Clear();
}
[Test]
public void OK_List()
{
var cont = new List<DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(_cache[i]);
cont.Clear();
Assert.That(
() => {
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(_cache[i]);
}, Is.Not.AllocatingGCMemory()
);
}
[Test]
public void OK_Dictionary()
{
var cont = new Dictionary<int, DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(i, _cache[i]);
cont.Clear();
Assert.That(
() => {
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(i, _cache[i]);
}, Is.Not.AllocatingGCMemory()
);
}
[Test]
public void OK_Queue()
{
var cont = new Queue<DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Enqueue(_cache[i]);
cont.Clear();
Assert.That(
() => {
for(int i=0; i<ELEM_COUNT; ++i)
cont.Enqueue(_cache[i]);
}, Is.Not.AllocatingGCMemory()
);
}
[Test]
public void OK_Stack()
{
var cont = new Stack<DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Push(_cache[i]);
cont.Clear();
Assert.That(
() => {
for(int i=0; i<ELEM_COUNT; ++i)
cont.Push(_cache[i]);
}, Is.Not.AllocatingGCMemory()
);
}
[Test]
public void OK_HashSet()
{
var cont = new HashSet<DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(_cache[i]);
cont.Clear();
Assert.That(
() => {
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(_cache[i]);
}, Is.Not.AllocatingGCMemory()
);
}
[Test]
public void OK_SortedList()
{
var cont = new SortedList<int, DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(i, _cache[i]);
cont.Clear();
Assert.That(
() => {
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(i, _cache[i]);
}, Is.Not.AllocatingGCMemory()
);
}
//////////////////////////////////////////////////////////////////
/// Below are tests that are GC_Dirty
//////////////////////////////////////////////////////////////////
[Test]
public void BAD_SortedDictionary()
{
var cont = new SortedDictionary<int, DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(i, _cache[i]);
cont.Clear();
Assert.That(
() => {
for(int i=0; i<ELEM_COUNT; ++i)
cont.Add(i, _cache[i]);
}, Is.AllocatingGCMemory()
);
}
[Test]
public void BAD_LinkedList()
{
var cont = new LinkedList<DummyObjA>();
for(int i=0; i<ELEM_COUNT; ++i)
cont.AddLast(_cache[i]);
cont.Clear();
Assert.That(
() => {
for(int i=0; i<ELEM_COUNT; ++i)
cont.AddLast(_cache[i]);
}, Is.AllocatingGCMemory()
);
}
}
}