xref: /netbsd-src/tests/usr.bin/c++/t_call_once.sh (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1#	$NetBSD: t_call_once.sh,v 1.1 2018/03/24 00:26:51 kamil Exp $
2#
3# Copyright (c) 2018 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28atf_test_case call_once
29call_once_head() {
30	atf_set "descr" "compile and run std::call_once"
31	atf_set "require.progs" "c++"
32}
33
34atf_test_case call_once_profile
35call_once_profile_head() {
36	atf_set "descr" "compile and run std::call_once with profiling option"
37	atf_set "require.progs" "c++"
38}
39
40atf_test_case call_once_pic
41call_once_pic_head() {
42	atf_set "descr" "compile and run PIC std::call_once"
43	atf_set "require.progs" "c++"
44}
45
46atf_test_case call_once_pic_32
47call_once_pic_32_head() {
48	atf_set "descr" "compile and run 32-bit PIC std::call_once"
49	atf_set "require.progs" "c++"
50}
51
52atf_test_case call_once_pic_profile
53call_once_pic_head() {
54	atf_set "descr" "compile and run PIC std::call_once with profiling flag"
55	atf_set "require.progs" "c++"
56}
57
58atf_test_case call_once_pic_profile_32
59call_once_pic_profile_32_head() {
60	atf_set "descr" "compile and run 32-bit PIC std::call_once with profiling flag"
61	atf_set "require.progs" "c++"
62}
63
64atf_test_case call_once_profile_32
65call_once_profile_32_head() {
66	atf_set "descr" "compile and run 32-bit std::call_once with profiling flag"
67	atf_set "require.progs" "c++"
68}
69
70atf_test_case call_once_pie
71call_once_pie_head() {
72	atf_set "descr" "compile and run position independent (PIE) std::call_once"
73	atf_set "require.progs" "c++"
74}
75
76atf_test_case call_once_32
77call_once_32_head() {
78	atf_set "descr" "compile and run std::call_once for/in netbsd32 emulation"
79	atf_set "require.progs" "c++ file diff cat"
80}
81
82atf_test_case call_once_static
83call_once_static_head() {
84	atf_set "descr" "compile and run std::call_once with static flag"
85	atf_set "require.progs" "c++"
86}
87
88call_once_body() {
89	cat > test.cpp << EOF
90#include <cstdio>
91#include <thread>
92#include <mutex>
93std::once_flag flag;
94int main(void) {
95        std::call_once(flag, [](){ printf("hello, world!\n"); });
96        return 0;
97}
98EOF
99	atf_check -s exit:0 -o ignore -e ignore c++ -o call_once test.cpp -pthread
100	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
101}
102
103call_once_profile_body() {
104	atf_expect_fail "profiling option doesn't work now"
105	cat > test.cpp << EOF
106#include <cstdio>
107#include <thread>
108#include <mutex>
109std::once_flag flag;
110int main(void) {
111        std::call_once(flag, [](){ printf("hello, world!\n"); });
112        return 0;
113}
114EOF
115	atf_check -s exit:0 -o ignore -e ignore c++ -pg -o call_once test.cpp -pthread
116	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
117}
118
119call_once_profile_32_body() {
120	atf_expect_fail "profiling option doesn't work now"
121	# check whether this arch is 64bit
122	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
123		atf_skip "this is not a 64 bit architecture"
124	fi
125	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
126		atf_skip "c++ -m32 not supported on this architecture"
127	else
128		if fgrep -q _LP64 ./def32; then
129			atf_fail "c++ -m32 does not generate netbsd32 binaries"
130		fi
131	fi
132
133	cat > test.cpp << EOF
134#include <cstdio>
135#include <thread>
136#include <mutex>
137std::once_flag flag;
138int main(void) {
139        std::call_once(flag, [](){ printf("hello, world!\n"); });
140        return 0;
141}
142EOF
143	atf_check -s exit:0 -o ignore -e ignore c++ -m32 -pg -o call_once test.cpp -pthread
144	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
145	atf_expect_fail "The combination of 32-bit and profiling should be fail"
146}
147
148call_once_pic_body() {
149	cat > test.cpp << EOF
150#include <stdlib.h>
151int callpic(void);
152int main(void) {callpic();exit(0);}
153EOF
154	cat > pic.cpp << EOF
155#include <cstdio>
156#include <thread>
157#include <mutex>
158std::once_flag flag;
159int callpic(void) {
160        std::call_once(flag, [](){ printf("hello, world!\n"); });
161        return 0;
162}
163EOF
164
165	atf_check -s exit:0 -o ignore -e ignore \
166	    c++ -fPIC -shared -o libtest.so pic.cpp
167	atf_check -s exit:0 -o ignore -e ignore \
168	    c++ -o call_once test.cpp -L. -ltest -pthread
169
170	export LD_LIBRARY_PATH=.
171	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
172}
173
174call_once_pic_32_body() {
175	# check whether this arch is 64bit
176	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
177		atf_skip "this is not a 64 bit architecture"
178	fi
179	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
180		atf_skip "c++ -m32 not supported on this architecture"
181	else
182		if fgrep -q _LP64 ./def32; then
183			atf_fail "c++ -m32 does not generate netbsd32 binaries"
184		fi
185	fi
186
187	cat > test.cpp << EOF
188#include <stdlib.h>
189int callpic(void);
190int main(void) {callpic();exit(0);}
191EOF
192	cat > pic.cpp << EOF
193#include <cstdio>
194#include <thread>
195#include <mutex>
196std::once_flag flag;
197int callpic(void) {
198        std::call_once(flag, [](){ printf("hello, world!\n"); });
199        return 0;
200}
201EOF
202
203	atf_check -s exit:0 -o ignore -e ignore \
204	    c++ -m32 -fPIC -shared -o libtest.so pic.cpp
205	atf_check -s exit:0 -o ignore -e ignore \
206	    c++ -m32 -o call_once test.cpp -L. -ltest -pthread
207
208	export LD_LIBRARY_PATH=.
209	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
210}
211
212call_once_pic_profile_body() {
213	atf_expect_fail "profiling option doesn't work now"
214	cat > test.cpp << EOF
215#include <stdlib.h>
216int callpic(void);
217int main(void) {callpic();exit(0);}
218EOF
219	cat > pic.cpp << EOF
220#include <cstdio>
221#include <thread>
222#include <mutex>
223std::once_flag flag;
224int callpic(void) {
225        std::call_once(flag, [](){ printf("hello, world!\n"); });
226        return 0;
227}
228EOF
229
230	atf_check -s exit:0 -o ignore -e ignore \
231	    c++ -pg -fPIC -shared -o libtest.so pic.cpp
232	atf_check -s exit:0 -o ignore -e ignore \
233	    c++ -pg -o call_once test.cpp -L. -ltest -pthread
234
235	export LD_LIBRARY_PATH=.
236	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
237}
238
239call_once_pic_profile_32_body() {
240	atf_expect_fail "profiling option doesn't work now"
241	# check whether this arch is 64bit
242	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
243		atf_skip "this is not a 64 bit architecture"
244	fi
245	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
246		atf_skip "c++ -m32 not supported on this architecture"
247	else
248		if fgrep -q _LP64 ./def32; then
249			atf_fail "c++ -m32 does not generate netbsd32 binaries"
250		fi
251	fi
252
253	cat > test.cpp << EOF
254#include <stdlib.h>
255int callpic(void);
256int main(void) {callpic();exit(0);}
257EOF
258	cat > pic.cpp << EOF
259#include <cstdio>
260#include <thread>
261#include <mutex>
262std::once_flag flag;
263int callpic(void) {
264        std::call_once(flag, [](){ printf("hello, world!\n"); });
265        return 0;
266}
267EOF
268
269	atf_check -s exit:0 -o ignore -e ignore \
270	    c++ -m32 -pg -fPIC -shared -o libtest.so pic.cpp
271	atf_check -s exit:0 -o ignore -e ignore \
272	    c++ -m32 -pg -o call_once test.cpp -L. -ltest -pthread
273
274	export LD_LIBRARY_PATH=.
275	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
276}
277
278call_once_pie_body() {
279	# check whether this arch supports -pie
280	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
281		atf_skip "c++ -pie not supported on this architecture"
282	fi
283	cat > test.cpp << EOF
284#include <cstdio>
285#include <thread>
286#include <mutex>
287std::once_flag flag;
288int main(void) {
289        std::call_once(flag, [](){ printf("hello, world!\n"); });
290        return 0;
291}
292EOF
293	atf_check -s exit:0 -o ignore -e ignore c++ -fpie -pie -o call_once test.cpp -pthread
294	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
295}
296
297call_once_32_body() {
298	# check whether this arch is 64bit
299	if ! c++ -dM -E - < /dev/null | fgrep -q _LP64; then
300		atf_skip "this is not a 64 bit architecture"
301	fi
302	if ! c++ -m32 -dM -E - < /dev/null 2>/dev/null > ./def32; then
303		atf_skip "c++ -m32 not supported on this architecture"
304	else
305		if fgrep -q _LP64 ./def32; then
306			atf_fail "c++ -m32 does not generate netbsd32 binaries"
307		fi
308	fi
309
310	cat > test.cpp << EOF
311#include <cstdio>
312#include <thread>
313#include <mutex>
314std::once_flag flag;
315int main(void) {
316        std::call_once(flag, [](){ printf("hello, world!\n"); });
317        return 0;
318}
319EOF
320	atf_check -s exit:0 -o ignore -e ignore c++ -o call_once_32 -m32 test.cpp -pthread
321	atf_check -s exit:0 -o ignore -e ignore c++ -o call_once_64 test.cpp -pthread
322	file -b ./call_once_32 > ./ftype32
323	file -b ./call_once_64 > ./ftype64
324	if diff ./ftype32 ./ftype64 >/dev/null; then
325		atf_fail "generated binaries do not differ"
326	fi
327	echo "32bit binaries on this platform are:"
328	cat ./ftype32
329	echo "While native (64bit) binaries are:"
330	cat ./ftype64
331	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once_32
332
333	# do another test with static 32bit binaries
334	cat > test.cpp << EOF
335#include <cstdio>
336#include <thread>
337#include <mutex>
338std::once_flag flag;
339int main(void) {
340        std::call_once(flag, [](){ printf("hello, world!\n"); });
341        return 0;
342}
343EOF
344	atf_check -s exit:0 -o ignore -e ignore c++ -o call_once -m32 -pthread \
345	    -static test.cpp
346	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
347}
348
349call_once_static_body() {
350	cat > test.cpp << EOF
351#include <cstdio>
352#include <thread>
353#include <mutex>
354std::once_flag flag;
355int main(void) {
356        std::call_once(flag, [](){ printf("hello, world!\n"); });
357        return 0;
358}
359EOF
360	atf_check -s exit:0 -o ignore -e ignore c++ -static -o call_once test.cpp -pthread
361	atf_check -s exit:0 -o inline:"hello, world!\n" ./call_once
362}
363
364atf_init_test_cases()
365{
366
367	atf_add_test_case call_once
368	atf_add_test_case call_once_profile
369	atf_add_test_case call_once_pic
370	atf_add_test_case call_once_pie
371	atf_add_test_case call_once_32
372	atf_add_test_case call_once_static
373	atf_add_test_case call_once_pic_32
374	atf_add_test_case call_once_pic_profile
375	atf_add_test_case call_once_pic_profile_32
376	atf_add_test_case call_once_profile_32
377}
378