www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

trivial-test.rkt (3401B)


      1 #lang typed/racket
      2 (require "../trivial.rkt"
      3          "rackunit-typechecking.rkt"
      4          typed/rackunit)
      5 
      6 ; testing #%datum (check no loop)
      7 (check-equal? 9 9)
      8 (check-type 9 : (Int 9))
      9 (check-type "9" : Any)
     10 
     11 (define vec10 (build-vector 10 (lambda (x) x)))
     12 
     13 ;; testing make-vector and vector-ref
     14 (check-equal? (make-vector 10) (make-vector 10))
     15 (check-type (make-vector 10) : (Vec 10))
     16 (check-equal? (vector-ref vec10 9) 9)
     17 (typecheck-fail (vector-ref (make-vector 10) 10)
     18  #:with-msg "index is out of range, given 10, valid range: \\[0, 9\\]")
     19 (typecheck-fail (vector-ref vec10 10)
     20  #:with-msg "index is out of range, given 10, valid range: \\[0, 9\\]")
     21 (typecheck-fail (vector-ref vec10 -1)
     22  #:with-msg "index is out of range, given -1, valid range: \\[0, 9\\]")
     23 
     24 ;; λ
     25 ;; TODO: resolve clash between TR and my annotations
     26 ;; for now, only allow TR annotations
     27 
     28 (check-equal? (procedure? (tr:λ (x) x)) #t)
     29 (check-equal? (procedure? (tr:λ ([x : tr:Any]) x)) #t)
     30 (check-equal? (procedure? (λ (x) x)) #t)
     31 (check-equal? (procedure? (λ ([v : VectorTop]) v)) #t)
     32 
     33 (define vec-ref9
     34   (λ ([v : VectorTop]) (vector-ref v 9)))
     35 (define vec-ref10
     36   (λ ([v : VectorTop]) (vector-ref v 10)))
     37 
     38 (check-equal? (procedure? vec-ref10) #t)
     39 
     40 (check-equal? (vec-ref9 vec10) 9)
     41 
     42 (typecheck-fail
     43  (vec-ref10 vec10)
     44  #:with-msg "index is out of range, given 10, valid range: \\[0, 9\\]")
     45 
     46 (check-equal?
     47  (vec-ref9 (make-vector 10))
     48  0)
     49 
     50 (typecheck-fail
     51  ((λ ([v : VectorTop]) (vector-ref v -1))
     52   (make-vector 10))
     53  #:with-msg "index is out of range, given -1, valid range: \\[0,.*")
     54  
     55 (check-equal? (vec-ref9 vec10) 9)
     56 
     57 (define (vec-ref8 [v : VectorTop])
     58   (vector-ref v 8))
     59 
     60 (typecheck-fail
     61  (vec-ref8 (make-vector 8))
     62  #:with-msg "index is out of range, given 8, valid range: \\[0, 7\\]")
     63 
     64 (define (my-vec-ref [v : VectorTop] [n : Integer])
     65   (vector-ref v n))
     66  
     67 (typecheck-fail
     68  (my-vec-ref vec10 10)
     69  #:with-msg "index is out of range, given 10, valid range: \\[0, 9\\]")
     70 
     71 (typecheck-fail
     72  (my-vec-ref vec10 -1)
     73  #:with-msg "index is out of range, given -1, valid range: \\[0, 9\\]")
     74 
     75 (check-equal?
     76  (my-vec-ref vec10 9)
     77  9)
     78 
     79 ;; add1
     80 (check-equal? (add1 1) 2)
     81 (check-type (add1 1) : (Int 2))
     82 (check-equal? (sub1 2) 1)
     83 (check-type (sub1 2) : (Int 1))
     84  
     85 (define (vec-ref-1 [v : VectorTop] [n : Integer])
     86   (vector-ref v (sub1 n)))
     87 
     88 (check-equal? (vec-ref-1 (make-vector 10) 10) 0)
     89 
     90 (typecheck-fail
     91  (vec-ref-1 vec10 11)
     92   #:with-msg "index is out of range, given 10, valid range: \\[0, 9\\]")
     93 
     94 (typecheck-fail
     95  (vec-ref-1 vec10 (add1 10))
     96   #:with-msg "index is out of range, given 10, valid range: \\[0, 9\\]")
     97 
     98 (define (vec-ref+4 [v : VectorTop] [n : Integer])
     99   (vector-ref v (+ n 4)))
    100 
    101 (typecheck-fail
    102  (vec-ref+4 vec10 6)
    103   #:with-msg "index is out of range, given 10, valid range: \\[0, 9\\]")
    104 
    105 (check-equal? (vec-ref+4 vec10 5) 9)
    106 
    107 (define (vec-ref-4 [v : VectorTop] [n : Integer])
    108   (vector-ref v (- n 4)))
    109 
    110 (typecheck-fail
    111  (vec-ref-4 vec10 14)
    112   #:with-msg "index is out of range, given 10, valid range: \\[0, 9\\]")
    113 
    114 (check-equal? (vec-ref-4 vec10 13) 9)
    115 
    116 (typecheck-fail
    117  (vec-ref-4 vec10 (+ (- 14 4) 4))
    118   #:with-msg "index is out of range, given 10, valid range: \\[0, 9\\]")
    119 
    120 (typecheck-fail
    121  (let ([x 10])
    122    (vec-ref-4 vec10 (+ x 4)))
    123  #:with-msg "index is out of range, given 10, valid range: \\[0, 9\\]")
    124 
    125 (check-equal?
    126  (let ([x 10])
    127    (let ([y (- x 1)])
    128      (vec-ref-4 vec10 (+ y 4))))
    129  9)