diff options
Diffstat (limited to 'tests/test-bstruct.scm')
-rw-r--r-- | tests/test-bstruct.scm | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/tests/test-bstruct.scm b/tests/test-bstruct.scm index 1bd54fb..c0e3aaa 100644 --- a/tests/test-bstruct.scm +++ b/tests/test-bstruct.scm @@ -15,6 +15,7 @@ (define-module (tests test-bstruct) #:use-module (bstruct) + #:use-module (rnrs bytevectors) #:use-module (srfi srfi-64) #:use-module (system foreign) #:use-module (tests utils)) @@ -26,6 +27,13 @@ (define-bstruct <vertex> (struct (xy <vec2>) (uv <vec2>))) +;; For testing bit fields. +(define-bstruct <date> + (bits + (year 32 s) + (month 4 u) + (day 5 u))) + ;; For testing arrays. (define-bstruct <matrix4> (array 16 f32)) @@ -95,7 +103,9 @@ (y 0)))) (bstruct->sexp (bstruct-alloc <event> (type 1)))) (test-equal '(struct (item 42) (next (* 0))) - (bstruct->sexp (bstruct-alloc <node> (item 42))))) + (bstruct->sexp (bstruct-alloc <node> (item 42)))) + (test-equal '(bits (year -2024) (month 9) (day 5)) + (bstruct->sexp (bstruct-alloc <date> (year -2024) (month 9) (day 5))))) (test-group "bstruct->pointer" (test-equal #vu8(0 0 40 66 0 0 138 66) @@ -127,12 +137,10 @@ list))) (test-group "bstruct-alignof" - (test-equal (alignof (list float float)) - (bstruct-alignof <vec2>)) + (test-equal (alignof (list float float)) (bstruct-alignof <vec2>)) (test-equal (alignof (list (list float float) (list float float))) (bstruct-alignof <vertex>)) - (test-equal (alignof (make-list 16 float)) - (bstruct-alignof <matrix4>)) + (test-equal (alignof (make-list 16 float)) (bstruct-alignof <matrix4>)) (test-equal (alignof (list uint8 int32 int32)) (bstruct-alignof <mouse-move-event>)) (test-equal (alignof (list uint8 uint8 uint8 int32 int32)) @@ -140,15 +148,14 @@ (test-equal (max (alignof (list uint8)) (alignof (list uint8 int32 int32)) (alignof (list uint8 uint8 uint8 int32 int32))) - (bstruct-alignof <event>))) + (bstruct-alignof <event>)) + (test-equal (alignof uint64) (bstruct-alignof <date>))) (test-group "bstruct-sizeof" - (test-equal (sizeof (list float float)) - (bstruct-sizeof <vec2>)) + (test-equal (sizeof (list float float)) (bstruct-sizeof <vec2>)) (test-equal (sizeof (list (list float float) (list float float))) (bstruct-sizeof <vertex>)) - (test-equal (sizeof (make-list 16 float)) - (bstruct-sizeof <matrix4>)) + (test-equal (sizeof (make-list 16 float)) (bstruct-sizeof <matrix4>)) (test-equal (sizeof (list uint8 int32 int32)) (bstruct-sizeof <mouse-move-event>)) (test-equal (sizeof (list uint8 uint8 uint8 int32 int32)) @@ -158,7 +165,8 @@ (sizeof (list uint8 uint8 uint8 int32 int32))) (bstruct-sizeof <event>)) (test-equal (sizeof (list uint64 uint32 uint64 uint32)) - (bstruct-sizeof <right-sized>))) + (bstruct-sizeof <right-sized>)) + (test-equal (sizeof uint64) (bstruct-sizeof <date>))) (test-group "bstruct-ref" (test-equal 69.0 @@ -172,7 +180,9 @@ (items (bytevector->pointer bv))) (items (* 3))))) (test-equal %null-pointer - (bstruct-ref <node> (bstruct-alloc <node>) next))) + (bstruct-ref <node> (bstruct-alloc <node>) next)) + (test-equal -2024 + (bstruct-ref <date> (bstruct-alloc <date> (year -2024)) year))) (test-group "bstruct-set!" (test-equal 42.0 @@ -197,7 +207,11 @@ (let* ((a (bstruct-alloc <node> (item 42))) (b (bstruct-alloc <node> (item 69)))) (bstruct-set! <node> a (next (bstruct->pointer <node> b))) - (bstruct-ref <node> a (next * item))))) + (bstruct-ref <node> a (next * item)))) + (test-equal 12 + (let ((date (bstruct-alloc <date> (month 11)))) + (bstruct-set! <date> date (month 12)) + (bstruct-ref <date> date month)))) (test-group "bstruct-pack!" (test-equal (f32vector 42 69) @@ -222,13 +236,17 @@ (bstruct-pack! <matrix4> bv 0 (0 1) (5 1) (10 1) (15 1)) bv)) (test-equal (u8vector 1 2 0 0 3 0 0 0 4 0 0 0) - (let ((bv (make-u8vector (bstruct-sizeof <event>) 0))) + (let ((bv (make-bytevector (bstruct-sizeof <event>)))) (bstruct-pack! <event> bv 0 ((mouse-button type) 1) ((mouse-button button) 2) ((mouse-button state) 0) ((mouse-button x) 3) ((mouse-button y) 4)) + bv)) + (test-equal (u8vector 232 7 0 0 123 1 0 0) + (let ((bv (make-bytevector (bstruct-sizeof <date>)))) + (bstruct-pack! <date> bv 0 (year 2024) (month 11) (day 23)) bv))) (test-group "bstruct-unpack" @@ -260,6 +278,11 @@ (mouse-button state) (mouse-button x) (mouse-button y))) + list))) + (test-equal '(2024 11 23) + (let ((bv (u8vector 232 7 0 0 123 1 0 0))) + (call-with-values (lambda () + (bstruct-unpack <date> bv 0 year month day)) list)))) (test-group "bstruct-copy" |