File: operators.hy

package info (click to toggle)
hy 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,632 kB
  • sloc: python: 7,299; makefile: 38; sh: 27
file content (395 lines) | stat: -rw-r--r-- 10,198 bytes parent folder | download | duplicates (2)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
(import pytest)
(import hy.pyops *)

(defmacro op-and-shadow-test [op #* body]
  ; Creates two tests with the given `body`, one where all occurrences
  ; of the symbol `f` are syntactically replaced with `op` (a test of
  ; the real operator), and one where the body is preceded by an
  ; assignment of `op` to `f` (a test of the shadow operator).
  ;
  ; `op` can also be a list of operators, in which case two tests are
  ; created for each operator.
  (import collections.abc [Iterable])
  (setv defns [])
  (for [o (if (isinstance op hy.models.Symbol) [op] op)]
    (defn replace [x]
      (cond
        (= x 'f)
          o
        (and (isinstance x Iterable) (not (isinstance x #(str bytes))))
          ((type x) (map replace x))
        True
          x))
    (.append defns `(defn ~(hy.models.Symbol (+ "test_operator_" o "_real")) []
      (setv f-name ~(hy.models.String o))
      ~@(replace body)))
    (.append defns `(defn ~(hy.models.Symbol (+ "test_operator_" o "_shadow")) []
      (setv f-name ~(hy.models.String o))
      (setv f ~o)
      ~@body)))
  `(do ~@defns))

(defmacro forbid [expr]
  (setv e (hy.gensym))
  `(do
     (with [~e (pytest.raises Exception)]
       (hy.eval '~expr))
     (assert (issubclass (. ~e type)
       #(TypeError SyntaxError hy.errors.HyMacroExpansionError)))))

(op-and-shadow-test +

  (assert (= (f) 0))

  (defclass C [object] (defn __pos__ [self] "called __pos__"))
  (assert (= (f (C)) "called __pos__"))

  (assert (= (f 1 2) 3))
  (assert (= (f 1 2 3 4) 10))
  (assert (= (f 1 2 3 4 5) 15))
  (assert (= (f #* [1 2 3 4 5]) 15))

  ; with strings
  (assert (= (f "a" "b" "c")
             "abc"))
  ; with lists
  (assert (= (f ["a"] ["b"] ["c"])
             ["a" "b" "c"])))


(op-and-shadow-test -
  (forbid (f))
  (assert (= (f 1) -1))
  (assert (= (f 2 1) 1))
  (assert (= (f 2 1 1) 0)))


(op-and-shadow-test *
  (assert (= (f) 1))
  (assert (= (f 3) 3))
  (assert (= (f 3 3) 9))
  (assert (= (f 2 3 4) 24))
  (assert (= (f "ke" 4) "kekekeke"))
  (assert (= (f [1 2 3] 2) [1 2 3 1 2 3])))


(op-and-shadow-test **
  (forbid (f))
  (forbid (f 1))
  (assert (= (f 3 2) 9))
  (assert (= (f 5 4 3 2) (** 5 (** 4 (** 3 2))))))
    ; Exponentiation is right-associative.


(op-and-shadow-test /
  (forbid (f))
  (assert (= (f 2) .5))
  (assert (= (f 3 2) 1.5))
  (assert (= (f 8 2) 4))
  (assert (= (f 8 2 2) 2))
  (assert (= (f 8 2 2 2) 1)))


(op-and-shadow-test //
  (forbid (f))
  (forbid (f 1))
  (assert (= (f 16 5) 3))
  (assert (= (f 8 2) 4))
  (assert (= (f 8 2 2) 2)))


(op-and-shadow-test %
  (forbid (f))
  (forbid (f 1))
  (assert (= (f 16 5) 1))
  (assert (= (f 8 2) 0))
  (assert (= (f "aa %s bb" 15) "aa 15 bb"))
  (assert (= (f "aa %s bb %s cc" #("X" "Y")) "aa X bb Y cc"))
  (forbid (f 1 2 3)))


(op-and-shadow-test @
  (defclass C [object]
    (defn __init__ [self content] (setv self.content content))
    (defn __matmul__ [self other] (C (+ self.content other.content))))
  (forbid (f))
  (assert (do (setv c (C "a")) (is (f c) c)))
  (assert (= (. (f (C "b") (C "c")) content) "bc"))
  (assert (= (. (f (C "d") (C "e") (C "f")) content) "def")))


(op-and-shadow-test <<
  (forbid (f))
  (forbid (f 1))
  (assert (= (f 0b101 2) 0b10100))
  (assert (= (f 0b101 2 3) 0b10100000)))


(op-and-shadow-test >>
  (forbid (f))
  (forbid (f 1))
  (assert (= (f 0b101 2) 0b1))
  (assert (= (f 0b101000010 2 3) 0b1010)))


(op-and-shadow-test &
  (forbid (f))
    ; Binary AND has no identity element for the set of all
    ; nonnegative integers, because we can't have a 1 in every bit
    ; when there are infinitely many bits.
  (assert (= (f 17) 17))
  (assert (= (f 0b0011 0b0101) 0b0001))
  (assert (= (f 0b111 0b110 0b100) 0b100)))


(op-and-shadow-test |
  (assert (= (f) 0))
  (assert (= (f 17) 17))
  (assert (= (f 0b0011 0b0101) 0b0111))
  (assert (= (f 0b11100 0b11000 0b10010) 0b11110)))


(op-and-shadow-test ^
  (forbid (f))
  (forbid (f 17))
  (assert (= (f 0b0011 0b0101) 0b0110))
  (forbid (f 0b111 0b110 0b100)))
    ; `xor` with 3 arguments is kill (https://github.com/hylang/hy/pull/1102),
    ; so we don't allow `^` with 3 arguments, either.


(op-and-shadow-test bnot
  (forbid (f))
  (assert (= (& (f 0b00101111) 0xFF)
                   0b11010000))
  (forbid (f 0b00101111 0b11010000)))


(op-and-shadow-test <

  (forbid (f))
  (assert (is (f "hello") True))
  (assert (is (f 1 2) True))
  (assert (is (f 2 1) False))
  (assert (is (f 1 1) False))
  (assert (is (f 1 2 3) True))
  (assert (is (f 3 2 1) False))
  (assert (is (f 1 3 2) False))
  (assert (is (f 1 2 2) False))

  ; Make sure chained comparisons use `and`, not `&`.
  ; https://github.com/hylang/hy/issues/1191
  (defclass C [object]
    (defn __init__ [self x]
      (setv self.x x))
    (defn __lt__ [self other]
      self.x))
  (assert (= (f (C "a") (C "b") (C "c")) "b")))


(op-and-shadow-test >
  (forbid (f))
  (assert (is (f "hello") True))
  (assert (is (f 1 2) False))
  (assert (is (f 2 1) True))
  (assert (is (f 1 1) False))
  (assert (is (f 1 2 3) False))
  (assert (is (f 3 2 1) True))
  (assert (is (f 1 3 2) False))
  (assert (is (f 2 1 1) False)))


(op-and-shadow-test <=
  (forbid (f))
  (assert (is (f "hello") True))
  (assert (is (f 1 2) True))
  (assert (is (f 2 1) False))
  (assert (is (f 1 1) True))
  (assert (is (f 1 2 3) True))
  (assert (is (f 3 2 1) False))
  (assert (is (f 1 3 2) False))
  (assert (is (f 1 2 2) True)))


(op-and-shadow-test >=
  (forbid (f))
  (assert (is (f "hello") True))
  (assert (is (f 1 2) False))
  (assert (is (f 2 1) True))
  (assert (is (f 1 1) True))
  (assert (is (f 1 2 3) False))
  (assert (is (f 3 2 1) True))
  (assert (is (f 1 3 2) False))
  (assert (is (f 2 1 1) True)))


(op-and-shadow-test [= is]
  (forbid (f))

  (assert (is (f "hello") True))

  ; Unary comparison operators, despite always returning True,
  ; should evaluate their argument.
  (setv p "a")
  (assert (is (f (do (setv p "b") "hello")) True))
  (assert (= p "b"))

  (defclass C)
  (setv x (get {"is" (C) "=" 0} f-name))
  (setv y (get {"is" (C) "=" 1} f-name))
  (assert (is (f x x) True))
  (assert (is (f y y) True))
  (assert (is (f x y) False))
  (assert (is (f y x) False))
  (assert (is (f x x x x x) True))
  (assert (is (f x x x y x) False))

  (setv n None)
  (assert (is (f n None) True))
  (assert (is (f n "b") False)))


(op-and-shadow-test [!= is-not]
  (forbid (f))
  (forbid (f "hello"))
  (defclass C)
  (setv x (get {"is-not" (C) "!=" 0} f-name))
  (setv y (get {"is-not" (C) "!=" 1} f-name))
  (setv z (get {"is-not" (C) "!=" 2} f-name))
  (assert (is (f x x) False))
  (assert (is (f y y) False))
  (assert (is (f x y) True))
  (assert (is (f y x) True))
  (assert (is (f x y z) True))
  (assert (is (f x x x) False))
  (assert (is (f x y x) True))
  (assert (is (f x x y) False)))


(op-and-shadow-test and
  (assert (is (f) True))
  (assert (= (f 17) 17))
  (assert (= (f 1 2) 2))
  (assert (= (f 1 0) 0))
  (assert (= (f 0 2) 0))
  (assert (= (f 0 0) 0))
  (assert (= (f 1 2 3) 3))
  (assert (= (f 1 0 3) 0))
  (assert (= (f "a" 1 True [1]) [1]))
  (assert (= (f #* [1 2 3]) 3)))


(op-and-shadow-test or
  (assert (is (f) None))
  (assert (= (f 17) 17))
  (assert (= (f 1 2) 1))
  (assert (= (f 1 0) 1))
  (assert (= (f 0 2) 2))
  (assert (= (f 0 0) 0))
  (assert (= (f 1 2 3) 1))
  (assert (= (f 0 0 3) 3))
  (assert (= (f "" None 0 False []) [])))


(op-and-shadow-test not
  (forbid (f))
  (assert (is (f "hello") False))
  (assert (is (f 0) True))
  (assert (is (f None) True)))


(op-and-shadow-test [in not-in]
  (forbid (f))
  (forbid (f 3))
  (assert (is (f 3 [1 2]) (!= f-name "in")))
  (assert (is (f 2 [1 2]) (= f-name "in")))
  (assert (is (f 2 [1 2] [[1 2] 3]) (= f-name "in")))
  (assert (is (f 3 [1 2] [[2 2] 3]) (!= f-name "in"))))


(op-and-shadow-test [get]
  (forbid (f))
  (forbid (f "hello"))
  (assert (= (f "hello" 1) "e"))
  (assert (= (f [[1 2 3] [4 5 6] [7 8 9]] 1 2) 6))
  (assert (= (f {"x" {"y" {"z" 12}}} "x" "y" "z") 12)))

(defn test-setv-get []
  (setv foo [0 1 2])
  (setv (get foo 0) 12)
  (assert (= (get foo 0) 12)))


(op-and-shadow-test [cut]
  (forbid (f))
  (setv x "abcdef")
  (assert (= (f x) "abcdef"))
  (assert (= (f x 3) "abc"))
  (assert (= (f x -2) "abcd"))
  (assert (= (f x 3 None) "def"))
  (assert (= (f x -2 None) "ef"))
  (assert (= (f x 3 5) "de"))
  (assert (= (f x 0 None 2) "ace"))
  (assert (= (list (f (range 100) 20 80 13)) [20 33 46 59 72])))

(defn test-setv-cut []
  (setv foo (list (range 20)))
  (setv (cut foo 2 18 3) (* [0] 6))
  (assert (=
    foo
    [0 1 0 3 4 0 6 7 0 9 10 0 12 13 0 15 16 0 18 19])))


(defn test-chained-comparison []
  (assert (chainc 2 = (+ 1 1) = (- 3 1)))
  (assert (not (chainc 2 = (+ 1 1) = (+ 3 1))))

  (assert (chainc 2 = 2 > 1))
  (assert (chainc 2 = (+ 1 1) > 1))
  (setv x 2)
  (assert (chainc 2 = x > 1))
  (assert (chainc 2 = x > (> 4 3)))
  (assert (not (chainc (> 4 3) = x > 1)))

  (assert (chainc 1 in [1] in [[1] [2 3]] not-in [5]))
  (assert (not (chainc 1 in [1] not-in [[1] [2 3]] not-in [5]))))


(defn test-augassign []
  (setv b 2  c 3  d 4)
  (defmacro same-as [expr1 expr2 expected-value]
    `(do
      (setv a 4)
      ~expr1
      (setv expr1-value a)
      (setv a 4)
      ~expr2
      (assert (= expr1-value a ~expected-value))))
  (same-as (+= a b c d) (+= a (+ b c d)) 13)
  (same-as (-= a b c d) (-= a (+ b c d)) -5)
  (same-as (*= a b c d) (*= a (* b c d)) 96)
  (same-as (**= a b c) (**= a (** b c)) 65,536)
  (same-as (/= a b c d) (/= a (* b c d)) (/ 1 6))
  (same-as (//= a b c d) (//= a (* b c d)) 0)
  (same-as (<<= a b c d) (<<= a (+ b c d)) 0b10_00000_00000)
  (same-as (>>= a b c d) (>>= a (+ b c d)) 0)
  (same-as (&= a b c d) (&= a (& b c d)) 0)
  (same-as (|= a b c d) (|= a (| b c d)) 0b111)

  (defclass C [object]
    (defn __init__ [self content] (setv self.content content))
    (defn __matmul__ [self other] (C (+ self.content other.content))))
  (setv  a (C "a")  b (C "b")  c (C "c")  d (C "d"))
  (@= a b c d)
  (assert (= a.content "abcd"))
  (setv a (C "a"))
  (@= a (@ b c d))
  (assert (= a.content "abcd"))

  (setv a 15)
  (%= a 9)
  (assert (= a 6))

  (setv a 0b1100)
  (^= a 0b1010)
  (assert (= a 0b0110)))