xref: /llvm-project/libc/test/integration/startup/gpu/init_fini_array_test.cpp (revision abd85cd473afedf112bf00630a22382fee4a7853)
1 //===-- Loader test to test init and fini array iteration -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "test/IntegrationTest/test.h"
10 
11 #include <stddef.h>
12 
13 int global_destroyed = false;
14 
15 class A {
16 private:
17   int val[1024];
18 
19 public:
A(int i,int a)20   A(int i, int a) {
21     for (int k = 0; k < 1024; ++k)
22       val[k] = 0;
23     val[i] = a;
24   }
25 
~A()26   ~A() { global_destroyed = true; }
27 
get(int i) const28   int get(int i) const { return val[i]; }
29 };
30 
31 int GLOBAL_INDEX = 512;
32 int INITVAL_INITIALIZER = 0x600D;
33 int BEFORE_INITIALIZER = 0xFEED;
34 
35 A global(GLOBAL_INDEX, INITVAL_INITIALIZER);
36 
37 int initval = 0;
38 int before = 0;
39 
run_before()40 __attribute__((constructor(101))) void run_before() {
41   before = BEFORE_INITIALIZER;
42 }
43 
run_after()44 __attribute__((constructor(65535))) void run_after() {
45   ASSERT_EQ(before, BEFORE_INITIALIZER);
46 }
47 
set_initval()48 __attribute__((constructor)) void set_initval() {
49   initval = INITVAL_INITIALIZER;
50 }
reset_initval()51 __attribute__((destructor(101))) void reset_initval() {
52   ASSERT_TRUE(global_destroyed);
53   initval = 0;
54 }
55 
TEST_MAIN(int argc,char ** argv,char ** env)56 TEST_MAIN(int argc, char **argv, char **env) {
57   ASSERT_EQ(global.get(GLOBAL_INDEX), INITVAL_INITIALIZER);
58   ASSERT_EQ(initval, INITVAL_INITIALIZER);
59   return 0;
60 }
61