File: use-std-map.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (152 lines) | stat: -rw-r--r-- 3,487 bytes parent folder | download
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
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=swift-6)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)

// Also test this with a bridging header instead of the StdMap module.
// RUN: %empty-directory(%t2)
// RUN: cp %S/Inputs/std-map.h %t2/std-map-bridging-header.h
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-map-bridging-header.h -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-map-bridging-header.h -cxx-interoperability-mode=swift-6)
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-map-bridging-header.h -cxx-interoperability-mode=upcoming-swift)

// REQUIRES: executable_test
//
// REQUIRES: OS=macosx || OS=linux-gnu

import StdlibUnittest
#if !BRIDGING_HEADER
import StdMap
#endif
import CxxStdlib
import Cxx

var StdMapTestSuite = TestSuite("StdMap")

StdMapTestSuite.test("init") {
  let m = Map()
  expectEqual(m.size(), 0)
  expectTrue(m.empty())
}

StdMapTestSuite.test("Map.subscript") {
  // This relies on the `std::map` conformance to `CxxDictionary` protocol.
  var m = initMap()
  let at1 = m[1]
  expectNotNil(at1)
  expectEqual(at1, 3)
  expectEqual(m[2], 2)
  expectEqual(m[3], 3)
  expectNil(m[-1])
  expectNil(m[5])
  
  m[1] = 111
  expectEqual(m[1], 111)

  m[5] = 555
  expectEqual(m[5], 555)

  m[5] = nil
  expectNil(m[5])
  expectNil(m[5])
}

StdMapTestSuite.test("MapStrings.subscript") {
  var m = MapStrings()
  expectNil(m[std.string()])
  expectNil(m[std.string()])
  m[std.string()] = std.string()
  expectNotNil(m[std.string()])

  m[std.string("abc")] = std.string("qwe")
  expectEqual(m[std.string("abc")], std.string("qwe"))
}

StdMapTestSuite.test("NestedMap.subscript") {
  var m = NestedMap()
  expectNil(m[0])
  expectNil(m[0])
  m[1] = Map()
  expectNotNil(m[1])

  expectNil(m[1]![0])
  m[1]![0] = 123
  expectEqual(m[1]![0], 123)

  m[1]![0] = nil
  expectNil(m[1]![0])

  m[1] = nil
  expectNil(m[1])
}

StdMapTestSuite.test("UnorderedMap.subscript") {
  // This relies on the `std::unordered_map` conformance to `CxxDictionary` protocol.
  var m = initUnorderedMap()
  expectEqual(m[1], 3)
  expectEqual(m[2], 2)
  expectEqual(m[3], 3)
  expectNil(m[-1])
  expectNil(m[5])

  m[1] = 777
  expectEqual(m[1], 777)

  m[-1] = 228
  expectEqual(m[-1], 228)

  m[-1] = nil
  expectNil(m[-1])
  expectNil(m[-1])
}

StdMapTestSuite.test("Map.filter") {
  var m = initMap()
  var n = initEmptyMap()

  expectNotNil(m[1])
  expectEqual(n.size(), 0)

  m = m.filter { k, v in k != 1 }
  n = n.filter { k, v in false }
 
  expectNil(m[1])
  expectEqual(m[2], 2)
  expectEqual(m[3], 3)
  expectTrue(n.empty())
}

StdMapTestSuite.test("UnorderedMap.filter") {
  var m = initUnorderedMap()
  var n = initEmptyUnorderedMap()

  expectNotNil(m[1])
  expectEqual(n.size(), 0)

  m = m.filter { k, v in k != 1 }
  n = n.filter { k, v in false }

  expectNil(m[1])
  expectEqual(m[2], 2)
  expectEqual(m[3], 3)
  expectTrue(n.empty())
}

StdMapTestSuite.test("Map.erase") {
  var m = initMap()
  expectNotNil(m[1])
  m.erase(1)
  expectNil(m[1])
  m.erase(1)
  expectNil(m[1])
}

StdMapTestSuite.test("UnorderedMap.erase") {
  var m = initUnorderedMap()
  expectNotNil(m[2])
  m.erase(2)
  expectNil(m[2])
  m.erase(2)
  expectNil(m[2])
}

runAllTests()