gnunet/tests/foreign-padded.scm

76 lines
2.8 KiB
Scheme

;;;; -*- mode: Scheme; indent-tabs-mode: nil; fill-column: 80; -*-
;;;;
;;;; Copyright © 2015 Rémi Delrue <asgeir@free.fr>
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(define-module (test-foreign-padded)
#:use-module (srfi srfi-64)
#:use-module (system foreign)
#:use-module (system foreign-padded))
;; union?
(test-equal #t (union? (make-union)))
(test-equal #t (union? (make-union '(#:foo *) '(#:bar * *))))
(test-equal #f (union? '(union 0)))
;; %next-multiple
(define next-multiple (@@ (system foreign-padded) next-multiple))
(test-equal 10 (next-multiple 7 5))
(test-equal 2 (next-multiple 1 2))
(test-equal 0 (next-multiple 0 1))
(test-equal 10 (next-multiple 10 5))
(test-error 'numerical-overflow (next-multiple 10 0))
;; alignof*
(test-equal (alignof '*)
(alignof* (make-union (list #:foo '*)
(list #:bar unsigned-int))))
(test-equal (alignof '*)
(alignof* (list (make-union (list #:foo '*)
(list #:bar unsigned-int)))))
;; sizeof* — unions
(let ((size (sizeof (list int64 int16)))
(align (alignof (list int64 int16))))
(test-equal (next-multiple size align)
(sizeof* (make-union (list #:foo int8)
(list #:bar int64 int16)))))
(test-equal 0 (sizeof* (make-union)))
(test-equal 1 (sizeof* uint8))
;; sizeof* — alignment padding
(let ((%type (list '* unsigned-int)))
(test-assert (zero? (remainder (sizeof* %type) (alignof %type)))))
;; padding
(test-equal 5 (length (padding 5)))
(test-equal 0 (length (padding 0)))
;; make-union
;; (let* ((longuest (list int32 int32))
;; (size (sizeof longuest))
;; (pad-size (sizeof* longuest))
;; (pad-rem (- pad-size size))
;; (align (alignof* longuest)))
;; (test-equal
;; `(union ,pad-size ,align
;; ((#:foo ,int32 ,int32 ,@(if (> pad-rem 0)
;; (padding pad-rem)
;; '()))
;; (#:bar ,(pad (list uint8) (sizeof int32))
;; ,(pad (list uint8) pad-size))))
;; (make-union `(#:foo ,int32 ,int32)
;; `(#:bar ,uint8))))