diff options
author | Andrea Grandi <agrandi@google.com> | 2022-10-20 17:01:13 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-10-21 17:58:13 +0000 |
commit | 012d263c875df4a5440777504732df829775979c (patch) | |
tree | 8777e1717578a603186c83f9c37d6f8488962eff | |
parent | 70ee19b25c19a5938e1e7042383bcce24a1461cc (diff) | |
download | chrome-ec-012d263c875df4a5440777504732df829775979c.tar.gz |
test: Add basic test of std::vector
Verify that dynamic memory allocation works correctly.
BUG=b:243964606
TEST=util/run_device_tests.py --board=dartmonkey \
--tests=std_vector
TEST=make run-std_vector
BRANCH=none
Signed-off-by: Andrea Grandi <agrandi@google.com>
Change-Id: Iae7ff1070786e3d5e3c8ba4dcddeeeb9979b0306
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3969861
Reviewed-by: Tom Hughes <tomhughes@chromium.org>
Reviewed-by: Bobby Casey <bobbycasey@google.com>
-rw-r--r-- | board/hatch_fp/build.mk | 1 | ||||
-rw-r--r-- | board/nocturne_fp/build.mk | 1 | ||||
-rw-r--r-- | test/build.mk | 2 | ||||
-rwxr-xr-x | test/run_device_tests.py | 1 | ||||
-rw-r--r-- | test/std_vector.cc | 84 | ||||
-rw-r--r-- | test/std_vector.tasklist | 9 |
6 files changed, 98 insertions, 0 deletions
diff --git a/board/hatch_fp/build.mk b/board/hatch_fp/build.mk index 4afdb1c1c3..828e7523b3 100644 --- a/board/hatch_fp/build.mk +++ b/board/hatch_fp/build.mk @@ -55,6 +55,7 @@ test-list-y=\ sha256_unrolled \ static_if \ stdlib \ + std_vector \ stm32f_rtc \ system_is_locked \ timer_dos \ diff --git a/board/nocturne_fp/build.mk b/board/nocturne_fp/build.mk index c15535555e..65ffd6c096 100644 --- a/board/nocturne_fp/build.mk +++ b/board/nocturne_fp/build.mk @@ -55,6 +55,7 @@ test-list-y=\ sha256_unrolled \ static_if \ stdlib \ + std_vector \ system_is_locked \ timer_dos \ utils \ diff --git a/test/build.mk b/test/build.mk index 4f9674b306..08e2d15c9f 100644 --- a/test/build.mk +++ b/test/build.mk @@ -98,6 +98,7 @@ test-list-host += static_if_error # toolchain's C standard library, so these tests are actually testing the # toolchain's C standard library. test-list-host += stdlib +test-list-host += std_vector test-list-host += system test-list-host += thermal test-list-host += timer_dos @@ -244,6 +245,7 @@ sha256_unrolled-y=sha256.o shmalloc-y=shmalloc.o static_if-y=static_if.o stdlib-y=stdlib.o +std_vector-y=std_vector.o stm32f_rtc-y=stm32f_rtc.o stress-y=stress.o system-y=system.o diff --git a/test/run_device_tests.py b/test/run_device_tests.py index 66109cbf93..4e0b0127e3 100755 --- a/test/run_device_tests.py +++ b/test/run_device_tests.py @@ -268,6 +268,7 @@ class AllTests: TestConfig(test_name="sha256_unrolled"), TestConfig(test_name="static_if"), TestConfig(test_name="stdlib"), + TestConfig(test_name="std_vector"), TestConfig( config_name="system_is_locked_wp_on", test_name="system_is_locked", diff --git a/test/std_vector.cc b/test/std_vector.cc new file mode 100644 index 0000000000..5873b416b6 --- /dev/null +++ b/test/std_vector.cc @@ -0,0 +1,84 @@ +/* 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 <array> +#include <vector> + +extern "C" { +#include "common.h" +#include "console.h" +#include "test_util.h" +} + +test_static int push_back_elements() +{ + std::vector<int32_t> vec; + + vec.push_back(0); + vec.push_back(1); + vec.push_back(2); + vec.push_back(3); + + TEST_EQ(static_cast<int32_t>(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<int32_t> vec; + + for (int i = 0; i < num_elements; ++i) + vec.push_back(i); + + TEST_EQ(static_cast<int>(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<std::vector<int32_t>, 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<int>(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(push_back_elements); + RUN_TEST(fill_one_vector); + RUN_TEST(fill_multiple_vectors); + + test_print_result(); +} diff --git a/test/std_vector.tasklist b/test/std_vector.tasklist new file mode 100644 index 0000000000..6d43377af5 --- /dev/null +++ b/test/std_vector.tasklist @@ -0,0 +1,9 @@ +/* Copyright 2022 The ChromiumOS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * See CONFIG_TASK_LIST in config.h for details. + */ +#define CONFIG_TEST_TASK_LIST |