2019-02-07 14:57:35 +01:00
|
|
|
;; Copyright 2019 the authors.
|
2017-04-27 23:16:57 +02:00
|
|
|
;; This file is part of Hy, which is free software licensed under the Expat
|
|
|
|
;; license. See the LICENSE.
|
|
|
|
|
2013-04-01 23:51:28 +02:00
|
|
|
(setv square (fn [x]
|
2013-04-28 16:31:31 +02:00
|
|
|
(* x x)))
|
2013-03-06 04:15:45 +01:00
|
|
|
|
|
|
|
|
2013-04-01 23:51:28 +02:00
|
|
|
(setv test_basic_math (fn []
|
2013-04-28 16:31:31 +02:00
|
|
|
"NATIVE: Test basic math."
|
|
|
|
(assert (= (+ 2 2) 4))))
|
2013-03-06 04:15:45 +01:00
|
|
|
|
2013-04-01 23:51:28 +02:00
|
|
|
(setv test_mult (fn []
|
2013-04-28 16:31:31 +02:00
|
|
|
"NATIVE: Test multiplication."
|
2013-12-15 07:12:57 +01:00
|
|
|
(assert (= 4 (square 2)))
|
|
|
|
(assert (= 8 (* 8)))
|
|
|
|
(assert (= 1 (*)))))
|
2013-03-06 04:15:45 +01:00
|
|
|
|
|
|
|
|
2013-04-01 23:51:28 +02:00
|
|
|
(setv test_sub (fn []
|
2013-04-28 16:31:31 +02:00
|
|
|
"NATIVE: Test subtraction"
|
|
|
|
(assert (= 4 (- 8 4)))
|
|
|
|
(assert (= -8 (- 8)))))
|
2013-03-06 04:15:45 +01:00
|
|
|
|
|
|
|
|
2013-04-01 23:51:28 +02:00
|
|
|
(setv test_add (fn []
|
2013-04-28 16:31:31 +02:00
|
|
|
"NATIVE: Test addition"
|
2013-12-15 07:12:57 +01:00
|
|
|
(assert (= 4 (+ 1 1 1 1)))
|
|
|
|
(assert (= 8 (+ 8)))
|
|
|
|
(assert (= 0 (+)))))
|
2013-03-06 04:15:45 +01:00
|
|
|
|
|
|
|
|
2016-09-20 22:05:52 +02:00
|
|
|
(defn test-add-unary []
|
|
|
|
"NATIVE: test that unary + calls __pos__"
|
|
|
|
|
|
|
|
(defclass X [object]
|
2019-07-02 18:02:03 +02:00
|
|
|
(defn __pos__ [self] "called __pos__"))
|
2016-09-20 22:05:52 +02:00
|
|
|
(assert (= (+ (X)) "called __pos__"))
|
|
|
|
|
|
|
|
; Make sure the shadowed version works, too.
|
|
|
|
(setv f +)
|
|
|
|
(assert (= (f (X)) "called __pos__")))
|
|
|
|
|
|
|
|
|
2013-04-01 23:51:28 +02:00
|
|
|
(setv test_div (fn []
|
2013-04-28 16:31:31 +02:00
|
|
|
"NATIVE: Test division"
|
|
|
|
(assert (= 25 (/ 100 2 2)))
|
|
|
|
; Commented out until float constants get implemented
|
|
|
|
; (assert (= 0.5 (/ 1 2)))
|
|
|
|
(assert (= 1 (* 2 (/ 1 2))))))
|
2013-03-19 02:46:58 +01:00
|
|
|
|
2013-04-11 10:09:15 +02:00
|
|
|
(setv test_int_div (fn []
|
2013-04-28 16:31:31 +02:00
|
|
|
"NATIVE: Test integer division"
|
|
|
|
(assert (= 25 (// 101 2 2)))))
|
2013-04-11 10:09:15 +02:00
|
|
|
|
2013-03-19 02:46:58 +01:00
|
|
|
(defn test-modulo []
|
|
|
|
"NATIVE: test mod"
|
|
|
|
(assert (= (% 10 2) 0)))
|
2013-04-14 17:37:18 +02:00
|
|
|
|
|
|
|
(defn test-pow []
|
|
|
|
"NATIVE: test pow"
|
|
|
|
(assert (= (** 10 2) 100)))
|
|
|
|
|
|
|
|
(defn test-lshift []
|
|
|
|
"NATIVE: test lshift"
|
|
|
|
(assert (= (<< 1 2) 4)))
|
|
|
|
|
|
|
|
(defn test-rshift []
|
|
|
|
"NATIVE: test lshift"
|
|
|
|
(assert (= (>> 8 1) 4)))
|
|
|
|
|
|
|
|
(defn test-bitor []
|
|
|
|
"NATIVE: test lshift"
|
|
|
|
(assert (= (| 1 2) 3)))
|
|
|
|
|
|
|
|
(defn test-bitxor []
|
|
|
|
"NATIVE: test xor"
|
|
|
|
(assert (= (^ 1 2) 3)))
|
|
|
|
|
|
|
|
(defn test-bitand []
|
|
|
|
"NATIVE: test lshift"
|
|
|
|
(assert (= (& 1 2) 0)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-add []
|
|
|
|
"NATIVE: test augassign add"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 1)
|
|
|
|
(+= x 41)
|
|
|
|
(assert (= x 42)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-sub []
|
|
|
|
"NATIVE: test augassign sub"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 1)
|
|
|
|
(-= x 41)
|
|
|
|
(assert (= x -40)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-mult []
|
|
|
|
"NATIVE: test augassign mult"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 1)
|
|
|
|
(*= x 41)
|
|
|
|
(assert (= x 41)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-div []
|
|
|
|
"NATIVE: test augassign div"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 42)
|
|
|
|
(/= x 2)
|
|
|
|
(assert (= x 21)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-floordiv []
|
|
|
|
"NATIVE: test augassign floordiv"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 42)
|
|
|
|
(//= x 2)
|
|
|
|
(assert (= x 21)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-mod []
|
|
|
|
"NATIVE: test augassign mod"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 42)
|
|
|
|
(%= x 2)
|
|
|
|
(assert (= x 0)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-pow []
|
|
|
|
"NATIVE: test augassign pow"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 2)
|
|
|
|
(**= x 3)
|
|
|
|
(assert (= x 8)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-lshift []
|
|
|
|
"NATIVE: test augassign lshift"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 2)
|
|
|
|
(<<= x 2)
|
|
|
|
(assert (= x 8)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-rshift []
|
|
|
|
"NATIVE: test augassign rshift"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 8)
|
|
|
|
(>>= x 1)
|
|
|
|
(assert (= x 4)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-bitand []
|
|
|
|
"NATIVE: test augassign bitand"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 8)
|
|
|
|
(&= x 1)
|
|
|
|
(assert (= x 0)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-bitor []
|
|
|
|
"NATIVE: test augassign bitand"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 0)
|
|
|
|
(|= x 2)
|
|
|
|
(assert (= x 2)))
|
2013-04-14 17:30:12 +02:00
|
|
|
|
|
|
|
(defn test-augassign-bitxor []
|
|
|
|
"NATIVE: test augassign bitand"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv x 1)
|
|
|
|
(^= x 1)
|
|
|
|
(assert (= x 0)))
|
2013-08-21 01:09:03 +02:00
|
|
|
|
|
|
|
(defn overflow-int-to-long []
|
|
|
|
"NATIVE: test if int does not raise an overflow exception"
|
|
|
|
(assert (integer? (+ 1 1000000000000000000000000))))
|
2015-04-13 05:35:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
(defclass HyTestMatrix [list]
|
2019-07-02 18:02:03 +02:00
|
|
|
(defn --matmul-- [self other]
|
2019-07-02 18:10:01 +02:00
|
|
|
(setv n (len self)
|
|
|
|
m (len (. other [0]))
|
|
|
|
result [])
|
|
|
|
(for [i (range m)]
|
|
|
|
(setv result-row [])
|
|
|
|
(for [j (range n)]
|
|
|
|
(setv dot-product 0)
|
|
|
|
(for [k (range (len (. self [0])))]
|
|
|
|
(+= dot-product (* (. self [i] [k])
|
|
|
|
(. other [k] [j]))))
|
|
|
|
(.append result-row dot-product))
|
|
|
|
(.append result result-row))
|
|
|
|
result))
|
2015-04-13 05:35:08 +02:00
|
|
|
|
2018-01-05 21:51:00 +01:00
|
|
|
(setv first-test-matrix (HyTestMatrix [[1 2 3]
|
|
|
|
[4 5 6]
|
|
|
|
[7 8 9]]))
|
2015-04-13 05:35:08 +02:00
|
|
|
|
2018-01-05 21:51:00 +01:00
|
|
|
(setv second-test-matrix (HyTestMatrix [[2 0 0]
|
|
|
|
[0 2 0]
|
|
|
|
[0 0 2]]))
|
2015-04-13 05:35:08 +02:00
|
|
|
|
2018-01-05 21:51:00 +01:00
|
|
|
(setv product-of-test-matrices (HyTestMatrix [[ 2 4 6]
|
|
|
|
[ 8 10 12]
|
|
|
|
[14 16 18]]))
|
2015-04-13 05:35:08 +02:00
|
|
|
|
|
|
|
(defn test-matmul []
|
|
|
|
"NATIVE: test matrix multiplication"
|
2019-05-20 20:46:52 +02:00
|
|
|
(assert (= (@ first-test-matrix second-test-matrix)
|
|
|
|
product-of-test-matrices)))
|
2015-04-13 05:35:08 +02:00
|
|
|
|
|
|
|
(defn test-augassign-matmul []
|
|
|
|
"NATIVE: test augmented-assignment matrix multiplication"
|
2017-02-04 18:07:27 +01:00
|
|
|
(setv matrix first-test-matrix
|
2015-08-17 09:07:32 +02:00
|
|
|
matmul-attempt (try (@= matrix second-test-matrix)
|
2017-02-04 18:07:27 +01:00
|
|
|
(except [e [Exception]] e)))
|
2019-05-20 20:46:52 +02:00
|
|
|
(assert (= product-of-test-matrices matrix)))
|