aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/ffi.old.lux
blob: e3ea06d487a47231a8dde7eafb4b19d9510e760a (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
(.require
 [library
  [lux (.except)
   [abstract
    [equivalence (.only Equivalence)]
    [monad (.only do)]]
   [control
    ["[0]" pipe]]
   [data
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format (.only format)]]]
   [math
    ["[0]" random]
    [number
     ["n" nat]
     ["i" int]
     ["f" frac]]]
   [meta
    ["[0]" type (.use "[1]#[0]" equivalence)]
    [macro
     ["[0]" template]]]
   [test
    ["_" property (.only Test)]]]]
 [\\library
  ["[0]" /]])

(/.import java/lang/Object
  "[1]::[0]")

(/.import java/lang/String
  "[1]::[0]")

(/.import java/lang/Exception
  "[1]::[0]"
  (new [java/lang/String]))

(/.import (java/lang/Class a)
  "[1]::[0]"
  (getName [] java/lang/String))

(/.class "final" (TestClass A) []
  ... Fields
  ("private" increase java/lang/Long)
  ("private" counter java/lang/Long)
  ... Methods
  ("public" [] (new [increase java/lang/Long counter java/lang/Long]) []
   (exec
     (:= ::increase increase)
     (:= ::counter counter)
     []))
  ("public" (currentC self []) java/lang/Long
   ::counter)
  ("public" (upC self []) void
   (:= ::counter (i.+ ::increase ::counter)))
  ("public" (downC self []) void
   (:= ::counter (i.- ::increase ::counter))))

(/.import (test/lux/ffi/TestClass a)
  "[1]::[0]"
  (new [java/lang/Long java/lang/Long])
  (currentC [] java/lang/Long)
  (upC [] void)
  (downC [] void))

(/.interface TestInterface
  ([] current [] java/lang/Long "throws" [java/lang/Exception])
  ([] up [] test/lux/ffi/TestInterface "throws" [java/lang/Exception])
  ([] down [] test/lux/ffi/TestInterface "throws" [java/lang/Exception]))

(/.import test/lux/ffi/TestInterface
  "[1]::[0]"
  (current [] java/lang/Long)
  (up [] java/lang/Long)
  (down [] java/lang/Long))

(def (test_object increase counter)
  (-> Int Int test/lux/ffi/TestInterface)
  (/.object [] [test/lux/ffi/TestInterface]
    []
    (test/lux/ffi/TestInterface
     [] (current self [])
     java/lang/Long
     counter)
    (test/lux/ffi/TestInterface
     [] (up self [])
     test/lux/ffi/TestInterface
     (test_object increase (i.+ increase counter)))
    (test/lux/ffi/TestInterface
     [] (down self [])
     test/lux/ffi/TestInterface
     (test_object increase (i.- increase counter)))))

(def conversions
  Test
  (do [! random.monad]
    [long random.int
     int (at ! each (|>> /.long_to_int) random.int)
     char (at ! each (|>> /.long_to_int /.int_to_char) random.int)
     double (|> random.frac
                (random.only (|>> f.not_a_number? not)))
     float (|> random.frac
               (random.only (|>> f.not_a_number? not))
               (at ! each (|>> /.double_to_float)))]
    (`` (all _.and
             (,, (with_template [<=> <sample> <to> <from>]
                   [(_.coverage [<to> <from>]
                      (or (|> <sample> <to> <from> (<=> <sample>))
                          (let [capped (|> <sample> <to> <from>)]
                            (|> capped <to> <from> (<=> capped)))))]

                   [i.= long /.long_to_byte /.byte_to_long]
                   [i.= long /.long_to_short /.short_to_long]
                   [i.= long /.long_to_int /.int_to_long]
                   [i.= long /.long_to_float /.float_to_long]
                   [i.= long /.long_to_double /.double_to_long]

                   [f.= double /.double_to_float /.float_to_double]
                   [f.= double /.double_to_int /.int_to_double]
                   ))
             (,, (with_template [<to> <from>]
                   [(_.coverage [<to>]
                      (or (|> int <to> <from> (i.= (/.int_to_long int)))
                          (let [capped (|> int <to> <from>)]
                            (|> capped /.long_to_int <to> <from> (i.= capped)))))]

                   [/.int_to_byte /.byte_to_long]
                   [/.int_to_short /.short_to_long]
                   [/.int_to_char /.char_to_long]
                   ))
             (,, (with_template [<sample> <to> <from>]
                   [(_.coverage [<to> <from>]
                      (or (|> <sample> <to> <from> /.float_to_double (f.= (/.float_to_double <sample>)))
                          (let [capped (|> <sample> <to> <from>)]
                            (|> capped <to> <from> /.float_to_double (f.= (/.float_to_double capped))))))]

                   [float /.float_to_int /.int_to_float]
                   ))
             (,, (with_template [<to> <from>]
                   [(_.coverage [<to>]
                      (or (|> char <to> <from> (i.= (|> char /.char_to_int /.int_to_long)))
                          (let [capped (|> char <to> <from>)]
                            (|> capped /.long_to_int /.int_to_char <to> <from> (i.= capped)))))]

                   [/.char_to_byte /.byte_to_long]
                   [/.char_to_short /.short_to_long]
                   ))
             (_.coverage [/.char_to_long]
               (with_expansions [<to> /.int_to_char
                                 <from> /.char_to_long]
                 (`` (or (|> int <to> <from> (i.= (/.int_to_long int)))
                         (let [capped (|> int <to> <from>)]
                           (|> capped /.long_to_int <to> <from> (i.= capped)))))))
             (_.coverage [/.char_to_int]
               (with_expansions [<to> /.int_to_char
                                 <from> /.char_to_int]
                 (`` (or (|> int <to> <from> /.int_to_long (i.= (/.int_to_long int)))
                         (let [capped (|> int <to> <from>)]
                           (|> capped <to> <from> /.int_to_long (i.= (/.int_to_long capped))))))))
             ))))

