summaryrefslogtreecommitdiff
path: root/tests/test-bstruct.scm
blob: 1bd54fb639b1c8d7e9a35409961e6937568ba273 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
;;; guile-bstruct -- Binary structures for Guile
;;; Copyright © 2024 David Thompson <dthompson2@worcester.edu>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;    http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

(define-module (tests test-bstruct)
  #:use-module (bstruct)
  #:use-module (srfi srfi-64)
  #:use-module (system foreign)
  #:use-module (tests utils))

;; For testing basic structs.
(define-bstruct <vec2>
  (struct (x f32) (y f32)))

(define-bstruct <vertex>
  (struct (xy <vec2>) (uv <vec2>)))

;; For testing arrays.
(define-bstruct <matrix4>
  (array 16 f32))

;; For testing variable length arrays.
(define-bstruct <floats>
  (struct (items (* f32))))

;; For testing unions.
(define-bstruct <mouse-move-event>
  (struct (type u8) (x s32) (y s32)))

(define-bstruct <mouse-button-event>
  (struct (type u8) (button u8) (state u8) (x s32) (y s32)))

(define-bstruct <event>
  (union (type u8)
         (mouse-move <mouse-move-event>)
         (mouse-button <mouse-button-event>)))

;; For testing recursive types.
(define-bstruct <node>
  (struct (item int) (next (* <node>))))

;; For testing proper sizing.
(define-bstruct <right-sized>
  (struct
   (a u64)
   (b u32)
   (c u64)
   (d u32)))

(test-suite "bstruct"
  (test-group "bstruct?"
    (test-assert (bstruct? (bstruct-alloc <vec2>)))
    (test-assert (not (bstruct? 'vec2)))
    (test-assert (bstruct? <vec2> (bstruct-alloc <vec2>)))
    (test-assert (not (bstruct? <vec2> (bstruct-alloc <vertex>)))))

  (test-group "bstruct-=?"
    (test-assert (bstruct-=? <vec2>
                             (bstruct-alloc <vec2> (x 42) (y 69))
                             (bstruct-alloc <vec2> (x 42) (y 69))))
    (test-assert (not (bstruct-=? <vec2>
                                  (bstruct-alloc <vec2> (x 42) (y 69))
                                  (bstruct-alloc <vec2> (x 77) (y 89))))))

  (test-group "bstruct->sexp"
    (test-equal '(struct (x 42.0) (y 69.0))
      (bstruct->sexp (bstruct-alloc <vec2> (x 42) (y 69))))
    (test-equal '(struct (xy (struct (x 42.0) (y 69.0)))
                         (uv (struct (x 77.0) (y 89.0))))
      (bstruct->sexp (bstruct-alloc <vertex>
                                    ((xy x) 42) ((xy y) 69)
                                    ((uv x) 77) ((uv y) 89))))
    (test-equal '(union
                  (type 1)
                  (mouse-move (struct
                               (type 1)
                               (x 0)
                               (y 0)))
                  (mouse-button (struct
                                 (type 1)
                                 (button 0)
                                 (state 0)
                                 (x 0)
                                 (y 0))))
      (bstruct->sexp (bstruct-alloc <event> (type 1))))
    (test-equal '(struct (item 42) (next (* 0)))
      (bstruct->sexp (bstruct-alloc <node> (item 42)))))

  (test-group "bstruct->pointer"
    (test-equal #vu8(0 0 40 66 0 0 138 66)
                (let ((v (bstruct-alloc <vec2> (x 42) (y 69))))
                  (pointer->bytevector (bstruct->pointer <vec2> v)
                                       (bstruct-sizeof <vec2>))))
    (let ((bv (f32vector 42 69)))
      (test-equal (bytevector->pointer bv 4)
        (bstruct->pointer <vec2> (bstruct-wrap <vec2> bv 0) y))))

  (test-group "pointer->bstruct"
    (test-equal 69.0
      (let ((v (bstruct-alloc <vec2> (x 42) (y 69))))
        (bstruct-ref <vec2>
                     (pointer->bstruct <vec2>
                                       (bstruct->pointer <vec2> v))
                     y))))

  (test-group "bstruct-wrap"
    (test-equal 69.0
      (bstruct-ref <vec2> (bstruct-wrap <vec2> (f32vector 13 42 69) 4) y)))

  (test-group "bstruct-unwrap"
    (test-equal '(#vu8(0 0 40 66 0 0 138 66) 0 1)
      (call-with-values
          (lambda ()
            (bstruct-unwrap <vec2>
                            (bstruct-alloc <vec2> (x 42) (y 69))))
        list)))

  (test-group "bstruct-alignof"
    (test-equal (alignof (list float float))
      (bstruct-alignof <vec2>))
    (test-equal (alignof (list (list float float) (list float float)))
      (bstruct-alignof <vertex>))
    (test-equal (alignof (make-list 16 float))
      (bstruct-alignof <matrix4>))
    (test-equal (alignof (list uint8 int32 int32))
      (bstruct-alignof <mouse-move-event>))
    (test-equal (alignof (list uint8 uint8 uint8 int32 int32))
      (bstruct-alignof <mouse-button-event>))
    (test-equal (max (alignof (list uint8))
                     (alignof (list uint8 int32 int32))
                     (alignof (list uint8 uint8 uint8 int32 int32)))
      (bstruct-alignof <event>)))

  (test-group "bstruct-sizeof"
    (test-equal (sizeof (list float float))
      (bstruct-sizeof <vec2>))
    (test-equal (sizeof (list (list float float) (list float float)))
      (bstruct-sizeof <vertex>))
    (test-equal (sizeof (make-list 16 float))
      (bstruct-sizeof <matrix4>))
    (test-equal (sizeof (list uint8 int32 int32))
      (bstruct-sizeof <mouse-move-event>))
    (test-equal (sizeof (list uint8 uint8 uint8 int32 int32))
      (bstruct-sizeof <mouse-button-event>))
    (test-equal (max (sizeof (list uint8))
                     (sizeof (list uint8 int32 int32))
                     (sizeof (list uint8 uint8 uint8 int32 int32)))
      (bstruct-sizeof <event>))
    (test-equal (sizeof (list uint64 uint32 uint64 uint32))
      (bstruct-sizeof <right-sized>)))

  (test-group "bstruct-ref"
    (test-equal 69.0
      (bstruct-ref <vec2> (bstruct-alloc <vec2> (x 42) (y 69)) y))
    (test-equal 42.0
      (bstruct-ref <vertex> (bstruct-alloc <vertex> ((uv x) 42)) (uv x)))
    (test-equal 4.0
      (let ((bv (f32vector 1 2 3 4)))
        (bstruct-ref <floats>
                     (bstruct-alloc <floats>
                                    (items (bytevector->pointer bv)))
                     (items (* 3)))))
    (test-equal %null-pointer
      (bstruct-ref <node> (bstruct-alloc <node>) next)))

  (test-group "bstruct-set!"
    (test-equal 42.0
      (let ((v (bstruct-alloc <vec2>)))
        (bstruct-set! <vec2> v (y 42))
        (bstruct-ref <vec2> v y)))
    (test-equal 42.0
      (let ((a (bstruct-alloc <matrix4>)))
        (bstruct-set! <matrix4> a (7 42))
        (bstruct-ref <matrix4> a 7)))
    (test-equal 42.0
      (let* ((bv (f32vector 0 0 0 0))
             (f (bstruct-alloc <floats>)))
        (bstruct-set! <floats> f (items (bytevector->pointer bv)))
        (bstruct-set! <floats> f ((items (* 3)) 42))
        (bstruct-ref <floats> f (items (* 3)))))
    (test-equal 42
      (let ((e (bstruct-alloc <event>)))
        (bstruct-set! <event> e ((mouse-move y) 42))
        (bstruct-ref <event> e (mouse-move y))))
    (test-equal 69
      (let* ((a (bstruct-alloc <node> (item 42)))
             (b (bstruct-alloc <node> (item 69))))
        (bstruct-set! <node> a (next (bstruct->pointer <node> b)))
        (bstruct-ref <node> a (next * item)))))

  (test-group "bstruct-pack!"
    (test-equal (f32vector 42 69)
      (let ((bv (f32vector 0 0)))
        (bstruct-pack! <vec2> bv 0 (x 42) (y 69))
        bv))
    (test-equal (f32vector 1 2 3 4)
      (let ((bv (f32vector 0 0 0 0)))
        (bstruct-pack! <vertex> bv 0
                       (xy (bstruct-alloc <vec2> (x 1) (y 2)))
                       ((uv x) 3)
                       ((uv y) 4))
        bv))
    (test-equal (f32vector 1 0 0 0
                           0 1 0 0
                           0 0 1 0
                           0 0 0 1)
      (let ((bv (f32vector 0 0 0 0
                           0 0 0 0
                           0 0 0 0
                           0 0 0 0)))
        (bstruct-pack! <matrix4> bv 0 (0 1) (5 1) (10 1) (15 1))
        bv))
    (test-equal (u8vector 1 2 0 0 3 0 0 0 4 0 0 0)
      (let ((bv (make-u8vector (bstruct-sizeof <event>) 0)))
        (bstruct-pack! <event> bv 0
                       ((mouse-button type) 1)
                       ((mouse-button button) 2)
                       ((mouse-button state) 0)
                       ((mouse-button x) 3)
                       ((mouse-button y) 4))
        bv)))

  (test-group "bstruct-unpack"
    (test-equal '(42.0 69.0)
      (let ((bv (f32vector 42 69)))
        (call-with-values (lambda () (bstruct-unpack <vec2> bv 0 x y))
          list)))
    (test-equal (list 1.0 2.0 3.0 4.0)
      (let ((bv (f32vector 1 2 3 4)))
        (call-with-values (lambda ()
                            (bstruct-unpack <vertex> bv 0
                                            (xy x) (xy y)
                                            (uv x) (uv y)))
          list)))
    (test-equal '(1.0 1.0 1.0 1.0)
      (let ((bv (f32vector 1 0 0 0
                           0 1 0 0
                           0 0 1 0
                           0 0 0 1)))
        (call-with-values (lambda ()
                            (bstruct-unpack <matrix4> bv 0 0 5 10 15))
          list)))
    (test-equal '(1 2 0 3 4)
      (let ((bv (u8vector 1 2 0 0 3 0 0 0 4 0 0 0)))
        (call-with-values (lambda ()
                            (bstruct-unpack <event> bv 0
                                            (mouse-button type)
                                            (mouse-button button)
                                            (mouse-button state)
                                            (mouse-button x)
                                            (mouse-button y)))
          list))))

  (test-group "bstruct-copy"
    (test-equal '(42.0 69.0)
      (let* ((v (bstruct-alloc <vec2> (x 42) (y 69)))
             (v* (bstruct-copy <vec2> v)))
        (list (bstruct-ref <vec2> v* x)
              (bstruct-ref <vec2> v* y)))))

  (test-group "bstruct-copy!"
    (test-equal '(42.0 69.0)
      (let* ((v (bstruct-alloc <vec2> (x 42) (y 69)))
             (v* (bstruct-alloc <vec2>)))
        (bstruct-copy! <vec2> v v*)
        (list (bstruct-ref <vec2> v* x)
              (bstruct-ref <vec2> v* y))))))