forked from google-research/dex-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.py
42 lines (36 loc) · 887 Bytes
/
api_test.py
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
# Copyright 2020 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
import unittest
import ctypes
import numpy as np
from textwrap import dedent
import dex
def test_eval():
cases = [
"2.5",
"4",
"[2, 3, 4]",
]
for expr in cases:
assert str(dex.eval(expr)) == expr
def test_module_attrs():
m = dex.Module(dedent("""
x = 2.5
y = [2, 3, 4]
"""))
assert str(m.x) == "2.5"
assert str(m.y) == "[2, 3, 4]"
def test_function_call():
m = dex.Module(dedent("""
def addOne (x: Float) : Float = x + 1.0
"""))
x = dex.eval("2.5")
y = dex.eval("[2, 3, 4]")
assert str(m.addOne(x)) == "3.5"
assert str(m.sum(y)) == "9"
def test_scalar_conversions():
assert float(dex.eval("5.0")) == 5.0
assert int(dex.eval("5")) == 5