boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

066-bytevector-append.scm (1538B)


      1 ; (bytevector-append bv ...) -- variadic concatenation, returns a
      2 ; fresh bytevector. R7RS: zero-arg form is the empty bytevector.
      3 
      4 ; Zero-arg form: empty bv.
      5 (define z (bytevector-append))
      6 (if (bytevector? z) 0 (sys-exit 1))
      7 (if (not (string? z)) 0 (sys-exit 2))
      8 (if (= (bytevector-length z) 0) 0 (sys-exit 3))
      9 
     10 ; One-arg form: equal contents but a fresh allocation.
     11 (define a (bytevector-append "abc"))
     12 (if (bytevector=? a "abc") 0 (sys-exit 4))
     13 (bytevector-u8-set! a 0 88)
     14 (if (bytevector=? "abc" "abc") 0 (sys-exit 5))   ; literal untouched
     15 
     16 ; Two-arg form: simple concat.
     17 (if (bytevector=? "abcdef" (bytevector-append "abc" "def")) 0 (sys-exit 6))
     18 
     19 ; Three+ args.
     20 (if (bytevector=? "abcdefghi"
     21                   (bytevector-append "abc" "def" "ghi")) 0 (sys-exit 7))
     22 
     23 ; Empty operands flush through.
     24 (if (bytevector=? "abc" (bytevector-append "" "abc"))     0 (sys-exit 8))
     25 (if (bytevector=? "abc" (bytevector-append "abc" ""))     0 (sys-exit 9))
     26 (if (bytevector=? "abc" (bytevector-append "" "abc" "")) 0 (sys-exit 10))
     27 (if (bytevector=? "" (bytevector-append "" "" ""))        0 (sys-exit 11))
     28 
     29 ; Non-literal bytevectors (built via make-bytevector + set!).
     30 (define p (make-bytevector 2 65))           ; "AA"
     31 (define q (make-bytevector 3 66))           ; "BBB"
     32 (if (bytevector=? "AABBB" (bytevector-append p q)) 0 (sys-exit 12))
     33 
     34 ; Result is independent: mutating the result doesn't affect the inputs.
     35 (define r (bytevector-append p q))
     36 (bytevector-u8-set! r 0 90)
     37 (if (= (bytevector-u8-ref p 0) 65) 0 (sys-exit 13))
     38 
     39 (sys-exit 0)