From b0b73e0663e2925f26b241e658e3317932c0d3c8 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 3 Oct 2024 08:54:40 -0400 Subject: First commit! --- tests/test-bstruct.scm | 283 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/utils.scm | 24 +++++ 2 files changed, 307 insertions(+) create mode 100644 tests/test-bstruct.scm create mode 100644 tests/utils.scm (limited to 'tests') diff --git a/tests/test-bstruct.scm b/tests/test-bstruct.scm new file mode 100644 index 0000000..1e303c0 --- /dev/null +++ b/tests/test-bstruct.scm @@ -0,0 +1,283 @@ +;;; guile-bstruct -- Binary structures for Guile +;;; Copyright © 2024 David Thompson +;;; +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +(define-module (tests test-bstruct) + #:use-module (bstruct) + #:use-module (srfi srfi-64) + #:use-module (system foreign) + #:use-module (tests utils)) + +;; For testing basic structs. +(define-bstruct + (struct (x f32) (y f32))) + +(define-bstruct + (struct (xy ) (uv ))) + +;; For testing arrays. +(define-bstruct + (array 16 f32)) + +;; For testing variable length arrays. +(define-bstruct + (struct (items (* f32)))) + +;; For testing unions. +(define-bstruct + (struct (type u8) (x s32) (y s32))) + +(define-bstruct + (struct (type u8) (button u8) (state u8) (x s32) (y s32))) + +(define-bstruct + (union (type u8) + (mouse-move ) + (mouse-button ))) + +;; For testing recursive types. +(define-bstruct + (struct (item int) (next (* )))) + +;; For testing proper sizing. +(define-bstruct + (struct + (a u64) + (b u32) + (c u64) + (d u32))) + +(test-suite "bstruct" + (test-group "bstruct?" + (test-assert (bstruct? (bstruct-alloc ))) + (test-assert (not (bstruct? 'vec2))) + (test-assert (bstruct? (bstruct-alloc ))) + (test-assert (not (bstruct? (bstruct-alloc ))))) + + (test-group "bstruct=?" + (test-assert (bstruct=? + (bstruct-alloc (x 42) (y 69)) + (bstruct-alloc (x 42) (y 69)))) + (test-assert (not (bstruct=? + (bstruct-alloc (x 42) (y 69)) + (bstruct-alloc (x 77) (y 89)))))) + + (test-group "bstruct->sexp" + (test-equal '(struct (x 42.0) (y 69.0)) + (bstruct->sexp (bstruct-alloc (x 42) (y 69)))) + (test-equal '(struct (xy (struct (x 42.0) (y 69.0))) + (uv (struct (x 77.0) (y 89.0)))) + (bstruct->sexp (bstruct-alloc + ((xy x) 42) ((xy y) 69) + ((uv x) 77) ((uv y) 89)))) + (test-equal '(union + (type 1) + (mouse-move (struct + (type 1) + (x 0) + (y 0))) + (mouse-button (struct + (type 1) + (button 0) + (state 0) + (x 0) + (y 0)))) + (bstruct->sexp (bstruct-alloc (type 1)))) + (test-equal '(struct (item 42) (next null)) + (bstruct->sexp (bstruct-alloc (item 42))))) + + (test-group "bstruct->pointer" + (test-equal #vu8(0 0 40 66 0 0 138 66) + (let ((v (bstruct-alloc (x 42) (y 69)))) + (pointer->bytevector (bstruct->pointer v) + (bstruct-sizeof ))))) + + (test-group "pointer->bstruct" + (test-equal 69.0 + (let ((v (bstruct-alloc (x 42) (y 69)))) + (bstruct-ref y + (pointer->bstruct + (bstruct->pointer v)))))) + + (test-group "bstruct-wrap" + (test-equal 69.0 + (bstruct-ref y (bstruct-wrap (f32vector 13 42 69) 4)))) + + (test-group "bstruct-unwrap" + (test-equal '(#vu8(0 0 40 66 0 0 138 66) 0) + (call-with-values + (lambda () + (bstruct-unwrap + (bstruct-alloc (x 42) (y 69)))) + list))) + + (test-group "bstruct-alignof" + (test-equal (alignof (list float float)) + (bstruct-alignof )) + (test-equal (alignof (list (list float float) (list float float))) + (bstruct-alignof )) + (test-equal (alignof (make-list 16 float)) + (bstruct-alignof )) + (test-equal (alignof (list uint8 int32 int32)) + (bstruct-alignof )) + (test-equal (alignof (list uint8 uint8 uint8 int32 int32)) + (bstruct-alignof )) + (test-equal (max (alignof (list uint8)) + (alignof (list uint8 int32 int32)) + (alignof (list uint8 uint8 uint8 int32 int32))) + (bstruct-alignof ))) + + (test-group "bstruct-sizeof" + (test-equal (sizeof (list float float)) + (bstruct-sizeof )) + (test-equal (sizeof (list (list float float) (list float float))) + (bstruct-sizeof )) + (test-equal (sizeof (make-list 16 float)) + (bstruct-sizeof )) + (test-equal (sizeof (list uint8 int32 int32)) + (bstruct-sizeof )) + (test-equal (sizeof (list uint8 uint8 uint8 int32 int32)) + (bstruct-sizeof )) + (test-equal (max (sizeof (list uint8)) + (sizeof (list uint8 int32 int32)) + (sizeof (list uint8 uint8 uint8 int32 int32))) + (bstruct-sizeof )) + (test-equal (sizeof (list uint64 uint32 uint64 uint32)) + (bstruct-sizeof ))) + + (test-group "bstruct-ref" + (test-equal 69.0 + (bstruct-ref y (bstruct-alloc (x 42) (y 69)))) + (test-equal 42.0 + (bstruct-ref (uv x) (bstruct-alloc ((uv x) 42)))) + (test-equal 4.0 + (let ((bv (f32vector 1 2 3 4))) + (bstruct-ref (items (* 3)) + (bstruct-alloc + (items (bytevector->pointer bv)))))) + (test-equal %null-pointer + (bstruct-ref next (bstruct-alloc )))) + + (test-group "bstruct-&ref" + (let ((bv (f32vector 42 69))) + (test-equal (bytevector->pointer bv 4) + (bstruct-&ref y (bstruct-wrap bv 0))))) + + (test-group "bstruct-set!" + (test-equal 42.0 + (let ((v (bstruct-alloc ))) + (bstruct-set! y v 42) + (bstruct-ref y v))) + (test-equal 42.0 + (let ((a (bstruct-alloc ))) + (bstruct-set! 7 a 42) + (bstruct-ref 7 a))) + (test-equal 42.0 + (let* ((bv (f32vector 0 0 0 0)) + (f (bstruct-alloc ))) + (bstruct-set! items f (bytevector->pointer bv)) + (bstruct-set! (items (* 3)) f 42) + (bstruct-ref (items (* 3)) f))) + (test-equal 42 + (let ((e (bstruct-alloc ))) + (bstruct-set! (mouse-move y) e 42) + (bstruct-ref (mouse-move y) e))) + (test-equal 69 + (let* ((a (bstruct-alloc (item 42))) + (b (bstruct-alloc (item 69)))) + (bstruct-set! next a (bstruct->pointer b)) + (bstruct-ref (next * item) a)))) + + (test-group "bstruct-pack!" + (test-equal (f32vector 42 69) + (let ((bv (f32vector 0 0))) + (bstruct-pack! ( (x 42) (y 69)) bv 0) + bv)) + (test-equal (f32vector 1 2 3 4) + (let ((bv (f32vector 0 0 0 0))) + (bstruct-pack! ( + (xy (bstruct-alloc (x 1) (y 2))) + ((uv x) 3) + ((uv y) 4)) + bv 0) + bv)) + (test-equal (f32vector 1 0 0 0 + 0 1 0 0 + 0 0 1 0 + 0 0 0 1) + (let ((bv (f32vector 0 0 0 0 + 0 0 0 0 + 0 0 0 0 + 0 0 0 0))) + (bstruct-pack! ( (0 1) (5 1) (10 1) (15 1)) + bv 0) + bv)) + (test-equal (u8vector 1 2 0 0 3 0 0 0 4 0 0 0) + (let ((bv (make-u8vector (bstruct-sizeof ) 0))) + (bstruct-pack! ( + ((mouse-button type) 1) + ((mouse-button button) 2) + ((mouse-button state) 0) + ((mouse-button x) 3) + ((mouse-button y) 4)) + bv 0) + bv))) + + (test-group "bstruct-unpack" + (test-equal '(42.0 69.0) + (let ((bv (f32vector 42 69))) + (call-with-values (lambda () (bstruct-unpack ( x y) bv 0)) + list))) + (test-equal (list 1.0 2.0 3.0 4.0) + (let ((bv (f32vector 1 2 3 4))) + (call-with-values (lambda () + (bstruct-unpack ( (xy x) (xy y) + (uv x) (uv y)) + bv 0)) + list))) + (test-equal '(1.0 1.0 1.0 1.0) + (let ((bv (f32vector 1 0 0 0 + 0 1 0 0 + 0 0 1 0 + 0 0 0 1))) + (call-with-values (lambda () + (bstruct-unpack ( 0 5 10 15) + bv 0)) + list))) + (test-equal '(1 2 0 3 4) + (let ((bv (u8vector 1 2 0 0 3 0 0 0 4 0 0 0))) + (call-with-values (lambda () + (bstruct-unpack ( + (mouse-button type) + (mouse-button button) + (mouse-button state) + (mouse-button x) + (mouse-button y)) + bv 0)) + list)))) + + (test-group "bstruct-copy" + (test-equal '(42.0 69.0) + (let* ((v (bstruct-alloc (x 42) (y 69))) + (v* (bstruct-copy v))) + (list (bstruct-ref x v*) + (bstruct-ref y v*))))) + + (test-group "bstruct-copy!" + (test-equal '(42.0 69.0) + (let* ((v (bstruct-alloc (x 42) (y 69))) + (v* (bstruct-alloc ))) + (bstruct-copy! v v*) + (list (bstruct-ref x v*) + (bstruct-ref y v*)))))) diff --git a/tests/utils.scm b/tests/utils.scm new file mode 100644 index 0000000..1b0d4cd --- /dev/null +++ b/tests/utils.scm @@ -0,0 +1,24 @@ +;;; guile-bstruct -- Binary structures for Guile +;;; Copyright © 2024 David Thompson +;;; +;;; Licensed under the Apache License, Version 2.0 (the "License"); +;;; you may not use this file except in compliance with the License. +;;; You may obtain a copy of the License at +;;; +;;; http://www.apache.org/licenses/LICENSE-2.0 +;;; +;;; Unless required by applicable law or agreed to in writing, software +;;; distributed under the License is distributed on an "AS IS" BASIS, +;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +;;; See the License for the specific language governing permissions and +;;; limitations under the License. + +(define-module (tests utils) + #:use-module (srfi srfi-64) + #:export (test-suite)) + +(define-syntax-rule (test-suite name body ...) + (begin + (test-begin name) + body ... + (exit (zero? (test-runner-fail-count (test-end)))))) -- cgit v1.2.3