-
Notifications
You must be signed in to change notification settings - Fork 215
/
Easy_100_Same_Tree_Test.swift
115 lines (113 loc) · 4.2 KB
/
Easy_100_Same_Tree_Test.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
//
// Easy_100_Same_Tree_Test.swift
// Solutions
//
// Created by Di Wu on 12/11/15.
// Copyright © 2015 diwu. All rights reserved.
//
import XCTest
class Easy_100_Same_Tree_Test: XCTestCase, SolutionsTestCase {
private typealias Node_Swift = Easy_100_Same_Tree.Node
private typealias Node_ObjC = ObjC_Easy_100_Same_Tree_Node
private func convertArrayToTree_swift(_ arr: [Int]) -> Node_Swift {
let root: Node_Swift = Node_Swift.init(value: arr[0], left: nil, right: nil)
var fifoQueue: [Node_Swift] = [root]
var i = 1
while i < arr.count {
let node: Node_Swift = fifoQueue.removeFirst()
if arr[i] == Int.min {
node.left = nil
} else {
node.left = Node_Swift.init(value: arr[i], left: nil, right: nil)
fifoQueue.append(node.left!)
}
if arr[i+1] == Int.min {
node.right = nil
} else {
node.right = Node_Swift.init(value: arr[i+1], left: nil, right: nil)
fifoQueue.append(node.right!)
}
i += 2
}
return root
}
private func convertArrayToTree_objc(_ arr: [Int]) -> Node_ObjC {
let root: Node_ObjC = Node_ObjC.init(value: arr[0], left: nil, right: nil)
var fifoQueue: [Node_ObjC] = [root]
var i = 1
while i < arr.count {
let node: Node_ObjC = fifoQueue.removeFirst()
if arr[i] == Int.min {
node.left = nil
} else {
node.left = Node_ObjC.init(value: arr[i], left: nil, right: nil)
fifoQueue.append(node.left!)
}
if arr[i+1] == Int.min {
node.right = nil
} else {
node.right = Node_ObjC.init(value: arr[i+1], left: nil, right: nil)
fifoQueue.append(node.right!)
}
i += 2
}
return root
}
func test_001() {
let input: [[Int]] = [
[4, 2, 6, 1, 3, 5, 7],
[4, 2, 6, 1, 3, 5, 7],
]
let expected: Bool = true
asyncHelper(input: input, expected: expected)
}
func test_002() {
let input: [[Int]] = [
[4, 2, 6, 1, 3, Int.min, 7],
[4, 2, 6, 1, 3, 7, Int.min],
]
let expected: Bool = false
asyncHelper(input: input, expected: expected)
}
func test_003() {
let input: [[Int]] = [
[4, 2, 6],
[4, 2, 6, 1, 3, 5, 7],
]
let expected: Bool = false
asyncHelper(input: input, expected: expected)
}
func test_004() {
let input: [[Int]] = [
[4, 2, 6, 1, 3, 5, 7],
[4, 2, 6, 1, Int.min],
]
let expected: Bool = false
asyncHelper(input: input, expected: expected)
}
func test_005() {
let input: [[Int]] = [
[4],
[4],
]
let expected: Bool = true
asyncHelper(input: input, expected: expected)
}
private func asyncHelper(input: [[Int]], expected: Bool) {
weak var expectation: XCTestExpectation? = self.expectation(description:timeOutName())
serialQueue().async(execute: { () -> Void in
let result_swift: Bool = Easy_100_Same_Tree.isSameTree(p: self.convertArrayToTree_swift(input[0]), q: self.convertArrayToTree_swift(input[1]))
let result_objc: Bool = ObjC_Easy_100_Same_Tree.isSameTree( withP: self.convertArrayToTree_objc(input[0]), q: self.convertArrayToTree_objc(input[1]))
assertHelper(expected == result_swift, problemName:self.problemName(), input: input, resultValue: result_swift, expectedValue: expected)
assertHelper(expected == result_objc, problemName:self.problemName(), input: input, resultValue: result_objc, expectedValue: expected)
if let unwrapped = expectation {
unwrapped.fulfill()
}
})
waitForExpectations(timeout:timeOut()) { (error: Error?) -> Void in
if error != nil {
assertHelper(false, problemName:self.problemName(), input: input, resultValue:self.timeOutName(), expectedValue: expected)
}
}
}
}