master
1#include <cassert>
2#include <iostream>
3
4#ifndef _LIBCPP_HAS_NO_THREADS
5#include <future>
6#endif
7
8thread_local unsigned int tls_counter = 1;
9
10// a non-optimized way of checking for prime numbers:
11bool is_prime(int x) {
12 for (int i = 2; i <x ; ++i) {
13 if (x % i == 0) {
14 return false;
15 }
16 }
17 return true;
18}
19
20class CTest {
21public:
22 CTest(int val) : m_val(val) {
23 tls_counter++;
24 };
25 virtual ~CTest() {}
26
27 virtual int getVal() const { return m_val; }
28 virtual void printVal() { std::cout << "val=" << m_val << std::endl; }
29private:
30 int m_val;
31};
32
33class GlobalConstructorTest {
34public:
35 GlobalConstructorTest(int val) : m_val(val) {};
36 virtual ~GlobalConstructorTest() {}
37
38 virtual int getVal() const { return m_val; }
39 virtual void printVal() { std::cout << "val=" << m_val << std::endl; }
40private:
41 int m_val;
42};
43
44
45volatile int runtime_val = 456;
46GlobalConstructorTest global(runtime_val); // test if global initializers are called.
47
48int main (int argc, char *argv[])
49{
50 assert(global.getVal() == 456);
51
52 auto t = std::make_unique<CTest>(123);
53 assert(t->getVal() != 456);
54 assert(tls_counter == 2);
55 if (argc > 1) {
56 t->printVal();
57 }
58 bool ok = t->getVal() == 123;
59
60 if (!ok) abort();
61
62#ifndef _LIBCPP_HAS_NO_THREADS
63 std::future<bool> fut = std::async(is_prime, 313);
64 bool ret = fut.get();
65 assert(ret);
66#endif
67
68#if !defined(__wasm__) && !defined(__APPLE__)
69 // WASM and macOS are not passing this yet.
70 // TODO file an issue for this and link it here.
71 try {
72 throw 20;
73 } catch (int e) {
74 assert(e == 20);
75 }
76#endif
77
78 return EXIT_SUCCESS;
79}