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 #pragma once 15 16 namespace pw::containers::test { 17 18 struct CopyOnly { CopyOnlyCopyOnly19 explicit CopyOnly(int val) : value(val) {} 20 CopyOnlyCopyOnly21 CopyOnly(const CopyOnly& other) { value = other.value; } 22 23 CopyOnly& operator=(const CopyOnly& other) { 24 value = other.value; 25 return *this; 26 } 27 28 CopyOnly(CopyOnly&&) = delete; 29 30 int value; 31 }; 32 33 struct MoveOnly { MoveOnlyMoveOnly34 explicit MoveOnly(int val) : value(val) {} 35 36 MoveOnly(const MoveOnly&) = delete; 37 MoveOnlyMoveOnly38 MoveOnly(MoveOnly&& other) { 39 value = other.value; 40 other.value = kDeleted; 41 } 42 43 static constexpr int kDeleted = -1138; 44 45 int value; 46 }; 47 48 struct Counter { 49 static int created; 50 static int destroyed; 51 static int moved; 52 ResetCounter53 static void Reset() { created = destroyed = moved = 0; } 54 CounterCounter55 Counter() : value(0) { created += 1; } 56 CounterCounter57 Counter(int val) : value(val) { created += 1; } 58 CounterCounter59 Counter(const Counter& other) : value(other.value) { created += 1; } 60 CounterCounter61 Counter(Counter&& other) : value(other.value) { 62 other.value = 0; 63 moved += 1; 64 } 65 66 Counter& operator=(const Counter& other) { 67 value = other.value; 68 created += 1; 69 return *this; 70 } 71 72 Counter& operator=(Counter&& other) { 73 value = other.value; 74 other.value = 0; 75 moved += 1; 76 return *this; 77 } 78 ~CounterCounter79 ~Counter() { destroyed += 1; } 80 81 int value; 82 }; 83 84 } // namespace pw::containers::test 85