df2507b71ec8addbbcde6b6287d5b7ed618e23d5
[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 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       ;; little endian
32       #-(or sparc sparc-v9 powerpc ppc big-endian)
33       (+ (* 1 (char-code #\A))
34          (* 256 (char-code #\B))
35          (* 65536 (char-code #\C))
36          (* 16777216 255))
37       ;; big endian
38       #+(or sparc sparc-v9 powerpc ppc big-endian)
39       (+ (* 16777216 (char-code #\A))
40          (* 65536 (char-code #\B))
41          (* 256 (char-code #\C))
42          (* 1 255)))
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))
51   (values))
52
53 #+test-uffi
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))
61              (* 16777216 128))
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))
69                #\A
70                :test #'eql
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))
74 ;;             t
75 ;;             :fail-info
76 ;;             "Error with negative int in union")
77     (util.test:test (plusp (uffi:get-slot-value u 'tunion1 'uint))
78                t
79                :fail-info
80                "Error with unsigned int in union")
81     (uffi:free-foreign-object u))
82   (values))
83
84 #+examples-uffi
85 (run-union-1)
86
87
88 #+test-uffi
89 (test-union-1)