klongpy 0.6.9__py3-none-any.whl → 0.7.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. klongpy/__init__.py +17 -1
  2. klongpy/adverbs.py +84 -82
  3. klongpy/autograd.py +299 -0
  4. klongpy/backend.py +38 -103
  5. klongpy/backends/__init__.py +26 -0
  6. klongpy/backends/base.py +469 -0
  7. klongpy/backends/numpy_backend.py +123 -0
  8. klongpy/backends/registry.py +76 -0
  9. klongpy/backends/torch_backend.py +1047 -0
  10. klongpy-0.6.9.data/scripts/kgpy → klongpy/cli.py +110 -90
  11. klongpy/core.py +113 -974
  12. klongpy/db/sys_fn_db.py +7 -6
  13. klongpy/db/sys_fn_kvs.py +2 -4
  14. klongpy/dyads.py +332 -160
  15. klongpy/interpreter.py +60 -15
  16. klongpy/monads.py +121 -75
  17. klongpy/parser.py +328 -0
  18. klongpy/repl.py +23 -5
  19. klongpy/sys_fn.py +170 -21
  20. klongpy/sys_fn_autograd.py +290 -0
  21. klongpy/sys_fn_ipc.py +22 -15
  22. klongpy/sys_fn_timer.py +13 -3
  23. klongpy/types.py +503 -0
  24. klongpy/web/sys_fn_web.py +14 -4
  25. klongpy/writer.py +122 -0
  26. klongpy/ws/sys_fn_ws.py +5 -8
  27. klongpy-0.7.1.dist-info/METADATA +544 -0
  28. klongpy-0.7.1.dist-info/RECORD +52 -0
  29. {klongpy-0.6.9.dist-info → klongpy-0.7.1.dist-info}/WHEEL +1 -1
  30. klongpy-0.7.1.dist-info/entry_points.txt +2 -0
  31. {klongpy-0.6.9.dist-info → klongpy-0.7.1.dist-info}/top_level.txt +0 -1
  32. klongpy-0.6.9.dist-info/METADATA +0 -448
  33. klongpy-0.6.9.dist-info/RECORD +0 -77
  34. tests/__init__.py +0 -6
  35. tests/gen_join_over.py +0 -119
  36. tests/gen_py_suite.py +0 -77
  37. tests/gen_test_fn.py +0 -259
  38. tests/perf_async.py +0 -25
  39. tests/perf_avg.py +0 -18
  40. tests/perf_duckdb.py +0 -32
  41. tests/perf_gen.py +0 -38
  42. tests/perf_ipc_overhead.py +0 -34
  43. tests/perf_join.py +0 -53
  44. tests/perf_load.py +0 -17
  45. tests/perf_prog.py +0 -18
  46. tests/perf_serdes.py +0 -52
  47. tests/perf_sys_fn_db.py +0 -263
  48. tests/perf_vector.py +0 -40
  49. tests/test_accel.py +0 -227
  50. tests/test_df_cache.py +0 -85
  51. tests/test_eval_monad_list.py +0 -34
  52. tests/test_examples.py +0 -64
  53. tests/test_extra_suite.py +0 -382
  54. tests/test_file_cache.py +0 -185
  55. tests/test_interop.py +0 -180
  56. tests/test_kg_asarray.py +0 -94
  57. tests/test_kgtests.py +0 -65
  58. tests/test_known_bugs.py +0 -206
  59. tests/test_prog.py +0 -107
  60. tests/test_reshape_strings.py +0 -33
  61. tests/test_suite.py +0 -1480
  62. tests/test_suite_file.py +0 -153
  63. tests/test_sys_fn.py +0 -420
  64. tests/test_sys_fn_db.py +0 -88
  65. tests/test_sys_fn_ipc.py +0 -587
  66. tests/test_sys_fn_timer.py +0 -133
  67. tests/test_sys_fn_web.py +0 -50
  68. tests/test_util.py +0 -233
  69. tests/utils.py +0 -126
  70. {klongpy-0.6.9.dist-info → klongpy-0.7.1.dist-info}/licenses/LICENSE +0 -0
