Apply LOCAL_PATCHES and remove not used ones.
[official-gcc.git] / libphobos / testsuite / libphobos.shared / lib.d
blob0e4eb4494018561efadfb203a57ec8915050c1b5
1 module lib;
3 // test EH
4 void throwException()
6 throw new Exception(null);
9 Exception collectException(void delegate() dg)
11 try
12 dg();
13 catch (Exception e)
14 return e;
15 return null;
18 // test GC
19 __gshared Object root;
20 void alloc() { root = new Object(); }
21 void access() { assert(root.toString() !is null); } // vtbl call will fail if finalized
22 void free() { root = null; }
24 Object tls_root;
25 void tls_alloc() { tls_root = new Object(); }
26 void tls_access() { assert(tls_root.toString() !is null); } // vtbl call will fail if finalized
27 void tls_free() { tls_root = null; }
29 void stack(alias func)()
31 // allocate some extra stack space to not keep references to GC memory on the scanned stack
32 ubyte[1024] buf = void;
33 func();
36 void testGC()
38 import core.memory;
40 stack!alloc();
41 stack!tls_alloc();
42 stack!access();
43 stack!tls_access();
44 GC.collect();
45 stack!tls_access();
46 stack!access();
47 stack!tls_free();
48 stack!free();
51 // test Init
52 import core.atomic : atomicOp;
53 shared uint shared_static_ctor, shared_static_dtor, static_ctor, static_dtor;
54 shared static this() { if (atomicOp!"+="(shared_static_ctor, 1) != 1) assert(0); }
55 shared static ~this() { if (atomicOp!"+="(shared_static_dtor, 1) != 1) assert(0); }
56 static this() { atomicOp!"+="(static_ctor, 1); }
57 static ~this() { atomicOp!"+="(static_dtor, 1); }
59 extern(C) int runTests()
61 try
62 runTestsImpl();
63 catch (Throwable)
64 return 0;
65 return 1;
68 void runTestsImpl()
70 import core.thread;
72 bool passed;
73 try
74 throwException();
75 catch (Exception e)
76 passed = true;
77 assert(passed);
78 assert(collectException({throwException();}) !is null);
80 testGC();
82 assert(shared_static_ctor == 1);
83 assert(static_ctor == 1);
84 static void run()
86 assert(static_ctor == 2);
87 assert(shared_static_ctor == 1);
88 testGC();
90 auto thr = new Thread(&run);
91 thr.start();
92 thr.join();
93 assert(static_dtor == 1);
95 passed = false;
96 foreach (m; ModuleInfo)
97 if (m.name == "lib") passed = true;
98 assert(passed);
101 // Provide a way to initialize D from C programs that are D agnostic.
102 import core.runtime : rt_init, rt_term;
104 extern(C) int lib_init()
106 return rt_init();
109 extern(C) int lib_term()
111 return rt_term();
114 shared size_t* _finalizeCounter;
116 class MyFinalizer
118 ~this()
120 import core.atomic;
121 atomicOp!"+="(*_finalizeCounter, 1);
125 class MyFinalizerBig : MyFinalizer
127 ubyte[4096] _big = void;
130 extern(C) void setFinalizeCounter(shared(size_t)* p)
132 _finalizeCounter = p;