@@ -10,16 +10,21 @@ import Swift
10
10
import Darwin
11
11
12
12
// Interpret the command line arguments.
13
- var arg = Process . arguments [ 1 ]
13
+ let arg = Process . arguments [ 1 ]
14
14
15
15
if arg == " env " {
16
16
setlocale ( LC_ALL, " " )
17
17
} else {
18
18
setlocale ( LC_ALL, arg)
19
19
}
20
20
21
- func stdlibTypesHaveDescription( ) {
22
- func hasDescription( _: CustomStringConvertible ) { }
21
+ import StdlibUnittest
22
+ let PrintTests = TestSuite ( " Print " )
23
+
24
+ PrintTests . test ( " stdlib types have description " ) {
25
+ func hasDescription( any: Any ) {
26
+ expectTrue ( any is CustomStringConvertible )
27
+ }
23
28
24
29
hasDescription ( Int ( 42 ) )
25
30
hasDescription ( UInt ( 42 ) )
@@ -109,165 +114,159 @@ func assertEquals(
109
114
}
110
115
}
111
116
112
- func test_StdlibTypesPrinted( ) {
113
- printedIs ( Float ( 1.0 ) , " 1.0 " )
114
- printedIs ( Float ( - 1.0 ) , " -1.0 " )
115
- printedIs ( Double ( 1.0 ) , " 1.0 " )
116
- printedIs ( Double ( - 1.0 ) , " -1.0 " )
117
-
118
- printedIs ( CChar ( 42 ) , " 42 " )
119
- printedIs ( CUnsignedChar ( 42 ) , " 42 " )
120
- printedIs ( CUnsignedShort ( 42 ) , " 42 " )
121
- printedIs ( CUnsignedInt ( 42 ) , " 42 " )
122
- printedIs ( CUnsignedLong ( 42 ) , " 42 " )
123
- printedIs ( CUnsignedLongLong ( 42 ) , " 42 " )
124
- printedIs ( CSignedChar ( 42 ) , " 42 " )
125
- printedIs ( CShort ( 42 ) , " 42 " )
126
- printedIs ( CInt ( 42 ) , " 42 " )
127
- printedIs ( CLong ( 42 ) , " 42 " )
128
- printedIs ( CLongLong ( 42 ) , " 42 " )
129
- printedIs ( CFloat ( 1.0 ) , " 1.0 " )
130
- printedIs ( CFloat ( - 1.0 ) , " -1.0 " )
131
- printedIs ( CDouble ( 1.0 ) , " 1.0 " )
132
- printedIs ( CDouble ( - 1.0 ) , " -1.0 " )
133
-
134
- printedIs ( CWideChar ( 42 ) , " * " )
135
- printedIs ( CChar16 ( 42 ) , " 42 " )
136
- printedIs ( CChar32 ( 42 ) , " * " )
137
- printedIs ( CBool ( true ) , " true " )
138
- printedIs ( CBool ( false ) , " false " )
139
-
140
- var s : String = " abc "
141
- printedIs ( s, " abc " )
142
- debugPrintedIs ( s, " \" abc \" " )
143
- s = " \\ \' \" \0 \n \r \t \u{05} "
144
- debugPrintedIs ( s, " \" \\ \\ \\ \' \\ \" \\ 0 \\ n \\ r \\ t \\ u{05} \" " )
117
+ PrintTests . test ( " test stdlib types printed " ) {
118
+ expectPrinted ( " 1.0 " , Float ( 1.0 ) )
119
+ expectPrinted ( " -1.0 " , Float ( - 1.0 ) )
120
+ expectPrinted ( " 1.0 " , Double ( 1.0 ) )
121
+ expectPrinted ( " -1.0 " , Double ( - 1.0 ) )
122
+
123
+ expectPrinted ( " 42 " , CChar ( 42 ) )
124
+ expectPrinted ( " 42 " , CUnsignedChar ( 42 ) )
125
+ expectPrinted ( " 42 " , CUnsignedShort ( 42 ) )
126
+ expectPrinted ( " 42 " , CUnsignedInt ( 42 ) )
127
+ expectPrinted ( " 42 " , CUnsignedLong ( 42 ) )
128
+ expectPrinted ( " 42 " , CUnsignedLongLong ( 42 ) )
129
+ expectPrinted ( " 42 " , CSignedChar ( 42 ) )
130
+ expectPrinted ( " 42 " , CShort ( 42 ) )
131
+ expectPrinted ( " 42 " , CInt ( 42 ) )
132
+ expectPrinted ( " 42 " , CLong ( 42 ) )
133
+ expectPrinted ( " 42 " , CLongLong ( 42 ) )
134
+ expectPrinted ( " 1.0 " , CFloat ( 1.0 ) )
135
+ expectPrinted ( " -1.0 " , CFloat ( - 1.0 ) )
136
+ expectPrinted ( " 1.0 " , CDouble ( 1.0 ) )
137
+ expectPrinted ( " -1.0 " , CDouble ( - 1.0 ) )
138
+
139
+ expectPrinted ( " * " , CWideChar ( 42 ) )
140
+ expectPrinted ( " 42 " , CChar16 ( 42 ) )
141
+ expectPrinted ( " * " , CChar32 ( 42 ) )
142
+ expectPrinted ( " true " , CBool ( true ) )
143
+ expectPrinted ( " false " , CBool ( false ) )
144
+
145
+
146
+ let s0 : String = " abc "
147
+ expectPrinted ( " abc " , s0)
148
+ expectDebugPrinted ( " \" abc \" " , s0)
149
+
150
+ let s1 : String = " \\ \' \" \0 \n \r \t \u{05} "
151
+ expectDebugPrinted ( " \" \\ \\ \\ \' \\ \" \\ 0 \\ n \\ r \\ t \\ u{05} \" " , s1)
145
152
146
153
let ch : Character = " a "
147
- printedIs ( ch, " a " )
148
- debugPrintedIs ( ch, " \" a \" " )
149
-
150
- var us : UnicodeScalar = " a "
151
- printedIs ( us, " a " )
152
- debugPrintedIs ( us, " \" a \" " )
153
- us = " \\ "
154
- printedIs ( us, " \\ " )
155
- assertEquals ( " \" \\ \\ \" " , us. description)
156
- debugPrintedIs ( us, " \" \\ \\ \" " )
157
- us = " あ "
158
- printedIs ( us, " あ " )
159
- assertEquals ( " \" あ \" " , us. description)
160
- debugPrintedIs ( us, " \" \\ u{3042} \" " )
154
+ expectPrinted ( " a " , ch)
155
+ expectDebugPrinted ( " \" a \" " , ch)
156
+
157
+ let us0 : UnicodeScalar = " a "
158
+ expectPrinted ( " a " , us0)
159
+ expectDebugPrinted ( " \" a \" " , us0)
160
+
161
+ let us1 : UnicodeScalar = " \\ "
162
+ expectPrinted ( " \\ " , us1)
163
+ expectEqual ( " \" \\ \\ \" " , us1. description)
164
+ expectDebugPrinted ( " \" \\ \\ \" " , us1)
165
+
166
+ let us2 : UnicodeScalar = " あ "
167
+ expectPrinted ( " あ " , us2)
168
+ expectEqual ( " \" あ \" " , us2. description)
169
+ expectDebugPrinted ( " \" \\ u{3042} \" " , us2)
170
+ }
161
171
162
- do {
163
- var implicitlyUnwrappedString : String ! = nil
164
- printedIs ( implicitlyUnwrappedString, " nil " )
165
- implicitlyUnwrappedString = " meow "
166
- printedIs ( implicitlyUnwrappedString, " meow " )
167
- }
168
- do {
169
- var optionalString : String ? = nil
170
- printedIs ( optionalString, " nil " )
171
- optionalString = " meow "
172
- printedIs ( optionalString, " Optional( \" meow \" ) " )
173
- }
174
- do {
175
- struct Wrapper : CustomStringConvertible {
176
- var x : CustomStringConvertible ? = nil
172
+ PrintTests . test ( " optional strings " ) {
173
+ expectEqual ( " nil " , String!( ) )
174
+ expectEqual ( " meow " , String!( " meow " ) )
175
+ expectEqual ( " nil " , String ? ( ) )
176
+ expectEqual ( " Optional( \" meow \" ) " , String ? ( " meow " ) )
177
+ }
177
178
178
- var description : String {
179
- return " Wrapper( " + x. debugDescription + " ) "
180
- }
179
+ PrintTests . test ( " custom string convertible structs " ) {
180
+ struct Wrapper : CustomStringConvertible {
181
+ var x : CustomStringConvertible ? = nil
182
+
183
+ var description : String {
184
+ return " Wrapper( \( x. debugDescription) ) "
181
185
}
182
- printedIs ( Wrapper ( ) , " Wrapper(nil) " )
183
- printedIs ( Wrapper ( x: Wrapper ( ) ) , " Wrapper(Optional(Wrapper(nil))) " )
184
- printedIs ( Wrapper ( x: Wrapper ( x: Wrapper ( ) ) ) ,
185
- " Wrapper(Optional(Wrapper(Optional(Wrapper(nil))))) " )
186
186
}
187
-
188
- print ( " test_StdlibTypesPrinted done " )
187
+ expectPrinted ( " Wrapper(nil) " , Wrapper ( ) )
188
+ expectPrinted ( " Wrapper(Optional(Wrapper(nil))) " ,
189
+ Wrapper ( x: Wrapper ( ) ) )
190
+ expectPrinted ( " Wrapper(Optional(Wrapper(Optional(Wrapper(nil))))) " ,
191
+ Wrapper ( x: Wrapper ( x: Wrapper ( ) ) ) )
189
192
}
190
- test_StdlibTypesPrinted ( )
191
- // CHECK: test_StdlibTypesPrinted done
192
193
193
- func test_IntegerPrinting ( ) {
194
+ PrintTests . test ( " integer printing " ) {
194
195
if ( UInt64 ( Int . max) > 0x1_0000_0000 as UInt64 ) {
195
- printedIs ( Int . min , " -9223372036854775808 " )
196
- printedIs ( Int . max , " 9223372036854775807 " )
196
+ expectPrinted ( " -9223372036854775808 " , Int . min )
197
+ expectPrinted ( " 9223372036854775807 " , Int . max )
197
198
} else {
198
- printedIs ( Int . min , " -2147483648 " )
199
- printedIs ( Int . max , " 2147483647 " )
199
+ expectPrinted ( " -2147483648 " , Int . min )
200
+ expectPrinted ( " 2147483647 " , Int . max )
200
201
}
201
- printedIs ( Int ( 0 ) , " 0 " )
202
- printedIs ( Int ( 42 ) , " 42 " )
203
- printedIs ( Int ( - 42 ) , " -42 " )
204
-
202
+
203
+ expectPrinted ( " 0 " , Int ( 0 ) )
204
+ expectPrinted ( " 42 " , Int ( 42 ) )
205
+ expectPrinted ( " -42 " , Int ( - 42 ) )
206
+
205
207
if ( UInt64 ( UInt . max) > 0x1_0000_0000 as UInt64 ) {
206
- printedIs ( UInt . max , " 18446744073709551615 " )
208
+ expectPrinted ( " 18446744073709551615 " , UInt . max )
207
209
} else {
208
- printedIs ( UInt . max , " 4294967295 " )
210
+ expectPrinted ( " 4294967295 " , UInt . max )
209
211
}
210
- printedIs ( UInt . min, " 0 " )
211
- printedIs ( UInt ( 0 ) , " 0 " )
212
- printedIs ( UInt ( 42 ) , " 42 " )
213
-
214
- printedIs ( Int8 . min, " -128 " )
215
- printedIs ( Int8 . max, " 127 " )
216
- printedIs ( Int8 ( 0 ) , " 0 " )
217
- printedIs ( Int8 ( 42 ) , " 42 " )
218
- printedIs ( Int8 ( - 42 ) , " -42 " )
219
-
220
- printedIs ( UInt8 . min, " 0 " )
221
- printedIs ( UInt8 . max, " 255 " )
222
- printedIs ( UInt8 ( 0 ) , " 0 " )
223
- printedIs ( UInt8 ( 42 ) , " 42 " )
224
-
225
- printedIs ( Int16 . min, " -32768 " )
226
- printedIs ( Int16 . max, " 32767 " )
227
- printedIs ( Int16 ( 0 ) , " 0 " )
228
- printedIs ( Int16 ( 42 ) , " 42 " )
229
- printedIs ( Int16 ( - 42 ) , " -42 " )
230
-
231
- printedIs ( UInt16 . min, " 0 " )
232
- printedIs ( UInt16 . max, " 65535 " )
233
- printedIs ( UInt16 ( 0 ) , " 0 " )
234
- printedIs ( UInt16 ( 42 ) , " 42 " )
235
-
236
- printedIs ( Int32 . min, " -2147483648 " )
237
- printedIs ( Int32 . max, " 2147483647 " )
238
- printedIs ( Int32 ( 0 ) , " 0 " )
239
- printedIs ( Int32 ( 42 ) , " 42 " )
240
- printedIs ( Int32 ( - 42 ) , " -42 " )
241
-
242
- printedIs ( UInt32 . min, " 0 " )
243
- printedIs ( UInt32 . max, " 4294967295 " )
244
- printedIs ( UInt32 ( 0 ) , " 0 " )
245
- printedIs ( UInt32 ( 42 ) , " 42 " )
246
-
247
- printedIs ( Int64 . min, " -9223372036854775808 " )
248
- printedIs ( Int64 . max, " 9223372036854775807 " )
249
- printedIs ( Int64 ( 0 ) , " 0 " )
250
- printedIs ( Int64 ( 42 ) , " 42 " )
251
- printedIs ( Int64 ( - 42 ) , " -42 " )
252
-
253
- printedIs ( UInt64 . min, " 0 " )
254
- printedIs ( UInt64 . max, " 18446744073709551615 " )
255
- printedIs ( UInt64 ( 0 ) , " 0 " )
256
- printedIs ( UInt64 ( 42 ) , " 42 " )
257
-
258
- printedIs ( Int8 ( - 42 ) , " -42 " )
259
- printedIs ( Int16 ( - 42 ) , " -42 " )
260
- printedIs ( Int32 ( - 42 ) , " -42 " )
261
- printedIs ( Int64 ( - 42 ) , " -42 " )
262
- printedIs ( UInt8 ( 42 ) , " 42 " )
263
- printedIs ( UInt16 ( 42 ) , " 42 " )
264
- printedIs ( UInt32 ( 42 ) , " 42 " )
265
- printedIs ( UInt64 ( 42 ) , " 42 " )
266
-
267
- print ( " test_IntegerPrinting done " )
212
+
213
+ expectPrinted ( " 0 " , UInt . min)
214
+ expectPrinted ( " 0 " , UInt ( 0 ) )
215
+ expectPrinted ( " 42 " , UInt ( 42 ) )
216
+
217
+ expectPrinted ( " -128 " , Int8 . min)
218
+ expectPrinted ( " 127 " , Int8 . max)
219
+ expectPrinted ( " 0 " , Int8 ( 0 ) )
220
+ expectPrinted ( " 42 " , Int8 ( 42 ) )
221
+ expectPrinted ( " -42 " , Int8 ( - 42 ) )
222
+
223
+ expectPrinted ( " 0 " , UInt8 . min)
224
+ expectPrinted ( " 255 " , UInt8 . max)
225
+ expectPrinted ( " 0 " , UInt8 ( 0 ) )
226
+ expectPrinted ( " 42 " , UInt8 ( 42 ) )
227
+
228
+ expectPrinted ( " -32768 " , Int16 . min)
229
+ expectPrinted ( " 32767 " , Int16 . max)
230
+ expectPrinted ( " 0 " , Int16 ( 0 ) )
231
+ expectPrinted ( " 42 " , Int16 ( 42 ) )
232
+ expectPrinted ( " -42 " , Int16 ( - 42 ) )
233
+
234
+ expectPrinted ( " 0 " , UInt16 . min)
235
+ expectPrinted ( " 65535 " , UInt16 . max)
236
+ expectPrinted ( " 0 " , UInt16 ( 0 ) )
237
+ expectPrinted ( " 42 " , UInt16 ( 42 ) )
238
+
239
+ expectPrinted ( " -2147483648 " , Int32 . min)
240
+ expectPrinted ( " 2147483647 " , Int32 . max)
241
+ expectPrinted ( " 0 " , Int32 ( 0 ) )
242
+ expectPrinted ( " 42 " , Int32 ( 42 ) )
243
+ expectPrinted ( " -42 " , Int32 ( - 42 ) )
244
+
245
+ expectPrinted ( " 0 " , UInt32 . min)
246
+ expectPrinted ( " 4294967295 " , UInt32 . max)
247
+ expectPrinted ( " 0 " , UInt32 ( 0 ) )
248
+ expectPrinted ( " 42 " , UInt32 ( 42 ) )
249
+
250
+ expectPrinted ( " -9223372036854775808 " , Int64 . min)
251
+ expectPrinted ( " 9223372036854775807 " , Int64 . max)
252
+ expectPrinted ( " 0 " , Int64 ( 0 ) )
253
+ expectPrinted ( " 42 " , Int64 ( 42 ) )
254
+ expectPrinted ( " -42 " , Int64 ( - 42 ) )
255
+
256
+ expectPrinted ( " 0 " , UInt64 . min)
257
+ expectPrinted ( " 18446744073709551615 " , UInt64 . max)
258
+ expectPrinted ( " 0 " , UInt64 ( 0 ) )
259
+ expectPrinted ( " 42 " , UInt64 ( 42 ) )
260
+
261
+ expectPrinted ( " -42 " , Int8 ( - 42 ) )
262
+ expectPrinted ( " -42 " , Int16 ( - 42 ) )
263
+ expectPrinted ( " -42 " , Int32 ( - 42 ) )
264
+ expectPrinted ( " -42 " , Int64 ( - 42 ) )
265
+ expectPrinted ( " 42 " , UInt8 ( 42 ) )
266
+ expectPrinted ( " 42 " , UInt16 ( 42 ) )
267
+ expectPrinted ( " 42 " , UInt32 ( 42 ) )
268
+ expectPrinted ( " 42 " , UInt64 ( 42 ) )
268
269
}
269
- test_IntegerPrinting ( )
270
- // CHECK: test_IntegerPrinting done
271
270
272
271
func test_FloatingPointPrinting( ) {
273
272
func asFloat32( f: Float32 ) -> Float32 { return f }
0 commit comments