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