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-15 18:16:21 +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-15 18:16:21 +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-23 01:55:28 +03:00
|
|
|
assert(SES([ a, b, c ], [ d, a, b, c ]), ==, [ .Insert(d), Copy(a), Copy(b), Copy(c) ])
|
2015-09-17 22:55:30 +03:00
|
|
|
}
|
2015-09-17 22:56:01 +03:00
|
|
|
|
|
|
|
func testSESCanDeleteAtHead() {
|
2015-10-23 01:55:28 +03:00
|
|
|
assert(SES([ d, a, b, c ], [ a, b, c ]), ==, [ .Delete(d), Copy(a), Copy(b), Copy(c) ])
|
2015-09-17 22:56:01 +03:00
|
|
|
}
|
2015-09-17 22:56:53 +03:00
|
|
|
|
|
|
|
func testSESCanInsertInMiddle() {
|
2015-10-23 01:55:28 +03:00
|
|
|
assert(SES([ a, b, c ], [ a, d, b, c ]), ==, [ Copy(a), .Insert(d), Copy(b), Copy(c) ])
|
2015-09-17 22:56:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSESCanDeleteInMiddle() {
|
2015-10-23 01:55:28 +03:00
|
|
|
assert(SES([ a, d, b, c ], [ a, b, c ]), ==, [ Copy(a), .Delete(d), Copy(b), Copy(c) ])
|
2015-09-17 22:56:53 +03:00
|
|
|
}
|
2015-09-28 21:28:36 +03:00
|
|
|
|
2015-10-07 00:29:06 +03:00
|
|
|
func testInsertsAtEnd() {
|
2015-10-23 01:55:28 +03:00
|
|
|
assert(SES([ a, b, c ], [ a, b, c, d ]), ==, [ Copy(a), Copy(b), Copy(c), .Insert(d) ])
|
2015-10-07 00:29:06 +03:00
|
|
|
}
|
|
|
|
|
2015-10-07 00:28:25 +03:00
|
|
|
func testDeletesAtEnd() {
|
2015-10-23 01:55:28 +03:00
|
|
|
assert(SES([ a, b, c, d ], [ a, b, c ]), ==, [ Copy(a), Copy(b), Copy(c), .Delete(d) ])
|
2015-10-07 00:28:25 +03:00
|
|
|
}
|
|
|
|
|
2015-09-28 21:28:36 +03:00
|
|
|
func testSESOfLongerSequences() {
|
2015-10-23 01:55:28 +03:00
|
|
|
assert(SES([ a, b, c, a, b, b, a ], [ c, b, a, b, a, c ]), ==, [ .Insert(c), .Delete(a), Copy(b), .Delete(c), Copy(a), .Delete(b), Copy(b), Copy(a), .Insert(c) ])
|
2015-09-28 21:28:36 +03:00
|
|
|
}
|
2015-09-16 16:56:28 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 16:08:30 +03:00
|
|
|
private typealias Term = Cofree<String, ()>
|
2015-10-22 17:30:38 +03:00
|
|
|
private typealias Diff = Free<String, (), Patch<Term>>
|
2015-10-06 19:57:48 +03:00
|
|
|
|
2015-10-23 01:54:10 +03:00
|
|
|
private func Copy(term: Term) -> Diff {
|
2015-10-23 01:51:14 +03:00
|
|
|
return hylo(Diff.Introduce(()), Term.unwrap)(term)
|
|
|
|
}
|
|
|
|
|
2015-10-15 16:08:30 +03:00
|
|
|
private let a = Term((), .Leaf("a"))
|
|
|
|
private let b = Term((), .Leaf("b"))
|
|
|
|
private let c = Term((), .Leaf("c"))
|
|
|
|
private let d = Term((), .Leaf("d"))
|
2015-10-06 19:58:07 +03:00
|
|
|
|
2015-10-06 20:12:53 +03:00
|
|
|
private func SES(a: [Term], _ b: [Term]) -> [Diff] {
|
2015-10-23 01:55:28 +03:00
|
|
|
return SES(a, b, cost: const(1)) { Cofree.equals(annotation: const(true), leaf: ==)($0, $1) ? Copy($1) : nil }
|
2015-10-06 20:12:53 +03:00
|
|
|
}
|
|
|
|
|
2015-10-06 23:23:48 +03:00
|
|
|
private func == (a: [Diff], b: [Diff]) -> Bool {
|
2015-10-23 01:55:28 +03:00
|
|
|
return a.count == b.count && zip(a, b).lazy.map(Diff.equals(pure: Patch.equals(Cofree.equals(annotation: const(true), leaf: ==)), leaf: ==, annotation: const(true))).reduce(true) { $0 && $1 }
|
2015-10-06 23:23:48 +03:00
|
|
|
}
|
|
|
|
|
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-10-14 01:05:11 +03:00
|
|
|
import Prelude
|
2015-09-16 16:56:28 +03:00
|
|
|
import XCTest
|