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
|
//===------------------------ TwoDMapTests.swift --------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import XCTest
import SwiftDriver
class TwoDMapTests: XCTestCase {
func testTwoDMap() {
var indices = [Int]()
var m = TwoDMap<Int, String, Double>()
m.verify { _, _, _ in XCTFail() }
XCTAssertEqual(nil, m.updateValue(3.4, forKey: (1, "a")))
m.verify { k, v, i in
XCTAssert(k.0 == 1 && k.1 == "a" && v == 3.4)
indices.append(i)
}
XCTAssertEqual(indices, [0, 1])
indices.removeAll()
XCTAssertEqual(3.4, m.updateValue(11, forKey: (1, "a")))
m.verify { _, _, _ in }
XCTAssertEqual(nil, m.updateValue(21, forKey: (2, "a")))
m.verify { _, _, _ in }
XCTAssertEqual(nil, m.updateValue(12, forKey: (1, "b")))
m.verify { _, _, _ in }
XCTAssertEqual(nil, m.updateValue(22, forKey: (2, "b")))
m.verify { _, _, _ in }
var n = 0
m.verify { k, v, i in
switch (k.0, k.1, v, i) {
case
(1, "a", 11, 0),
(1, "a", 11, 1),
(2, "a", 21, 0),
(2, "a", 21, 1),
(1, "b", 12, 0),
(1, "b", 12, 1),
(2, "b", 22, 0),
(2, "b", 22, 1):
n += 1
default: XCTFail()
}
}
XCTAssertEqual(n, 8)
XCTAssertEqual(21, m.removeValue(forKey: (2, "a")))
n = 0
m.verify { k, v, i in
switch (k.0, k.1, v, i) {
case
(1, "a", 11, 0),
(1, "a", 11, 1),
(1, "b", 12, 0),
(1, "b", 12, 1),
(2, "b", 22, 0),
(2, "b", 22, 1):
n += 1
default: XCTFail()
}
}
XCTAssertEqual(n, 6)
do {
let a = m[1]
XCTAssertEqual(a, ["a": 11, "b": 12])
let b = m[2]
XCTAssertEqual(b, ["b": 22])
let c = m["a"]
XCTAssertEqual(c, [1: 11])
let d = m["b"]
XCTAssertEqual(d, [1: 12, 2: 22])
let e = m[3]
XCTAssertEqual(e, nil)
let f = m["c"]
XCTAssertEqual(f, nil)
}
do {
let a = m[(1, "a")]
let b = m[(1, "b")]
let c = m[(2, "a")]
let d = m[(2, "b")]
let e = m[(3, "b")]
let f = m[(3, "c")]
XCTAssertEqual([a, b, c, d, e, f], [11, 12, nil, 22, nil, nil])
}
}
}
|