1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: UFFI Example file to test unions
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Mar 2002
10 ;;;; $Id: union.cl,v 1.7 2002/04/03 00:57:48 kevin Exp $
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
21 (uffi:def-union tunion1
29 (let ((u (uffi:allocate-foreign-object 'tunion1)))
30 (setf (uffi:get-slot-value u 'tunion1 'uint)
32 (+ (* 1 (char-code #\A))
33 (* 256 (char-code #\B))
34 (* 65536 (char-code #\C))
37 (+ (* 16777216 (char-code #\A))
38 (* 65536 (char-code #\B))
39 (* 256 (char-code #\C))
41 (format *standard-output* "~&Should be #\A: ~S"
42 (uffi:ensure-char-character
43 (uffi:get-slot-value u 'tunion1 'char)))
44 (format *standard-output* "~&Should be negative number: ~D"
45 (uffi:get-slot-value u 'tunion1 'int))
46 (format *standard-output* "~&Should be positive number: ~D"
47 (uffi:get-slot-value u 'tunion1 'uint))
48 (uffi:free-foreign-object u))
51 (defun test-union-1 ()
52 (let ((u (uffi:allocate-foreign-object 'tunion1)))
53 (setf (uffi:get-slot-value u 'tunion1 'uint)
55 (+ (* 1 (char-code #\A))
56 (* 256 (char-code #\B))
57 (* 65536 (char-code #\C))
60 (+ (* 16777216 (char-code #\A))
61 (* 65536 (char-code #\B))
62 (* 256 (char-code #\C))
63 (* 1 128))) ;set signed bit
64 (util.test:test (uffi:ensure-char-character
65 (uffi:get-slot-value u 'tunion1 'char))
68 :fail-info "Error with union character")
70 (util.test:test (> 0 (uffi:get-slot-value u 'tunion1 'int))
73 "Error with negative int in union")
74 (util.test:test (plusp (uffi:get-slot-value u 'tunion1 'uint))
77 "Error with unsigned int in union")
78 (uffi:free-foreign-object u))