bd6e5b4fbab628f7244de67068df86c3568f654d
[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.5 2002/04/02 23:27:05 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       (+ (char-code #\A) 
32          (* 256 (char-code #\B))
33          (* 65536 (char-code #\C))
34          (* 16777216 255)))
35     (format *standard-output* "~&Should be #\A: ~S" 
36             (uffi:ensure-char-character 
37              (uffi:get-slot-value u 'tunion1 'char)))
38     (format *standard-output* "~&Should be negative number: ~D" 
39             (uffi:get-slot-value u 'tunion1 'int))
40     (format *standard-output* "~&Should be positive number: ~D"
41             (uffi:get-slot-value u 'tunion1 'uint))
42     (uffi:free-foreign-object u))
43   (values))
44
45 (defun test-union-1 ()
46   (let ((u (uffi:allocate-foreign-object 'tunion1)))
47     (setf (uffi:get-slot-value u 'tunion1 'uint) 
48       (+ (char-code #\A) 
49          (* 256 (char-code #\B))
50          (* 65536 (char-code #\C))
51          (* 16777216 255)))
52     (util.test:test (uffi:ensure-char-character 
53                 (uffi:get-slot-value u 'tunion1 'char))
54                #\A
55                :test #'eql
56                :fail-info "Error with union character")
57     
58     (util.test:test (> 0 (uffi:get-slot-value u 'tunion1 'int))
59                t
60                :fail-info
61                "Error with negative int in union")
62     (util.test:test (plusp (uffi:get-slot-value u 'tunion1 'uint))
63                t
64                :fail-info
65                "Error with unsigned int in union")
66     (uffi:free-foreign-object u))
67   (values))
68
69 #+examples-uffi
70 (run-union-1)
71
72
73 #+test-uffi
74 (test-union-1)