blob: d2d9af6987467d54641a258fe88321c00f3ac50b [file] [log] [blame]
James Kuszmaul65891e02023-11-06 13:09:07 -08001#include "aos/containers/inlined_vector.h"
2
Austin Schuh99f7c6a2024-06-25 22:07:44 -07003#include "absl/flags/declare.h"
4#include "absl/flags/flag.h"
5#include "absl/flags/reflection.h"
James Kuszmaul65891e02023-11-06 13:09:07 -08006#include "gtest/gtest.h"
7
8#include "aos/realtime.h"
9
Austin Schuh99f7c6a2024-06-25 22:07:44 -070010ABSL_DECLARE_FLAG(bool, die_on_malloc);
James Kuszmaul65891e02023-11-06 13:09:07 -080011
Stephan Pleinesf63bde82024-01-13 15:59:33 -080012namespace aos::testing {
James Kuszmaul65891e02023-11-06 13:09:07 -080013
14// Checks that we don't malloc until/unless we need to increase the size of the
15// vector.
16TEST(SizedArrayTest, NoUnnecessaryMalloc) {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070017 absl::FlagSaver flag_saver;
18 absl::SetFlag(&FLAGS_die_on_malloc, true);
James Kuszmaul65891e02023-11-06 13:09:07 -080019 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 Kuszmaul6c813962023-11-14 10:34:51 -080032// Malloc hooks don't work with asan/msan.
33#if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer)
James Kuszmaul65891e02023-11-06 13:09:07 -080034 EXPECT_DEATH(
35 {
36 aos::ScopedRealtime realtime;
37 a.push_back(4);
38 },
39 "Malloced");
James Kuszmaul6c813962023-11-14 10:34:51 -080040#endif
James Kuszmaul65891e02023-11-06 13:09:07 -080041}
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).
45TEST(SizedArrayTest, ZeroLengthVector) {
46 InlinedVector<int, 0> zero;
47 zero.push_back(1);
48 ASSERT_EQ(1, zero[0]);
49}
Stephan Pleinesf63bde82024-01-13 15:59:33 -080050} // namespace aos::testing