tests/test_interop.py DELETED
@@ -1,180 +0,0 @@
1
- import random
2
- import unittest
3
- from datetime import datetime
4
-
5
- from utils import *
6
-
7
- from klongpy import KlongInterpreter
8
-
9
-
10
- class TestPythonInterop(unittest.TestCase):
11
-
12
- def test_python_lambda_as_argument(self):
13
- """
14
- Test that a python lambda can be passed as an argument to a klong function.
15
- """
16
- klong = KlongInterpreter()
17
- klong['fn'] = lambda x: x+10
18
- klong('foo::{x(2)}')
19
- r = klong('foo(fn)')
20
- self.assertEqual(r, 12)
21
-
22
- def test_ext_var(self):
23
- klong = KlongInterpreter()
24
- x = random.random()
25
- klong['foo'] = x
26
- self.assertEqual(klong['foo'],x)
27
- # test reassignment
28
- x = random.random()
29
- x = [x]
30
- klong['foo'] = x
31
- self.assertTrue(kg_equal(klong['foo'],x))
32
-
33
- def test_del_var(self):
34
- klong = KlongInterpreter()
35
- with self.assertRaises(KeyError):
36
- del klong['foo']
37
- klong['foo'] = 1
38
- del klong['foo']
39
- with self.assertRaises(KeyError):
40
- del klong['foo']
41
-
42
- def test_python_lambda(self):
43
- klong = KlongInterpreter()
44
- klong['fn'] = lambda x: x+10
45
- self.assertEqual(klong('fn(2)'), 12)
46
-
47
- def test_call_klong_python_lambda(self):
48
- klong = KlongInterpreter()
49
- klong['fn'] = lambda x: x+10
50
- fn = klong['fn']
51
- self.assertEqual(fn(1),11)
52
-
53
- def test_invalid_call_klong_python_lambda(self):
54
- klong = KlongInterpreter()
55
- klong['fn'] = lambda x: x+10
56
- fn = klong['fn']
57
- with self.assertRaises(RuntimeError):
58
- self.assertEqual(fn(1,2),11)
59
-
60
- def test_call_klong_fn_with_scalar(self):
61
- klong = KlongInterpreter()
62
- klong('fn::{x+10}')
63
- r = klong['fn'](2)
64
- self.assertEqual(r, 12)
65
-
66
- def test_call_klong_fn_with_multiple_scalars(self):
67
- klong = KlongInterpreter()
68
- klong = KlongInterpreter()
69
- klong("fn::{(x*1000) + y - z}")
70
- r = klong['fn'](3, 10, 20)
71
- self.assertEqual(r, 2990)
72
-
73
- def test_call_klong_fn_with_array(self):
74
- klong = KlongInterpreter()
75
- klong("fn::{{x+10}'x}")
76
- r = klong['fn']([2])
77
- self.assertTrue(kg_equal(r, [12]))
78
-
79
- def test_datetime_parsing_example(self):
80
- klong = KlongInterpreter()
81
- klong['strptime'] = lambda x: datetime.strptime(x, "%d %B, %Y")
82
- r = klong('a::strptime("21 June, 2018")')
83
- self.assertEqual(r, datetime(2018, 6, 21, 0, 0))
84
- r = klong['a']
85
- self.assertEqual(r, datetime(2018, 6, 21, 0, 0))
86
- r = klong('d:::{};d,"timestamp",a')
87
- self.assertEqual(r, {'timestamp': datetime(2018, 6, 21, 0, 0)})
88
-
89
- def test_datetime_parsing_example_one_call(self):
90
- # run everything in one go
91
- klong = KlongInterpreter()
92
- klong['strptime'] = lambda x: datetime.strptime(x, "%d %B, %Y")
93
- r = klong("""a::strptime("21 June, 2018");d:::{};d,"timestamp",a""")
94
- self.assertEqual(r, {'timestamp': datetime(2018, 6, 21, 0, 0)})
95
- r = klong['a']
96
- self.assertEqual(r, datetime(2018, 6, 21, 0, 0))
97
-
98
- def test_callback_into_assertion(self):
99
- def assert_date(x):
100
- self.assertEqual(x, {'timestamp': datetime(2018, 6, 21, 0, 0)})
101
- return 42
102
- klong = KlongInterpreter()
103
- klong['strptime'] = lambda x: datetime.strptime(x, "%d %B, %Y")
104
- klong['assert'] = assert_date
105
- r = klong("""a::strptime("21 June, 2018");d:::{};d,"timestamp",a;assert(d)""")
106
- self.assertEqual(r, 42)
107
-
108
- def test_lambda_nilad(self):
109
- klong = KlongInterpreter()
110
- klong['f'] = lambda: 1000
111
- r = klong('f()')
112
- self.assertEqual(r, 1000)
113
-
114
- def test_lambda_monad(self):
115
- klong = KlongInterpreter()
116
- klong['f'] = lambda x: x*1000
117
- r = klong('f(3)')
118
- self.assertEqual(r, 3000)
119
-
120
- def test_lambda_dyad(self):
121
- klong = KlongInterpreter()
122
- klong['f'] = lambda x, y: x*1000 + y
123
- r = klong('f(3;10)')
124
- self.assertEqual(r, 3 * 1000 + 10)
125
-
126
- def test_lambda_triad(self):
127
- klong = KlongInterpreter()
128
- klong['f'] = lambda x, y, z: x*1000 + y - z
129
- r = klong('f(3; 10; 20)')
130
- self.assertEqual(r, 3 * 1000 + 10 - 20)
131
-
132
- def test_lambda_projection(self):
133
- klong = KlongInterpreter()
134
- klong['f'] = lambda x, y, z: ((x*1000) + y) - z
135
- klong("g::f(3;;)") # TODO: can we make the y and z vals implied None?
136
- r = klong('g(10;20)')
137
- self.assertEqual(r, ((3 * 1000) + 10) - 20)
138
- klong("h::g(10;)")
139
- r = klong('h(20)')
140
- self.assertEqual(r, ((3 * 1000) + 10) - 20)
141
-
142
- def test_setitem(self):
143
- klong = KlongInterpreter()
144
- klong['A'] = 1
145
- r = klong("A")
146
- self.assertEqual(r, 1)
147
- self.assertEqual(klong['A'], 1)
148
-
149
- def test_define_simple(self):
150
- klong = KlongInterpreter()
151
- r = klong("A::1; A")
152
- self.assertEqual(r, 1)
153
- self.assertEqual(klong['A'], 1)
154
-
155
- def test_define_reassign(self):
156
- klong = KlongInterpreter()
157
- klong('A::1')
158
- r = klong('A')
159
- self.assertEqual(r, 1)
160
- klong('A::2')
161
- r = klong('A')
162
- self.assertEqual(r, 2)
163
-
164
- def test_avg_mixed(self):
165
- data = np.random.rand(10**5)
166
- klong = KlongInterpreter()
167
- klong('avg::{(+/x)%#x}')
168
- klong['data'] = data
169
- start = time.perf_counter_ns()
170
- r = klong('avg(data)')
171
- stop = time.perf_counter_ns()
172
- # print((stop - start) / (10**9))
173
- self.assertEqual(r, np.average(data))
174
-
175
- def test_join_np_array_and_list(self):
176
- klong = KlongInterpreter()
177
- klong("A::[];A::A,:{};A::A,:{}")
178
- klong['B'] = [{}, {}, {}]
179
- r = klong("A,B")
180
- self.assertTrue(kg_equal(r, [{}, {}, {}, {}, {}]))
tests/test_kg_asarray.py DELETED
@@ -1,94 +0,0 @@
1
- import unittest
2
-
3
- import numpy as np
4
-
5
- from klongpy.core import kg_asarray, KGChar, KGSym
6
-
7
-
8
- class TestKGAsArray(unittest.TestCase):
9
-
10
- def test_kg_asarray_scalar_int(self):
11
- x = 42
12
- arr = kg_asarray(x)
13
- assert np.isarray(arr)
14
- assert arr.dtype == int
15
- assert arr.shape == ()
16
- assert arr.item() == 42
17
-
18
- def test_kg_asarray_scalar_float(self):
19
- x = 3.14
20
- arr = kg_asarray(x)
21
- assert np.isarray(arr)
22
- assert arr.dtype == float
23
- assert arr.item() == 3.14
24
-
25
- def test_kg_asarray_empty_list(self):
26
- x = []
27
- arr = kg_asarray(x)
28
- assert np.isarray(arr)
29
- assert arr.dtype == float # default dtype for empty list
30
- assert arr.size == 0
31
-
32
- def test_kg_asarray_string(self):
33
- s = "hello"
34
- arr = kg_asarray(s)
35
- assert np.isarray(arr)
36
- assert arr.dtype == object # assuming KGChar is object dtype
37
- assert "".join(arr) == "hello"
38
-
39
- def test_kg_asarray_list_of_ints(self):
40
- x = [1, 2, 3]
41
- arr = kg_asarray(x)
42
- assert np.isarray(arr)
43
- assert arr.dtype == int
44
- assert np.array_equal(arr, [1, 2, 3])
45
-
46
- def test_kg_asarray_nested_list_uniform(self):
47
- x = [[1, 2], [3, 4]]
48
- arr = kg_asarray(x)
49
- assert np.isarray(arr)
50
- assert arr.shape == (2,2)
51
- assert arr.dtype == int
52
- assert np.array_equal(arr, [[1,2],[3,4]])
53
-
54
- @unittest.skip("what is the expected behavior for this case?")
55
- def test_kg_asarray_nested_list_heterogeneous(self):
56
- # should embedded strings be expanded to individual characters?
57
- x = [[1, 2], "abc", [3.14, None]]
58
- arr = kg_asarray(x)
59
- assert np.isarray(arr)
60
- # Because of heterogeneous data, dtype should be object.
61
- assert arr.dtype == object
62
- # Check that sub-elements are arrays
63
- assert np.isarray(arr[0])
64
- assert np.isarray(arr[1])
65
- assert np.isarray(arr[2])
66
-
67
- def test_kg_asarray_already_array(self):
68
- x = np.array([1, 2, 3])
69
- arr = kg_asarray(x)
70
- # Should return as-is because already suitable dtype
71
- assert arr is x
72
-
73
- def test_kg_asarray_jagged_list(self):
74
- x = [[1, 2, 3], [4, 5], [6]]
75
- arr = kg_asarray(x)
76
- assert np.isarray(arr)
77
- # Jagged => object dtype
78
- assert arr.dtype == object
79
- # Each element should be an array
80
- assert all(np.isarray(e) for e in arr)
81
- assert np.array_equal(arr[0], [1, 2, 3])
82
- assert np.array_equal(arr[1], [4, 5])
83
- assert np.array_equal(arr[2], [6])
84
-
85
-
86
- def benchmark_kg_asarray(self):
87
- import timeit
88
- x = [[1, 2], [3, 4]]
89
- print(timeit.timeit(lambda: kg_asarray(x), number=100_000))
90
-
91
-
92
- if __name__ == "__main__":
93
- # run the benchmark
94
- TestKGAsArray().benchmark_kg_asarray()
tests/test_kgtests.py DELETED
@@ -1,65 +0,0 @@
1
- import glob
2
- import math
3
- import os
4
- import unittest
5
-
6
- from utils import *
7
-
8
-
9
- class TestKgTests(unittest.TestCase):
10
-
11
- def test_known_failure(self):
12
- klong = KlongInterpreter()
13
- klong['fullpath'] = "tests/kgtests/known_failure.kg"
14
- klong('.l("tests/kgtests/runner.kg")')
15
- self.assertEqual(klong['err'], 1)
16
-
17
- def eval_file_by_lines(self, fname):
18
- """
19
- Test the suite file line by line using our own t()
20
- """
21
- klong = create_test_klong()
22
- with open(fname, "r") as f:
23
- skip_header = True
24
- i = 0
25
- for r in f.readlines():
26
- if skip_header:
27
- if r.startswith("t::"):
28
- skip_header = False
29
- else:
30
- continue
31
- r = r.strip()
32
- if len(r) == 0:
33
- continue
34
- i += 1
35
- klong.exec(r)
36
- self.assertEqual(klong['err'],0)
37
- print(f"executed {i} lines")
38
-
39
-
40
- def test_kgtests(self):
41
- """
42
- Recursively run all tests under the kgtests folder that begin with "test" and end with ".kg".
43
- """
44
- ran_tests = False
45
- root_dir = os.path.join(os.getcwd(), "tests", "kgtests")
46
-
47
- for dirpath, _, filenames in os.walk(root_dir):
48
- for fname in filenames:
49
- if (fname.startswith("test") or fname.startswith("gen")) and fname.endswith(".kg"):
50
- ran_tests = True
51
- klong = KlongInterpreter()
52
- fullpath = os.path.join(dirpath, fname)
53
- klong['fullpath'] = fullpath
54
- try:
55
- klong('.l("tests/kgtests/runner.kg")')
56
- if fname.startswith("gen"):
57
- print(f"testing (line by line) {fname}...")
58
- self.eval_file_by_lines(fullpath)
59
- except Exception as e:
60
- print(e)
61
- self.assertEqual(klong['err'], 1)
62
- finally:
63
- self.assertEqual(klong['err'], 0)
64
-
65
- self.assertTrue(ran_tests)
tests/test_known_bugs.py DELETED
@@ -1,206 +0,0 @@
1
- import unittest
2
-
3
- import pandas as pd
4
- from utils import *
5
-
6
- from klongpy import KlongInterpreter
7
- from klongpy.core import KGChar, KGSym
8
-
9
-
10
- class TestKnownBugsSuite(unittest.TestCase):
11
-
12
- @unittest.skip
13
- def test_array_substitution(self):
14
- # [!10] should eval to [:! 10] but it doesn't
15
- klong = KlongInterpreter()
16
- r = klong('[!10]')
17
- self.assertTrue(kg_equal(r, [KGSym('!'), 10]))
18
- # [3*a^2] should eval to [3 :* :a :^ 2] but it doesn't
19
- r = klong('[3*a^2]')
20
- self.assertTrue(kg_equal(r, [3, KGSym('*'), KGSym('a'), KGSym('^'), 2]))
21
-
22
- @unittest.skip
23
- def test_should_fail_parsing(self):
24
- """
25
- this is a bug in both original Klong and KlongPy:
26
- fn::{[a b c]::1;a::1;b::2;c::3} the :: after variable declaration is a syntax error
27
- """
28
- pass
29
-
30
- @unittest.skip
31
- def test_calling_time_with_lambda(self):
32
- """
33
- .l("time")
34
- a::!1000;#a
35
- time({{{x*x}'a}'!1000})
36
- """
37
- # time will evaluate the lambda in such a way that the gap between the first .pc() and the second .pc() is
38
- # very small, so the time will be close to 0.
39
-
40
- @unittest.skip
41
- def test_table_access_with_at(self):
42
- data = {'col1': np.arange(10)}
43
- df = pd.DataFrame(data)
44
- klong = KlongInterpreter()
45
- klong['df'] = df
46
- klong('.py("klongpy.db")')
47
- klong('T::.table(df)')
48
- # @ should work the same as a dictionary
49
- r = klong('T@"col1"')
50
- self.assertTrue(kg_equal(r, data['col1']))
51
-
52
- @unittest.skip
53
- def test_extra_spaces(self):
54
- klong = KlongInterpreter()
55
- r = klong("a::{ 1 + 1 };a()")
56
- self.assertEqual(r, 2)
57
-
58
- @unittest.skip
59
- def test_semicolon_string_arg(self):
60
- klong = KlongInterpreter()
61
- klong('f::{x,y}')
62
- r = klong('f("hello";";")')
63
- self.assertEqual(r, "hello;")
64
-
65
- @unittest.skip
66
- def test_wrap_join(self):
67
- klong = KlongInterpreter()
68
- klong("q::[3 8]")
69
- r = klong("q[0],q[-1]")
70
- self.assertTrue(kg_equal(r, [3,8]))
71
- r = klong("(q[0],q[-1])")
72
- self.assertTrue(kg_equal(r, [3,8]))
73
-
74
- @unittest.skip
75
- def test_append_empty_dictionaries(self):
76
- klong = KlongInterpreter()
77
- r = klong("A::[];A::A,:{};A::A,:{};A::A,:{};A")
78
- self.assertTrue(kg_equal(r, [{}, {}, {}]))
79
-
80
- @unittest.skip
81
- def test_extra_chars_ignored(self):
82
- klong = KlongInterpreter()
83
- with self.assertRaises(Exception):
84
- klong("aggs::{[a];a:::{}}}}")
85
-
86
- @unittest.skip
87
- def test_tested_arity(self):
88
- # inner x is not seen in arity calculation
89
- # {.pyc(x,"ticker";[];:{})}'1#symbols
90
- pass
91
-
92
- @unittest.skip
93
- def test_monad_argument_returned(self):
94
- """
95
- Test that a monadic lambda can be passed as an argument, returned, and then called.
96
- """
97
- klong = KlongInterpreter()
98
- klong('fn::{x+10}')
99
- klong('foo::{x}')
100
- r = klong('foo(fn)(2)')
101
- self.assertEqual(r, 12)
102
-
103
- @unittest.skip
104
- def test_triad_as_arguments_with_currying(self):
105
- """
106
- Test that two monads can be passed as arguments to a klong function.
107
- """
108
- klong = KlongInterpreter()
109
- klong('fn::{x+10+y*z}')
110
- klong('foo::{x(2;;)}')
111
- r = klong('w::foo(fn;;)')
112
- r = klong('w(;3;5)')
113
- self.assertEqual(r, 2+10+3*5)
114
-
115
- @unittest.skip
116
- def test_fail_non_terminated_string(self):
117
- klong = KlongInterpreter()
118
- with self.assertRaises(Exception):
119
- klong('a::"T')
120
-
121
- @unittest.skip
122
- def test_define_nilad_with_subcall(self):
123
- klong = KlongInterpreter()
124
- klong("nt::{x}")
125
- klong('newt::{nt([["1" 2] ["3" 4] ["5" 6]])}')
126
-
127
- @unittest.skip
128
- def test_join_two_dict(self):
129
- klong = KlongInterpreter()
130
- klong("b:::{[1 2]}")
131
- klong("c:::{[3 4]}")
132
- r = klong("b,c")
133
- self.assertEqual(r, {1: 2, 3: 4})
134
-
135
- @unittest.skip
136
- def test_nested_dict(self):
137
- klong = KlongInterpreter()
138
- klong('c:::{["GET" :{["/" 2]}]}')
139
- r = klong('(c?"GET")?"/"')
140
- self.assertEqual(r, 2)
141
-
142
- @unittest.skip
143
- def test_dict_inner_create_syntax(self):
144
- klong = KlongInterpreter()
145
- with self.assertRaises(RuntimeError):
146
- # should fail to parse
147
- r = klong(":{[1 :{[2 3]}}")
148
-
149
- @unittest.skip
150
- def test_dict_inner_create(self):
151
- # this creates a KGCall to wrap the inner dict, which is generally correct for
152
- klong = KlongInterpreter()
153
- r = klong(":{[1 :{[2 3]}]}")
154
- self.assertEqual(r[1], {2: 3})
155
-
156
- @unittest.skip
157
- def test_match_nested_array(self):
158
- klong = KlongInterpreter()
159
- r = klong('[7,7,7]~[7,7,7]')
160
- self.assertEqual(r,1)
161
-
162
- @unittest.skip
163
- def test_monad_not_getting_called(self):
164
- klong = KlongInterpreter()
165
- klong("""
166
- BESTF::10^9;RESETF::{BESTF::10^9}
167
- ITER::{1};
168
- RUNX::{{x;ITER()}{x}:~ITER()}
169
- SCAN::{RESETF();RUNX();BESTF}
170
- """)
171
- r = klong("SCAN()")
172
- self.assertEqual(r,99)
173
-
174
- @unittest.skip
175
- def test_take_nested_array(self):
176
- klong = KlongInterpreter()
177
- r = klong("(4)#[[0 0]]")
178
- self.assertTrue(kg_equal(r,[[0,0],[0,0],[0,0],[0,0]]))
179
-
180
- @unittest.skip
181
- def test_fall_call_undefined_fn(self):
182
- klong = KlongInterpreter()
183
- with self.assertRaises(RuntimeError):
184
- klong('R(1)')
185
-
186
- @unittest.skip
187
- def test_at_in_depth_strings(self):
188
- # DIFF: this isn't yet supported in Klong
189
- klong = KlongInterpreter()
190
- r = klong('["1234","5678"]:@[0 1]')
191
- self.assertEqual(r,KGChar('2'))
192
-
193
- @unittest.skip
194
- def test_dot_f_complex(self):
195
- """ TODO: this is from the ref doc but doesn't work in klong either """
196
- klong = KlongInterpreter()
197
- klong("fr::{:[@x;0;1+|/{.f(x)}'x]}")
198
- self.assert_eval_cmp('fr(0)', '[]', klong=klong)
199
- self.assert_eval_cmp('fr(1)', '[1]', klong=klong)
200
- self.assert_eval_cmp('fr(3)', '[1 1 1]', klong=klong)
201
- self.assert_eval_cmp('fr(10)', '[1 1 1 1 1 1 1 1 1 1]', klong=klong)
202
- klong("fr::{:[@x;0;1+|/.f'x]}")
203
- self.assert_eval_cmp('fr(0)', '[]', klong=klong)
204
- self.assert_eval_cmp('fr(1)', '[1]', klong=klong)
205
- self.assert_eval_cmp('fr(3)', '[1 1 1]', klong=klong)
206
- self.assert_eval_cmp('fr(10)', '[1 1 1 1 1 1 1 1 1 1]', klong=klong)
tests/test_prog.py DELETED
@@ -1,107 +0,0 @@
1
- import unittest
2
- from klongpy import KlongInterpreter
3
- from klongpy.core import KGSym, read_sys_comment
4
- from utils import *
5
-
6
- class TestProg(unittest.TestCase):
7
-
8
- def test_read_string_vs_op(self):
9
- """
10
- This test makes sure that we can parse a string that's the same character string as an operation.
11
- """
12
- klong = KlongInterpreter()
13
- r = klong("""
14
- bar::{x}
15
- lin2::{bar("+")}
16
- lin2()
17
- """)
18
- self.assertEqual(r,'+')
19
-
20
- def test_read_string_arg(self):
21
- """
22
- This test makes sure that we can parse a string that's the same character string as an operation.
23
- """
24
- klong = KlongInterpreter()
25
- r = klong("""
26
- lin2::{.d("+");{.d((2+#x):^"-");.d("+")}'x;.p("");x}
27
- lin2(1)
28
- """)
29
- self.assertEqual(r, 1)
30
-
31
- def test_prog_arr(self):
32
- klong = KlongInterpreter()
33
- r = klong.prog("[[]]")[1][0]
34
- r = [x.tolist() for x in r]
35
- self.assertEqual(str(r), "[[]]")
36
-
37
- @unittest.skip
38
- def test_prog_define(self):
39
- klong = KlongInterpreter()
40
- r = klong.prog('t::{:[~y~z;fail(x;y;z);[]]}')[1]
41
- self.assertEqual(len(r), 1)
42
- self.assertEqual(r[0].a.a, "::")
43
- self.assertEqual(r[0].args[0], KGSym("t"))
44
- self.assertEqual(len(r[0].args[1]), 1)
45
- self.assertEqual(len(r[0].args[1][0]), 3)
46
-
47
- @unittest.skip
48
- def test_prog_fn(self):
49
- klong = KlongInterpreter()
50
- r = klong.prog('t("fv()" ; fv() ; 1)')[1]
51
- self.assertEqual(len(r), 1)
52
- self.assertEqual(len(r[0]), 2)
53
- self.assertEqual(len(r[0][1]), 3)
54
-
55
- @unittest.skip
56
- def test_prog_multi_prog(self):
57
- klong = KlongInterpreter()
58
- r = klong.prog('fv::{[a];a::1;a} ; t("fv()" ; fv() ; 1)')[1]
59
- self.assertEqual(len(r), 2)
60
- self.assertEqual(len(r[0]), 3)
61
- self.assertEqual(len(r[0][2]), 1)
62
- self.assertEqual(len(r[0][2][0]), 3)
63
- self.assertEqual(len(r[0][2][0][0]), 1)
64
- self.assertEqual(len(r[0][2][0][1]), 3)
65
- self.assertEqual(len(r[1]), 2)
66
- self.assertEqual(len(r[1][1]), 3)
67
- r = klong('fv::{[a];a::1;a} ; fv()')
68
- self.assertEqual(r[1], 1)
69
-
70
- @unittest.skip
71
- def test_prog(self):
72
- klong = KlongInterpreter()
73
- s = '"{x-y}:(1;)@2" ; {x-y}:(1;)@2 ; -1'
74
- i,p = klong.prog(s)
75
- self.assertEqual(i, len(s))
76
- self.assertEqual(p[0], '{x-y}:(1;)@2')
77
- self.assertEqual(str(p[1]), "[[[[:x, '-', :y]], [1, None]], '@', 2]")
78
- self.assertEqual(str(p[2]), "['-', 1]")
79
- a = klong.call(p[1])
80
- b = klong.call(p[2])
81
- self.assertEqual(a, b)
82
-
83
- def test_sys_comment_once(self):
84
- t = """
85
- .comment("*****")
86
-
87
- .fastpow when set, x^y compiles to .pow(x;y)
88
-
89
- *****X
90
- """
91
- x = t.index("X")
92
- cmd = '.comment("*****")'
93
- i = read_sys_comment(t, t.index(cmd)+len(cmd), "*****")
94
- assert i == x
95
-
96
- def test_sys_comment_replicated(self):
97
- t = """
98
- .comment("*****")
99
-
100
- .fastpow when set, x^y compiles to .pow(x;y)
101
-
102
- ************************************************************************X
103
- """
104
- x = t.index("X")
105
- cmd = '.comment("*****")'
106
- i = read_sys_comment(t, t.index(cmd)+len(cmd), "*****")
107
- assert i == x
@@ -1,33 +0,0 @@
1
- import unittest
2
- import numpy as np
3
-
4
- from klongpy import KlongInterpreter
5
- from tests.utils import kg_equal
6
-
7
- class TestReshapeStrings(unittest.TestCase):
8
- def setUp(self):
9
- self.klong = KlongInterpreter()
10
-
11
- def test_reshape_string_len1(self):
12
- r = self.klong('[2 2]:^"a"')
13
- self.assertTrue(kg_equal(r, np.asarray(["aa", "aa"], dtype=object)))
14
-
15
- def test_reshape_string_len2(self):
16
- r = self.klong('[2 2]:^"ab"')
17
- self.assertTrue(kg_equal(r, np.asarray(["ab", "ab"], dtype=object)))
18
-
19
- def test_reshape_string_len3(self):
20
- r = self.klong('[2 2]:^"abc"')
21
- self.assertTrue(kg_equal(r, np.asarray(["ab", "ca"], dtype=object)))
22
-
23
- def test_reshape_string_len4(self):
24
- r = self.klong('[2 2]:^"abcd"')
25
- self.assertTrue(kg_equal(r, np.asarray(["ab", "cd"], dtype=object)))
26
-
27
- def test_reshape_string_larger_shape(self):
28
- r = self.klong('[3 3]:^"abcd"')
29
- self.assertTrue(kg_equal(r, np.asarray(["abc", "dab", "cda"], dtype=object)))
30
-
31
-
32
- if __name__ == '__main__':
33
- unittest.main()