OpenShot Library | OpenShotAudio  0.2.2
juce_OwnedArray.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - 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 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 : 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  parent.expectEquals (o->data, 956);
61  }
62  }
63 
64  OwnedArrayTest& parent;
65  OwnedArray<DestructorObj>& objectArray;
66  int data = 956;
67 
68  JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DestructorObj)
69  };
70 
71  OwnedArrayTest()
72  : UnitTest ("OwnedArray", UnitTestCategories::containers)
73  {}
74 
75  void runTest() override
76  {
77  beginTest ("After converting move construction, ownership is transferred");
78  {
79  OwnedArray<Derived> derived { new Derived{}, new Derived{}, new Derived{} };
80 
81  OwnedArray<Base> base { std::move (derived) };
82 
83  expectEquals (base.size(), 3);
84  expectEquals (derived.size(), 0);
85  }
86 
87  beginTest ("After converting move assignment, ownership is transferred");
88  {
89  OwnedArray<Base> base;
90 
91  base = OwnedArray<Derived> { new Derived{}, new Derived{}, new Derived{} };
92 
93  expectEquals (base.size(), 3);
94  }
95 
96  beginTest ("Iterate in destructor");
97  {
98  {
99  OwnedArray<DestructorObj> arr;
100 
101  for (int i = 0; i < 2; ++i)
102  arr.add (new DestructorObj (*this, arr));
103  }
104 
105  OwnedArray<DestructorObj> arr;
106 
107  for (int i = 0; i < 1025; ++i)
108  arr.add (new DestructorObj (*this, arr));
109 
110  while (! arr.isEmpty())
111  arr.remove (0);
112 
113  for (int i = 0; i < 1025; ++i)
114  arr.add (new DestructorObj (*this, arr));
115 
116  arr.removeRange (1, arr.size() - 3);
117 
118  for (int i = 0; i < 1025; ++i)
119  arr.add (new DestructorObj (*this, arr));
120 
121  arr.set (500, new DestructorObj (*this, arr));
122  }
123  }
124 } ownedArrayTest;
125 
126 #endif
127 
128 }