OpenShot Audio Library | OpenShotAudio: juce_SharedResourcePointer_test.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_SharedResourcePointer_test.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26class SharedResourcePointerTest final : public UnitTest
27{
28public:
29 SharedResourcePointerTest()
30 : UnitTest ("SharedResourcePointer", UnitTestCategories::memory) {}
31
32 void runTest() final
33 {
34 beginTest ("Only one instance is created");
35 {
36 static int count = 0;
37 struct CountIncrementer { CountIncrementer() { ++count; } };
38 expect (count == 0);
39
40 const SharedResourcePointer<CountIncrementer> instance1;
41 expect (count == 1);
42
43 const SharedResourcePointer<CountIncrementer> instance2;
44 expect (count == 1);
45
46 expect (&instance1.get() == &instance2.get());
47 }
48
49 beginTest ("The shared object is destroyed when the reference count reaches 0");
50 {
51 static int count = 0;
52 struct ReferenceCounter
53 {
54 ReferenceCounter() { ++count; }
55 ~ReferenceCounter() { --count; }
56 };
57
58 expect (count == 0);
59
60 {
61 const SharedResourcePointer<ReferenceCounter> instance1;
62 const SharedResourcePointer<ReferenceCounter> instance2;
63 expect (count == 1);
64 }
65
66 expect (count == 0);
67 }
68
69 beginTest ("getInstanceWithoutCreating()");
70 {
71 struct Object{};
72
73 expect (SharedResourcePointer<Object>::getSharedObjectWithoutCreating() == std::nullopt);
74
75 {
76 const SharedResourcePointer<Object> instance;
77 expect (&SharedResourcePointer<Object>::getSharedObjectWithoutCreating()->get() == &instance.get());
78 }
79
80 expect (SharedResourcePointer<Object>::getSharedObjectWithoutCreating() == std::nullopt);
81 }
82
83 beginTest ("Create objects with private constructors");
84 {
85 class ObjectWithPrivateConstructor
86 {
87 private:
88 ObjectWithPrivateConstructor() = default;
89 friend SharedResourcePointer<ObjectWithPrivateConstructor>;
90 };
91
92 SharedResourcePointer<ObjectWithPrivateConstructor> instance;
93 }
94 }
95};
96
97static SharedResourcePointerTest sharedResourcePointerTest;
98
99} // namespace juce
juce::SharedResourcePointer::getSharedObjectWithoutCreating
static std::optional< SharedResourcePointer > getSharedObjectWithoutCreating()
Definition juce_SharedResourcePointer.h:125
juce::UnitTest::UnitTest
UnitTest(const String &name, const String &category=String())
Definition juce_UnitTest.cpp:26
juce::UnitTest::beginTest
void beginTest(const String &testName)
Definition juce_UnitTest.cpp:89
juce::UnitTest::expect
void expect(bool testResult, const String &failureMessage=String())
Definition juce_UnitTest.cpp:97