1 /*
2  * Copyright (c) 2019 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <array>
8 #include <functional>
9 #include <memory>
10 #include <vector>
11 #include <zephyr/ztest.h>
12 #include <zephyr/kernel.h>
13 
14 BUILD_ASSERT(__cplusplus == 201703);
15 
16 std::array<int, 4> array = {1, 2, 3, 4};
17 std::vector<int> vector;
18 
ZTEST(libcxx_tests,test_array)19 ZTEST(libcxx_tests, test_array)
20 {
21 	zassert_equal(array.size(), 4, "unexpected size");
22 	zassert_equal(array[0], 1, "array[0] wrong");
23 	zassert_equal(array[3], 4, "array[3] wrong");
24 
25 	std::array<uint8_t, 2> local = {1, 2};
26 	zassert_equal(local.size(), 2, "unexpected size");
27 	zassert_equal(local[0], 1, "local[0] wrong");
28 	zassert_equal(local[1], 2, "local[1] wrong");
29 }
30 
ZTEST(libcxx_tests,test_vector)31 ZTEST(libcxx_tests, test_vector)
32 {
33 	zassert_equal(vector.size(), 0, "vector init nonzero");
34 	for (auto v : array) {
35 		vector.push_back(v);
36 	}
37 	zassert_equal(vector.size(), array.size(), "vector store failed");
38 }
39 
40 struct make_unique_data {
41 	static int ctors;
42 	static int dtors;
43 	int inst;
44 
make_unique_datamake_unique_data45 	make_unique_data () :
46 	inst{++ctors}
47 	{ }
48 
~make_unique_datamake_unique_data49 	~make_unique_data ()
50 	{
51 		++dtors;
52 	}
53 };
54 int make_unique_data::ctors;
55 int make_unique_data::dtors;
56 
ZTEST(libcxx_tests,test_make_unique)57 ZTEST(libcxx_tests, test_make_unique)
58 {
59 	zassert_equal(make_unique_data::ctors, 0, "ctor count not initialized");
60 	zassert_equal(make_unique_data::dtors, 0, "dtor count not initialized");
61 	auto d = std::make_unique<make_unique_data>();
62 	zassert_true(static_cast<bool>(d), "allocation failed");
63 	zassert_equal(make_unique_data::ctors, 1, "ctr update failed");
64 	zassert_equal(d->inst, 1, "instance init failed");
65 	zassert_equal(make_unique_data::dtors, 0, "dtor count not zero");
66 	d.reset();
67 	zassert_false(d, "release failed");
68 	zassert_equal(make_unique_data::dtors, 1, "dtor count not incremented");
69 }
70 
71 #if defined(CONFIG_CPP_EXCEPTIONS) && !defined(CONFIG_BOARD_M2GL025_MIV)
throw_exception(void)72 static void throw_exception(void)
73 {
74 	throw 42;
75 }
76 
ZTEST(libcxx_tests,test_exception)77 ZTEST(libcxx_tests, test_exception)
78 {
79 	try
80 	{
81 		throw_exception();
82 	}
83 	catch (int i)
84 	{
85 		zassert_equal(i, 42, "Incorrect exception value");
86 		return;
87 	}
88 
89 	zassert_unreachable("Missing exception catch");
90 }
91 #else
92 
ZTEST(libcxx_tests,test_exception)93 ZTEST(libcxx_tests, test_exception)
94 {
95 	ztest_test_skip();
96 }
97 #endif
98 
99 struct ThreadStack
100 {
101 	K_KERNEL_STACK_MEMBER(threadStack, 1024) {};
102 };
103 
ZTEST(libcxx_tests,test_new_aligned)104 ZTEST(libcxx_tests, test_new_aligned)
105 {
106 	auto test_aligned = std::make_unique<ThreadStack>();
107 	zassert_not_null(test_aligned, "aligned allocation failed");
108 }
109 
libcxx_tests_setup(void)110 static void *libcxx_tests_setup(void)
111 {
112 	TC_PRINT("version %u\n", (uint32_t)__cplusplus);
113 
114 	return NULL;
115 }
116 ZTEST_SUITE(libcxx_tests, NULL, libcxx_tests_setup, NULL, NULL, NULL);
117