Created
May 19, 2013 17:18
-
-
Save dleslie/5608322 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(test-group "records" | |
;; Define a record type with setters for each field | |
(define-record test (setter bit) (setter byte) (setter nibble) (setter bytes)) | |
;; Define a 'bits' conversion function | |
(define test-bits | |
(make-bits | |
(bit test-bit) ; 1 bit | |
(byte test-byte) ; 8 bits | |
(nibble test-nibble) ; 4 bits | |
(bytes 2 test-bytes))) ; 16 bits | |
;; Define a blob to read/write from | |
(define test-blob (make-blob 4)) ; 32 bits | |
(let loop ((count 0)) | |
(define bit (= 1 (random 2))) | |
(define nibble (random 16)) | |
(define byte (random 256)) | |
(define byte-high (random 256)) | |
(define byte-low (random 256)) | |
(define test-thing (make-test bit byte nibble (u8vector->blob (list->u8vector (list byte-high byte-low))))) | |
(test (format "Write record ~S" count) 29 | |
(test-bits test-thing #f test-blob 0)) | |
(test (format "Read record ~S" count) 29 | |
(test-bits test-thing #t test-blob 0)) | |
(test (format "Correct test-bit ~S" count) bit | |
(test-bit test-thing)) | |
(test (format "Correct test-byte ~S" count) byte | |
(test-byte test-thing)) | |
(test (format "Correct test-nibble ~S" count) nibble | |
(test-nibble test-thing)) | |
(test (format "Correct test-bytes (high) ~S" count) byte-high | |
(u8vector-ref (blob->u8vector/shared (test-bytes test-thing)) 0)) | |
(test (format "Correct test-bytes (low) ~S" count) byte-low | |
(u8vector-ref (blob->u8vector/shared (test-bytes test-thing)) 1)) | |
(when (> 16 count) | |
(loop (+ count 1))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment