-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeakKeyMapTableTests.swift
74 lines (57 loc) · 1.91 KB
/
WeakKeyMapTableTests.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
//
// WeakKeyMapTableTests.swift
// CrystalTests
//
// Created by yunhao on 2020/8/13.
// Copyright © 2020 yunhao. All rights reserved.
//
import XCTest
@testable import Crystal
class WeakKeyMapTableTests: XCTestCase {
private var mapTable: WeakKeyMapTable<SimpleKey, SimpleObject>!
override func setUp() {
super.setUp()
mapTable = WeakKeyMapTable<SimpleKey, SimpleObject>()
}
func testMapTableSubscriptSetter() {
let total = 10
var strongRetained: [SimpleKey] = []
for _ in 0..<total {
let key = SimpleKey()
strongRetained.append(key)
mapTable[key] = SimpleObject()
}
// All element is retained in the map table.
XCTAssertEqual(mapTable.storage.count, total)
}
func testMapTableSubscriptGetter() {
let total = 10
var strongDict: [SimpleKey: SimpleObject] = [:]
for _ in 0..<total {
let key = SimpleKey()
let object = SimpleObject()
strongDict[key] = object
mapTable[key] = object
}
// Get all elements successfully.
for (key, object) in strongDict {
XCTAssertEqual(mapTable[key], object)
}
}
func testMapTableWeakRetain() {
let total = 10
var strongRetained: [SimpleKey] = []
for _ in 0..<total {
let key = SimpleKey()
strongRetained.append(key)
mapTable[key] = SimpleObject()
}
// All element is retained in the map table.
XCTAssertEqual(mapTable.storage.count, total)
strongRetained.removeLast(total - 1)
// Will remove outdated elements automatically.
XCTAssertEqual(mapTable.storage.count, 1)
}
}
fileprivate class SimpleKey: NSObject {}
fileprivate class SimpleObject: NSObject {}