1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <fbl/inline_array.h>
6 
7 #include <fbl/algorithm.h>
8 #include <fbl/alloc_checker.h>
9 #include <lib/unittest/unittest.h>
10 #include <stddef.h>
11 
12 namespace {
13 
14 struct TestType {
TestType__anon2d7071a40111::TestType15     TestType() {
16         ctor_run_count++;
17     }
18 
~TestType__anon2d7071a40111::TestType19     ~TestType() {
20         dtor_run_count++;
21     }
22 
ResetRunCounts__anon2d7071a40111::TestType23     static void ResetRunCounts() {
24         ctor_run_count = 0u;
25         dtor_run_count = 0u;
26     }
27 
28     static size_t ctor_run_count;
29     static size_t dtor_run_count;
30 };
31 
32 size_t TestType::ctor_run_count = 0u;
33 size_t TestType::dtor_run_count = 0u;
34 
inline_test()35 bool inline_test() {
36     BEGIN_TEST;
37 
38     for (size_t sz = 0u; sz <= 3u; sz++) {
39         TestType::ResetRunCounts();
40         {
41             fbl::AllocChecker ac;
42             fbl::InlineArray<TestType, 3u> ia(&ac, sz);
43             EXPECT_TRUE(ac.check(), "");
44         }
45         EXPECT_EQ(TestType::ctor_run_count, sz, "");
46         EXPECT_EQ(TestType::dtor_run_count, sz, "");
47     }
48 
49     END_TEST;
50 }
51 
non_inline_test()52 bool non_inline_test() {
53     static const size_t test_sizes[] = { 4u, 5u, 6u, 10u, 100u, 1000u };
54 
55     BEGIN_TEST;
56 
57     for (size_t i = 0u; i < fbl::count_of(test_sizes); i++) {
58         size_t sz = test_sizes[i];
59 
60         TestType::ResetRunCounts();
61         {
62             fbl::AllocChecker ac;
63             fbl::InlineArray<TestType, 3u> ia(&ac, sz);
64             EXPECT_TRUE(ac.check(), "");
65         }
66         EXPECT_EQ(TestType::ctor_run_count, sz, "");
67         EXPECT_EQ(TestType::dtor_run_count, sz, "");
68     }
69 
70     END_TEST;
71 }
72 
73 }  // namespace
74 
75 UNITTEST_START_TESTCASE(inline_array_tests)
76 UNITTEST("inline test", inline_test)
77 UNITTEST("non-inline test", non_inline_test)
78 UNITTEST_END_TESTCASE(inline_array_tests, "inlinearraytests", "Inline array test");
79