cdc27eaf2e5773ee36a3c814e9117a7c3bc1e15a
[reactos.git] / rostests / apitests / crt / static_construct.cpp
1 /*
2 * PROJECT: ReactOS API tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for static C++ object construction
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #include <apitest.h>
9 #include <strsafe.h>
10 #include "dll_startup.h"
11
12 extern "C"
13 {
14 extern int static_init_counter;
15
16 static int static_init_counter_at_startup;
17 static int static_construct_counter_at_startup;
18 static int m_uninit_at_startup;
19
20 int static_construct_counter = 789;
21 }
22
23 static struct init_static
24 {
25 int m_uninit;
26 int m_counter;
27
28 init_static() :
29 m_counter(2)
30 {
31 static_init_counter_at_startup = static_init_counter;
32 static_construct_counter_at_startup = static_construct_counter;
33 m_uninit_at_startup = m_uninit;
34 static_construct_counter++;
35 m_uninit++;
36 }
37 } init_static;
38
39 static
40 VOID
41 TestInitStatic(VOID)
42 {
43 ok(static_init_counter_at_startup == 123, "static_init_counter at startup: %d\n", static_init_counter_at_startup);
44 ok(static_construct_counter_at_startup == 789, "static_construct_counter at startup: %d\n", static_construct_counter_at_startup);
45 ok(m_uninit_at_startup == 0, "init_static.m_uninit at startup: %d\n", m_uninit_at_startup);
46
47 ok(static_init_counter == 123, "static_init_counter: %d\n", static_init_counter);
48
49 ok(static_construct_counter == 790, "static_construct_counter: %d\n", static_construct_counter);
50 ok(init_static.m_counter == 2, "init_static.m_counter: %d\n", init_static.m_counter);
51 ok(init_static.m_uninit == 1, "init_static.m_uninit: %d\n", init_static.m_uninit);
52 }
53
54 static
55 VOID
56 TestDllStartup(VOID)
57 {
58 #if defined(TEST_MSVCRT)
59 const PCWSTR DllName = L"msvcrt_crt_dll_startup.dll";
60 #elif defined(TEST_STATIC_CRT)
61 const PCWSTR DllName = L"static_crt_dll_startup.dll";
62 #else
63 #error This test only makes sense for static CRT and msvcrt.dll
64 #endif
65 WCHAR DllPath[MAX_PATH];
66 GetModuleFileNameW(NULL, DllPath, _countof(DllPath));
67 wcsrchr(DllPath, L'\\')[1] = UNICODE_NULL;
68 StringCchCatW(DllPath, _countof(DllPath), DllName);
69
70 HMODULE hDll = LoadLibraryW(DllPath);
71 if (hDll == NULL)
72 {
73 skip("Helper dll not found\n");
74 return;
75 }
76 SET_COUNTER_VALUES_POINTER *pSetCounterValuesPointer = reinterpret_cast<SET_COUNTER_VALUES_POINTER*>(GetProcAddress(hDll, "SetCounterValuesPointer"));
77 if (pSetCounterValuesPointer == NULL)
78 {
79 skip("Helper function not found\n");
80 FreeLibrary(hDll);
81 return;
82 }
83 counter_values values;
84 pSetCounterValuesPointer(&values);
85 ok(values.m_uninit_at_startup == 0, "m_uninit_at_startup = %d\n", values.m_uninit_at_startup);
86 ok(values.m_uninit == 1, "m_uninit = %d\n", values.m_uninit);
87 ok(values.m_counter == 2, "m_counter = %d\n", values.m_counter);
88 ok(values.static_construct_counter_at_startup == 5656, "static_construct_counter_at_startup = %d\n", values.static_construct_counter_at_startup);
89 ok(values.static_construct_counter == 5657, "static_construct_counter = %d\n", values.static_construct_counter);
90 ok(values.dtor_counter_at_detach == 0, "dtor_counter_at_detach = %d\n", values.dtor_counter_at_detach);
91 ok(values.dtor_counter == 0, "dtor_counter = %d\n", values.dtor_counter);
92 values.dtor_counter_at_detach = 78789;
93 values.dtor_counter = 7878;
94 FreeLibrary(hDll);
95 ok(values.m_uninit_at_startup == 0, "m_uninit_at_startup = %d\n", values.m_uninit_at_startup);
96 ok(values.m_uninit == 1, "m_uninit = %d\n", values.m_uninit);
97 ok(values.m_counter == 2, "m_counter = %d\n", values.m_counter);
98 ok(values.static_construct_counter_at_startup == 5656, "static_construct_counter_at_startup = %d\n", values.static_construct_counter_at_startup);
99 ok(values.static_construct_counter == 5657, "static_construct_counter = %d\n", values.static_construct_counter);
100 ok(values.dtor_counter_at_detach == 7878, "dtor_counter_at_detach = %d\n", values.dtor_counter_at_detach);
101 ok(values.dtor_counter == 7879, "dtor_counter = %d\n", values.dtor_counter);
102 }
103
104 START_TEST(static_construct)
105 {
106 TestInitStatic();
107 TestDllStartup();
108 }