xref: /aosp_15_r20/system/chre/util/tests/copyable_fixed_size_vector_test.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gtest/gtest.h"
18 
19 #include "chre/util/copyable_fixed_size_vector.h"
20 #include "chre/util/fixed_size_vector.h"
21 
22 using chre::CopyableFixedSizeVector;
23 using chre::FixedSizeVector;
24 
TEST(CopyableFixedSizeVector,CopyConstructible)25 TEST(CopyableFixedSizeVector, CopyConstructible) {
26   constexpr int kValue = 1234;
27   CopyableFixedSizeVector<int, 2> a;
28   a.push_back(kValue);
29 
30   CopyableFixedSizeVector<int, 2> b(a);
31   EXPECT_EQ(b.size(), 1);
32   EXPECT_EQ(a[0], kValue);
33   EXPECT_EQ(b[0], kValue);
34 }
35 
TEST(CopyableFixedSizeVector,CopyAssignable)36 TEST(CopyableFixedSizeVector, CopyAssignable) {
37   constexpr int kValue = 1234;
38   CopyableFixedSizeVector<int, 2> a;
39   a.push_back(kValue);
40 
41   CopyableFixedSizeVector<int, 2> b;
42   EXPECT_EQ(b.size(), 0);
43   b = a;
44   EXPECT_EQ(b.size(), 1);
45   EXPECT_EQ(a[0], kValue);
46   EXPECT_EQ(b[0], kValue);
47 }
48 
TEST(CopyableFixedSizeVector,NonTrivialElement)49 TEST(CopyableFixedSizeVector, NonTrivialElement) {
50   static int ctorCount;
51   static int dtorCount;
52   class Foo {
53    public:
54     Foo() {
55       ctorCount++;
56     }
57     Foo(const Foo & /*other*/) {
58       ctorCount++;
59     }
60 
61     ~Foo() {
62       dtorCount++;
63     }
64   };
65 
66   ctorCount = dtorCount = 0;
67   {
68     CopyableFixedSizeVector<Foo, 4> v;
69     {
70       Foo f;
71       EXPECT_EQ(ctorCount, 1);
72       v.push_back(f);
73     }
74     EXPECT_EQ(ctorCount, 2);
75     EXPECT_EQ(dtorCount, 1);
76     v.pop_back();
77     EXPECT_EQ(dtorCount, 2);
78     v.emplace_back();
79     EXPECT_EQ(ctorCount, 3);
80   }
81   EXPECT_EQ(dtorCount, 3);
82 }
83 
TEST(CopyableFixedSizeVector,Nestable)84 TEST(CopyableFixedSizeVector, Nestable) {
85   struct Foo {
86     int id;
87     CopyableFixedSizeVector<float, 3> vec;
88   };
89 
90   FixedSizeVector<Foo, 4> container;
91 
92   Foo f;
93   f.id = 1;
94   container.push_back(f);
95   container.emplace_back();
96   container.back().id = 2;
97   container.back().vec.push_back(1.23f);
98   container.back().vec.push_back(3.21f);
99   EXPECT_EQ(container.front().id, 1);
100   container.erase(0);
101   EXPECT_EQ(container.front().id, 2);
102   EXPECT_EQ(container.front().vec.size(), 2);
103   EXPECT_EQ(container.front().vec[0], 1.23f);
104 }
105