huangcm
2025-08-25 f350412dc55c15118d0a7925d1071877498e5e24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python3
#  Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
from fruit_test_common import *
 
COMMON_DEFINITIONS = '''
    #include "test_common.h"
    
    #define IN_FRUIT_CPP_FILE 1
    #include <fruit/impl/data_structures/fixed_size_vector.templates.h>
    
    using namespace std;
    using namespace fruit::impl;
    
    struct X {
      int y;
      
      static int num_instances;
      
      X(int y) : y(y) {
        Assert(std::uintptr_t(this) % alignof(X) == 0);
        ++num_instances;
      }
      
      ~X() {
        --num_instances;
      }
    };
    
    int X::num_instances = 0;
    
    struct Y {
      static int num_instances;
      
      Y() {
        Assert(std::uintptr_t(this) % alignof(Y) == 0);
        ++num_instances;
      }
      
      ~Y() {
        --num_instances;
      }
    };
    
    int Y::num_instances = 0;
    
    template <int n>
    struct alignas(n) TypeWithAlignment {
      TypeWithAlignment() {
        Assert(std::uintptr_t(this) % n == 0);
      }
    };
    '''
 
def test_empty_allocator():
    source = '''
        int main() {
          FixedSizeAllocator allocator;
        }
        '''
    expect_success(
        COMMON_DEFINITIONS,
        source,
        locals())
 
def test_2_types():
    source = '''
        int main() {
          {
            FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
            allocator_data.addType(getTypeId<X>());
            allocator_data.addType(getTypeId<Y>());
            FixedSizeAllocator allocator(allocator_data);
            allocator.constructObject<X>(15);
            allocator.constructObject<Y>();
            Assert(X::num_instances == 1);
            Assert(Y::num_instances == 1);
          }
          Assert(X::num_instances == 0);
          Assert(Y::num_instances == 0);
        }
        '''
    expect_success(
        COMMON_DEFINITIONS,
        source,
        locals())
 
def test_externally_allocated_only():
    source = '''
        int main() {
          {
            FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
            allocator_data.addExternallyAllocatedType(getTypeId<X>());
            FixedSizeAllocator allocator(allocator_data);
            allocator.registerExternallyAllocatedObject(new X(15));
            // The allocator takes ownership.  Valgrind will report an error if  X is not deleted.
            Assert(X::num_instances == 1);
          }
          Assert(X::num_instances == 0);
        }
        '''
    expect_success(
        COMMON_DEFINITIONS,
        source,
        locals())
 
def test_mix():
    source = '''
        int main() {
          {
            FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
            allocator_data.addExternallyAllocatedType(getTypeId<X>());
            allocator_data.addType(getTypeId<Y>());
            FixedSizeAllocator allocator(allocator_data);
            allocator.registerExternallyAllocatedObject(new X(15));
            // The allocator takes ownership.  Valgrind will report an error if  X is not deleted.
            allocator.constructObject<Y>();
            Assert(X::num_instances == 1);
            Assert(Y::num_instances == 1);
          }
          Assert(X::num_instances == 0);
          Assert(Y::num_instances == 0);
        }
        '''
    expect_success(
        COMMON_DEFINITIONS,
        source,
        locals())
 
def test_alignment():
    source = '''
        int main() {
          FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
          allocator_data.addType(getTypeId<TypeWithAlignment<1>>());
          allocator_data.addType(getTypeId<TypeWithAlignment<8>>());
          allocator_data.addType(getTypeId<TypeWithAlignment<2>>());
          allocator_data.addType(getTypeId<TypeWithAlignment<128>>());
          allocator_data.addType(getTypeId<TypeWithAlignment<2>>());
          allocator_data.addType(getTypeId<TypeWithAlignment<8>>());
          allocator_data.addType(getTypeId<TypeWithAlignment<1>>());
          FixedSizeAllocator allocator(allocator_data);
          // TypeWithLargeAlignment::TypeWithLargeAlignment() will assert that the alignment is correct.
          allocator.constructObject<TypeWithAlignment<2>>();
          allocator.constructObject<TypeWithAlignment<8>>();
          allocator.constructObject<TypeWithAlignment<1>>();
          allocator.constructObject<TypeWithAlignment<128>>();
          allocator.constructObject<TypeWithAlignment<1>>();
          allocator.constructObject<TypeWithAlignment<8>>();
          allocator.constructObject<TypeWithAlignment<2>>();
        }
        '''
    expect_success(
        COMMON_DEFINITIONS,
        source,
        locals())
 
def test_move_constructor():
    source = '''
        int main() {
          {
            FixedSizeAllocator::FixedSizeAllocatorData allocator_data;
            allocator_data.addType(getTypeId<X>());
            allocator_data.addType(getTypeId<Y>());
            FixedSizeAllocator allocator(allocator_data);
            allocator.constructObject<X>(15);
            FixedSizeAllocator allocator2(std::move(allocator));
            allocator2.constructObject<Y>();
            Assert(X::num_instances == 1);
            Assert(Y::num_instances == 1);
          }
          Assert(X::num_instances == 0);
          Assert(Y::num_instances == 0);
        }
        '''
    expect_success(
        COMMON_DEFINITIONS,
        source,
        locals())
 
if __name__== '__main__':
    main(__file__)