Skip to content

Commit

Permalink
fix kgtests
Browse files Browse the repository at this point in the history
  • Loading branch information
briangu committed Sep 23, 2023
1 parent d0d8e83 commit f7d6c87
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 41 deletions.
4 changes: 2 additions & 2 deletions klongpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .interpreter import KlongInterpreter
__all__ = ["KlongInterpreter"]
from .interpreter import KlongInterpreter, KlongException
__all__ = ["KlongInterpreter", "KlongException"]
2 changes: 1 addition & 1 deletion tests/gen_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_tests():
have_interp = False
in_header = True
test_names = set()
with open("kgtests/klong_suite.kg", "r") as f:
with open("kgtests/test_suite.kg", "r") as f:
for s in f:
s = s.strip()
if in_header:
Expand Down
9 changes: 9 additions & 0 deletions tests/kgtests/interop/test_import.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
err::0
wl::{.w(x);.p("")}
fail::{err::1;.d("failed: ");.p(x);.d("expected: ");wl(z);.d("got: ");wl(y);[]}
t::{:[~y~z;fail(x;y;z);[]]}

t(".py(""math"")"; .py("math"); 1)
t("sqrt(4)"; sqrt(4); 2)

:[err;[];.p("ok!")]
10 changes: 10 additions & 0 deletions tests/kgtests/interop/test_import_multiple_sub_modules.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
err::0
wl::{.w(x);.p("")}
fail::{err::1;.d("failed: ");.p(x);.d("expected: ");wl(z);.d("got: ");wl(y);[]}
t::{:[~y~z;fail(x;y;z);[]]}

t(".py(""math"";[""sqrt"" ""fsum""])"; .py("math";["sqrt" "fsum"]); 1)
t("sqrt(4)"; sqrt(4); 2)
t("fsum(!100)"; fsum(!100); +/!100)

:[err;[];.p("ok!")]
9 changes: 9 additions & 0 deletions tests/kgtests/interop/test_import_sub_module.kg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
err::0
wl::{.w(x);.p("")}
fail::{err::1;.d("failed: ");.p(x);.d("expected: ");wl(z);.d("got: ");wl(y);[]}
t::{:[~y~z;fail(x;y;z);[]]}

t(".py(""math"";""sqrt"")"; .py("math";"sqrt"); 1)
t("sqrt(4)"; sqrt(4); 2)

:[err;[];.p("ok!")]
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/perf_prog.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ def parse_suite_file(fname, number=10):

if __name__ == "__main__":
number = 20
b,r,bps,avg = parse_suite_file("klong_join_over.kg", number=number)
b,r,bps,avg = parse_suite_file("test_join_over.kg", number=number)
print(f"bytes processed: {b} time: {round(r,5)} bytes-per-second: {bps} time-per-pass: {round(avg,5)}")
5 changes: 0 additions & 5 deletions tests/test_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,3 @@ def test_nested_x_scope_dyad_projection(self):
r = klong('F("hello")')
self.assertEqual(r, "o")

if __name__ == "__main__":
import timeit
print(timeit.timeit('run_file("kgtests/klong_fn.kg")', number=10, globals=locals()) / 10)


13 changes: 3 additions & 10 deletions tests/test_join_over.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_no_join_array(self):
klong = KlongInterpreter()
r = klong(",/[1 2 3 4 5 6 7 8 9]")
self.assertTrue(kg_equal(r, np.array([1,2,3,4,5,6,7,8,9])))

def test_1D_arrays(self):
klong = KlongInterpreter()
r = klong(",/[[1 2 3] [4 5 6] [7 8 9]]")
Expand All @@ -59,7 +59,7 @@ def test_mixed_dim_arrays(self):
r = klong(",/[[] [] [[]]]")
self.assertTrue(kg_equal(r, np.array([[]])))

