#lang racket (require "string-lang.rkt" rackunit) ;;; ;;; Strings ;;; ; (s i) = (string-ref s i) (check-eqv? {"foo" 0} #\f) (check-eqv? {"foo" 1} #\o) (check-exn exn? (λ () ("foo" 3))) (check-exn exn? (λ () ("foo" 'a))) ; (s i j) = (substring s i j) (check-equal? {"foobar" 0 3} "foo") (check-equal? {"foobar" 0 0} "") (let ([s "foobar"]) (check-equal? {s 0 (string-length s)} s)) ; (s i _) = (let ([t s)) (substring t i (string-length t)) (check-equal? {"foobar" 0 _} "foobar") (check-equal? {"foobar" 3 _} "bar") ; (s _ j) = (let ([t s)) (substring t 0 j) (check-equal? {"foobar" _ 3} "foo") (check-equal? {"foobar" _ 6} "foobar") ; (string: expr ...) converts the exprs to strings and concanates the values (check-equal? (string: "foo") "foo") (check-equal? (string: 'foo) "foo") (check-equal? (string: #\f) "f") (check-equal? (string: 42) "42") (check-equal? (string: 2.5) "2.5") (check-equal? (string: #\f 'o #\o "b" 'ar) "foobar") (check-equal? (string: 'x 1) "x1") (check-equal? (let ([x 2]) (string: 'x x)) "x2") (check-equal? (string: 'foo #\b {"abc" 0} 'r) "foobar") ;;; ;;; Vectors ;;; ; (s i) = (vector-ref s i) (check-eqv? {#(f o o) 0} 'f) (check-eqv? {#(f o o) 2} 'o) (check-exn exn? (λ () {#(f o o) 3})) (check-exn exn? (λ () {#(f o o) 'a})) ; (s i j) = (vector-copy s i j) (check-equal? {#(f o o b a r) 0 3} #{f o o}) (check-equal? {#(f o o b a r) 0 0} #()) (let ([s #(f o o b a r)]) (check-equal? {s 0 (vector-length s)} s)) ; (s i _) = (let ([t s)) (vector-copy t i (vector-length t)) (check-equal? {#(f o o b a r) 0 _} #(f o o b a r)) (check-equal? {#(f o o b a r) 3 _} #(b a r)) ; (s _ j) = (let ([t s)) (vector-copy t 0 j) (check-equal? {#(f o o b a r) _ 3} #(f o o)) (check-equal? {#(f o o b a r) _ 6} #(f o o b a r)) ;;; ;;; Lists ;;; ; (s i) = (list-ref s i) (check-eqv? {'(f o o) 0} 'f) (check-eqv? {'(f o o) 2} 'o) (check-exn exn? (λ () {'(f o o) 3})) (check-exn exn? (λ () {'(f o o) 'a})) ; (s i j) ~ (list-copy s i j) (check-equal? {'(f o o b a r) 0 3} '{f o o}) (check-equal? {'(f o o b a r) 0 0} '()) (let ([s '(f o o b a r)]) (check-equal? {s 0 (length s)} s)) ; (s i _) = (let ([t s)) (list-copy t i (length t)) (check-equal? {'(f o o b a r) 0 _} '(f o o b a r)) (check-equal? {'(f o o b a r) 3 _} '(b a r)) ; (s _ j) = (let ([t s)) (list-copy t 0 j) (check-equal? {'(f o o b a r) _ 3} '(f o o)) (check-equal? {'(f o o b a r) _ 6} '(f o o b a r)) ;;; ;;; Hash Tables ;;; (define h #hash((a . 1) (b . 2) (c . 3) (d . 4))) ; (h i) = (hash-ref h i) (check-equal? {h 'a} 1) (check-exn exn? (λ () {h 'x})) ; (h i f) = (hash-ref h i f) (check-equal? {h 'x #f} #f) (check-equal? {h 'a #f} 1) ;;; ;;; Structs ;;; (define-syntax (@ stx) (syntax-case stx () [(_ v i) #'{v i}] [(_ v i j ...) #'(@ {v i} j ...)])) (define-struct person (first-name last-name age height) #:transparent) (define john (person "John" "Doe" 42 1.80)) ;(dot john age) ; only works in repl ?!? ;{john .age} ; only works in repl ?!? (check-equal? (person-first-name john) "John") ;(check-equal? {john .first-name} "John") ;(check-equal? {john .last-name} 42) ;{john .first-name} #;(begin (define persons (vector (person "John" "Doe" 42 1.80) (person "Jane" "Dae" 43 1.81))) (define i 1) (define initials (string: {{{persons i} .first-name} 0} {{{persons i} .last-name} 0})) (check-equal? initials "JD")) #;(begin (define persons (vector (person "John" "Doe" 42 1.80) (person "Jane" "Dae" 43 1.81))) (define i 1) (define initials (string (@ persons i .first-name 0) (@ persons i .last-name 0))) (check-equal? initials "JD"))