hy/tests/native_tests/contrib/sequences.hy

95 lines
3.0 KiB
Hy
Raw Normal View History

2019-02-07 14:57:35 +01:00
;; Copyright 2019 the authors.
;; This file is part of Hy, which is free software licensed under the Expat
;; license. See the LICENSE.
2016-11-08 05:28:12 +01:00
(require [hy.contrib.sequences [seq defseq]])
(import [hy.contrib.sequences [Sequence end-sequence]])
(defn test-infinite-sequence []
"NATIVE: test creating infinite sequence"
(assert (= (list (take 5 (seq [n] n)))
[0 1 2 3 4])))
(defn test-indexing-sequence []
"NATIVE: test indexing sequence"
(defseq shorty [n]
(cond [(< n 10) n]
2016-11-30 22:45:21 +01:00
[True (end-sequence)]))
(setv 0-to-9 (list (range 10)))
2016-11-08 05:28:12 +01:00
(assert (= (get shorty 0)
(get 0-to-9 0))
2016-11-08 05:28:12 +01:00
"getting first element failed")
(assert (= (get shorty 5)
(get 0-to-9 5))
2016-11-08 05:28:12 +01:00
"getting 5th element failed")
(assert (= (get shorty -1)
(get 0-to-9 -1))
2016-11-08 05:28:12 +01:00
"getting element -1 failed"))
(defn test-slicing-sequence []
"NATIVE: test slicing sequence"
(defseq shorty [n]
(cond [(< n 10) n]
2016-11-30 22:45:21 +01:00
[True (end-sequence)]))
(setv 0-to-9 (list (range 10)))
2016-11-08 05:28:12 +01:00
(assert (= (first shorty)
(first 0-to-9))
2016-11-08 05:28:12 +01:00
"getting first failed")
(assert (= (list (rest shorty))
(list (rest 0-to-9)))
2016-11-08 05:28:12 +01:00
"getting rest failed")
(assert (= (list (cut shorty 2 6))
(list (cut 0-to-9 2 6)))
2016-11-08 05:28:12 +01:00
"cutting 2-6 failed")
(assert (= (list (cut shorty 2 8 2))
(list (cut 0-to-9 2 8 2)))
2016-11-08 05:28:12 +01:00
"cutting 2-8-2 failed")
(assert (= (list (cut shorty 8 2 -2))
(list (cut 0-to-9 8 2 -2)))
2016-11-08 05:28:12 +01:00
"negative cut failed"))
(defn test-recursive-sequence []
"NATIVE: test defining a recursive sequence"
(defseq fibonacci [n]
(cond [(= n 0) 0]
[(= n 1) 1]
2016-11-30 22:45:21 +01:00
[True (+ (get fibonacci (- n 1))
2016-11-08 05:28:12 +01:00
(get fibonacci (- n 2)))]))
(assert (= (first fibonacci)
0)
"first element of fibonacci didn't match")
(assert (= (second fibonacci)
2016-12-08 01:04:59 +01:00
1)
2016-11-08 05:28:12 +01:00
"second element of fibonacci didn't match")
(assert (= (get fibonacci 40)
102334155)
"40th element of fibonacci didn't match")
(assert (= (list (take 9 fibonacci))
[0 1 1 2 3 5 8 13 21])
"taking 8 elements of fibonacci didn't match"))
(defn test-nested-functions []
"NATIVE: test that defining nested functions is possible"
(defseq primes [n]
"infinite sequence of prime numbers"
(defn divisible? [n prevs]
"is n divisible by any item in prevs?"
(any (map (fn [x]
(not (% n x)))
prevs)))
(defn previous-primes [n]
"previous prime numbers"
(take (dec n) primes))
(defn next-possible-prime [n]
"next possible prime after nth prime"
(inc (get primes (dec n))))
(cond [(= n 0) 2]
2016-11-30 22:45:21 +01:00
[True (do (setv guess (next-possible-prime n))
(while (divisible? guess (previous-primes n))
(setv guess (inc guess)))
guess)]))
(assert (= (list (take 10 primes))
[2 3 5 7 11 13 17 19 23 29])
"prime sequence didn't match"))