tests/application_development/libcxx: add test of basic smart pointer use

Confirm that std::make_unique<> functions as intended.  This is an
indirect test of new/delete using best-practices API.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
This commit is contained in:
Peter A. Bigot 2019-09-07 07:27:19 -05:00 committed by Kumar Gala
commit 4ccf4f4ccb

View file

@ -4,9 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include <vector>
#include <array>
#include <functional>
#include <memory>
#include <vector>
#include <ztest.h>
BUILD_ASSERT(__cplusplus == 201703);
@ -35,12 +36,44 @@ static void test_vector(void)
zassert_equal(vector.size(), array.size(), "vector store failed");
}
struct make_unique_data {
static int ctors;
static int dtors;
int inst;
make_unique_data () :
inst{++ctors}
{ }
~make_unique_data ()
{
++dtors;
}
};
int make_unique_data::ctors;
int make_unique_data::dtors;
static void test_make_unique(void)
{
zassert_equal(make_unique_data::ctors, 0, "ctor count not initialized");
zassert_equal(make_unique_data::dtors, 0, "dtor count not initialized");
auto d = std::make_unique<make_unique_data>();
zassert_true(static_cast<bool>(d), "allocation failed");
zassert_equal(make_unique_data::ctors, 1, "ctr update failed");
zassert_equal(d->inst, 1, "instance init failed");
zassert_equal(make_unique_data::dtors, 0, "dtor count not zero");
d.reset();
zassert_false(d, "release failed");
zassert_equal(make_unique_data::dtors, 1, "dtor count not incremented");
}
void test_main(void)
{
TC_PRINT("version %u\n", (u32_t)__cplusplus);
ztest_test_suite(libcxx_tests,
ztest_unit_test(test_array),
ztest_unit_test(test_vector)
ztest_unit_test(test_vector),
ztest_unit_test(test_make_unique)
);
ztest_run_test_suite(libcxx_tests);