2021-05-24 23:11:00 +01:00
|
|
|
#include <cassert>
|
2021-11-01 04:36:30 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_HAS_NO_THREADS
|
|
|
|
#include <future>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
thread_local unsigned int tls_counter = 1;
|
|
|
|
|
|
|
|
// a non-optimized way of checking for prime numbers:
|
|
|
|
bool is_prime(int x) {
|
|
|
|
for (int i = 2; i <x ; ++i) {
|
|
|
|
if (x % i == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-24 23:11:00 +01:00
|
|
|
|
|
|
|
class CTest {
|
|
|
|
public:
|
2021-11-01 04:36:30 +00:00
|
|
|
CTest(int val) : m_val(val) {
|
|
|
|
tls_counter++;
|
|
|
|
};
|
|
|
|
virtual ~CTest() {}
|
2021-05-24 23:11:00 +01:00
|
|
|
|
2021-11-01 04:36:30 +00:00
|
|
|
virtual int getVal() const { return m_val; }
|
|
|
|
virtual void printVal() { std::cout << "val=" << m_val << std::endl; }
|
2021-05-24 23:11:00 +01:00
|
|
|
private:
|
2021-11-01 04:36:30 +00:00
|
|
|
int m_val;
|
2021-05-24 23:11:00 +01:00
|
|
|
};
|
|
|
|
|
2022-05-11 00:38:37 +01:00
|
|
|
class GlobalConstructorTest {
|
|
|
|
public:
|
|
|
|
GlobalConstructorTest(int val) : m_val(val) {};
|
|
|
|
virtual ~GlobalConstructorTest() {}
|
|
|
|
|
|
|
|
virtual int getVal() const { return m_val; }
|
|
|
|
virtual void printVal() { std::cout << "val=" << m_val << std::endl; }
|
|
|
|
private:
|
|
|
|
int m_val;
|
|
|
|
};
|
|
|
|
|
2022-01-13 08:20:38 +00:00
|
|
|
|
|
|
|
volatile int runtime_val = 456;
|
2022-05-11 00:38:37 +01:00
|
|
|
GlobalConstructorTest global(runtime_val); // test if global initializers are called.
|
2022-01-13 08:20:38 +00:00
|
|
|
|
2021-05-24 23:11:00 +01:00
|
|
|
int main (int argc, char *argv[])
|
|
|
|
{
|
2021-11-01 04:36:30 +00:00
|
|
|
assert(global.getVal() == 456);
|
2022-05-11 00:38:37 +01:00
|
|
|
|
2021-11-01 04:36:30 +00:00
|
|
|
auto t = std::make_unique<CTest>(123);
|
|
|
|
assert(t->getVal() != 456);
|
|
|
|
assert(tls_counter == 2);
|
|
|
|
if (argc > 1) {
|
|
|
|
t->printVal();
|
|
|
|
}
|
|
|
|
bool ok = t->getVal() == 123;
|
2022-01-13 08:20:38 +00:00
|
|
|
|
2021-11-01 04:36:30 +00:00
|
|
|
if (!ok) abort();
|
2022-01-13 08:20:38 +00:00
|
|
|
|
2021-11-01 04:36:30 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_THREADS
|
|
|
|
std::future<bool> fut = std::async(is_prime, 313);
|
|
|
|
bool ret = fut.get();
|
|
|
|
assert(ret);
|
|
|
|
#endif
|
2021-05-24 23:11:00 +01:00
|
|
|
|
2022-05-11 00:38:37 +01:00
|
|
|
#if !defined(__wasm__) && !defined(__APPLE__)
|
|
|
|
// WASM and macOS are not passing this yet.
|
|
|
|
// TODO file an issue for this and link it here.
|
2021-11-01 04:36:30 +00:00
|
|
|
try {
|
|
|
|
throw 20;
|
|
|
|
} catch (int e) {
|
|
|
|
assert(e == 20);
|
|
|
|
}
|
|
|
|
#endif
|
2021-05-24 23:11:00 +01:00
|
|
|
|
2021-11-01 04:36:30 +00:00
|
|
|
return EXIT_SUCCESS;
|
2021-05-24 23:11:00 +01:00
|
|
|
}
|