aboutsummaryrefslogtreecommitdiff
path: root/test/standalone/c_compiler/test.cpp
blob: 1f3fe173d28a46a67e338ef7d57dd5466316d8fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <cassert>
#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;
}

class CTest {
public:
  CTest(int val) : m_val(val) {
    tls_counter++;
  };
  virtual ~CTest() {}

  virtual int getVal() const { return m_val; }
  virtual void printVal() { std::cout << "val=" << m_val << std::endl; }
private:
  int m_val;
};

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;
};


volatile int runtime_val = 456;
GlobalConstructorTest global(runtime_val);	// test if global initializers are called.

int main (int argc, char *argv[])
{
  assert(global.getVal() == 456);

  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;

  if (!ok) abort();

#ifndef _LIBCPP_HAS_NO_THREADS
  std::future<bool> fut = std::async(is_prime, 313);
  bool ret = fut.get();
  assert(ret);
#endif

#if !defined(__wasm__) && !defined(__APPLE__)
  // WASM and macOS are not passing this yet.
  // TODO file an issue for this and link it here.
  try {
    throw 20;
  } catch (int e) {
    assert(e == 20);
  }
#endif

  return EXIT_SUCCESS;
}