-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_native_kernel_bridge.py
More file actions
417 lines (343 loc) · 17.7 KB
/
test_native_kernel_bridge.py
File metadata and controls
417 lines (343 loc) · 17.7 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
from __future__ import annotations
import shutil
from pathlib import Path
import pytest
from Implementations.Reference.Runtime.build_native_kernel_library import build_native_kernel_library
from Implementations.Reference.Runtime.python.native_kernel import (
load_native_bool_kernel_bridge,
load_native_enum_kernel_bridge,
load_native_kernel_bridge,
load_native_string_kernel_bridge,
)
from Implementations.Reference.Runtime.python.runtime_core import Slice05RuntimeCore, find_repo_root
from Implementations.Reference.Runtime.python.ui_runtime import (
BrowserUiRuntime,
BooleanBrowserUiRuntime,
BooleanRuntimeCore,
ButtonBrowserUiRuntime,
ButtonRuntimeCore,
EnumBrowserUiRuntime,
EnumRuntimeCore,
PathBrowserUiRuntime,
PathRuntimeCore,
StringBrowserUiRuntime,
StringRuntimeCore,
default_example06_contract_path,
default_example06_wfrog_path,
default_example07_contract_path,
default_example07_wfrog_path,
default_example08_contract_path,
default_example08_wfrog_path,
default_example09_contract_path,
default_example09_wfrog_path,
default_example10_contract_path,
default_example10_wfrog_path,
default_example11_contract_path,
default_example11_wfrog_path,
default_example12_contract_path,
default_example12_wfrog_path,
default_example13_contract_path,
default_example13_wfrog_path,
default_example14_contract_path,
default_example14_wfrog_path,
default_example15_contract_path,
default_example15_wfrog_path,
)
ROOT = find_repo_root(Path(__file__).resolve())
EXAMPLE05_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/05_bounded_ui_accumulator/native_kernel_manifest.json"
EXAMPLE06_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/06_boolean_value_roundtrip/native_kernel_manifest.json"
EXAMPLE07_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/07_string_value_roundtrip/native_kernel_manifest.json"
EXAMPLE08_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/08_enum_value_roundtrip/native_kernel_manifest.json"
EXAMPLE09_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/09_path_value_roundtrip/native_kernel_manifest.json"
EXAMPLE10_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/10_button_press_to_boolean/native_kernel_manifest.json"
EXAMPLE11_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/11_button_switch_when_pressed/native_kernel_manifest.json"
EXAMPLE12_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/12_button_switch_when_released/native_kernel_manifest.json"
EXAMPLE13_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/13_button_latch_when_pressed/native_kernel_manifest.json"
EXAMPLE14_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/14_button_latch_when_released/native_kernel_manifest.json"
EXAMPLE15_MANIFEST = ROOT / "Implementations/Reference/LLVM/examples/15_button_latch_until_released/native_kernel_manifest.json"
def _build_library(tmp_path: Path, example: str, manifest: Path) -> Path:
if shutil.which("clang") is None:
pytest.skip("clang is required for the dynamic native kernel bridge test")
suffix = ".dll" if __import__("sys").platform == "win32" else ".dylib" if __import__("sys").platform == "darwin" else ".so"
return build_native_kernel_library(
manifest_path=manifest,
output_path=tmp_path / f"example{example}_kernel{suffix}",
)
def test_python_dynamic_native_kernel_bridge_executes_example05(tmp_path: Path) -> None:
library = _build_library(tmp_path, "05", EXAMPLE05_MANIFEST)
bridge = load_native_kernel_bridge(EXAMPLE05_MANIFEST, library)
assert bridge.run(6).result == 30
runtime = Slice05RuntimeCore()
artifact = runtime.execute_with_native_kernel_bridge(bridge, control_value=6)
assert artifact["outputs"]["public"]["result"] == 30
assert artifact["outputs"]["ui"]["ind_result"] == 30
ui = BrowserUiRuntime(native_kernel_bridge=bridge, open_browser=False)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native kernel artifact" in html
def test_python_dynamic_native_kernel_bridge_executes_example06(tmp_path: Path) -> None:
library = _build_library(tmp_path, "06", EXAMPLE06_MANIFEST)
bridge = load_native_bool_kernel_bridge(EXAMPLE06_MANIFEST, library)
assert bridge.run(True).result is True
assert bridge.run(False).result is False
core = BooleanRuntimeCore()
artifact = core.execute_with_native_kernel_bridge(bridge, True)
assert artifact["outputs"]["public"]["result"] is True
assert artifact["outputs"]["ui"]["bool_result"] is True
ui = BooleanBrowserUiRuntime(
contract_path=default_example06_contract_path(),
wfrog_path=default_example06_wfrog_path(),
native_kernel_bridge=bridge,
open_browser=False,
)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native bool kernel artifact" in html
assert 'data-compiler-backend="llvm"' in html
assert 'data-execution-path="native_kernel_bridge"' in html
def test_python_dynamic_native_kernel_bridge_executes_example07(tmp_path: Path) -> None:
library = _build_library(tmp_path, "07", EXAMPLE07_MANIFEST)
bridge = load_native_string_kernel_bridge(EXAMPLE07_MANIFEST, library)
assert bridge.run("hello world").result == "hello world"
core = StringRuntimeCore()
artifact = core.execute_with_native_kernel_bridge(bridge, "hello world")
assert artifact["outputs"]["public"]["result_text"] == "hello world"
assert artifact["outputs"]["ui"]["str_result"] == "hello world"
ui = StringBrowserUiRuntime(
contract_path=default_example07_contract_path(),
wfrog_path=default_example07_wfrog_path(),
native_kernel_bridge=bridge,
open_browser=False,
)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native string kernel artifact" in html
assert 'data-compiler-backend="llvm"' in html
assert 'data-execution-path="native_kernel_bridge"' in html
assert "data-frog-visual-law='wfrog-realization-state-map'" in html
assert "Current runtime snapshot" not in html
assert "<pre>" not in html
def test_python_dynamic_native_kernel_bridge_executes_example08(tmp_path: Path) -> None:
library = _build_library(tmp_path, "08", EXAMPLE08_MANIFEST)
bridge = load_native_enum_kernel_bridge(EXAMPLE08_MANIFEST, library)
assert bridge.run(2).result_numeric_value == 2
core = EnumRuntimeCore()
artifact = core.execute_with_native_kernel_bridge(bridge, "fault")
assert artifact["outputs"]["public"]["result_mode"] == "fault"
assert artifact["outputs"]["ui"]["mode_result"] == "fault"
ui = EnumBrowserUiRuntime(
contract_path=default_example08_contract_path(),
wfrog_path=default_example08_wfrog_path(),
native_kernel_bridge=bridge,
open_browser=False,
)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native enum kernel artifact" in html
assert 'data-compiler-backend="llvm"' in html
assert 'data-execution-path="native_kernel_bridge"' in html
assert "data-frog-visual-law='wfrog-realization-state-map'" in html
def test_python_dynamic_native_kernel_bridge_executes_example09(tmp_path: Path) -> None:
library = _build_library(tmp_path, "09", EXAMPLE09_MANIFEST)
bridge = load_native_string_kernel_bridge(EXAMPLE09_MANIFEST, library)
assert bridge.run("C:/FROG/from_python_native.txt").result == "C:/FROG/from_python_native.txt"
core = PathRuntimeCore()
artifact = core.execute_with_native_kernel_bridge(bridge, "C:/FROG/from_python_native.txt")
assert artifact["outputs"]["public"]["result_path"] == "C:/FROG/from_python_native.txt"
assert artifact["outputs"]["ui"]["path_result"] == "C:/FROG/from_python_native.txt"
ui = PathBrowserUiRuntime(
contract_path=default_example09_contract_path(),
wfrog_path=default_example09_wfrog_path(),
native_kernel_bridge=bridge,
open_browser=False,
)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native path kernel artifact" in html
assert 'data-compiler-backend="llvm"' in html
assert 'data-execution-path="native_kernel_bridge"' in html
assert "data-frog-visual-law='wfrog-realization-state-map'" in html
def test_python_dynamic_native_kernel_bridge_executes_example10(tmp_path: Path) -> None:
library = _build_library(tmp_path, "10", EXAMPLE10_MANIFEST)
bridge = load_native_bool_kernel_bridge(EXAMPLE10_MANIFEST, library)
assert bridge.run(True).result is True
assert bridge.run(False).result is False
core = ButtonRuntimeCore(
contract_path=default_example10_contract_path(),
wfrog_path=default_example10_wfrog_path(),
)
artifact = core.execute_with_native_kernel_bridge(bridge, True)
assert artifact["outputs"]["public"]["pressed"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is True
assert artifact["outputs"]["ui"]["pressed_indicator"] is True
ui = ButtonBrowserUiRuntime(
contract_path=default_example10_contract_path(),
wfrog_path=default_example10_wfrog_path(),
native_kernel_bridge=bridge,
open_browser=False,
)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native Button bool kernel artifact" in html
assert 'data-compiler-backend="llvm"' in html
assert 'data-execution-path="native_kernel_bridge"' in html
assert "data-frog-visual-law='wfrog-realization-state-map'" in html
assert "data-asset-route='/asset/button_rectangular_svg'" in html
assert "class='button-press-overlay' type='button'" in html
def test_python_dynamic_native_kernel_bridge_executes_example11(tmp_path: Path) -> None:
library = _build_library(tmp_path, "11", EXAMPLE11_MANIFEST)
bridge = load_native_bool_kernel_bridge(EXAMPLE11_MANIFEST, library)
assert bridge.run(True).result is True
assert bridge.run(False).result is False
core = ButtonRuntimeCore(
contract_path=default_example11_contract_path(),
wfrog_path=default_example11_wfrog_path(),
)
artifact = core.execute_with_native_kernel_bridge(bridge, True)
assert artifact["outputs"]["public"]["switched"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is True
assert artifact["outputs"]["ui"]["switched_indicator"] is True
release_core = ButtonRuntimeCore(
contract_path=default_example12_contract_path(),
wfrog_path=default_example12_wfrog_path(),
)
release_library = _build_library(tmp_path, "12", EXAMPLE12_MANIFEST)
release_bridge = load_native_bool_kernel_bridge(EXAMPLE12_MANIFEST, release_library)
release_artifact = release_core.execute_with_native_kernel_bridge(release_bridge, False)
assert release_artifact["outputs"]["public"]["switched"] is True
assert release_artifact["outputs"]["ui"]["trigger_button"] is True
assert release_artifact["outputs"]["ui"]["switched_indicator"] is True
ui = ButtonBrowserUiRuntime(
contract_path=default_example11_contract_path(),
wfrog_path=default_example11_wfrog_path(),
native_kernel_bridge=bridge,
open_browser=False,
)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native Button bool kernel artifact" in html
assert 'data-compiler-backend="llvm"' in html
assert 'data-execution-path="native_kernel_bridge"' in html
assert "data-frog-visual-law='wfrog-realization-state-map'" in html
assert "data-asset-route='/asset/button_rectangular_svg'" in html
assert "data-frog-public-input-id='trigger_value'" in html
assert "switch_when_pressed" in html
def test_python_dynamic_native_kernel_bridge_executes_example12(tmp_path: Path) -> None:
library = _build_library(tmp_path, "12", EXAMPLE12_MANIFEST)
bridge = load_native_bool_kernel_bridge(EXAMPLE12_MANIFEST, library)
assert bridge.run(True).result is True
assert bridge.run(False).result is False
core = ButtonRuntimeCore(
contract_path=default_example12_contract_path(),
wfrog_path=default_example12_wfrog_path(),
)
artifact = core.execute_with_native_kernel_bridge(bridge, True)
assert artifact["outputs"]["public"]["switched"] is False
assert artifact["outputs"]["ui"]["trigger_button"] is False
assert artifact["outputs"]["ui"]["switched_indicator"] is False
ui = ButtonBrowserUiRuntime(
contract_path=default_example12_contract_path(),
wfrog_path=default_example12_wfrog_path(),
native_kernel_bridge=bridge,
open_browser=False,
)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native Button bool kernel artifact" in html
assert 'data-compiler-backend="llvm"' in html
assert 'data-execution-path="native_kernel_bridge"' in html
assert "data-frog-visual-law='wfrog-realization-state-map'" in html
assert "data-asset-route='/asset/button_rectangular_svg'" in html
assert "data-frog-public-input-id='trigger_value'" in html
assert "switch_when_released" in html
def test_python_dynamic_native_kernel_bridge_executes_example13(tmp_path: Path) -> None:
library = _build_library(tmp_path, "13", EXAMPLE13_MANIFEST)
bridge = load_native_bool_kernel_bridge(EXAMPLE13_MANIFEST, library)
assert bridge.run(True).result is True
assert bridge.run(False).result is False
core = ButtonRuntimeCore(
contract_path=default_example13_contract_path(),
wfrog_path=default_example13_wfrog_path(),
)
artifact = core.execute_with_native_kernel_bridge(bridge, True)
assert artifact["outputs"]["public"]["latched"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is False
assert artifact["outputs"]["ui"]["latched_indicator"] is True
assert artifact["execution_summary"]["button_stored_value"] is False
assert artifact["execution_summary"]["program_read_value"] is True
ui = ButtonBrowserUiRuntime(
contract_path=default_example13_contract_path(),
wfrog_path=default_example13_wfrog_path(),
native_kernel_bridge=bridge,
open_browser=False,
)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native Button bool kernel artifact" in html
assert 'data-compiler-backend="llvm"' in html
assert 'data-execution-path="native_kernel_bridge"' in html
assert "data-frog-visual-law='wfrog-realization-state-map'" in html
assert "data-asset-route='/asset/button_rectangular_svg'" in html
assert "data-frog-public-input-id='trigger_value'" in html
assert "latch_when_pressed" in html
def test_python_dynamic_native_kernel_bridge_executes_example14(tmp_path: Path) -> None:
library = _build_library(tmp_path, "14", EXAMPLE14_MANIFEST)
bridge = load_native_bool_kernel_bridge(EXAMPLE14_MANIFEST, library)
assert bridge.run(True).result is True
assert bridge.run(False).result is False
core = ButtonRuntimeCore(
contract_path=default_example14_contract_path(),
wfrog_path=default_example14_wfrog_path(),
)
artifact = core.execute_with_native_kernel_bridge(bridge, False)
assert artifact["outputs"]["public"]["latched"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is False
assert artifact["outputs"]["ui"]["latched_indicator"] is True
assert artifact["execution_summary"]["button_stored_value"] is False
assert artifact["execution_summary"]["program_read_value"] is True
ui = ButtonBrowserUiRuntime(
contract_path=default_example14_contract_path(),
wfrog_path=default_example14_wfrog_path(),
native_kernel_bridge=bridge,
open_browser=False,
)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native Button bool kernel artifact" in html
assert 'data-compiler-backend="llvm"' in html
assert 'data-execution-path="native_kernel_bridge"' in html
assert "data-frog-visual-law='wfrog-realization-state-map'" in html
assert "data-asset-route='/asset/button_rectangular_svg'" in html
assert "data-frog-public-input-id='trigger_value'" in html
assert "latch_when_released" in html
def test_python_dynamic_native_kernel_bridge_executes_example15(tmp_path: Path) -> None:
library = _build_library(tmp_path, "15", EXAMPLE15_MANIFEST)
bridge = load_native_bool_kernel_bridge(EXAMPLE15_MANIFEST, library)
assert bridge.run(True).result is True
assert bridge.run(False).result is False
core = ButtonRuntimeCore(
contract_path=default_example15_contract_path(),
wfrog_path=default_example15_wfrog_path(),
)
artifact = core.execute_with_native_kernel_bridge(bridge, True)
assert artifact["outputs"]["public"]["latched"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is True
assert artifact["outputs"]["ui"]["latched_indicator"] is True
assert artifact["execution_summary"]["button_stored_value"] is True
assert artifact["execution_summary"]["button_physical_pressed"] is True
assert artifact["execution_summary"]["program_read_value"] is True
ui = ButtonBrowserUiRuntime(
contract_path=default_example15_contract_path(),
wfrog_path=default_example15_wfrog_path(),
native_kernel_bridge=bridge,
open_browser=False,
)
html = ui.render_html()
assert "native kernel bridge" in html
assert "LLVM native Button bool kernel artifact" in html
assert 'data-compiler-backend="llvm"' in html
assert 'data-execution-path="native_kernel_bridge"' in html
assert "data-frog-visual-law='wfrog-realization-state-map'" in html
assert "data-asset-route='/asset/button_rectangular_svg'" in html
assert "data-frog-public-input-id='trigger_value'" in html
assert "latch_until_released" in html