From 4dcf2909fe3286560babf6d20dcf0a38b4e52201 Mon Sep 17 00:00:00 2001 From: Xin Tong Date: Wed, 16 Sep 2015 02:49:00 +0000 Subject: [PATCH] Add ObjC-specific test cases to separate file. post-commit for r31902 Swift SVN r31988 --- unittests/runtime/CMakeLists.txt | 1 + unittests/runtime/Refcounting.cpp | 147 ++++++++++++++++++++++++++++++ unittests/runtime/Refcounting.mm | 131 +------------------------- 3 files changed, 149 insertions(+), 130 deletions(-) create mode 100644 unittests/runtime/Refcounting.cpp diff --git a/unittests/runtime/CMakeLists.txt b/unittests/runtime/CMakeLists.txt index f4cbc9942a3dd..70c92fef936fb 100644 --- a/unittests/runtime/CMakeLists.txt +++ b/unittests/runtime/CMakeLists.txt @@ -18,6 +18,7 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND add_swift_unittest(SwiftRuntimeTests Metadata.cpp Enum.cpp + Refcounting.cpp ${PLATFORM_SOURCES} ) diff --git a/unittests/runtime/Refcounting.cpp b/unittests/runtime/Refcounting.cpp new file mode 100644 index 0000000000000..d7825680bbe27 --- /dev/null +++ b/unittests/runtime/Refcounting.cpp @@ -0,0 +1,147 @@ +//===swift/unittests/runtime/Refcounting.cpp - Reference-counting for swift===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See http://swift.org/LICENSE.txt for license information +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#include "swift/Runtime/HeapObject.h" +#include "swift/Runtime/Metadata.h" +#include "gtest/gtest.h" + +using namespace swift; + +struct TestObject : HeapObject { + size_t *Addr; + size_t Value; +}; + +static void destroyTestObject(HeapObject *_object) { + auto object = static_cast(_object); + assert(object->Addr && "object already deallocated"); + *object->Addr = object->Value; + object->Addr = nullptr; + swift_deallocObject(object, sizeof(TestObject), alignof(TestObject) - 1); +} + +static const FullMetadata TestClassObjectMetadata = { + { { &destroyTestObject }, { &_TWVBo } }, + { { { MetadataKind::Class } }, 0, /*rodata*/ 1, + ClassFlags::UsesSwift1Refcounting, nullptr, 0, 0, 0, 0, 0 } +}; + +/// Create an object that, when deallocated, stores the given value to +/// the given pointer. +static TestObject *allocTestObject(size_t *addr, size_t value) { + auto result = + static_cast(swift_allocObject(&TestClassObjectMetadata, + sizeof(TestObject), + alignof(TestObject) - 1)); + result->Addr = addr; + result->Value = value; + return result; +} + +TEST(RefcountingTest, release) { + size_t value = 0; + auto object = allocTestObject(&value, 1); + EXPECT_EQ(0u, value); + swift_release(object); + EXPECT_EQ(1u, value); +} + +TEST(RefcountingTest, retain_release) { + size_t value = 0; + auto object = allocTestObject(&value, 1); + EXPECT_EQ(0u, value); + auto retainResult = swift_retain(object); + EXPECT_EQ(object, retainResult); + EXPECT_EQ(0u, value); + swift_release(object); + EXPECT_EQ(0u, value); + swift_release(object); + EXPECT_EQ(1u, value); +} + +TEST(RefcountingTest, pin_unpin) { + size_t value = 0; + auto object = allocTestObject(&value, 1); + EXPECT_EQ(0u, value); + auto pinResult = swift_tryPin(object); + EXPECT_EQ(object, pinResult); + EXPECT_EQ(0u, value); + swift_release(object); + EXPECT_EQ(0u, value); + swift_unpin(object); + EXPECT_EQ(1u, value); +} + +TEST(RefcountingTest, pin_pin_unpin_unpin) { + size_t value = 0; + auto object = allocTestObject(&value, 1); + EXPECT_EQ(0u, value); + auto pinResult = swift_tryPin(object); + EXPECT_EQ(object, pinResult); + EXPECT_EQ(0u, value); + auto pinResult2 = swift_tryPin(object); + EXPECT_EQ(nullptr, pinResult2); + EXPECT_EQ(0u, value); + swift_unpin(pinResult2); + EXPECT_EQ(0u, value); + swift_release(object); + EXPECT_EQ(0u, value); + swift_unpin(object); + EXPECT_EQ(1u, value); +} + +TEST(RefcountingTest, retain_release_n) { + size_t value = 0; + auto object = allocTestObject(&value, 1); + EXPECT_EQ(0u, value); + auto retainResult = swift_retain_n(object, 32); + EXPECT_EQ(object, retainResult); + retainResult = swift_retain(object); + EXPECT_EQ(object, retainResult); + EXPECT_EQ(0u, value); + EXPECT_EQ(34u, swift_retainCount(object)); + swift_release_n(object, 31); + EXPECT_EQ(0u, value); + EXPECT_EQ(3u, swift_retainCount(object)); + swift_release(object); + EXPECT_EQ(0u, value); + EXPECT_EQ(2u, swift_retainCount(object)); + swift_release_n(object, 1); + EXPECT_EQ(0u, value); + EXPECT_EQ(1u, swift_retainCount(object)); + swift_release(object); + EXPECT_EQ(1u, value); +} + +TEST(RefcountingTest, unknown_retain_release_n) { + size_t value = 0; + auto object = allocTestObject(&value, 1); + EXPECT_EQ(0u, value); + auto retainResult = swift_unknownRetain_n(object, 32); + EXPECT_EQ(object, retainResult); + retainResult = swift_unknownRetain(object); + EXPECT_EQ(object, retainResult); + EXPECT_EQ(0u, value); + EXPECT_EQ(34u, swift_retainCount(object)); + swift_unknownRelease_n(object, 31); + EXPECT_EQ(0u, value); + EXPECT_EQ(3u, swift_retainCount(object)); + swift_unknownRelease(object); + EXPECT_EQ(0u, value); + EXPECT_EQ(2u, swift_retainCount(object)); + swift_unknownRelease_n(object, 1); + EXPECT_EQ(0u, value); + EXPECT_EQ(1u, swift_retainCount(object)); + swift_unknownRelease(object); + EXPECT_EQ(1u, value); +} + diff --git a/unittests/runtime/Refcounting.mm b/unittests/runtime/Refcounting.mm index 59a29cef45858..4fb858c7121a9 100644 --- a/unittests/runtime/Refcounting.mm +++ b/unittests/runtime/Refcounting.mm @@ -1,4 +1,4 @@ -//===- swift/unittests/runtime/Refcounting.cpp - Reference-counting -------===// +//=== swift/unittests/runtime/Refcounting.mm - Reference-counting for ObjC-===// // // This source file is part of the Swift.org open source project // @@ -31,135 +31,6 @@ - (void) dealloc { return static_cast([ObjCTestClass new]); } -struct TestObject : HeapObject { - size_t *Addr; - size_t Value; -}; - -static void destroyTestObject(HeapObject *_object) { - auto object = static_cast(_object); - assert(object->Addr && "object already deallocated"); - *object->Addr = object->Value; - object->Addr = nullptr; - swift_deallocObject(object, sizeof(TestObject), alignof(TestObject) - 1); -} - -static const FullMetadata TestClassObjectMetadata = { - { { &destroyTestObject }, { &_TWVBo } }, - { { { MetadataKind::Class } }, 0, /*rodata*/ 1, - ClassFlags::UsesSwift1Refcounting, nullptr, 0, 0, 0, 0, 0 } -}; - -/// Create an object that, when deallocated, stores the given value to -/// the given pointer. -static TestObject *allocTestObject(size_t *addr, size_t value) { - auto result = - static_cast(swift_allocObject(&TestClassObjectMetadata, - sizeof(TestObject), - alignof(TestObject) - 1)); - result->Addr = addr; - result->Value = value; - return result; -} - -TEST(RefcountingTest, release) { - size_t value = 0; - auto object = allocTestObject(&value, 1); - EXPECT_EQ(0u, value); - swift_release(object); - EXPECT_EQ(1u, value); -} - -TEST(RefcountingTest, retain_release) { - size_t value = 0; - auto object = allocTestObject(&value, 1); - EXPECT_EQ(0u, value); - auto retainResult = swift_retain(object); - EXPECT_EQ(object, retainResult); - EXPECT_EQ(0u, value); - swift_release(object); - EXPECT_EQ(0u, value); - swift_release(object); - EXPECT_EQ(1u, value); -} - -TEST(RefcountingTest, pin_unpin) { - size_t value = 0; - auto object = allocTestObject(&value, 1); - EXPECT_EQ(0u, value); - auto pinResult = swift_tryPin(object); - EXPECT_EQ(object, pinResult); - EXPECT_EQ(0u, value); - swift_release(object); - EXPECT_EQ(0u, value); - swift_unpin(object); - EXPECT_EQ(1u, value); -} - -TEST(RefcountingTest, pin_pin_unpin_unpin) { - size_t value = 0; - auto object = allocTestObject(&value, 1); - EXPECT_EQ(0u, value); - auto pinResult = swift_tryPin(object); - EXPECT_EQ(object, pinResult); - EXPECT_EQ(0u, value); - auto pinResult2 = swift_tryPin(object); - EXPECT_EQ(nullptr, pinResult2); - EXPECT_EQ(0u, value); - swift_unpin(pinResult2); - EXPECT_EQ(0u, value); - swift_release(object); - EXPECT_EQ(0u, value); - swift_unpin(object); - EXPECT_EQ(1u, value); -} - -TEST(RefcountingTest, retain_release_n) { - size_t value = 0; - auto object = allocTestObject(&value, 1); - EXPECT_EQ(0u, value); - auto retainResult = swift_retain_n(object, 32); - EXPECT_EQ(object, retainResult); - retainResult = swift_retain(object); - EXPECT_EQ(object, retainResult); - EXPECT_EQ(0u, value); - EXPECT_EQ(34u, swift_retainCount(object)); - swift_release_n(object, 31); - EXPECT_EQ(0u, value); - EXPECT_EQ(3u, swift_retainCount(object)); - swift_release(object); - EXPECT_EQ(0u, value); - EXPECT_EQ(2u, swift_retainCount(object)); - swift_release_n(object, 1); - EXPECT_EQ(0u, value); - EXPECT_EQ(1u, swift_retainCount(object)); - swift_release(object); - EXPECT_EQ(1u, value); -} - -TEST(RefcountingTest, unknown_retain_release_n) { - size_t value = 0; - auto object = allocTestObject(&value, 1); - EXPECT_EQ(0u, value); - auto retainResult = swift_unknownRetain_n(object, 32); - EXPECT_EQ(object, retainResult); - retainResult = swift_unknownRetain(object); - EXPECT_EQ(object, retainResult); - EXPECT_EQ(0u, value); - EXPECT_EQ(34u, swift_retainCount(object)); - swift_unknownRelease_n(object, 31); - EXPECT_EQ(0u, value); - EXPECT_EQ(3u, swift_retainCount(object)); - swift_unknownRelease(object); - EXPECT_EQ(0u, value); - EXPECT_EQ(2u, swift_retainCount(object)); - swift_unknownRelease_n(object, 1); - EXPECT_EQ(0u, value); - EXPECT_EQ(1u, swift_retainCount(object)); - swift_unknownRelease(object); - EXPECT_EQ(1u, value); -} - TEST(RefcountingTest, objc_unknown_retain_release_n) { auto object = make_objc_object(); auto retainResult = swift_unknownRetain_n(object, 32);