OpenShot Audio Library | OpenShotAudio  0.6.0
juce_OwnedArray.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 
23 namespace juce
24 {
25 
26 #if JUCE_UNIT_TESTS
27 
28 static struct OwnedArrayTest : public UnitTest
29 {
30  struct Base
31  {
32  Base() = default;
33  virtual ~Base() = default;
34 
35  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Base)
36  };
37 
38  struct Derived final : public Base
39  {
40  Derived() = default;
41 
42  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Derived)
43  };
44 
45  struct DestructorObj
46  {
47  DestructorObj (OwnedArrayTest& p,
48  OwnedArray<DestructorObj>& arr)
49  : parent (p), objectArray (arr)
50  {}
51 
52  ~DestructorObj()
53  {
54  data = 0;
55 
56  for (auto* o : objectArray)
57  {
58  parent.expect (o != nullptr);
59  parent.expect (o != this);
60 
61  if (o != nullptr)
62  parent.expectEquals (o->data, 956);
63  }
64  }
65 
66  OwnedArrayTest& parent;
67  OwnedArray<DestructorObj>& objectArray;
68  int data = 956;
69 
70  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DestructorObj)
71  };
72 
73  OwnedArrayTest()
74  : UnitTest ("OwnedArray", UnitTestCategories::containers)
75  {}
76 
77  void runTest() override
78  {
79  beginTest ("After converting move construction, ownership is transferred");
80  {
81  OwnedArray<Derived> derived { new Derived{}, new Derived{}, new Derived{} };
82 
83  OwnedArray<Base> base { std::move (derived) };
84 
85  expectEquals (base.size(), 3);
86  expectEquals (derived.size(), 0);
87  }
88 
89  beginTest ("After converting move assignment, ownership is transferred");
90  {
91  OwnedArray<Base> base;
92 
93  base = OwnedArray<Derived> { new Derived{}, new Derived{}, new Derived{} };
94 
95  expectEquals (base.size(), 3);
96  }
97 
98  beginTest ("Iterate in destructor");
99  {
100  {
101  OwnedArray<DestructorObj> arr;
102 
103  for (int i = 0; i < 2; ++i)
104  arr.add (new DestructorObj (*this, arr));
105  }
106 
107  OwnedArray<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 } ownedArrayTest;
127 
128 #endif
129 
130 }