OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
juce_ReferenceCountedArray.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2018 - ROLI Ltd.
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
23 namespace juce
24{
25
26#if JUCE_UNIT_TESTS
27
28 class ReferenceCountedArrayTests : public UnitTest
29{
30 public:
31 ReferenceCountedArrayTests()
32 : UnitTest ("ReferenceCountedArray", UnitTestCategories::containers)
33 {}
34
35 //==============================================================================
36 void runTest() override
37 {
38 beginTest ("Add derived objects");
39 {
40 ReferenceCountedArray<TestDerivedObj> derivedArray;
41 derivedArray.add (static_cast<TestDerivedObj*> (new TestBaseObj()));
42 expectEquals (derivedArray.size(), 1);
43 expectEquals (derivedArray.getObjectPointer (0)->getReferenceCount(), 1);
44 expectEquals (derivedArray[0]->getReferenceCount(), 2);
45
46 for (auto o : derivedArray)
47 expectEquals (o->getReferenceCount(), 1);
48
49 ReferenceCountedArray<TestBaseObj> baseArray;
50 baseArray.addArray (derivedArray);
51
52 for (auto o : baseArray)
53 expectEquals (o->getReferenceCount(), 2);
54
55 derivedArray.clearQuick();
56 baseArray.clearQuick();
57
58 auto* baseObject = new TestBaseObj();
59 TestBaseObj::Ptr baseObjectPtr = baseObject;
60 expectEquals (baseObject->getReferenceCount(), 1);
61
62 auto* derivedObject = new TestDerivedObj();
63 TestDerivedObj::Ptr derivedObjectPtr = derivedObject;
64 expectEquals (derivedObject->getReferenceCount(), 1);
65
66 baseArray.add (baseObject);
67 baseArray.add (derivedObject);
68
69 for (auto o : baseArray)
70 expectEquals (o->getReferenceCount(), 2);
71
72 expectEquals (baseObject->getReferenceCount(), 2);
73 expectEquals (derivedObject->getReferenceCount(), 2);
74
75 derivedArray.add (derivedObject);
76
77 for (auto o : derivedArray)
78 expectEquals (o->getReferenceCount(), 3);
79
80 derivedArray.clearQuick();
81 baseArray.clearQuick();
82
83 expectEquals (baseObject->getReferenceCount(), 1);
84 expectEquals (derivedObject->getReferenceCount(), 1);
85
86 baseArray.add (baseObjectPtr);
87 baseArray.add (derivedObjectPtr.get());
88
89 for (auto o : baseArray)
90 expectEquals (o->getReferenceCount(), 2);
91
92 derivedArray.add (derivedObjectPtr);
93
94 for (auto o : derivedArray)
95 expectEquals (o->getReferenceCount(), 3);
96 }
97
98 beginTest ("Iterate in destructor");
99 {
100 {
101 ReferenceCountedArray<DestructorObj> arr;
102
103 for (int i = 0; i < 2; ++i)
104 arr.add (new DestructorObj (*this, arr));
105 }
106
107 ReferenceCountedArray<DestructorObj> arr;
108
109 for (int i = 0; i < 1025; ++i)
110 arr.add (new DestructorObj (*this, arr));
111
112 while (! arr.isEmpty())
113 arr.remove (0);
114
115 for (int i = 0; i < 1025; ++i)
116 arr.add (new DestructorObj (*this, arr));
117
118 arr.removeRange (1, arr.size() - 3);
119
120 for (int i = 0; i < 1025; ++i)
121 arr.add (new DestructorObj (*this, arr));
122
123 arr.set (500, new DestructorObj (*this, arr));
124 }
125 }
126
127 private:
128 struct TestBaseObj : public ReferenceCountedObject
129 {
130 using Ptr = ReferenceCountedObjectPtr<TestBaseObj>;
131
132 TestBaseObj() = default;
133
134 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TestBaseObj)
135 };
136
137 struct TestDerivedObj : public TestBaseObj
138 {
139 using Ptr = ReferenceCountedObjectPtr<TestDerivedObj>;
140
141 TestDerivedObj() = default;
142
143 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TestDerivedObj)
144 };
145
146 struct DestructorObj : public ReferenceCountedObject
147 {
148 DestructorObj (ReferenceCountedArrayTests& p,
149 ReferenceCountedArray<DestructorObj>& arr)
150 : parent (p), objectArray (arr)
151 {}
152
153 ~DestructorObj()
154 {
155 data = 0;
156
157 for (auto* o : objectArray)
158 {
159 parent.expect (o != nullptr);
160 parent.expect (o != this);
161 parent.expectEquals (o->data, 374);
162 }
163 }
164
165 ReferenceCountedArrayTests& parent;
166 ReferenceCountedArray<DestructorObj>& objectArray;
167 int data = 374;
168
169 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DestructorObj)
170 };
171};
172
173 static ReferenceCountedArrayTests referenceCountedArrayTests;
174
175#endif
176
177} // namespace juce