/* 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. * * Basic test of std::vector and dynamic memory allocation. */ #include #include extern "C" { #include "common.h" #include "console.h" #include "test_util.h" } test_static int stack_init_elements() { std::vector vec{ 10, 11, 12, 13, 14 }; TEST_EQ(static_cast(vec.size()), 5, "%d"); TEST_EQ(vec[0], 10, "%d"); TEST_EQ(vec[1], 11, "%d"); TEST_EQ(vec[2], 12, "%d"); TEST_EQ(vec[3], 13, "%d"); TEST_EQ(vec[4], 14, "%d"); return EC_SUCCESS; } test_static int static_init_elements() { static std::vector vec{ 20, 21, 22, 23, 24 }; TEST_EQ(static_cast(vec.size()), 5, "%d"); TEST_EQ(vec[0], 20, "%d"); TEST_EQ(vec[1], 21, "%d"); TEST_EQ(vec[2], 22, "%d"); TEST_EQ(vec[3], 23, "%d"); TEST_EQ(vec[4], 24, "%d"); return EC_SUCCESS; } test_static int push_back_elements() { std::vector vec; vec.push_back(0); vec.push_back(1); vec.push_back(2); vec.push_back(3); TEST_EQ(static_cast(vec.size()), 4, "%d"); TEST_EQ(vec[0], 0, "%d"); TEST_EQ(vec[1], 1, "%d"); TEST_EQ(vec[2], 2, "%d"); TEST_EQ(vec[3], 3, "%d"); return EC_SUCCESS; } test_static int fill_one_vector() { // This test allocates 64kB of memory in total in a single std::vector constexpr int num_elements = 16 * 1024; std::vector vec; for (int i = 0; i < num_elements; ++i) vec.push_back(i); TEST_EQ(static_cast(vec.size()), num_elements, "%d"); for (int i = 0; i < num_elements; ++i) { TEST_ASSERT(vec[i] == i); // Using TEST_EQ floods the console and trigger the watchdog // TEST_EQ(vec[i], i, "%d"); // cflush(); } return EC_SUCCESS; } test_static int fill_multiple_vectors() { // This test allocates 64kB of memory in total split in 8 std::vectors constexpr int num_elements = 2 * 1024; std::array, 8> vecs; for (int i = 0; i < num_elements; ++i) for (auto &vec : vecs) vec.push_back(i); for (auto &vec : vecs) { TEST_EQ(static_cast(vec.size()), num_elements, "%d"); for (int i = 0; i < num_elements; ++i) { TEST_ASSERT(vec[i] == i); } } return EC_SUCCESS; } test_static int create_and_destroy_two_vectors() { // This allocates 64kB of memory twice. // The first vector is declared in a local scope and the memory is // free'd at the end of the block. constexpr int num_elements = 16 * 1024; { std::vector vec; for (int i = 0; i < num_elements; ++i) vec.push_back(i); TEST_EQ(static_cast(vec.size()), num_elements, "%d"); for (int i = 0; i < num_elements; ++i) { TEST_ASSERT(vec[i] == i); } } std::vector vec; for (int i = 0; i < num_elements; ++i) vec.push_back(i); TEST_EQ(static_cast(vec.size()), num_elements, "%d"); for (int i = 0; i < num_elements; ++i) { TEST_ASSERT(vec[i] == i); } return EC_SUCCESS; } extern "C" void run_test(int argc, const char **argv) { test_reset(); RUN_TEST(stack_init_elements); RUN_TEST(static_init_elements); RUN_TEST(push_back_elements); RUN_TEST(fill_one_vector); RUN_TEST(fill_multiple_vectors); RUN_TEST(create_and_destroy_two_vectors); test_print_result(); }