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.lisp,v 1.2 2002/12/03 06:58:39 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 #-(or sparc sparc-v9 powerpc ppc little-endian)
33 (+ (* 1 (char-code #\A))
34 (* 256 (char-code #\B))
35 (* 65536 (char-code #\C))
38 #+(or sparc sparc-v9 powerpc ppc big-endian)
39 (+ (* 16777216 (char-code #\A))
40 (* 65536 (char-code #\B))
41 (* 256 (char-code #\C))
43 (format *standard-output* "~&Should be #\A: ~S"
44 (uffi:ensure-char-character
45 (uffi:get-slot-value u 'tunion1 'char)))
46 ;; (format *standard-output* "~&Should be negative number: ~D"
47 ;; (uffi:get-slot-value u 'tunion1 'int))
48 (format *standard-output* "~&Should be positive number: ~D"
49 (uffi:get-slot-value u 'tunion1 'uint))
50 (uffi:free-foreign-object u))
54 (defun test-union-1 ()
55 (let ((u (uffi:allocate-foreign-object 'tunion1)))
56 (setf (uffi:get-slot-value u 'tunion1 'uint)
57 #-(or sparc sparc-v9 powerpc ppc)
58 (+ (* 1 (char-code #\A))
59 (* 256 (char-code #\B))
60 (* 65536 (char-code #\C))
62 #+(or sparc sparc-v9 powerpc ppc)
63 (+ (* 16777216 (char-code #\A))
64 (* 65536 (char-code #\B))
65 (* 256 (char-code #\C))
66 (* 1 128))) ;set signed bit
67 (util.test:test (uffi:ensure-char-character
68 (uffi:get-slot-value u 'tunion1 'char))
71 :fail-info "Error with union character")
72 #-(or sparc sparc-v9 mcl)
73 ;; (util.test:test (> 0 (uffi:get-slot-value u 'tunion1 'int))
76 ;; "Error with negative int in union")
77 (util.test:test (plusp (uffi:get-slot-value u 'tunion1 'uint))
80 "Error with unsigned int in union")
81 (uffi:free-foreign-object u))