Skip to content

Commit 6bdb958

Browse files
committed
barometer -- 的测试带上 promises
1 parent a124ee8 commit 6bdb958

File tree

4 files changed

+89
-14
lines changed

4 files changed

+89
-14
lines changed

TODO.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
>
33
> - https://github.com/cicada-lang/propagator/issues/2
44
5-
`barometer` -- 的测试带上 `promises` -- 用论文中讲故事的方式来写测试
5+
`coercing`
66

77
fix `isContradiction` for `Supported`
88

src/dependency/supportedMerge.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ export function supportedMerge<A, B>(
66
content: Supported<A>,
77
increment: Supported<B>,
88
): Supported<A | B> | Contradiction {
9-
const mergedContent = merge(content.value, increment.value)
9+
const mergedValue = merge(content.value, increment.value)
1010

1111
// 这里的 cases 可以写成更对称的样子,
1212
// 但是这里为了效率(少调用 merge 的次数),
1313
// 写的不是那么对称了。
1414

15-
if (mergedContent === content.value) {
15+
if (mergedValue === content.value) {
1616
// 正向和反向的 implies 代表等价。
17-
if (implies(increment, mergedContent)) {
17+
if (implies(increment.value, mergedValue)) {
1818
// 倾向于 content,除非 increment 真的有更多信息。
1919
if (setIsSubsetOf(content.supports, increment.supports)) {
2020
return content
@@ -26,10 +26,10 @@ export function supportedMerge<A, B>(
2626
return content
2727
}
2828

29-
if (mergedContent === increment.value) {
29+
if (mergedValue === increment.value) {
3030
return increment
3131
}
3232

3333
const mergedSupports = setUnion(content.supports, increment.supports)
34-
return Supported(mergedContent, mergedSupports)
34+
return Supported(mergedValue, mergedSupports)
3535
}

src/examples/barometer.test.ts

+63-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,68 @@ test("examples / barometer / with supported value", async () => {
8181
put(barometerHeight, Supported(Interval(0.3, 0.32), new Set(["shadows"])))
8282
put(barometerShadow, Supported(Interval(0.36, 0.37), new Set(["shadows"])))
8383

84-
// await run()
84+
await run()
85+
86+
assert.deepStrictEqual(buildingHeight.content.supports, new Set(["shadows"]))
87+
assert(
88+
intervalAlmostEqual(
89+
buildingHeight.content.value,
90+
Interval(44.514, 48.978),
91+
0.001,
92+
),
93+
)
94+
95+
const fallTime = Cell()
96+
fallDuration(fallTime, buildingHeight)
97+
put(fallTime, Supported(Interval(2.9, 3.3), new Set(["lousy-fall-time"])))
98+
99+
await run()
100+
101+
assert.deepStrictEqual(buildingHeight.content.supports, new Set(["shadows"]))
102+
assert(
103+
intervalAlmostEqual(
104+
buildingHeight.content.value,
105+
Interval(44.514, 48.978),
106+
0.001,
107+
),
108+
)
109+
110+
put(fallTime, Supported(Interval(2.9, 3.1), new Set(["better-fall-time"])))
111+
112+
await run()
113+
114+
assert.deepStrictEqual(
115+
buildingHeight.content.supports,
116+
new Set(["shadows", "better-fall-time"]),
117+
)
118+
assert(
119+
intervalAlmostEqual(
120+
buildingHeight.content.value,
121+
Interval(44.514, 47.243),
122+
0.001,
123+
),
124+
)
125+
126+
put(buildingHeight, Supported(45, new Set(["superintendent"])))
127+
128+
await run()
129+
130+
assert.deepStrictEqual(
131+
buildingHeight.content.supports,
132+
new Set(["superintendent"]),
133+
)
134+
assert.deepStrictEqual(buildingHeight.content.value, 45)
135+
136+
console.log(barometerHeight.content)
137+
console.log(barometerShadow.content)
138+
console.log(buildingShadow.content)
139+
console.log(fallTime.content)
140+
141+
{
142+
const [t, h] = fallDuration()
143+
put(h, 45)
85144

86-
// console.log(buildingHeight)
145+
await run()
146+
console.log(t.content)
147+
}
87148
})

src/merge/merge.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isNothing } from "../cell/index.js"
2-
import { isSupported, supportedMerge } from "../dependency/index.js"
2+
import { Supported, isSupported, supportedMerge } from "../dependency/index.js"
33
import { defineGeneric, defineHandler } from "../generic/index.js"
44
import {
55
intervalContainsNumber,
@@ -36,11 +36,15 @@ import { theContradiction } from "./Contradiction.js"
3636
export const merge = defineGeneric({
3737
default(...args) {
3838
// no default, be explicit.
39-
console.error({
40-
who: "merge",
41-
message: "Unhandled args.",
42-
args,
43-
})
39+
console.dir(
40+
{
41+
who: "merge",
42+
message: "Unhandled args.",
43+
args,
44+
this: this,
45+
},
46+
{ depth: null },
47+
)
4448

4549
throw new Error(`[merge] Unhandled args.`)
4650
},
@@ -78,4 +82,14 @@ defineHandler(merge, [isNumber, isInterval], (content, increment) =>
7882
intervalContainsNumber(increment, content) ? content : theContradiction,
7983
)
8084

85+
function isSimple(x: any): boolean {
86+
return isNumber(x) || isInterval(x)
87+
}
88+
8189
defineHandler(merge, [isSupported, isSupported], supportedMerge)
90+
defineHandler(merge, [isSimple, isSupported], (v, m) =>
91+
supportedMerge(Supported(v, new Set()), m),
92+
)
93+
defineHandler(merge, [isSupported, isSimple], (m, v) =>
94+
supportedMerge(m, Supported(v, new Set())),
95+
)

0 commit comments

Comments
 (0)