summaryrefslogtreecommitdiff
path: root/test/global_initialization.cc
blob: 5168494ab44494bc72d32ee779c326ba3060b46f (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
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/**
 * @brief Tests initialization of global/static objects.
 */

#include "test_util.h"

#include <cstdio>
#include <cstring>

struct TestObj {
	TestObj()
	{
		/*
		 * In order to make sure the compiler can't perform the
		 * initialization at compile time (e.g., initialization of a
		 * POD value), we make a function call.
		 *
		 * In order to keep the test focused on initialization only
		 * and not the behavior of the heap, we avoid objects that
		 * would use dynamic allocation (such as std::vector).
		 *
		 * When making changes to this test, make sure to disable the
		 * call to call constructors of global objects
		 * (__libc_init_array) and make sure the test fails.
		 */
		snprintf(val, sizeof(val), "test %d", 42);
	}

	char val[8];
};

static TestObj global_obj;

static int test_global_init()
{
	TEST_EQ(strcmp(global_obj.val, "test 42"), 0, "%d");
	return EC_SUCCESS;
}

extern "C" void run_test(int, const char **)
{
	test_reset();
	RUN_TEST(test_global_init);
	test_print_result();
}