forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestNSValue.swift
137 lines (117 loc) · 5.13 KB
/
TestNSValue.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif
class TestNSValue : XCTestCase {
static var allTests: [(String, (TestNSValue) -> () throws -> Void)] {
return [
( "test_valueWithLong", test_valueWithLong ),
( "test_valueWithCGPoint", test_valueWithCGPoint ),
( "test_valueWithCGSize", test_valueWithCGSize ),
( "test_valueWithCGRect", test_valueWithCGRect ),
( "test_valueWithNSEdgeInsets", test_valueWithNSEdgeInsets ),
( "test_valueWithNSRange", test_valueWithNSRange ),
( "test_valueWithShortArray", test_valueWithShortArray ),
( "test_valueWithULongLongArray", test_valueWithULongLongArray ),
( "test_valueWithCharPtr", test_valueWithULongLongArray ),
( "test_isEqual", test_isEqual ),
]
}
func test_valueWithCGPoint() {
let point = CGPoint(x: CGFloat(1.0), y: CGFloat(2.0234))
let value = NSValue(point: point)
XCTAssertEqual(value.pointValue, point)
var expected = CGPoint()
value.getValue(&expected)
XCTAssertEqual(expected, point)
}
func test_valueWithCGSize() {
let size = CGSize(width: CGFloat(1123.234), height: CGFloat(3452.234))
let value = NSValue(size: size)
XCTAssertEqual(value.sizeValue, size)
var expected = CGSize()
value.getValue(&expected)
XCTAssertEqual(expected, size)
}
func test_valueWithCGRect() {
let point = CGPoint(x: CGFloat(1.0), y: CGFloat(2.0234))
let size = CGSize(width: CGFloat(1123.234), height: CGFloat(3452.234))
let rect = CGRect(origin: point, size: size)
let value = NSValue(rect: rect)
XCTAssertEqual(value.rectValue, rect)
var expected = CGRect()
value.getValue(&expected)
XCTAssertEqual(expected, rect)
}
func test_valueWithNSRange() {
let range = NSRange(location: 1, length: 2)
let value = NSValue(range: range)
XCTAssertEqual(value.rangeValue.location, range.location)
XCTAssertEqual(value.rangeValue.length, range.length)
var expected = NSRange()
value.getValue(&expected)
XCTAssertEqual(expected.location, range.location)
XCTAssertEqual(expected.length, range.length)
}
func test_valueWithNSEdgeInsets() {
let edgeInsets = NSEdgeInsets(top: CGFloat(234.0), left: CGFloat(23.20), bottom: CGFloat(0.0), right: CGFloat(99.0))
let value = NSValue(edgeInsets: edgeInsets)
XCTAssertEqual(value.edgeInsetsValue.top, edgeInsets.top)
XCTAssertEqual(value.edgeInsetsValue.left, edgeInsets.left)
XCTAssertEqual(value.edgeInsetsValue.bottom, edgeInsets.bottom)
XCTAssertEqual(value.edgeInsetsValue.right, edgeInsets.right)
var expected = NSEdgeInsets()
value.getValue(&expected)
XCTAssertEqual(expected.top, edgeInsets.top)
XCTAssertEqual(expected.left, edgeInsets.left)
XCTAssertEqual(expected.bottom, edgeInsets.bottom)
XCTAssertEqual(expected.right, edgeInsets.right)
}
func test_valueWithLong() {
var long: Int32 = 123456
var expected: Int32 = 0
NSValue(bytes: &long, objCType: "l").getValue(&expected)
XCTAssertEqual(long, expected)
}
func test_valueWithULongLongArray() {
let array: Array<UInt64> = [12341234123, 23452345234, 23475982345, 9893563243, 13469816598]
array.withUnsafeBufferPointer { cArray in
var expected = [UInt64](repeating: 0, count: 5)
NSValue(bytes: cArray.baseAddress!, objCType: "[5Q]").getValue(&expected)
XCTAssertEqual(array, expected)
}
}
func test_valueWithShortArray() {
let array: Array<Int16> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
let objctype = "[" + String(array.count) + "s]"
array.withUnsafeBufferPointer { cArray in
var expected = [Int16](repeating: 0, count: array.count)
NSValue(bytes: cArray.baseAddress!, objCType: objctype).getValue(&expected)
XCTAssertEqual(array, expected)
}
}
func test_valueWithCharPtr() {
let charArray = [UInt8]("testing123".utf8)
var charPtr = UnsafeMutablePointer(mutating: charArray)
var expectedPtr: UnsafeMutablePointer<UInt8>? = nil
NSValue(bytes: &charPtr, objCType: "*").getValue(&expectedPtr)
XCTAssertEqual(charPtr, expectedPtr)
}
func test_isEqual() {
let number = NSNumber(value: Int(123))
var long: Int32 = 123456
let value = NSValue(bytes: &long, objCType: "l")
XCTAssertFalse(value.isEqual(number))
}
}