r2785: *** empty log message ***
[uffi.git] / examples / union.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          union.cl
6 ;;;; Purpose:       UFFI Example file to test unions
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id: union.cl,v 1.8 2002/09/20 05:38:01 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package :cl-user)
20
21 (uffi:def-union tunion1 
22     (char :char)
23   (int :int)
24   (uint :unsigned-int)
25   (sf :float)
26   (df :double))
27
28 (defun run-union-1 ()
29   (let ((u (uffi:allocate-foreign-object 'tunion1)))
30     (setf (uffi:get-slot-value u 'tunion1 'uint)
31           #-sparc
32           (+ (* 1 (char-code #\A))
33              (* 256 (char-code #\B))
34              (* 65536 (char-code #\C))
35              (* 16777216 255))
36           #+(or sparc sparc-v9)
37           (+ (* 16777216 (char-code #\A))
38              (* 65536 (char-code #\B))
39              (* 256 (char-code #\C))
40              (* 1 255)))
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))
49   (values))
50
51 #+test-uffi
52 (defun test-union-1 ()
53   (let ((u (uffi:allocate-foreign-object 'tunion1)))
54     (setf (uffi:get-slot-value u 'tunion1 'uint)
55           #-sparc
56           (+ (* 1 (char-code #\A))
57              (* 256 (char-code #\B))
58              (* 65536 (char-code #\C))
59              (* 16777216 128))
60           #+(or sparc sparc-v9)
61           (+ (* 16777216 (char-code #\A))
62              (* 65536 (char-code #\B))
63              (* 256 (char-code #\C))
64              (* 1 128))) ;set signed bit
65     (util.test:test (uffi:ensure-char-character 
66                 (uffi:get-slot-value u 'tunion1 'char))
67                #\A
68                :test #'eql
69                :fail-info "Error with union character")
70     #-(or sparc sparc-v9)
71     (util.test:test (> 0 (uffi:get-slot-value u 'tunion1 'int))
72                t
73                :fail-info
74                "Error with negative int in union")
75     (util.test:test (plusp (uffi:get-slot-value u 'tunion1 'uint))
76                t
77                :fail-info
78                "Error with unsigned int in union")
79     (uffi:free-foreign-object u))
80   (values))
81
82 #+examples-uffi
83 (run-union-1)
84
85
86 #+test-uffi
87 (test-union-1)