1 /*
2  * PROJECT:     ReactOS api tests
3  * LICENSE:     LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4  * PURPOSE:     Test for CAtlArray
5  * COPYRIGHT:   Copyright 2016-2019 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
6  *              Copyright 2019 Mark Jansen (mark.jansen@reactos.org)
7  */
8 
9 #ifdef HAVE_APITEST
10     #include <apitest.h>
11 #else
12     #include "atltest.h"
13 #endif
14 
15 #include <atlbase.h>
16 #include <atlcoll.h>
17 
18 struct CCreature
19 {
20     static int s_nCtorCount;
21     static int s_nCtorCount_Default;
22     static int s_nCCtorCount;
23     static int s_nDtorCount;
24     static int s_nOpIsCount;
25 
26     int m_id;
27 
28     CCreature(int id = 0x123456)
29         :m_id(id)
30     {
31         CCreature::s_nCtorCount++;
32         if (id == 0x123456)
33             s_nCtorCount_Default++;
34     }
35     CCreature(const CCreature& c)
36         :m_id(c.m_id)
37     {
38         CCreature::s_nCCtorCount++;
39     }
40     ~CCreature()
41     {
42         CCreature::s_nDtorCount++;
43     }
44     CCreature& operator=(const CCreature& other)
45     {
46         m_id = other.m_id;
47         CCreature::s_nOpIsCount++;
48         return *this;
49     }
50 };
51 
52 int CCreature::s_nCtorCount = 0;
53 int CCreature::s_nCtorCount_Default = 0;
54 int CCreature::s_nCCtorCount = 0;
55 int CCreature::s_nDtorCount = 0;
56 int CCreature::s_nOpIsCount = 0;
57 
58 
59 START_TEST(CAtlArray)
60 {
61     {
62         CAtlArray<CCreature> array1;
63 
64         ok_int(CCreature::s_nCtorCount, 0);
65         ok_int(CCreature::s_nCtorCount_Default, 0);
66         ok_int(CCreature::s_nCCtorCount, 0);
67         ok_int(CCreature::s_nDtorCount, 0);
68         ok_int(CCreature::s_nOpIsCount, 0);
69 
70         array1.SetCount(2);
71 
72         ok_int(CCreature::s_nCtorCount, 2);
73         ok_int(CCreature::s_nCtorCount_Default, 2);
74         ok_int(CCreature::s_nCCtorCount, 0);
75         ok_int(CCreature::s_nDtorCount, 0);
76         ok_int(CCreature::s_nOpIsCount, 0);
77 
78         array1.SetCount(1);
79 
80         ok_int(CCreature::s_nCtorCount, 2);
81         ok_int(CCreature::s_nCtorCount_Default, 2);
82         ok_int(CCreature::s_nCCtorCount, 0);
83         ok_int(CCreature::s_nDtorCount, 1);
84         ok_int(CCreature::s_nOpIsCount, 0);
85 
86         CCreature test(111);
87 
88         ok_int(CCreature::s_nCtorCount, 3);
89         ok_int(CCreature::s_nCtorCount_Default, 2);
90         ok_int(CCreature::s_nCCtorCount, 0);
91         ok_int(CCreature::s_nDtorCount, 1);
92         ok_int(CCreature::s_nOpIsCount, 0);
93 
94         ok_size_t(array1.GetCount(), 1u);
95         ok_int(array1[0].m_id, 0x123456);
96         ok_int(array1.GetAt(0).m_id, 0x123456);
97 
98         array1.Add(test);
99 
100         ok_int(CCreature::s_nCtorCount, 3);
101         ok_int(CCreature::s_nCtorCount_Default, 2);
102         ok_int(CCreature::s_nCCtorCount, 1);
103         ok_int(CCreature::s_nDtorCount, 1);
104         ok_int(CCreature::s_nOpIsCount, 0);
105 
106         ok_size_t(array1.GetCount(), 2u);
107         ok_int(array1[0].m_id, 0x123456);
108         ok_int(array1.GetAt(0).m_id, 0x123456);
109         ok_int(array1[1].m_id, 111);
110         ok_int(array1.GetAt(1).m_id, 111);
111 
112         test.m_id = 222;
113         array1[0] = test;
114 
115         ok_int(CCreature::s_nCtorCount, 3);
116         ok_int(CCreature::s_nCtorCount_Default, 2);
117         ok_int(CCreature::s_nCCtorCount, 1);
118         ok_int(CCreature::s_nDtorCount, 1);
119         ok_int(CCreature::s_nOpIsCount, 1);
120 
121         // Default traits does not call anything when relocating objects!
122         array1.SetCount(100);
123 
124         ok_int(CCreature::s_nCtorCount, 101);
125         ok_int(CCreature::s_nCtorCount_Default, 100);
126         ok_int(CCreature::s_nCCtorCount, 1);
127         ok_int(CCreature::s_nDtorCount, 1);
128         ok_int(CCreature::s_nOpIsCount, 1);
129 
130         // Does not compile:
131         //CAtlArray<CCreature> array2(array1);
132 
133         // Does not compile:
134         //CAtlArray<CCreature> array2;
135         //array2 = array1;
136     }
137 
138     // Objects are cleaned up when the list goes away
139     ok_int(CCreature::s_nCtorCount, 101);
140     ok_int(CCreature::s_nCtorCount_Default, 100);
141     ok_int(CCreature::s_nCCtorCount, 1);
142     ok_int(CCreature::s_nDtorCount, 102);
143     ok_int(CCreature::s_nOpIsCount, 1);
144 }
145