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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
// 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
//
import Dispatch
class TestProgress : XCTestCase {
func test_totalCompletedChangeAffectsFractionCompleted() {
let parent = Progress(parent: nil)
parent.totalUnitCount = 100
// Test self
parent.completedUnitCount = 50
XCTAssertEqual(0.5, parent.fractionCompleted, accuracy: 0.01)
parent.completedUnitCount = 0
// Test child
parent.becomeCurrent(withPendingUnitCount: 10)
let child1 = Progress(totalUnitCount: 100)
child1.completedUnitCount = 50
// half of 10% is done in parent
XCTAssertEqual(0.05, parent.fractionCompleted, accuracy: 0.01)
XCTAssertEqual(0.5, child1.fractionCompleted, accuracy: 0.01)
// Up the total amount of work
parent.totalUnitCount = 200
XCTAssertEqual(0.5 * (10.0 / 200.0) /* 0.025 */, parent.fractionCompleted, accuracy: 0.01)
XCTAssertEqual(0.5, child1.fractionCompleted, accuracy: 0.01)
// Change the total in the child, doubling total amount of work
child1.totalUnitCount = 200
XCTAssertEqual(50.0 / 200.0, child1.fractionCompleted, accuracy: 0.01)
XCTAssertEqual((50.0 / 200.0) * (10.0 / 200), parent.fractionCompleted, accuracy: 0.01)
// Change the total in the child, the other direction, halving the amount of work
child1.totalUnitCount = 100
XCTAssertEqual(50.0 / 100.0, child1.fractionCompleted, accuracy: 0.01)
XCTAssertEqual((50.0 / 100.0) * (10.0 / 200), parent.fractionCompleted, accuracy: 0.01)
}
func test_multipleChildren() {
// Verify that when multiple children are added to one group, they do the right thing
// n.b. prior to 10.11 / 9.0, this split up the pending unit among all of the children. After that, we give all of the pending unit count to the first child. This is because if you split up the pending unit count, the progress will almost certainly go backwards. API was added to give more explicit control over adding multiple children.
let progress = Progress(parent: nil)
progress.totalUnitCount = 100
// Create two children
progress.becomeCurrent(withPendingUnitCount: 100)
let child1 = Progress(totalUnitCount: 5)
let child2 = Progress(totalUnitCount: 5)
XCTAssertEqual(progress.fractionCompleted, 0, accuracy: 0.01)
child2.completedUnitCount = 5
// Child2 does not affect the parent's fraction completed (it should only be child1 that makes a difference)
XCTAssertEqual(progress.fractionCompleted, 0, accuracy: 0.01)
let _ = Progress(totalUnitCount: 5)
XCTAssertEqual(progress.fractionCompleted, 0, accuracy: 0.01)
// Update child #1
child1.completedUnitCount = 5
XCTAssertEqual(progress.fractionCompleted, 1.0, accuracy: 0.01)
}
func test_indeterminateChildrenAffectFractionCompleted() {
let parent = Progress(parent: nil)
parent.totalUnitCount = 1000
parent.becomeCurrent(withPendingUnitCount: 100)
let child1 = Progress(totalUnitCount: 10)
child1.completedUnitCount = 5
XCTAssertEqual(parent.fractionCompleted, 0.05, accuracy: 0.01)
// Child1 becomes indeterminate
child1.completedUnitCount = -1
XCTAssertEqual(parent.fractionCompleted, 0.0, accuracy: 0.01)
// Become determinate
// childProgress1's completed unit count is 100% of its total of 10 (10)
// childProgress1 is 10% of the overall unit count (100 / 1000)
// the overall count done should be 100% of 10% of 1000, or 1.0 * 0.1 * 1000 = 100
// the overall percentage done should be 100 / 1000 = 0.1
child1.completedUnitCount = 10
XCTAssertEqual(parent.fractionCompleted, 0.1, accuracy: 0.01)
// Become indeterminate again
child1.completedUnitCount = -1
XCTAssertEqual(parent.fractionCompleted, 0.0, accuracy: 0.01)
parent.resignCurrent()
}
func test_indeterminateChildrenAffectFractionCompleted2() {
let parent = Progress(parent: nil)
parent.totalUnitCount = 100
parent.becomeCurrent(withPendingUnitCount: 50)
let child1 = Progress(totalUnitCount: 2)
parent.resignCurrent()
parent.becomeCurrent(withPendingUnitCount: 50)
let child2 = Progress(totalUnitCount: 2)
parent.resignCurrent()
XCTAssertEqual(parent.fractionCompleted, 0.0, accuracy: 0.01)
child1.completedUnitCount = 1
child2.completedUnitCount = 1
// half done
XCTAssertEqual(parent.fractionCompleted, 0.5, accuracy: 0.01)
// Move a child to indeterminate
child1.completedUnitCount = -1
XCTAssertEqual(parent.fractionCompleted, 0.25, accuracy: 0.01)
// Move it back to determinate
child1.completedUnitCount = 1
XCTAssertEqual(parent.fractionCompleted, 0.5, accuracy: 0.01)
}
func test_childCompletionFinishesGroups() {
let root = Progress(totalUnitCount: 2)
let child1 = Progress(totalUnitCount: 1)
let child2 = Progress(totalUnitCount: 1)
root.addChild(child1, withPendingUnitCount: 1)
root.addChild(child2, withPendingUnitCount: 1)
child1.completedUnitCount = 1
XCTAssertEqual(root.fractionCompleted, 0.5, accuracy: 0.01)
child2.completedUnitCount = 1
XCTAssertEqual(root.fractionCompleted, 1.0, accuracy: 0.01)
XCTAssertEqual(root.completedUnitCount, 2)
}
func test_childrenAffectFractionCompleted_explicit() {
let parent = Progress(parent: nil)
parent.totalUnitCount = 100
XCTAssertEqual(parent.fractionCompleted, 0.0)
let child1 = Progress(totalUnitCount: 100)
parent.addChild(child1, withPendingUnitCount: 10)
child1.completedUnitCount = 50
// let's say some of this work is done inside the become/resign pair
// half of 10% is done
XCTAssertEqual(0.05, parent.fractionCompleted)
// ... and the rest is done outside the become/resign pair
child1.completedUnitCount = 100;
// Now the rest is done
XCTAssertEqual(0.10, parent.fractionCompleted)
// Add another child
let child2 = Progress(totalUnitCount: 100)
parent.addChild(child2, withPendingUnitCount: 90)
child2.completedUnitCount = 50
XCTAssertEqual(0.10 + 0.9 / 2.0, parent.fractionCompleted)
}
func test_childrenAffectFractionCompleted_explicit_partial() {
let parent = Progress(parent: nil)
parent.totalUnitCount = 2
XCTAssertEqual(parent.fractionCompleted, 0.0)
// Add a child, then update after adding
let child1 = Progress(totalUnitCount: 100)
parent.addChild(child1, withPendingUnitCount: 1)
child1.completedUnitCount = 50
// Half of 50% is done
XCTAssertEqual(0.25, parent.fractionCompleted)
// Add a new child, but it is already in process
let child2 = Progress(totalUnitCount: 100)
child2.completedUnitCount = 50
parent.addChild(child2, withPendingUnitCount: 1)
// half of 50% is done + half of 50% is done == 50% of overall work is done
XCTAssertEqual(0.50, parent.fractionCompleted)
}
func test_childrenAffectFractionCompleted_explicit_child_already_complete() {
// Adding children who are already partially completed should cause the parent fraction completed to be updated
let parent = Progress(parent: nil)
parent.totalUnitCount = 2
XCTAssertEqual(parent.fractionCompleted, 0.0)
// Add a child, then update after adding
let child1 = Progress(totalUnitCount: 100)
child1.completedUnitCount = 100
parent.addChild(child1, withPendingUnitCount: 1)
// all of 50% is done
XCTAssertEqual(0.5, parent.fractionCompleted)
}
func test_grandchildrenAffectFractionCompleted_explicit() {
// The parent's progress is entirely represented by the 1 grandchild
let parent = Progress(parent: nil)
parent.totalUnitCount = 100
XCTAssertEqual(parent.fractionCompleted, 0.0)
let child = Progress(totalUnitCount: 100)
parent.addChild(child, withPendingUnitCount: 100)
let grandchild = Progress(totalUnitCount: 100)
child.addChild(grandchild, withPendingUnitCount: 100)
// Now we have parentProgress <- childProgress <- grandchildProgress
XCTAssertEqual(parent.fractionCompleted, 0.0)
grandchild.completedUnitCount = 50
XCTAssertEqual(0.50, parent.fractionCompleted)
}
func test_grandchildrenAffectFractionCompleted() {
// The parent's progress is entirely represented by the 1 grandchild
let parent = Progress(parent: nil)
parent.totalUnitCount = 100
XCTAssertEqual(parent.fractionCompleted, 0.0)
parent.becomeCurrent(withPendingUnitCount: 100)
let child = Progress(totalUnitCount: 100)
parent.resignCurrent()
child.becomeCurrent(withPendingUnitCount: 100)
let grandchild = Progress(totalUnitCount: 100)
child.resignCurrent()
// Now we have parentProgress <- childProgress <- grandchildProgress
XCTAssertEqual(parent.fractionCompleted, 0.0)
grandchild.completedUnitCount = 50
XCTAssertEqual(0.50, parent.fractionCompleted)
}
func test_mixedExplicitAndImplicitChildren() {
let parent = Progress(parent: nil)
parent.totalUnitCount = 3
let child1 = Progress(totalUnitCount: 10)
let child2 = Progress(totalUnitCount: 10)
parent.addChild(child1, withPendingUnitCount: 1)
parent.addChild(child2, withPendingUnitCount: 1)
// child1 is half done. This means the parent is half of 1/3 done.
child1.completedUnitCount = 5
XCTAssertEqual(parent.fractionCompleted, (1.0 / 3.0) / 2.0, accuracy: 0.01)
// child2 is half done. This means the parent is (half of 1/3 done) + (half of 1/3 done).
child2.completedUnitCount = 5
XCTAssertEqual(parent.fractionCompleted, ((1.0 / 3.0) / 2.0) * 2.0, accuracy: 0.01)
// add an implict child
parent.becomeCurrent(withPendingUnitCount: 1)
let child3 = Progress(totalUnitCount: 10)
parent.resignCurrent()
// Total completed of parent should not change
XCTAssertEqual(parent.fractionCompleted, ((1.0 / 3.0) / 2.0) * 2.0, accuracy: 0.01)
// child3 is half done. This means the parent is (half of 1/3 done) * 3.
child3.completedUnitCount = 5
XCTAssertEqual(parent.fractionCompleted, ((1.0 / 3.0) / 2.0) * 3.0, accuracy: 0.01)
// Finish child3
child3.completedUnitCount = 10
XCTAssertTrue(child3.isFinished)
XCTAssertEqual(parent.fractionCompleted, (((1.0 / 3.0) / 2.0) * 2.0) + (1.0 / 3.0), accuracy: 0.01)
// Finish child2
child2.completedUnitCount = 10;
XCTAssertTrue(child2.isFinished)
XCTAssertEqual(parent.fractionCompleted, ((1.0 / 3.0) / 2.0) + ((1.0 / 3.0) * 2.0), accuracy: 0.01)
// Finish child1
child1.completedUnitCount = 10;
XCTAssertTrue(child1.isFinished)
XCTAssertEqual(parent.fractionCompleted, 1.0, accuracy: 0.01)
XCTAssertTrue(parent.isFinished)
XCTAssertEqual(parent.completedUnitCount, parent.totalUnitCount)
}
func test_notReturningNaN() {
let p = Progress(parent: nil)
let tests = [(-1, -1, true, 0.0),
(0, -1, true, 0.0),
(1, -1, true, 0.0),
(-1, 0, true, 0.0),
(0, 0, true, 0.0),
(1, 0, false, 1.0),
(-1, 1, true, 0.0),
(0, 1, false, 0.0),
(1, 1, false, 1.0)]
for t in tests {
p.completedUnitCount = Int64(t.0)
p.totalUnitCount = Int64(t.1)
XCTAssertEqual(t.2, p.isIndeterminate, "failed with \(t)")
XCTAssertEqual(t.3, p.fractionCompleted, "failed with \(t)")
}
}
func test_handlers() {
let parent = Progress(parent: nil)
let parentSema = DispatchSemaphore(value: 0)
let child = Progress(parent: nil)
let childSema = DispatchSemaphore(value: 0)
parent.addChild(child, withPendingUnitCount: 1)
parent.cancellationHandler = { parentSema.signal() }
child.cancellationHandler = { childSema.signal() }
parent.pausingHandler = { parentSema.signal() }
child.pausingHandler = { childSema.signal() }
parent.resumingHandler = { parentSema.signal() }
child.resumingHandler = { childSema.signal() }
parent.cancel()
XCTAssertEqual(.success, parentSema.wait(timeout: .now() + .seconds(3)))
XCTAssertEqual(.success, childSema.wait(timeout: .now() + .seconds(3)))
parent.pause()
XCTAssertEqual(.success, parentSema.wait(timeout: .now() + .seconds(3)))
XCTAssertEqual(.success, childSema.wait(timeout: .now() + .seconds(3)))
parent.resume()
XCTAssertEqual(.success, parentSema.wait(timeout: .now() + .seconds(3)))
XCTAssertEqual(.success, childSema.wait(timeout: .now() + .seconds(3)))
}
func test_alreadyCancelled() {
let parent = Progress(parent: nil)
let parentSema = DispatchSemaphore(value: 0)
let child = Progress(parent: nil)
let childSema = DispatchSemaphore(value: 0)
parent.addChild(child, withPendingUnitCount: 1)
parent.cancel()
parent.cancellationHandler = { parentSema.signal() }
child.cancellationHandler = { childSema.signal() }
XCTAssertEqual(.success, parentSema.wait(timeout: .now() + .seconds(3)))
XCTAssertEqual(.success, childSema.wait(timeout: .now() + .seconds(3)))
}
func test_userInfo() {
let p = Progress(parent: nil, userInfo: [.estimatedTimeRemainingKey : 1000])
XCTAssertEqual(p.userInfo[.estimatedTimeRemainingKey] as? Int, 1000)
p.setUserInfoObject(2000, forKey: .fileTotalCountKey)
XCTAssertEqual(p.userInfo[.fileTotalCountKey] as? Int, 2000)
p.setUserInfoObject(nil, forKey: .fileTotalCountKey)
XCTAssertEqual(p.userInfo[.fileTotalCountKey] as? Int, nil)
}
}
|