-
Notifications
You must be signed in to change notification settings - Fork 2
/
NdArrayTests.swift
88 lines (76 loc) · 3.02 KB
/
NdArrayTests.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
import XCTest
import Darwin
@testable import NdArray
private func address<T>(_ p: UnsafeBufferPointer<T>) -> String {
String(format: "%x", Int(bitPattern: p.baseAddress))
}
private func address<T>(_ p: UnsafePointer<T>) -> String {
String(format: "%x", Int(bitPattern: p))
}
final class NdArrayTests: XCTestCase {
func testInitEmptyShouldCreateEmptyArrayWhenEmptyElementsIsZero() {
let a = NdArray<Double>(empty: 0)
XCTAssertEqual(a.count, 0)
XCTAssertEqual(a.dataArray, [])
XCTAssertEqual(a.strides, [1])
XCTAssertEqual(a.shape, [0])
XCTAssertEqual(a.ndim, 1)
}
func testNdArrayShouldNotOwnDataWhenIsView() {
let a = NdArray<Double>([[1, 2, 3], [4, 5, 6]])
let b = NdArray<Double>(a)
XCTAssert(a.ownsData)
XCTAssert(!b.ownsData)
}
func testNdArrayShouldOwnDataWhenCopied() {
let a = NdArray<Double>([1, 2, 3])
let b = NdArray<Double>(copy: a)
XCTAssert(a.ownsData)
XCTAssert(b.ownsData)
XCTAssertEqual(b.dataArray, [1, 2, 3])
}
func testNdArrayShouldCopyCorrectlyWhenStrideIsNotSizeOfT() {
struct MyType: Equatable {
let i: Int
let b: Bool
}
XCTAssertNotEqual(MemoryLayout<MyType>.size, MemoryLayout<MyType>.stride)
let a = NdArray<MyType>([MyType(i: 0, b: true), MyType(i: 1, b: false)])
let b = NdArray<MyType>(copy: a)
XCTAssertEqual(b.dataArray, [MyType(i: 0, b: true), MyType(i: 1, b: false)])
}
func testDescription() {
XCTAssertEqual(NdArray<Double>([]).description, "[]")
XCTAssertEqual(NdArray<Double>([1, 2, 3]).description, "[1.0, 2.0, 3.0]")
XCTAssertEqual(NdArray<Double>([[1, 2, 3], [4, 5, 6]]).description, "[[1.0, 2.0, 3.0],\n [4.0, 5.0, 6.0]]")
XCTAssert(NdArray<Double>([Double]()).description.hasPrefix("[]"))
XCTAssert(NdArray<Double>([[Double]]()).description.hasPrefix("[]"))
}
func testDebugDescription() {
XCTAssert(NdArray<Double>([]).debugDescription.hasPrefix("NdArray("))
XCTAssert(NdArray<Double>([1, 2, 3]).debugDescription.contains("shape"))
XCTAssert(NdArray<Double>([1, 2, 3]).debugDescription.contains("strides"))
XCTAssert(NdArray<Double>([1, 2, 3]).debugDescription.contains("data"))
}
func testDataArrayShouldReturnEmptyArrayWhenArrayIsEmpty() {
XCTAssertEqual(NdArray<Double>.zeros([]).dataArray, [])
XCTAssertEqual(NdArray<Double>.zeros([1, 0]).dataArray, [])
}
func testOverlaps() {
let a = NdArray<Double>.range(to: 11)
let b = a[[...5]]
let c = a[[5...]]
XCTAssert(a.overlaps(b))
XCTAssert(b.overlaps(a))
XCTAssert(b.overlaps(c))
XCTAssert(c.overlaps(b))
XCTAssert(a.overlaps(c))
XCTAssert(c.overlaps(a))
}
func testEmptyArrayShouldBeCAndFContiguous() {
let a = NdArray<Double>.zeros([])
XCTAssert(a.isCContiguous)
XCTAssert(a.isFContiguous)
XCTAssert(a.isContiguous)
}
}