danderson@11: // vim: set sts=8 ts=2 sw=2 tw=99 et: danderson@11: // danderson@11: // Copyright (C) 2013, David Anderson and AlliedModders LLC danderson@11: // All rights reserved. danderson@11: // danderson@11: // Redistribution and use in source and binary forms, with or without danderson@11: // modification, are permitted provided that the following conditions are met: danderson@11: // danderson@11: // * Redistributions of source code must retain the above copyright notice, this danderson@11: // list of conditions and the following disclaimer. danderson@11: // * Redistributions in binary form must reproduce the above copyright notice, danderson@11: // this list of conditions and the following disclaimer in the documentation danderson@11: // and/or other materials provided with the distribution. danderson@11: // * Neither the name of AlliedModders LLC nor the names of its contributors danderson@11: // may be used to endorse or promote products derived from this software danderson@11: // without specific prior written permission. danderson@11: // danderson@11: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" danderson@11: // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE danderson@11: // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE danderson@11: // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE danderson@11: // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR danderson@11: // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF danderson@11: // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS danderson@11: // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN danderson@11: // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) danderson@11: // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE danderson@11: // POSSIBILITY OF SUCH DAMAGE. danderson@11: danderson@11: #include danderson@11: #include danderson@11: #include "runner.h" danderson@11: danderson@11: using namespace ke; danderson@11: danderson@11: static size_t sDtors = 0; danderson@11: danderson@11: class Counted : public Refcounted danderson@11: { danderson@11: public: danderson@11: ~Counted() { danderson@11: sDtors++; danderson@11: } danderson@11: }; danderson@11: danderson@11: static inline PassRef danderson@11: PassThrough(const Ref &obj) danderson@11: { danderson@11: return obj; danderson@11: } danderson@11: danderson@11: class TestRefcounting : public Test danderson@11: { danderson@11: public: danderson@11: TestRefcounting() danderson@11: : Test("Refcounting") danderson@11: { danderson@11: } danderson@11: danderson@11: bool Run() KE_OVERRIDE danderson@11: { danderson@11: { danderson@11: Ref obj(Newborn(new Counted())); danderson@11: } danderson@11: if (!check(sDtors == 1, "Ref/Newborn counted properly")) danderson@11: return false; danderson@11: danderson@11: { danderson@11: Ref obj(Newborn(new Counted())); danderson@11: Ref obj2 = PassThrough(obj); danderson@11: Ref obj3 = PassThrough(obj); danderson@11: if (!check(sDtors == 1, "destructor not called early")) danderson@11: return false; danderson@11: Ref obj4 = PassThrough(PassThrough(PassThrough(obj))); danderson@11: if (!check(sDtors == 1, "destructor not called early")) danderson@11: return false; danderson@11: } danderson@11: if (!check(sDtors == 2, "PassRef/Ref counted properly")) danderson@11: return false; danderson@11: danderson@11: return true; danderson@11: } danderson@11: } sTestRefcounting; danderson@11: