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
|
package forge
import forge.util.OSet
import utest._
import utest.framework.TestPath
object EvaluationTests extends TestSuite{
val tests = Tests{
val graphs = new TestGraphs()
import graphs._
'evaluateSingle - {
class Checker[T: Discovered](base: T)(implicit tp: TestPath) {
val workspace = ammonite.ops.pwd / 'target / 'workspace / tp.value
ammonite.ops.rm(ammonite.ops.Path(workspace, ammonite.ops.pwd))
// Make sure data is persisted even if we re-create the evaluator each time
def evaluator = new Evaluator(
workspace,
implicitly[Discovered[T]].apply(base).map(_.swap).toMap
)
def apply(target: Target[_], expValue: Any,
expEvaled: OSet[Target[_]],
extraEvaled: Int = 0) = {
val Evaluator.Results(returnedValues, returnedEvaluated) = evaluator.evaluate(OSet(target))
val (matchingReturnedEvaled, extra) = returnedEvaluated.items.partition(expEvaled.contains)
assert(
returnedValues == Seq(expValue),
matchingReturnedEvaled.toSet == expEvaled.toSet,
extra.length == extraEvaled
)
// Second time the value is already cached, so no evaluation needed
val Evaluator.Results(returnedValues2, returnedEvaluated2) = evaluator.evaluate(OSet(target))
assert(
returnedValues2 == returnedValues,
returnedEvaluated2 == OSet()
)
}
}
'singleton - {
import singleton._
val check = new Checker(singleton)
// First time the target is evaluated
check(single, expValue = 0, expEvaled = OSet(single))
single.counter += 1
// After incrementing the counter, it forces re-evaluation
check(single, expValue = 1, expEvaled = OSet(single))
}
'pair - {
import pair._
val check = new Checker(pair)
check(down, expValue = 0, expEvaled = OSet(up, down))
println("=" * 20 + "incrementing down.counter" + "=" * 20)
down.counter += 1
check(down, expValue = 1, expEvaled = OSet(down))
println("=" * 20 + "incrementing up.counter" + "=" * 20)
up.counter += 1
check(down, expValue = 2, expEvaled = OSet(up, down))
}
'anonTriple - {
import anonTriple._
val check = new Checker(anonTriple)
val middle = down.inputs(0)
check(down, expValue = 0, expEvaled = OSet(up, middle, down))
down.counter += 1
check(down, expValue = 1, expEvaled = OSet(middle, down))
up.counter += 1
check(down, expValue = 2, expEvaled = OSet(up, middle, down))
middle.asInstanceOf[Target.Test].counter += 1
check(down, expValue = 3, expEvaled = OSet(middle, down))
}
'diamond - {
import diamond._
val check = new Checker(diamond)
check(down, expValue = 0, expEvaled = OSet(up, left, right, down))
down.counter += 1
check(down, expValue = 1, expEvaled = OSet(down))
up.counter += 1
// Increment by 2 because up is referenced twice: once by left once by right
check(down, expValue = 3, expEvaled = OSet(up, left, right, down))
left.counter += 1
check(down, expValue = 4, expEvaled = OSet(left, down))
right.counter += 1
check(down, expValue = 5, expEvaled = OSet(right, down))
}
'anonDiamond - {
import anonDiamond._
val check = new Checker(anonDiamond)
val left = down.inputs(0).asInstanceOf[Target.Test]
val right = down.inputs(1).asInstanceOf[Target.Test]
check(down, expValue = 0, expEvaled = OSet(up, left, right, down))
down.counter += 1
check(down, expValue = 1, expEvaled = OSet(left, right, down))
up.counter += 1
// Increment by 2 because up is referenced twice: once by left once by right
check(down, expValue = 3, expEvaled = OSet(up, left, right, down))
left.counter += 1
check(down, expValue = 4, expEvaled = OSet(left, right, down))
right.counter += 1
check(down, expValue = 5, expEvaled = OSet(left, right, down))
}
'bigSingleTerminal - {
import bigSingleTerminal._
val check = new Checker(bigSingleTerminal)
check(j, expValue = 0, expEvaled = OSet(a, b, e, f, i, j), extraEvaled = 22)
j.counter += 1
check(j, expValue = 1, expEvaled = OSet(j), extraEvaled = 3)
i.counter += 1
// increment value by 2 because `i` is used twice on the way to `j`
check(j, expValue = 3, expEvaled = OSet(j, i), extraEvaled = 8)
b.counter += 1
// increment value by 4 because `b` is used four times on the way to `j`
check(j, expValue = 7, expEvaled = OSet(b, e, f, i, j), extraEvaled = 20)
}
}
}
}
|