2015-10-16 01:18:38 +03:00
|
|
|
final class InterpreterTests: XCTestCase {
|
2015-10-16 01:25:35 +03:00
|
|
|
func testRestrictsComparisons() {
|
2015-10-16 01:29:17 +03:00
|
|
|
let comparable: (Term, Term) -> Bool = { $0.extract == 0 && $1.extract == 0 }
|
2015-10-23 01:55:28 +03:00
|
|
|
let i = Interpreter(equal: ==, comparable: comparable, cost: const(1))
|
|
|
|
let d = i.run(a, b)
|
|
|
|
assert(d, ==, restricted)
|
2015-10-14 23:43:08 +03:00
|
|
|
}
|
2015-10-15 00:41:51 +03:00
|
|
|
|
2015-10-15 00:44:19 +03:00
|
|
|
func testComparisonsOfDisjointlyCategorizedTermsAreRestricted() {
|
2015-10-15 00:51:31 +03:00
|
|
|
var effects = 0
|
2015-10-16 01:29:23 +03:00
|
|
|
let categorize: Term -> Set<Int> = { $0.extract == 0 ? [ 0 ] : [ effects++ ] }
|
|
|
|
assert(Interpreter(equal: ==, comparable: Interpreter.comparable(categorize), cost: const(1)).run(a, b), ==, restricted)
|
2015-10-15 00:41:51 +03:00
|
|
|
}
|
2015-10-15 00:47:07 +03:00
|
|
|
|
2015-10-16 01:25:35 +03:00
|
|
|
func testUnrestrictedComparisonsComputeReplacements() {
|
|
|
|
assert(Interpreter(equal: ==, comparable: const(true), cost: const(1)).run(a, b), ==, unrestricted)
|
2015-10-15 00:47:07 +03:00
|
|
|
}
|
2015-10-15 00:51:40 +03:00
|
|
|
|
|
|
|
func testComparisonsOfUncategorizedTermsAreUnrestricted() {
|
2015-10-16 01:25:35 +03:00
|
|
|
assert(Interpreter(equal: ==, comparable: Interpreter.comparable { _ in Set<String>() }, cost: const(1)).run(a, b), ==, unrestricted)
|
2015-10-15 00:51:40 +03:00
|
|
|
}
|
2015-10-14 22:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-14 23:09:58 +03:00
|
|
|
private typealias Term = Cofree<String, Int>
|
2015-10-22 23:02:03 +03:00
|
|
|
private typealias Diff = Free<String, (Int, Int), Patch<Term>>
|
2015-10-14 23:09:58 +03:00
|
|
|
|
2015-10-14 23:10:23 +03:00
|
|
|
private let a = Term(0, [ Term(1, .Leaf("a")), Term(2, .Leaf("b")), Term(3, .Leaf("c")) ])
|
|
|
|
private let b = Term(0, [ Term(1, .Leaf("c")), Term(2, .Leaf("b")), Term(3, .Leaf("a")) ])
|
2015-10-14 23:07:07 +03:00
|
|
|
|
2015-10-23 01:55:28 +03:00
|
|
|
private let restricted = Diff.Roll((0, 0), [
|
2015-10-15 00:39:53 +03:00
|
|
|
.Pure(.Insert(Term(1, .Leaf("c")))),
|
|
|
|
.Pure(.Delete(Term(1, .Leaf("a")))),
|
2015-10-23 01:55:28 +03:00
|
|
|
.Roll((2, 2), .Leaf("b")),
|
2015-10-15 00:39:53 +03:00
|
|
|
.Pure(.Insert(Term(3, .Leaf("a")))),
|
|
|
|
.Pure(.Delete(Term(3, .Leaf("c")))),
|
|
|
|
])
|
|
|
|
|
2015-10-23 01:55:28 +03:00
|
|
|
private let unrestricted = Diff.Roll((0, 0), [
|
2015-10-15 00:46:50 +03:00
|
|
|
.Pure(.Replace(Term(1, .Leaf("a")), Term(1, .Leaf("c")))),
|
2015-10-23 01:55:28 +03:00
|
|
|
.Roll((2, 2), .Leaf("b")),
|
2015-10-15 00:46:50 +03:00
|
|
|
.Pure(.Replace(Term(3, .Leaf("c")), Term(3, .Leaf("a")))),
|
|
|
|
])
|
|
|
|
|
2015-10-14 23:07:07 +03:00
|
|
|
|
2015-10-14 22:57:58 +03:00
|
|
|
import Assertions
|
|
|
|
@testable import Doubt
|
2015-10-14 23:43:08 +03:00
|
|
|
import Prelude
|
2015-10-14 22:57:58 +03:00
|
|
|
import XCTest
|