boot2

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

066-bytevector-append.scm (1495B)


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