(def arrays
  Test
  (do [! random.monad]
    [size (|> random.nat (at ! each (|>> (n.% 100) (n.max 1))))
     idx (|> random.nat (at ! each (n.% size)))
     value random.int]
    (all _.and
         (_.coverage [/.array /.length]
           (n.= size (/.length (/.array java/lang/Long size))))
         (_.coverage [/.write! /.read!]
           (|> (/.array java/lang/Long size)
               (/.write! idx value)
               (/.read! idx)
               (i.= value)))
         )))

(def null
  Test
  (do random.monad
    [sample (random.ascii 1)]
    (all _.and
         (_.coverage [/.null /.null?]
           (and (/.null? (/.null))
                (not (/.null? sample))))
         (_.coverage [/.???]
           (and (|> (is (Maybe java/lang/Object) (/.??? (/.null)))
                    (pipe.when
                      {.#None}
                      true
                      
                      _
                      false))
                (|> (is (Maybe java/lang/Object) (/.??? sample))
                    (pipe.when
                      {.#Some _}
                      true
                      
                      _
                      false))))
         (_.coverage [/.!!!]
           (and (/.null? (/.!!! (/.??? (/.null))))
                (not (/.null? (/.!!! (/.??? sample))))))
         )))

(def miscellaneous
  Test
  (do random.monad
    [sample (random.ascii 1)
     counter random.int
     increase random.int]
    (all _.and
         (_.coverage [/.as]
           (and (when (/.as java/lang/String sample) {.#Some _} true {.#None} false)
                (when (/.as java/lang/Long sample) {.#Some _} false {.#None} true)
                (when (/.as java/lang/Object sample) {.#Some _} true {.#None} false)
                (when (/.as java/lang/Object (/.null)) {.#Some _} false {.#None} true)))
         (_.coverage [/.synchronized]
           (/.synchronized sample true))
         (_.coverage [/.class_for /.import]
           (|> (/.class_for java/lang/Class)
               java/lang/Class::getName
               (text#= "java.lang.Class")))
         (_.coverage [/.class /.to]
           (|> (/.to (test/lux/ffi/TestClass::new increase counter)
                     (test/lux/ffi/TestClass::upC)
                     (test/lux/ffi/TestClass::upC)
                     (test/lux/ffi/TestClass::downC))
               test/lux/ffi/TestClass::currentC
               (i.= (i.+ increase counter))))
         (_.coverage [/.interface /.object]
           (|> (..test_object increase counter)
               test/lux/ffi/TestInterface::up
               test/lux/ffi/TestInterface::up
               test/lux/ffi/TestInterface::down
               test/lux/ffi/TestInterface::current
               (i.= (i.+ increase counter))))
         (_.coverage [/.type]
           (and (type#= (Nominal "java.lang.Char")
                        (/.type java/lang/Char))
                (type#= (Nominal "java.util.List" [(Nominal "java.lang.Byte")])
                        (/.type (java/util/List java/lang/Byte)))))
         )))

(def .public test
  (<| (_.covering /._)
      (all _.and
           ..conversions
           ..arrays
           ..null
           ..miscellaneous
           )))