68 lines · c
1//===- CommonTestUtils.h --------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef ORC_RT_UNITTEST_COMMONTESTUTILS_H10#define ORC_RT_UNITTEST_COMMONTESTUTILS_H11 12#include <cstddef>13 14template <size_t Idx = 0> class OpCounter {15public:16 OpCounter() { ++DefaultConstructions; }17 OpCounter(const OpCounter &Other) { ++CopyConstructions; }18 OpCounter &operator=(const OpCounter &Other) {19 ++CopyAssignments;20 return *this;21 }22 OpCounter(OpCounter &&Other) { ++MoveConstructions; }23 OpCounter &operator=(OpCounter &&Other) {24 ++MoveAssignments;25 return *this;26 }27 ~OpCounter() { ++Destructions; }28 29 static size_t defaultConstructions() { return DefaultConstructions; }30 static size_t copyConstructions() { return CopyConstructions; }31 static size_t copyAssignments() { return CopyAssignments; }32 static size_t copies() { return copyConstructions() + copyAssignments(); }33 static size_t moveConstructions() { return MoveConstructions; }34 static size_t moveAssignments() { return MoveAssignments; }35 static size_t moves() { return moveConstructions() + moveAssignments(); }36 static size_t destructions() { return Destructions; }37 38 static bool destructionsMatch() {39 return destructions() == defaultConstructions() + copies() + moves();40 }41 42 static void reset() {43 DefaultConstructions = 0;44 CopyConstructions = 0;45 CopyAssignments = 0;46 MoveConstructions = 0;47 MoveAssignments = 0;48 Destructions = 0;49 }50 51private:52 static size_t DefaultConstructions;53 static size_t CopyConstructions;54 static size_t CopyAssignments;55 static size_t MoveConstructions;56 static size_t MoveAssignments;57 static size_t Destructions;58};59 60template <size_t Idx> size_t OpCounter<Idx>::DefaultConstructions = 0;61template <size_t Idx> size_t OpCounter<Idx>::CopyConstructions = 0;62template <size_t Idx> size_t OpCounter<Idx>::CopyAssignments = 0;63template <size_t Idx> size_t OpCounter<Idx>::MoveConstructions = 0;64template <size_t Idx> size_t OpCounter<Idx>::MoveAssignments = 0;65template <size_t Idx> size_t OpCounter<Idx>::Destructions = 0;66 67#endif // ORC_RT_UNITTEST_COMMONTESTUTILS_H68