MCPcopy Create free account
hub / github.com/defold/defold / TEST

Function TEST

engine/dlib/src/test/test_objectpool.cpp:32–120  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

30};
31
32TEST(dmObjectPool, Test)
33{
34 dmObjectPool<Object> pool;
35 const uint32_t n = 16;
36 pool.SetCapacity(n);
37 std::map<uint32_t, uint32_t> mapping;
38
39 for (uint32_t i = 0; i < n; i++) {
40 uint32_t id = pool.Alloc();
41 mapping[id] = i;
42 Object* o = &pool.Get(id);
43 o->m_Value = i;
44 }
45 ASSERT_TRUE(pool.Full());
46
47 uint32_t sum = 0;
48 uint32_t sum2 = 0;
49 for (uint32_t i = 0; i < n; i++) {
50 sum += pool.GetRawObjects()[i].m_Value;
51 }
52 for (std::map<uint32_t, uint32_t>::iterator i = mapping.begin(); i != mapping.end(); ++i) {
53 sum2 += pool.Get(i->first).m_Value;
54 }
55
56 ASSERT_EQ((n - 1) * n / 2, sum);
57 ASSERT_EQ(sum, sum2);
58
59 pool.Free(12, true);
60 pool.Free(5, true);
61 mapping.erase(12);
62 mapping.erase(5);
63 ASSERT_FALSE(pool.Full());
64
65 sum = 0;
66 for (uint32_t i = 0; i < n - 2; i++) {
67 sum += pool.GetRawObjects()[i].m_Value;
68 }
69
70 sum2 = 0;
71 for (std::map<uint32_t, uint32_t>::iterator i = mapping.begin(); i != mapping.end(); ++i) {
72 sum2 += pool.Get(i->first).m_Value;
73 }
74
75 ASSERT_EQ((n - 1) * n / 2 - 12 - 5, sum);
76 ASSERT_EQ(sum, sum2);
77
78 uint32_t id1 = pool.Alloc();
79 uint32_t id2 = pool.Alloc();
80 ASSERT_TRUE(pool.Full());
81 pool.Get(id1).m_Value = 100;
82 pool.Get(id2).m_Value = 200;
83 mapping[id1] = 100;
84 mapping[id2] = 200;
85
86 sum = 0;
87 for (uint32_t i = 0; i < n; i++) {
88 sum += pool.GetRawObjects()[i].m_Value;
89 }

Callers

nothing calls this directly

Calls 11

randClass · 0.85
AllocMethod · 0.80
SetCapacityMethod · 0.45
GetMethod · 0.45
FullMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
FreeMethod · 0.45
eraseMethod · 0.45
insertMethod · 0.45
SizeMethod · 0.45

Tested by

no test coverage detected