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