James Kuszmaul | 65891e0 | 2023-11-06 13:09:07 -0800 | [diff] [blame^] | 1 | #include "aos/containers/inlined_vector.h" |
| 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | #include "aos/realtime.h" |
| 6 | |
| 7 | DECLARE_bool(die_on_malloc); |
| 8 | |
| 9 | namespace aos { |
| 10 | namespace testing { |
| 11 | |
| 12 | // Checks that we don't malloc until/unless we need to increase the size of the |
| 13 | // vector. |
| 14 | TEST(SizedArrayTest, NoUnnecessaryMalloc) { |
| 15 | gflags::FlagSaver flag_saver; |
| 16 | FLAGS_die_on_malloc = true; |
| 17 | RegisterMallocHook(); |
| 18 | InlinedVector<int, 5> a; |
| 19 | { |
| 20 | aos::ScopedRealtime realtime; |
| 21 | a.push_back(9); |
| 22 | a.push_back(7); |
| 23 | a.push_back(1); |
| 24 | a.push_back(2); |
| 25 | a.push_back(3); |
| 26 | |
| 27 | // And double-check that we can actually construct a new object at realtime. |
| 28 | InlinedVector<int, 5> b; |
| 29 | } |
| 30 | EXPECT_DEATH( |
| 31 | { |
| 32 | aos::ScopedRealtime realtime; |
| 33 | a.push_back(4); |
| 34 | }, |
| 35 | "Malloced"); |
| 36 | } |
| 37 | |
| 38 | // Tests that we can create/define a vector with zero statically allocated |
| 39 | // elements (the absl::InlinedVector does not allow this for some reason). |
| 40 | TEST(SizedArrayTest, ZeroLengthVector) { |
| 41 | InlinedVector<int, 0> zero; |
| 42 | zero.push_back(1); |
| 43 | ASSERT_EQ(1, zero[0]); |
| 44 | } |
| 45 | } // namespace testing |
| 46 | } // namespace aos |