def test_string_elements(self):
def test_string_elements(self):
klong = KlongInterpreter()
r = klong(',/[["a" "b"] ["c" "d"]]')
self.assertTrue(kg_equal(r, np.array(['a', 'b', 'c', 'd'])))
Expand All @@ -78,7 +78,7 @@ def test_file_by_lines(self):
Test the suite file line by line using our own t()
"""
klong = create_test_klong()
with open("tests/kgtests/klong_join_over.kg", "r") as f:
with open("tests/kgtests/test_join_over.kg", "r") as f:
skip_header = True
i = 0
for r in f.readlines():
Expand All @@ -92,10 +92,3 @@ def test_file_by_lines(self):
i += 1
klong.exec(r)
print(f"executed {i} lines")


if __name__ == "__main__":
import timeit
number = 20
print(timeit.timeit('run_file("kgtests/klong_join_over.kg")', number=number, globals=locals()) / number)

19 changes: 12 additions & 7 deletions tests/test_kgtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ class TestKgTests(unittest.TestCase):

def test_kgtests(self):
"""
Run all tests under the kgtests folder.
Recursively run all tests under the kgtests folder that begin with "test" and end with ".kg".
"""
ran_tests = False
for x in glob.glob(os.path.join(os.getcwd(), os.path.join("tests", os.path.join("kgtests", "*.kg")))):
ran_tests = True
fname = os.path.basename(x)
print(f"testing: {fname}")
klong, _ = run_file(x)
self.assertEqual(klong['err'], 0)
root_dir = os.path.join(os.getcwd(), "tests", "kgtests")

for dirpath, dirnames, filenames in os.walk(root_dir):
for fname in filenames:
if fname.startswith("test") and fname.endswith(".kg"):
ran_tests = True
full_path = os.path.join(dirpath, fname)
print(f"TESTING: {fname}")
klong, _ = run_file(full_path)
self.assertEqual(klong['err'], 0)

self.assertTrue(ran_tests)
16 changes: 2 additions & 14 deletions tests/test_suite_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_file_by_lines(self):
Test the suite file line by line using our own t()
"""
klong = create_test_klong()
with open("tests/kgtests/klong_suite.kg", "r") as f:
with open("tests/kgtests/test_suite.kg", "r") as f:
skip_header = True
i = 0
for r in f.readlines():
Expand All @@ -142,24 +142,12 @@ def test_file_custom_test(self):
Test the suite file in one go using our own t()
"""
klong = create_test_klong()
with open("tests/kgtests/klong_suite.kg", "r") as f:
with open("tests/kgtests/test_suite.kg", "r") as f:
r = f.read()
i = r.index('rnd::')
r = r[i:]
klong(r)

# def test_file(self):
# """
# Test the entire suite file.
# """
# klong = KlongInterpreter()
# with open("tests/kgtests/klong_suite.kg", "r") as f:
# klong(f.read())
# r = klong('err')
# self.assertEqual(r, 0)
# r = klong['err']
# self.assertEqual(r, 0)


if __name__ == '__main__':
unittest.main(failfast=True, exit=False)
10 changes: 9 additions & 1 deletion tests/test_sys_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from utils import kg_equal

from klongpy import KlongInterpreter
from klongpy import KlongInterpreter, KlongException
from klongpy.sys_fn import *


Expand Down Expand Up @@ -221,17 +221,25 @@ def test_sys_python_load_module(self):

def test_sys_python_load_sub_module(self):
klong = KlongInterpreter()
with self.assertRaises(KlongException):
r = klong('sqrt(64)')
r = klong(f'.pyf("math";"sqrt")')
self.assertEqual(r,1)
r = klong('sqrt(64)')
self.assertEqual(r,8)
with self.assertRaises(KlongException):
r = klong('fsum(1+!10)')
r = klong(f'.pyf("math";"fsum")')
self.assertEqual(r,1)
r = klong('fsum(1+!10)')
self.assertEqual(r,55)

def test_sys_python_load_multiple_sub_modules(self):
klong = KlongInterpreter()
with self.assertRaises(KlongException):
r = klong('sqrt(64)')
with self.assertRaises(KlongException):
r = klong('fsum(1+!10)')
r = klong(f'.pyf("math";["sqrt" "fsum"])')
self.assertEqual(r,1)
r = klong('sqrt(64)')
Expand Down

0 comments on commit f7d6c87

Please sign in to comment.