]> gitweb.factorcode.org Git - factor.git/blob - extra/python/python-tests.factor
Revert "factor: rename [ ] [ ] unit-test -> { } [ ] unit-test using a refactoring...
[factor.git] / extra / python / python-tests.factor
1 USING: accessors alien arrays assocs calendar continuations destructors
2 destructors.private fry kernel math memory namespaces python python.errors
3 python.ffi python.objects sequences strings tools.test ;
4 IN: python
5
6 : py-test ( result quot -- )
7     '[ _ with-destructors ] unit-test ; inline
8
9 ! None testing
10 { t } [
11     "__builtin__" py-import "None" getattr <none> =
12 ] py-test
13
14 ! Pretty sure the # of None references should stay constant.
15 { t } [
16     [
17         "sys" py-import "getrefcount" getattr
18         <none> <1py-tuple> call-object py>
19     ] with-destructors
20     [
21         "sys" py-import "getrefcount" getattr
22         <none> <1py-tuple> call-object py>
23     ] with-destructors =
24 ] unit-test
25
26 { } [ { f f f } >py drop ] py-test
27
28 ! Destructors
29 { 1 } [ 33 >py drop always-destructors get length ] py-test
30
31 { 1 } [ f >py drop always-destructors get length ] py-test
32
33 ! The tuple steals the reference properly now.
34 { 1 } [ 33 >py <1py-tuple> drop always-destructors get length ] py-test
35
36 { 1 } [ { } >py drop always-destructors get length ] py-test
37
38 { 1 } [ V{ 1 2 3 4 } >py drop always-destructors get length ] py-test
39
40 { 2 } [
41     99 >py V{ 1 2 3 4 } >py 2drop always-destructors get length
42 ] py-test
43
44 { 1 } [
45     { { { 33 } } } >py drop always-destructors get length
46 ] py-test
47
48 { } [ 123 <alien> unsteal-ref ] unit-test
49
50 [ t ] [ Py_GetVersion string? ] unit-test
51
52 [ "os" ] [ "os" py-import PyModule_GetName ] py-test
53
54 [ t ] [
55     "os" py-import "getpid" getattr
56     { } >py call-object py> 0 >
57 ] py-test
58
59 [ t ] [ Py_IsInitialized ] py-test
60
61 ! py-importing
62 [ { "ImportError" "No module named kolobi" f } ] [
63     [ "kolobi" py-import ] [
64         [ type>> ] [ message>> ] [ traceback>> ] tri 3array
65     ] recover
66 ] py-test
67
68 ! setattr
69 [ 73 ] [
70     "sys" py-import "testit" [ 73 >py setattr ] [ getattr py> ] 2bi
71 ] py-test
72
73 ! Tuples
74 [ 2 ] [ 2 <py-tuple> py-tuple-size ] py-test
75
76 : py-datepy> ( py-obj -- timestamp )
77     { "year" "month" "day" } [ getattr py> ] with map
78     first3 0 0 0 instant <timestamp> ;
79
80 ! Lists
81 [ t ] [ V{ 4 8 15 16 23 42 } dup >py py> = ] py-test
82
83 ! ! Datetimes
84 [ t ] [
85     [ py-datepy> ] "date" py-type-dispatch get set-at
86     "datetime" py-import "date" getattr "today" getattr
87     { } >py call-object py>
88     today instant >>gmt-offset =
89 ] py-test
90
91 ! Unicode
92 [ "غثههح" ] [
93     "os.path" py-import "basename" getattr { "غثههح" } >py call-object py>
94 ] py-test
95
96 ! Instance variables
97 [ 7 ] [
98     "datetime" py-import "timedelta" getattr
99     { 7 } >py call-object "days" getattr py>
100 ] py-test
101
102 ! Create a dictonary
103 [ 0 ] [ <py-dict> py-dict-size ] py-test
104
105 ! Dictionary with object keys
106 [ 1 ] [
107     <py-dict> dup 0 >py 33 >py py-dict-set-item py-dict-size
108 ] py-test
109
110 ! Dictionary with string keys
111 [ 1 ] [
112     <py-dict> [ "foo" 33 >py py-dict-set-item-string ] [ py-dict-size ] bi
113 ] py-test
114
115 ! Get dictionary items
116 [ 33 ] [
117     <py-dict> "tjaba"
118     [ 33 >py  py-dict-set-item-string ]
119     [ py-dict-get-item-string py> ] 2bi
120 ] py-test
121
122 ! Nest dicts
123 [ 0 ] [
124     <py-dict> "foo"
125     [ <py-dict> py-dict-set-item-string ]
126     [ py-dict-get-item-string ] 2bi
127     py-dict-size
128 ] py-test
129
130 ! Nested tuples
131 [ 3 ] [
132     1 <py-tuple> dup 0 3 <py-tuple> py-tuple-set-item
133     0 py-tuple-get-item py-tuple-size
134 ] py-test
135
136 ! Round tripping!
137 [ { "foo" { 99 77 } } ] [ { "foo" { 99 77 } } >py py> ] py-test
138
139 [ H{ { "foo" "bar" } { 3 4 } } ] [
140     H{ { "foo" "bar" } { 3 4 } } >py py>
141 ] py-test
142
143 ! Kwargs
144 [ 2014 10 22 ] [
145     "datetime" py-import "date" getattr
146     { } >py H{ { "year" 2014 } { "month" 10 } { "day" 22 } } >py
147     call-object-full py>
148     [ year>> ] [ month>> ] [ day>> ] tri
149 ] py-test
150
151 ! Modules
152 [ t ] [
153     "os" py-import PyModule_GetDict dup Py_IncRef &Py_DecRef py-dict-size 100 >
154 ] py-test
155
156 ! CFunctions
157 { t } [
158     1234 <alien> "foo" f <PyMethodDef>
159     ml_flags>> METH_VARARGS METH_KEYWORDS bitor =
160 ] unit-test
161
162 { f 3 } [
163     1234 <alien> <py-cfunction>
164     [ "__doc__" getattr py> ] [ PyCFunction_GetFlags ] bi
165 ] py-test
166
167 { "cfunction" } [
168     1234 <alien> <py-cfunction>
169     ! Force nursery flush
170     10000 [ 1000 0xff <array> drop ] times
171     "__name__" getattr py>
172 ] py-test
173
174 { 3 } [
175     1234 <alien> <py-cfunction> drop always-destructors get length
176 ] py-test
177
178 ! Callbacks
179 : py-map ( -- alien )
180     "__builtin__" py-import "map" getattr ;
181
182 : py-map-call ( alien-cb -- seq )
183     [
184         <py-cfunction> py-map swap { 1 2 } >py 2array array>py-tuple f
185         call-object-full
186     ] with-callback py> ;
187
188 : always-33-fun ( -- alien )
189     [ 3drop 33 >py ] PyCallback ;
190
191 { V{ 33 33 } } [ always-33-fun py-map-call ] py-test
192
193 : id-fun ( -- alien )
194     [ drop nip py> first >py ] PyCallback ;
195
196 { V{ 1 2 } } [ id-fun py-map-call ] py-test