Skip to content

Commit

Permalink
tests/application_development/libcxx: add test of basic smart pointer…
Browse files Browse the repository at this point in the history
… 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>
  • Loading branch information
pabigot committed Oct 29, 2019
1 parent 3a82102 commit 931adaf
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions tests/application_development/libcxx/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 931adaf

Please sign in to comment.