r10608: update license
[uffi.git] / examples / union.lisp
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$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package :cl-user)
17
18 (uffi:def-union tunion1 
19     (char :char)
20   (int :int)
21   (uint :unsigned-int)
22   (sf :float)
23   (df :double))
24
25 (defun run-union-1 ()
26   (let ((u (uffi:allocate-foreign-object 'tunion1)))
27     (setf (uffi:get-slot-value u 'tunion1 'uint)
28       ;; little endian
29       #-(or sparc sparc-v9 powerpc ppc big-endian)
30       (+ (* 1 (char-code #\A))
31          (* 256 (char-code #\B))
32          (* 65536 (char-code #\C))
33          (* 16777216 255))
34       ;; big endian
35       #+(or sparc sparc-v9 powerpc ppc big-endian)
36       (+ (* 16777216 (char-code #\A))
37          (* 65536 (char-code #\B))
38          (* 256 (char-code #\C))
39          (* 1 255)))
40     (format *standard-output* "~&Should be #\A: ~S" 
41             (uffi:ensure-char-character 
42              (uffi:get-slot-value u 'tunion1 'char)))
43 ;;    (format *standard-output* "~&Should be negative number: ~D" 
44 ;;          (uffi:get-slot-value u 'tunion1 'int))
45     (format *standard-output* "~&Should be positive number: ~D"
46             (uffi:get-slot-value u 'tunion1 'uint))
47     (uffi:free-foreign-object u))
48   (values))
49
50 #+test-uffi
51 (defun test-union-1 ()
52   (let ((u (uffi:allocate-foreign-object 'tunion1)))
53     (setf (uffi:get-slot-value u 'tunion1 'uint)
54           #-(or sparc sparc-v9 powerpc ppc)
55           (+ (* 1 (char-code #\A))
56              (* 256 (char-code #\B))
57              (* 65536 (char-code #\C))
58              (* 16777216 128))
59           #+(or sparc sparc-v9 powerpc ppc)
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))
66                #\A
67                :test #'eql
68                :fail-info "Error with union character")
69     #-(or sparc sparc-v9 mcl)
70 ;;    (util.test:test (> 0 (uffi:get-slot-value u 'tunion1 'int))
71 ;;             t
72 ;;             :fail-info
73 ;;             "Error with negative int in union")
74     (util.test:test (plusp (uffi:get-slot-value u 'tunion1 'uint))
75                t
76                :fail-info
77                "Error with unsigned int in union")
78     (uffi:free-foreign-object u))
79   (values))
80
81 #+examples-uffi
82 (run-union-1)
83
84
85 #+test-uffi
86 (test-union-1)