msvcrt: Implement 64-bit version of __std_type_info_hash.
[wine.git] / dlls / ucrtbase / tests / cpp.c
blob163e1968069f832c0c39681e9655cba8dc49f37e
1 /*
2 * Copyright 2016 Daniel Lehman (Esri)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <wchar.h>
23 #include <stdio.h>
25 #include <windef.h>
26 #include <winbase.h>
27 #include "wine/test.h"
29 typedef unsigned char MSVCRT_bool;
31 typedef struct {
32 const char *what;
33 MSVCRT_bool dofree;
34 } __std_exception_data;
36 typedef struct
38 char *name;
39 char mangled[32];
40 } type_info140;
42 typedef struct _type_info_list
44 SLIST_ENTRY entry;
45 char name[1];
46 } type_info_list;
48 static void* (CDECL *p_malloc)(size_t);
49 static void (CDECL *p___std_exception_copy)(const __std_exception_data*, __std_exception_data*);
50 static void (CDECL *p___std_exception_destroy)(__std_exception_data*);
51 static int (CDECL *p___std_type_info_compare)(const type_info140*, const type_info140*);
52 static const char* (CDECL *p___std_type_info_name)(type_info140*, SLIST_HEADER*);
53 static void (CDECL *p___std_type_info_destroy_list)(SLIST_HEADER*);
54 static size_t (CDECL *p___std_type_info_hash)(type_info140*);
57 static BOOL init(void)
59 HMODULE module;
61 module = LoadLibraryA("ucrtbase.dll");
62 if (!module)
64 win_skip("ucrtbase.dll not installed\n");
65 return FALSE;
68 p_malloc = (void*)GetProcAddress(module, "malloc");
69 p___std_exception_copy = (void*)GetProcAddress(module, "__std_exception_copy");
70 p___std_exception_destroy = (void*)GetProcAddress(module, "__std_exception_destroy");
71 p___std_type_info_compare = (void*)GetProcAddress(module, "__std_type_info_compare");
72 p___std_type_info_name = (void*)GetProcAddress(module, "__std_type_info_name");
73 p___std_type_info_destroy_list = (void*)GetProcAddress(module, "__std_type_info_destroy_list");
74 p___std_type_info_hash = (void*)GetProcAddress(module, "__std_type_info_hash");
75 return TRUE;
78 static void test___std_exception(void)
80 __std_exception_data src;
81 __std_exception_data dst;
83 if (0) /* crash on Windows */
85 p___std_exception_copy(NULL, &src);
86 p___std_exception_copy(&dst, NULL);
88 src.what = "invalid free";
89 src.dofree = 1;
90 p___std_exception_destroy(&src);
91 p___std_exception_destroy(NULL);
94 src.what = "what";
95 src.dofree = 0;
96 p___std_exception_copy(&src, &dst);
97 ok(dst.what == src.what, "expected what to be same, got src %p dst %p\n", src.what, dst.what);
98 ok(!dst.dofree, "expected 0, got %d\n", dst.dofree);
100 src.dofree = 0x42;
101 p___std_exception_copy(&src, &dst);
102 ok(dst.what != src.what, "expected what to be different, got src %p dst %p\n", src.what, dst.what);
103 ok(dst.dofree == 1, "expected 1, got %d\n", dst.dofree);
105 p___std_exception_destroy(&dst);
106 ok(!dst.what, "expected NULL, got %p\n", dst.what);
107 ok(!dst.dofree, "expected 0, got %d\n", dst.dofree);
109 src.what = NULL;
110 src.dofree = 0;
111 p___std_exception_copy(&src, &dst);
112 ok(!dst.what, "dst.what != NULL\n");
113 ok(!dst.dofree, "dst.dofree != FALSE\n");
115 src.what = NULL;
116 src.dofree = 1;
117 p___std_exception_copy(&src, &dst);
118 ok(!dst.what, "dst.what != NULL\n");
119 ok(!dst.dofree, "dst.dofree != FALSE\n");
122 static void test___std_type_info(void)
124 type_info140 ti1 = { NULL, ".?AVa@@" };
125 type_info140 ti2 = { NULL, ".?AVb@@" };
126 type_info140 ti3 = ti1;
127 SLIST_HEADER header;
128 type_info_list *elem;
129 const char *ret;
130 size_t hash1, hash2;
131 int eq;
134 InitializeSListHead(&header);
135 p___std_type_info_destroy_list(&header);
137 elem = p_malloc(sizeof(*elem));
138 memset(elem, 0, sizeof(*elem));
139 InterlockedPushEntrySList(&header, &elem->entry);
140 p___std_type_info_destroy_list(&header);
141 ok(!InterlockedPopEntrySList(&header), "list is not empty\n");
143 ret = p___std_type_info_name(&ti1, &header);
144 ok(!strcmp(ret, "class a"), "__std_type_info_name(&ti1) = %s\n", ret);
145 ok(ti1.name == ret, "ti1.name = %p, ret = %p\n", ti1.name, ret);
147 p___std_type_info_destroy_list(&header);
148 ok(!InterlockedPopEntrySList(&header), "list is not empty\n");
149 ok(ti1.name == ret, "ti1.name = %p, ret = %p\n", ti1.name, ret);
150 ti1.name = NULL;
152 eq = p___std_type_info_compare(&ti1, &ti1);
153 ok(eq == 0, "__std_type_info_compare(&ti1, &ti1) = %d\n", eq);
155 eq = p___std_type_info_compare(&ti1, &ti2);
156 ok(eq == -1, "__std_type_info_compare(&ti1, &ti2) = %d\n", eq);
158 eq = p___std_type_info_compare(&ti1, &ti3);
159 ok(eq == 0, "__std_type_info_compare(&ti1, &ti3) = %d\n", eq);
161 ti1.mangled[0] = 0;
162 ti1.mangled[1] = 0;
163 ti1.mangled[2] = 0;
164 hash1 = p___std_type_info_hash(&ti1);
165 #ifdef _WIN64
166 ok(hash1 == 0xcbf29ce44fd0bfc1, "hash = %p\n", (void*)hash1);
167 #else
168 ok(hash1 == 0x811c9dc5, "hash = %p\n", (void*)hash1);
169 #endif
171 ti1.mangled[0] = 1;
172 hash2 = p___std_type_info_hash(&ti1);
173 ok(hash1 == hash2, "hash1 != hash2 (first char not ignored)\n");
175 ti1.mangled[1] = 1;
176 hash1 = p___std_type_info_hash(&ti1);
177 #ifdef _WIN64
178 ok(hash1 == 0xaf63bc4c29620a60, "hash = %p\n", (void*)hash1);
179 #else
180 ok(hash1 == 0x40c5b8c, "hash = %p\n", (void*)hash1);
181 #endif
182 ok(hash1 != hash2, "hash1 == hash2 for different strings\n");
184 ti1.mangled[1] = 2;
185 hash2 = p___std_type_info_hash(&ti1);
186 ok(hash1 != hash2, "hash1 == hash2 for different strings\n");
188 hash1 = p___std_type_info_hash(&ti2);
189 ok(hash1 != hash2, "hash1 == hash2 for different strings\n");
192 START_TEST(cpp)
194 if (!init()) return;
195 test___std_exception();
196 test___std_type_info();