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
|
// 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
//
class TestNSValue : XCTestCase {
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() {
var charArray = [UInt8]("testing123".utf8)
charArray.withUnsafeMutableBufferPointer {
var charPtr = $0.baseAddress!
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))
}
}
|