-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_runtime.py
More file actions
146 lines (129 loc) · 5.17 KB
/
test_runtime.py
File metadata and controls
146 lines (129 loc) · 5.17 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
from __future__ import annotations
import doctest
import pytest
from egglog.declarations import *
from egglog.exp import array_api
from egglog.runtime import *
from egglog.thunk import *
from egglog.type_constraint_solver import *
def test_type_str():
decls = Declarations(
_classes={
Ident.builtin("i64"): ClassDecl(),
Ident.builtin("Map"): ClassDecl(type_vars=(TypeVarRef(Ident.builtin("K")), TypeVarRef(Ident.builtin("V")))),
}
)
i64 = RuntimeClass(Thunk.value(decls), TypeRefWithVars(Ident.builtin("i64")))
Map = RuntimeClass(Thunk.value(decls), TypeRefWithVars(Ident.builtin("Map")))
assert str(i64) == "i64"
assert str(Map[i64, i64]) == "Map[i64, i64]"
def test_function_call():
decls = Declarations(
_classes={
Ident.builtin("i64"): ClassDecl(),
},
_functions={
Ident.builtin("one"): FunctionDecl(FunctionSignature(return_type=TypeRefWithVars(Ident.builtin("i64")))),
},
)
one = RuntimeFunction(Thunk.value(decls), Thunk.value(FunctionRef(Ident.builtin("one"))))
assert (
one().__egg_typed_expr__ # type: ignore[union-attr]
== TypedExprDecl(JustTypeRef(Ident.builtin("i64")), CallDecl(FunctionRef(Ident.builtin("one"))))
)
def test_classmethod_call():
K, V = TypeVarRef(Ident.builtin("K")), TypeVarRef(Ident.builtin("V"))
decls = Declarations(
_classes={
Ident.builtin("i64"): ClassDecl(),
Ident.builtin("unit"): ClassDecl(),
Ident.builtin("Map"): ClassDecl(
type_vars=(K, V),
class_methods={
"create": FunctionDecl(FunctionSignature(return_type=TypeRefWithVars(Ident.builtin("Map"), (K, V))))
},
),
},
)
Map = RuntimeClass(Thunk.value(decls), TypeRefWithVars(Ident.builtin("Map")))
with pytest.raises(TypeConstraintError):
Map.create()
i64 = RuntimeClass(Thunk.value(decls), TypeRefWithVars(Ident.builtin("i64")))
unit = RuntimeClass(Thunk.value(decls), TypeRefWithVars(Ident.builtin("unit")))
assert (
Map[i64, unit].create().__egg_typed_expr__ # type: ignore[union-attr]
== TypedExprDecl(
JustTypeRef(Ident.builtin("Map"), (JustTypeRef(Ident.builtin("i64")), JustTypeRef(Ident.builtin("unit")))),
CallDecl(
ClassMethodRef(Ident.builtin("Map"), "create"),
(),
(JustTypeRef(Ident.builtin("i64")), JustTypeRef(Ident.builtin("unit"))),
),
)
)
def test_expr_special():
decls = Declarations(
_classes={
Ident.builtin("i64"): ClassDecl(
methods={
"__add__": FunctionDecl(
FunctionSignature(
(TypeRefWithVars(Ident.builtin("i64")), TypeRefWithVars(Ident.builtin("i64"))),
("a", "b"),
(None, None),
TypeRefWithVars(Ident.builtin("i64")),
)
)
},
class_methods={
"__init__": FunctionDecl(
FunctionSignature(
(TypeRefWithVars(Ident.builtin("i64")),),
("self",),
(None,),
TypeRefWithVars(Ident.builtin("i64")),
)
)
},
),
},
)
i64 = RuntimeClass(Thunk.value(decls), TypeRefWithVars(Ident.builtin("i64")))
one = i64(1)
res = one + one # type: ignore[operator]
assert res.__egg_typed_expr__ == TypedExprDecl(
JustTypeRef(Ident.builtin("i64")),
CallDecl(
MethodRef(Ident.builtin("i64"), "__add__"),
(
TypedExprDecl(JustTypeRef(Ident.builtin("i64")), LitDecl(1)),
TypedExprDecl(JustTypeRef(Ident.builtin("i64")), LitDecl(1)),
),
),
)
def test_class_variable():
decls = Declarations(
_classes={
Ident.builtin("i64"): ClassDecl(
class_variables={"one": ConstantDecl(JustTypeRef(Ident.builtin("i64")), None)}
),
},
)
i64 = RuntimeClass(Thunk.value(decls), TypeRefWithVars(Ident.builtin("i64")))
one = i64.one
assert isinstance(one, RuntimeExpr)
assert one.__egg_typed_expr__ == TypedExprDecl(
JustTypeRef(Ident.builtin("i64")), CallDecl(ClassVariableRef(Ident.builtin("i64"), "one"))
)
def test_runtime_class_attr_lookup_is_stable():
assert array_api.TupleInt.__getitem__ is array_api.TupleInt.__getitem__
assert array_api.TupleInt.__dict__["__getitem__"] is array_api.TupleInt.__dict__["__getitem__"]
def test_doctest_finder_collects_runtime_function_docstrings():
names = {test.name for test in doctest.DocTestFinder().find(array_api)}
assert {
"egglog.exp.array_api.TupleInt.__getitem__",
"egglog.exp.array_api.TupleInt.if_",
"egglog.exp.array_api.TupleTupleInt.product",
"egglog.exp.array_api.Value.diff",
"egglog.exp.array_api.RecursiveValue.__getitem__",
} <= names