-
Notifications
You must be signed in to change notification settings - Fork 233
/
swiftzTests.swift
401 lines (342 loc) · 11.5 KB
/
swiftzTests.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
// swiftzTests.swift
// swiftzTests
//
// Created by Maxwell Swadling on 3/06/2014.
// Copyright (c) 2014 Maxwell Swadling. All rights reserved.
//
import XCTest
import swiftz
// A user example
// an example of why we need SYB, Generics or macros
class User: JSONDecode {
typealias J = User
let name: String
let age: Int
let tweets: Array<String>
let attrs: Dictionary<String, String>
init(_ n: String, _ a: Int, _ t: Array<String>, _ r: Dictionary<String, String>) {
name = n
age = a
tweets = t
attrs = r
}
class func create(x: String) -> Int -> Array<String> -> Dictionary<String, String> -> User {
return { (y: Int) in { (z: Array<String>) in { User(x, y, z, $0) } } }
}
class func fromJSON(x: JSValue) -> User? {
var n: String?
var a: Int?
var t: Array<String>?
var r: Dictionary<String, String>?
switch x {
case let .JSObject(d):
n = d["name"] >>= JString.fromJSON
a = d["age"] >>= JInt.fromJSON
t = d["tweets"] >>= JArray<String, JString>.fromJSON
r = d["attrs"] >>= JDictionary<String, JString>.fromJSON
return (User.create <^> n <*> a <*> t <*> r)
default:
return .None
}
}
// lens
class func luserName() -> Lens<User, String>.LensConst {
return { (fn: (String -> Const<String, User>)) -> User -> Const<String, User> in
return { (a: User) -> Const<String, User> in
return fn(a.name)
}
}
}
class func luserName() -> Lens<User, String>.LensId {
return { (fn: (String -> Id<String>)) -> User -> Id<User> in
return { (a: User) -> Id<User> in
return Id<User>(User(fn(a.name).runId(), a.age, a.tweets, a.attrs))
}
}
}
}
func ==(lhs: User, rhs: User) -> Bool {
return lhs.name == rhs.name && lhs.age == rhs.age && lhs.tweets == rhs.tweets && lhs.attrs == rhs.attrs
}
// lens ex
class Party {
let host: User
init(h: User) {
host = h
}
// lens
class func lpartyHost() -> Lens<Party, User>.LensConst {
return { (fn: (User -> Const<User, Party>)) -> Party -> Const<User, Party> in
return { (a: Party) -> Const<User, Party> in
return fn(a.host)
}
}
}
class func lpartyHost() -> Lens<Party, User>.LensId {
return { (fn: (User -> Id<User>)) -> Party -> Id<Party> in
return { (a: Party) -> Id<Party> in
let x = fn(a.host).runId()
return Id<Party>(Party(h: fn(a.host).runId()))
}
}
}
}
// shape example for SYB
enum Shape : Dataable {
case Boat
case Plane(wingspan: Int)
static func typeRep() -> Any.Type {
return reflect(self).valueType
}
static func fromRep(r: Data) -> Shape? {
switch (r.con, r.vals) {
case let (0, xs):
return Boat
case let (1, xs):
let x1 = ind(xs, 0)
let x2 = x1 >>= { $1 as? Int }
return { Plane(wingspan: $0) } <^> x2
default:
return .None
}
}
func toRep() -> Data {
switch self {
case .Boat: return Data(con: 0, vals: [])
case let .Plane(w): return Data(con: 1, vals: [("wingspan", w)])
}
}
}
func ==(lhs: Shape, rhs: Shape) -> Bool {
switch lhs {
case .Boat: switch rhs {
case .Boat: return true
case let .Plane(q): return false
}
case let .Plane(w): switch rhs {
case let .Plane(q): return w == q
case let .Boat: return false
}
}
}
class swiftzTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testConcurrentFuture() {
var e: NSError?
let x: Future<Int> = Future(exec: gcdExecutionContext, {
usleep(1)
return 4
})
XCTAssert(x.result() == x.result(), "future")
XCTAssert(x.map({ $0.description }).result() == "4", "future map")
XCTAssert(x.flatMap({ (x: Int) -> Future<Int> in
return Future(exec: gcdExecutionContext, { usleep(1); return x + 1 })
}).result() == 5, "future flatMap")
// let x: Future<Int> = Future(exec: gcdExecutionContext, {
// return NSString.stringWithContentsOfURL(NSURL.URLWithString("http://github.com"), encoding: 0, error: nil)
// })
// let x1 = (x.result().lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
// let x2 = (x.result().lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
// XCTAssert(x1 == x2)
}
func testConcurrentChan() {
var chan: Chan<Int> = Chan()
let ft = Future<Int>(exec: gcdExecutionContext, { usleep(1); chan.write(2); return 2 })
XCTAssert(chan.read() == ft.result(), "simple read chan")
}
func testConcurrentMVar() {
var mvar: MVar<String> = MVar()
let ft = Future<Void>(exec: gcdExecutionContext, { mvar.put("hello"); mvar.put("max") })
XCTAssert(mvar.isEmpty(), "mvar is full")
XCTAssert(mvar.take() == "hello", "mvar read")
XCTAssert(mvar.take() == "max", "mvar read")
XCTAssert(mvar.isEmpty(), "mvar empty")
}
func testControlBase() {
let x = 1
let y = 2
XCTAssert(identity(x) == x, "identity")
XCTAssert(curry({ (ab: (Int, Int)) -> Int in switch ab {
case let (l, r): return (l + r)
}}, x, y) == 3, "curry")
XCTAssert(uncurry({
(a: Int) -> Int -> Int in
return ({(b: Int) -> Int in
return (a + b)
})
}, (x, y)) == 3, "uncurry")
XCTAssert((x |> {(a: Int) -> String in return a.description}) == "1", "thrush")
// XCTAssert((x <| {(a: Int) -> String in return a.description}) == 1, "thrush")
let xs = [1, 2, 3]
func fs(x: Int) -> Array<Int> {
return [x, x+1, x+2]
}
let rs = xs >>= fs
XCTAssert(rs == [1, 2, 3, 2, 3, 4, 3, 4, 5])
}
func testControlLens() {
let party = Party(h: User("max", 1, [], Dictionary()))
// 10 points to who ever works out how to get it to work with function comp.
// Party -> Host -> Name
let hostname: Party -> String = { Lens.get(Party.lpartyHost(), $0) |> { Lens.get(User.luserName(), $0) } }
let updatedParty: Party = Lens.modify(Party.lpartyHost(), { (Lens.set(User.luserName(), "Max", $0)) }, party)
XCTAssert(hostname(party) == "max")
XCTAssert(hostname(updatedParty) == "Max")
}
func testThrush() {
let x = 1 |> ({$0.advancedBy($0)}) |> ({$0.advancedBy($0)}) |> ({$0 * $0})
XCTAssertTrue(x == 16, "Should equal 16")
}
func testDataEither() {
func divTwoEvenly(x: Int) -> Either<String, Int> {
if x % 2 == 0 {
return .Left({ "\(x) was div by 2" })
} else {
return .Right({ x / 2 })
}
}
let start = 17
let first: Either<String, Int> = divTwoEvenly(start)
let prettyPrinted: Either<String, String> = { $0.description } <^> first
let snd = first >>= divTwoEvenly
XCTAssert(prettyPrinted == .Right({ "8" }))
XCTAssert(snd == .Left({ "8 was div by 2" }))
}
func testDataJSON() {
let js: NSData = "[1,\"foo\"]".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let lhs: JSValue = JSValue.decode(js)
let rhs: JSValue = .JSArray([.JSNumber(1), .JSString("foo")])
XCTAssert(lhs == rhs)
XCTAssert(rhs.encode() == js)
// user example
let userjs: NSData = "{\"name\": \"max\", \"age\": 10, \"tweets\": [\"hello\"], \"attrs\": {\"one\": \"1\"}}"
.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let user: User? = JSValue.decode(userjs) >>= User.fromJSON
XCTAssert(user! == User("max", 10, ["hello"], ["one": "1"]))
// not a user, missing age
let notuserjs: NSData = "{\"name\": \"max\", \"tweets\": [\"hello\"], \"attrs\": {\"one\": \"1\"}}".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let notUser: User? = JSValue.decode(notuserjs) >>= User.fromJSON
if notUser {
XCTFail("expected none")
}
}
func testDataSemigroup() {
let xs = [1, 2, 0, 3, 4]
XCTAssert(sconcat(Min(), 2, xs) == 0, "sconcat works")
}
func testDataMonoid() {
let xs: Array<Int8> = [1, 2, 0, 3, 4]
XCTAssert(mconcat(Sum <Int8, NInt8>(i: { return nint8 }), xs) == 10, "monoid sum works")
XCTAssert(mconcat(Product<Int8, NInt8>(i: { return nint8 }), xs) == 0, "monoid product works")
}
// func testDataList() {
// let ls: List<Int> = .Nil()
// println(ls)
//
// XCTAssert(true, "Pass")
// }
func testDataOptionalExt() {
let x = Optional.Some(4)
func f(i: Int) -> Optional<Int> {
if i % 2 == 0 {
return .Some(i / 2)
} else {
return .None
}
}
// rdar://17149404
// XCTAssert(x.flatMap(f) == .Some(2), "optional flatMap")
// maybe(...)
// XCTAssert(Optional.None.getOrElse(1) == 1, "optional getOrElse")
}
func testMaybeOptionalExt() {
let x = Optional.Some(4)
let y = Optional<Int>.None
//XCTAssert(x.maybe(0, { x in x + 1}) == 4, "maybe for Some works")
//XCTAssert(y.maybe(0, { x in x + 1}) == 0, "maybe for None works")
}
func testDataArrayExt() {
// segfaults. rdar://17148872
// let xsO: Array<Optional<Int>> = [Optional.Some(1), .Some(2), .None]
// let exO: Array<Int> = mapFlatten(xsO)
// XCTAssert(exO == [1, 2], "mapflatten option")
//
// let exJ = join([[1, 2], [3, 4]])
// XCTAssert(exJ == [1, 2, 3, 4], "mapflatten option")
}
func testGenericsSYB() {
let b = Shape.Plane(wingspan: 2)
let b2 = Shape.fromRep(b.toRep()) // identity?
XCTAssert(b == b2!)
// not sure why you would use SYB at the moment...
// without some kind of extendable generic dispatch, it isn't very useful.
func gJSON(d: Data) -> JSValue {
var r = Dictionary<String, JSValue>()
for (n, vs) in d.vals {
switch vs {
case let x as Int: r[n] = JSValue.JSNumber(Double(x))
case let x as String: r[n] = JSValue.JSString(x)
case let x as Bool: r[n] = JSValue.JSBool(x)
case let x as Double: r[n] = JSValue.JSNumber(x)
default: r[n] = JSValue.JSNull()
}
}
return .JSObject(r)
}
XCTAssert(gJSON(b.toRep()) == .JSObject(["wingspan" : .JSNumber(2)]))
}
func testTestingSwiftCheck() {
func prop_reverseLen(xs: Array<Int>) {
XCTAssert(xs.reverse().count == xs.count)
}
func prop_reverseReverse(xs: Array<Int>) {
XCTAssert(xs.reverse().reverse() == xs)
}
swiftCheck(prop_reverseLen)
swiftCheck(prop_reverseReverse)
// test guards work (they don't)
func prop_linesUnlines(xs: String) {
// guard(xs.last == "\n") { // just to test guarding, it's a bad guard
XCTAssert((xs.lines() |> String.unlines) == xs)
// }
}
// swiftCheck(prop_linesUnlines) // can't explain this one!
}
func testMaybeFunctor() {
let x = Maybe.just(2)
let y = MaybeF(m: x).fmap({ $0 * 2 })
XCTAssert(y == Maybe.just(4))
let a = Maybe<Int>.none()
let b = MaybeF(m: a).fmap({ $0 * 2 })
XCTAssert(b == a);
}
func testPerformanceExample() {
// This is an example of a performance test case.
func term(ch: Chan<CDouble>, k: CDouble) -> Void {
ch.write(4 * pow(-1, k) / (2 * k + 1))
}
func pi(n: Int) -> CDouble {
let ch = Chan<CDouble>()
for k in (0..n) {
Future<Void>(exec: gcdExecutionContext, a: { return term(ch, CDouble(k)) })
}
var f = 0.0
for k in (0..n) {
f = f + ch.read()
}
return f
}
self.measureBlock() {
pi(200)
Void()
}
}
}