Skip to content

Commit 2d2c76f

Browse files
committed
stdlib: AnyHashable tests: finish tests for CF types
1 parent 438712c commit 2d2c76f

File tree

5 files changed

+108
-8
lines changed

5 files changed

+108
-8
lines changed

stdlib/private/StdlibUnittest/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ add_swift_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STD
1515
# filename.
1616
StdlibUnittest.swift.gyb
1717

18+
InspectValue.cpp
19+
InspectValue.swift
1820
InterceptTraps.cpp
1921
LifetimeTracked.swift
2022
MinimalTypes.swift.gyb
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/Runtime/Config.h"
14+
#include "swift/Runtime/Metadata.h"
15+
16+
using namespace swift;
17+
18+
SWIFT_CC(swift)
19+
extern "C"
20+
uint32_t swift_StdlibUnittest_getMetadataKindOf(
21+
OpaqueValue *value,
22+
const Metadata *type
23+
) {
24+
auto result = uint32_t(type->getKind());
25+
type->vw_destroy(value);
26+
return result;
27+
}
28+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// namespace
14+
public enum SwiftRuntime {
15+
public enum MetadataKind : Int {
16+
case `class` = 0
17+
case `struct` = 1
18+
case `enum` = 2
19+
case optional = 3
20+
case opaque = 8
21+
case tuple = 9
22+
case function = 10
23+
case existential = 12
24+
case metatype = 13
25+
case objCClassWrapper = 14
26+
case existentialMetatype = 15
27+
case foreignClass = 16
28+
case heapLocalVariable = 64
29+
case heapGenericLocalVariable = 65
30+
case errorObject = 128
31+
}
32+
33+
@_silgen_name("swift_StdlibUnittest_getMetadataKindOf")
34+
static func _metadataKindImpl<T>(of value: T) -> UInt32
35+
36+
public static func metadataKind<T>(of value: T) -> MetadataKind {
37+
return MetadataKind(rawValue: Int(_metadataKindImpl(of: value)))!
38+
}
39+
}
40+

stdlib/public/stubs/AnyHashableSupport.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ extern "C" void _swift_stdlib_makeAnyHashableUpcastingToHashableBaseType(
103103
case MetadataKind::Class:
104104
case MetadataKind::ObjCClassWrapper:
105105
case MetadataKind::ForeignClass: {
106-
// FIXME(id-as-any): handle ForeignClass.
107106
_swift_stdlib_makeAnyHashableUsingDefaultRepresentation(
108107
value, anyHashableResultPointer, findHashableBaseType(type),
109108
hashableWT);

validation-test/stdlib/AnyHashable.swift.gyb

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// RUN: %target-run-simple-swiftgyb
22
// REQUIRES: executable_test
33

4-
// FIXME(id-as-any): add tests that use CF objects (is this even
5-
// possible?)
6-
74
// FIXME(id-as-any): add tests that use Swift errors and
85
// Objective-C errors.
96

@@ -471,27 +468,61 @@ let interestingBitVectorArrays: [[UInt8]] = [
471468
[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88],
472469
]
473470

474-
AnyHashableTests.test("CFBitVector/Hashable") {
475-
let bitVectors = interestingBitVectorArrays.map(CFBitVector.makeImmutable)
471+
AnyHashableTests.test("AnyHashable with CFBitVector") {
472+
let bitVectors: [CFBitVector] =
473+
interestingBitVectorArrays.map(CFBitVector.makeImmutable)
476474
let arrays = bitVectors.map { $0.asArray }
477475
func isEq(_ lhs: [[UInt8]], _ rhs: [[UInt8]]) -> Bool {
478476
return zip(lhs, rhs).map { $0 == $1 }.reduce(true, { $0 && $1 })
479477
}
480478
expectEqualTest(interestingBitVectorArrays, arrays, sameValue: isEq)
481479
checkHashable(bitVectors, equalityOracle: { $0 == $1 })
480+
481+
expectEqual(.foreignClass, SwiftRuntime.metadataKind(of: bitVectors.first!))
482+
483+
checkHashable(
484+
bitVectors.map(AnyHashable.init),
485+
equalityOracle: { $0 == $1 })
486+
487+
let bitVectorsAsAnyObjects: [NSObject] = bitVectors.map {
488+
($0 as AnyObject) as! NSObject
489+
}
490+
expectEqual(
491+
.objCClassWrapper,
492+
SwiftRuntime.metadataKind(of: bitVectorsAsAnyObjects.first!))
493+
checkHashable(
494+
bitVectorsAsAnyObjects.map(AnyHashable.init),
495+
equalityOracle: { $0 == $1 })
482496
}
483497

484-
AnyHashableTests.test("CFMutableBitVector/Hashable") {
498+
AnyHashableTests.test("AnyHashable with CFMutableBitVector") {
485499
// CFMutableBitVector inherits the Hashable conformance from
486500
// CFBitVector.
487-
let bitVectors =
501+
let bitVectors: [CFMutableBitVector] =
488502
interestingBitVectorArrays.map(CFMutableBitVector.makeMutable)
489503
let arrays = bitVectors.map { $0.asArray }
490504
func isEq(_ lhs: [[UInt8]], _ rhs: [[UInt8]]) -> Bool {
491505
return zip(lhs, rhs).map { $0 == $1 }.reduce(true, { $0 && $1 })
492506
}
493507
expectEqualTest(interestingBitVectorArrays, arrays, sameValue: isEq)
494508
checkHashable(bitVectors, equalityOracle: { $0 == $1 })
509+
510+
expectEqual(.foreignClass, SwiftRuntime.metadataKind(of: bitVectors.first!))
511+
512+
checkHashable(
513+
bitVectors.map(AnyHashable.init),
514+
equalityOracle: { $0 == $1 })
515+
516+
let bitVectorsAsAnyObjects: [NSObject] = bitVectors.map {
517+
($0 as AnyObject) as! NSObject
518+
}
519+
checkHashable(bitVectors, equalityOracle: { $0 == $1 })
520+
expectEqual(
521+
.objCClassWrapper,
522+
SwiftRuntime.metadataKind(of: bitVectorsAsAnyObjects.first!))
523+
checkHashable(
524+
bitVectorsAsAnyObjects.map(AnyHashable.init),
525+
equalityOracle: { $0 == $1 })
495526
}
496527

497528
#endif

0 commit comments

Comments
 (0)