blob: b8aab6bcf430330d3fbf47f9f00d0f82a951f214 [file] [log] [blame]
James Kuszmaul65891e02023-11-06 13:09:07 -08001#include "aos/containers/inlined_vector.h"
2
Stephan Pleines5fc35072024-05-22 17:33:18 -07003#include "gflags/gflags.h"
James Kuszmaul65891e02023-11-06 13:09:07 -08004#include "gtest/gtest.h"
5
6#include "aos/realtime.h"
7
8DECLARE_bool(die_on_malloc);
9
Stephan Pleinesf63bde82024-01-13 15:59:33 -080010namespace aos::testing {
James Kuszmaul65891e02023-11-06 13:09:07 -080011
12// Checks that we don't malloc until/unless we need to increase the size of the
13// vector.
14TEST(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 }
James Kuszmaul6c813962023-11-14 10:34:51 -080030// Malloc hooks don't work with asan/msan.
31#if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer)
James Kuszmaul65891e02023-11-06 13:09:07 -080032 EXPECT_DEATH(
33 {
34 aos::ScopedRealtime realtime;
35 a.push_back(4);
36 },
37 "Malloced");
James Kuszmaul6c813962023-11-14 10:34:51 -080038#endif
James Kuszmaul65891e02023-11-06 13:09:07 -080039}
40
41// Tests that we can create/define a vector with zero statically allocated
42// elements (the absl::InlinedVector does not allow this for some reason).
43TEST(SizedArrayTest, ZeroLengthVector) {
44 InlinedVector<int, 0> zero;
45 zero.push_back(1);
46 ASSERT_EQ(1, zero[0]);
47}
Stephan Pleinesf63bde82024-01-13 15:59:33 -080048} // namespace aos::testing