| 1 | #include <cassert> |
| 2 | #include <iostream> |
| 3 | |
| 4 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 5 | #include <future> |
| 6 | #endif |
| 7 | |
| 8 | thread_local unsigned int tls_counter = 1; |
| 9 | |
| 10 | // a non-optimized way of checking for prime numbers: |
| 11 | bool 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 | |
| 20 | class CTest { |
| 21 | public: |
| 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; } |
| 29 | private: |
| 30 | int m_val; |
| 31 | }; |
| 32 | |
| 33 | class GlobalConstructorTest { |
| 34 | public: |
| 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; } |
| 40 | private: |
| 41 | int m_val; |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | volatile int runtime_val = 456; |
| 46 | GlobalConstructorTest global(runtime_val);	// test if global initializers are called. |
| 47 | |
| 48 | int 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 | } |