1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #include "pw_containers_private/test_helpers.h"
15
16 #include "pw_assert/check.h"
17
18 namespace pw::containers::test {
19
Destructed()20 void Counter::ObjectCounter::Destructed() {
21 PW_CHECK_UINT_GT(count_, 0, "");
22 count_ -= 1;
23 }
24
~ObjectCounter()25 Counter::ObjectCounter::~ObjectCounter() { PW_CHECK_UINT_EQ(count_, 0, ""); }
26
operator =(const Counter & other)27 Counter& Counter::operator=(const Counter& other) {
28 PW_CHECK_PTR_EQ(this, set_to_this_when_constructed_);
29 value = other.value;
30 created += 1;
31 return *this;
32 }
33
operator =(Counter && other)34 Counter& Counter::operator=(Counter&& other) {
35 PW_CHECK_PTR_EQ(this, set_to_this_when_constructed_);
36 value = other.value;
37 other.value = 0;
38 moved += 1;
39 return *this;
40 }
41
~Counter()42 Counter::~Counter() {
43 PW_CHECK_PTR_EQ(this, set_to_this_when_constructed_);
44 destroyed += 1;
45 objects_.Destructed();
46 set_to_this_when_constructed_ = nullptr;
47 }
48
49 int Counter::created = 0;
50 int Counter::destroyed = 0;
51 int Counter::moved = 0;
52 Counter::ObjectCounter Counter::objects_;
53
54 } // namespace pw::containers::test
55