blob: 23266e9c5b02ef316bb33c4ab186cbfc8ee942ef [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
9namespace aos {
10namespace testing {
11
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 }
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).
40TEST(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