blob: c9f2dd225fcfd343249ea960fba4035d815fc2c9 [file] [log] [blame]
James Kuszmaul65891e02023-11-06 13:09:07 -08001#include "aos/containers/inlined_vector.h"
2
3#include "gtest/gtest.h"
4
5#include "aos/realtime.h"
6
7DECLARE_bool(die_on_malloc);
8
Stephan Pleinesf63bde82024-01-13 15:59:33 -08009namespace aos::testing {
James Kuszmaul65891e02023-11-06 13:09:07 -080010
11// Checks that we don't malloc until/unless we need to increase the size of the
12// vector.
13TEST(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 Kuszmaul6c813962023-11-14 10:34:51 -080029// Malloc hooks don't work with asan/msan.
30#if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer)
James Kuszmaul65891e02023-11-06 13:09:07 -080031 EXPECT_DEATH(
32 {
33 aos::ScopedRealtime realtime;
34 a.push_back(4);
35 },
36 "Malloced");
James Kuszmaul6c813962023-11-14 10:34:51 -080037#endif
James Kuszmaul65891e02023-11-06 13:09:07 -080038}
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).
42TEST(SizedArrayTest, ZeroLengthVector) {
43 InlinedVector<int, 0> zero;
44 zero.push_back(1);
45 ASSERT_EQ(1, zero[0]);
46}
Stephan Pleinesf63bde82024-01-13 15:59:33 -080047} // namespace aos::testing