2015-10-06 21:25:18 +03:00
|
|
|
final class SESTests: XCTestCase {
|
2015-09-16 16:56:28 +03:00
|
|
|
func testSESOverEmptyCollectionsIsEmpty() {
|
2015-10-06 20:16:43 +03:00
|
|
|
assert(SES([], []), ==, [])
|
2015-09-16 16:56:28 +03:00
|
|
|
}
|
2015-09-16 17:02:20 +03:00
|
|
|
|
|
|
|
func testSESOverEmptyAndNonEmptyCollectionsIsInsertions() {
|
2015-10-06 20:22:54 +03:00
|
|
|
assert(SES([], [ a, b ]), ==, [ insert(a), insert(b) ])
|
2015-09-16 17:02:20 +03:00
|
|
|
}
|
2015-09-16 17:04:30 +03:00
|
|
|
|
|
|
|
func testSESOverNonEmptyAndEmptyCollectionsIsDeletions() {
|
2015-10-06 20:26:44 +03:00
|
|
|
assert(SES([ a, b ], []), ==, [ delete(a), delete(b) ])
|
2015-09-16 17:04:30 +03:00
|
|
|
}
|
2015-09-17 22:55:30 +03:00
|
|
|
|
|
|
|
func testSESCanInsertAtHead() {
|
2015-10-06 20:25:27 +03:00
|
|
|
assert(SES([ a, b, c ], [ d, a, b, c ]), ==, [ insert(d), roll(a), roll(b), roll(c) ])
|
2015-09-17 22:55:30 +03:00
|
|
|
}
|
2015-09-17 22:56:01 +03:00
|
|
|
|
|
|
|
func testSESCanDeleteAtHead() {
|
2015-10-06 20:25:27 +03:00
|
|
|
assert(SES([ d, a, b, c ], [ a, b, c ]), ==, [ delete(d), roll(a), roll(b), roll(c) ])
|
2015-09-17 22:56:01 +03:00
|
|
|
}
|
2015-09-17 22:56:53 +03:00
|
|
|
|
|
|
|
func testSESCanInsertInMiddle() {
|
2015-10-06 20:25:56 +03:00
|
|
|
assert(SES([ a, b, c ], [ a, d, b, c ]), ==, [ roll(a), insert(d), roll(b), roll(c) ])
|
2015-09-17 22:56:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSESCanDeleteInMiddle() {
|
2015-10-06 20:25:56 +03:00
|
|
|
assert(SES([ a, d, b, c ], [ a, b, c ]), ==, [ roll(a), delete(d), roll(b), roll(c) ])
|
2015-09-17 22:56:53 +03:00
|
|
|
}
|
2015-09-28 21:28:36 +03:00
|
|
|
|
|
|
|
func testSESOfLongerSequences() {
|
2015-10-06 20:27:06 +03:00
|
|
|
assert(SES([ a, b, c, a, b, b, a ], [ c, b, a, b, a, c ]), ==, [ Diff.Pure(.Replace(a, c)), roll(b), delete(c), roll(a), delete(b), roll(b), roll(a), insert(c) ])
|
2015-09-28 21:28:36 +03:00
|
|
|
}
|
2015-09-16 16:56:28 +03:00
|
|
|
}
|
|
|
|
|
2015-10-06 20:22:37 +03:00
|
|
|
private func insert(term: Term) -> Diff {
|
|
|
|
return Diff.Pure(.Insert(term))
|
|
|
|
}
|
|
|
|
|
|
|
|
private func delete(term: Term) -> Diff {
|
|
|
|
return Diff.Pure(.Delete(term))
|
|
|
|
}
|
|
|
|
|
2015-10-06 20:25:09 +03:00
|
|
|
private func roll(term: Term) -> Diff {
|
|
|
|
return Diff.Roll(term.out.map(roll))
|
2015-10-06 20:22:37 +03:00
|
|
|
}
|
|
|
|
|
2015-10-06 19:57:48 +03:00
|
|
|
private typealias Term = Fix<Info>
|
2015-10-06 20:16:32 +03:00
|
|
|
private typealias Diff = Free<Info, Patch<Info>>
|
2015-10-06 19:57:48 +03:00
|
|
|
|
2015-10-06 19:58:07 +03:00
|
|
|
private let a = Term.In(.Leaf(.Literal("a", [])))
|
|
|
|
private let b = Term.In(.Leaf(.Literal("b", [])))
|
|
|
|
private let c = Term.In(.Leaf(.Literal("c", [])))
|
|
|
|
private let d = Term.In(.Leaf(.Literal("d", [])))
|
|
|
|
|
2015-10-06 20:12:53 +03:00
|
|
|
private func SES(a: [Term], _ b: [Term]) -> [Diff] {
|
|
|
|
return SES(a, b, equals: ==, recur: { Diff.Pure(Patch.Replace($0, $1)) })
|
|
|
|
}
|
|
|
|
|
2015-10-06 23:23:48 +03:00
|
|
|
private func == (a: [Diff], b: [Diff]) -> Bool {
|
|
|
|
return a.count == b.count && zip(a, b).lazy.map(Diff.equals(ifPure: Patch.equals(==), ifRoll: ==)).reduce(true) { $0 && $1 }
|
|
|
|
}
|
|
|
|
|
2015-09-16 17:02:20 +03:00
|
|
|
|
2015-10-06 20:16:23 +03:00
|
|
|
import Assertions
|
2015-10-06 20:10:45 +03:00
|
|
|
@testable import Doubt
|
2015-09-16 16:56:28 +03:00
|
|
|
import XCTest
|