Skip to content

Commit

Permalink
Add ObjC-specific test cases to separate file. post-commit for r31902
Browse files Browse the repository at this point in the history
Swift SVN r31988
  • Loading branch information
trentxintong committed Sep 16, 2015
1 parent 82fe88f commit 4dcf290
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 130 deletions.
1 change: 1 addition & 0 deletions unittests/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)

Expand Down
147 changes: 147 additions & 0 deletions unittests/runtime/Refcounting.cpp
Original file line number Diff line number Diff line change
@@ -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<TestObject*>(_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<ClassMetadata> 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<TestObject *>(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);
}

131 changes: 1 addition & 130 deletions unittests/runtime/Refcounting.mm
Original file line number Diff line number Diff line change
@@ -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
//
Expand Down Expand Up @@ -31,135 +31,6 @@ - (void) dealloc {
return static_cast<HeapObject *>([ObjCTestClass new]);
}

struct TestObject : HeapObject {
size_t *Addr;
size_t Value;
};

static void destroyTestObject(HeapObject *_object) {
auto object = static_cast<TestObject*>(_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<ClassMetadata> 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<TestObject *>(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);
Expand Down

0 comments on commit 4dcf290

Please sign in to comment.