-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtests.lisp
More file actions
288 lines (263 loc) · 13.8 KB
/
tests.lisp
File metadata and controls
288 lines (263 loc) · 13.8 KB
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
279
280
281
282
283
284
285
286
287
288
;; Copyright (c) 2012, Miron Brezuleanu
;; All rights reserved.
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are met:
;; * Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; * Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;; DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
;; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
;; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(defpackage #:messagepack-tests
(:use #:cl #:fiveam)
(:nicknames #:mpk-tests))
(in-package :mpk-tests)
(def-suite cl-messagepack-tests
:description "Tests for cl-messagepack.")
(in-suite cl-messagepack-tests)
(test integers
"Test encoding of integers."
(is (equalp #(0) (mpk:encode 0)))
(is (equalp #(127) (mpk:encode 127)))
(is (equalp #(#xCC 128) (mpk:encode 128)))
(is (equalp #(255) (mpk:encode -1)))
(is (equalp #(#xE0) (mpk:encode -32)))
(is (equalp #(#xD0 #xDF) (mpk:encode -33)))
(is (equalp #(#xCD #x01 #x00) (mpk:encode 256)))
(is (equalp #(#xCD #xFF #xFF) (mpk:encode 65535)))
(is (equalp #(#xCE #x00 #x01 #x00 #x00) (mpk:encode 65536)))
(is (equalp #(#xCE #xFF #xFF #xFF #xFF) (mpk:encode (1- (expt 2 32)))))
(is (equalp #(#xCF #xFF #xFF #xFF #xFF #xFF #xFF #xFF #xFF) (mpk:encode (1- (expt 2 64)))))
(is (equalp #(#xD0 #x80) (mpk:encode -128)))
(is (equalp #(#xD1 #x80 #x00) (mpk:encode (- (expt 2 15)))))
(is (equalp #(#xD2 #x80 #x00 #x00 #x00) (mpk:encode (- (expt 2 31)))))
(is (equalp #(#xD3 #x80 #x00 #x00 #x00 #x00 #x00 #x00 #x00) (mpk:encode (- (expt 2 63))))))
(test nil-true-false
"Test encoding of NIL and T."
(is (equalp #(#xc0) (mpk:encode nil)))
(is (equalp #(#xc3) (mpk:encode t))))
(test floating-point
"Test encoding of single and double precision floating point
numbers."
#+sbcl (is (equalp #(#xCA #x3F #x80 #x00 #x00) (mpk:encode 1.0s0)))
#+sbcl (is (equalp #(#xCA #xBF #x80 #x00 #x00) (mpk:encode -1.0s0)))
(is (equalp #(#xCB #x3F #xF0 #x00 #x00 #x00 #x00 #x00 #x00) (mpk:encode 1.0d0)))
(is (equalp #(#xCB #xBF #xF0 #x00 #x00 #x00 #x00 #x00 #x00) (mpk:encode -1.0d0))))
(test strings
"Test encoding of strings (raw bytes in msgpack parlance, strings
since I am interested in using msgpack as a binary JSON). Use strings
of various lengths to make sure string length is encoded properly."
(is (equalp #(#xAB #x54 #x65 #x73 #x74 #x20 #x73 #x74 #x72 #x69 #x6E #x67)
(mpk:encode "Test string")))
(is (equalp #(#xDA #x00 #x3C #x73 #x74 #x72 #x69 #x6E #x67 #x73 #x74 #x72 #x69 #x6E #x67 #x73
#x74 #x72 #x69 #x6E #x67 #x73 #x74 #x72 #x69 #x6E #x67 #x73 #x74 #x72 #x69 #x6E
#x67 #x73 #x74 #x72 #x69 #x6E #x67 #x73 #x74 #x72 #x69 #x6E #x67 #x73 #x74 #x72
#x69 #x6E #x67 #x73 #x74 #x72 #x69 #x6E #x67 #x73 #x74 #x72 #x69 #x6E #x67)
(mpk:encode (with-output-to-string (str)
(loop repeat 10 do (princ "string" str))))))
(is (equalp #(#xDB #x00 #x05 #x7E #x40 #x73 #x74 #x72 #x69 #x6E #x67 #x73 #x74 #x72 #x69)
(subseq (mpk:encode (let ((str (make-string 360000)))
(loop for i from 1 to 60000
do (setf (subseq str (* 6 (1- i)) (* 6 i)) "string"))
str))
0 15))))
(test arrays
"Test encoding of arrays of various sizes. Make sure that arrays
encode properly."
(is (equalp #(#x9F #x01 #x02 #x03 #x04 #x05 #x06 #x07 #x08 #x09 #x0A #x0B #x0C #x0D #x0E #x0F)
(mpk:encode '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15))))
(is (equalp #(#xDC #x00 #x10 #x01 #x02 #x03 #x04 #x05 #x06 #x07 #x08 #x09 #x0A #x0B #x0C #x0D
#x0E #x0F #x10)
(mpk:encode (loop for i from 1 to 16 collect i))))
(is (equalp #(#xDD #x00 #x01 #x00 #x00 #x01 #x02 #x03 #x04 #x05 #x06 #x07 #x08 #x09 #x0A)
(subseq (mpk:encode (loop for i from 1 to 65536 collect i))
0
15))))
(test alists
"Test alists can be encoded into either map or lists of lists depending on
*encode-alist-as-hash*."
(let ((alist '((1 11 12) ("b" "bc"))))
(is (equalp #(#x82 #x01 #x92 #x0B #x0C #xA1 #x62 #x91 #xA2 #x62 #x63)
(mpk:encode alist)))
(is (equalp #(#x92 #x93 #x01 #x0B #x0C #x92 #xA1 #x62 #xA2 #x62 #x63)
(let ((mpk:*encode-alist-as-map*)) (mpk:encode alist))))))
(test maps
"Test maps of various sizes, make sure that lengths are encoded
properly."
(labels ((make-map (size)
(let ((result (make-hash-table)))
(loop
for i from 1 to size
do (setf (gethash i result) (- i)))
result))
(make-alist (size)
(let ((result ()))
(loop
for i from 1 to size
do (push (cons i (- i)) result))
result))
(make-plist (size)
(loop
for i from 1 to size
append (list (intern (write-to-string i) :keyword) (- i)))))
(is (equalp #(#x8A)
(subseq (mpk:encode (make-map 10)) 0 1)))
(is (equalp #(#x8A)
(subseq (mpk:encode (make-alist 10)) 0 1)))
(is (equalp #(#x8A)
(subseq (mpk:encode (make-plist 10)) 0 1)))
(is (equalp #(#xDE #x00 #x10)
(subseq (mpk:encode (make-map 16)) 0 3)))
(is (equalp #(#xDE #x00 #x10)
(subseq (mpk:encode (make-alist 16)) 0 3)))
(is (equalp #(#xDE #x00 #x10)
(subseq (mpk:encode (make-plist 16)) 0 3)))
(is (equalp #(#xDF #x00 #x01 #x00 #x00)
(subseq (mpk:encode (make-map 65536))
0 5)))
(is (equalp #(#xDF #x00 #x01 #x00 #x00)
(subseq (mpk:encode (make-alist 65536))
0 5)))
(is (equalp #(#xDF #x00 #x01 #x00 #x00)
(subseq (mpk:encode (make-plist 65536))
0 5)))))
(test extension-types
"Test that extended types are encoded properly."
(let ((mpk:*extended-types*
(mpk:define-extension-types
'(7 type1))))
(is (equalp #(#xC7 #x01 #x07 #x09)
(mpk:encode (make-instance 'type1 'messagepack-sym:id 9))))))
(test decoding-integers
"Test that (equalp (decode (encode data)) data) for integers (that
can be encoded as Message Pack integers."
(is (eql 100 (mpk:decode (mpk:encode 100))))
(is (eql -30 (mpk:decode (mpk:encode -30))))
(is (eql 12345 (mpk:decode (mpk:encode 12345))))
(is (eql 2390493 (mpk:decode (mpk:encode 2390493))))
(is (eql 2390493000 (mpk:decode (mpk:encode 2390493000)))))
(test decoding-bools
"Test that (equalp (decode (encode data)) data) for bools."
(is (eql t (mpk:decode (mpk:encode t))))
(is (eql nil (mpk:decode (mpk:encode nil))))
(is (eql 'null (let ((mpk:*use-null* t))
(mpk:decode (mpk:encode nil))))))
(test decoding-floats
"Test that (equalp (decode (encode data)) data) for floats."
#+ (or sbcl ccl) (is (eql 100d0 (mpk:decode (mpk:encode 100d0))))
#+ (or sbcl ccl) (is (eql -100d0 (mpk:decode (mpk:encode -100d0))))
#+ (or sbcl ccl) (is (eql -1.2345678901234567e19
(mpk:decode (mpk:encode -1.2345678901234567e19))))
#+ sbcl (is (eql 102s0 (mpk:decode (mpk:encode 102s0))))
#+ sbcl (is (eql -102s0 (mpk:decode (mpk:encode -102s0)))))
(test decoding-strings
"Test that (equalp (decode (encode data)) data) holds for strings."
(let ((*print-pretty* nil))
(let ((short-string "test")
(medium-string (with-output-to-string (str)
(loop repeat 10 do (princ "test" str))))
(long-string (with-output-to-string (str)
(loop repeat (expt 10 5) do (princ "test" str)))))
(is (string= short-string (mpk:decode (mpk:encode short-string))))
(is (string= medium-string (mpk:decode (mpk:encode medium-string))))
(is (string= long-string (mpk:decode (mpk:encode long-string)))))))
(test decoding-binary
"Test decoding of binary type."
(is (equalp #() (mpk:decode #(#xc4 #x00))))
(is (equalp #() (mpk:decode #(#xc5 #x00 #x00))))
(is (equalp #() (mpk:decode #(#xc6 #x00 #x00 #x00 #x00))))
(flet ((to-b8array (&rest contents)
(apply #'concatenate '(vector (unsigned-byte 8)) contents)))
(let ((short-array #(1 2 3))
(medium-array (make-array 1000 :initial-element #xd1))
(long-array (make-array 70000 :initial-element #xd1)))
(is (equalp short-array (mpk:decode (to-b8array #(#xc4 #x03) short-array))))
(is (equalp short-array (mpk:decode (to-b8array #(#xc5 #x00 #x03) short-array))))
(is (equalp medium-array (mpk:decode (to-b8array #(#xc5 #x03 #xe8) medium-array))))
(is (equalp short-array (mpk:decode (to-b8array #(#xc6 #x00 #x00 #x00 #x03) short-array))))
(is (equalp medium-array (mpk:decode (to-b8array #(#xc6 #x00 #x00 #x03 #xe8) medium-array))))
(is (equalp long-array (mpk:decode (to-b8array #(#xc6 #x00 #x01 #x11 #x70) long-array)))))
(let ((mpk:*decode-bin-as-string* T)
(short-string "tes")
(medium-string (with-output-to-string (str)
(loop repeat (/ 1000 5) do (princ "tests" str))))
(long-string (with-output-to-string (str)
(loop repeat (/ 70000 5) do (princ "tests" str)))))
(is (string= short-string (mpk:decode (to-b8array #(#xc4 #x03) (babel:string-to-octets short-string)))))
(is (string= short-string (mpk:decode (to-b8array #(#xc5 #x00 #x03) (babel:string-to-octets short-string)))))
(is (string= medium-string (mpk:decode (to-b8array #(#xc5 #x03 #xe8) (babel:string-to-octets medium-string)))))
(is (string= short-string (mpk:decode (to-b8array #(#xc6 #x00 #x00 #x00 #x03) (babel:string-to-octets short-string)))))
(is (string= medium-string (mpk:decode (to-b8array #(#xc6 #x00 #x00 #x03 #xe8) (babel:string-to-octets medium-string)))))
(is (string= long-string (mpk:decode (to-b8array #(#xc6 #x00 #x01 #x11 #x70) (babel:string-to-octets long-string))))))))
(test decoding-arrays
"Test that (equalp (decode (encode data)) data) holds for arrays."
(let ((short-array #(1 2 3))
(medium-array (make-array 1000 :initial-element 10))
(long-array (make-array 70000 :initial-element 10)))
(is (equalp short-array (mpk:decode (mpk:encode short-array))))
(is (equalp medium-array (mpk:decode (mpk:encode medium-array))))
(is (equalp long-array (mpk:decode (mpk:encode long-array))))))
(test decoding-lists
"Test that (equalp (decode (encode data)) data) holds for lists,
with the proper options."
(labels ((mk-list (size)
(let (result)
(dotimes (i size)
(push i result))
result)))
(let ((mpk:*decoder-prefers-lists* t))
(let ((short-list (mk-list 10))
(medium-list (mk-list 1000))
(long-list (mk-list 70000)))
(is (equalp short-list (mpk:decode (mpk:encode short-list))))
(is (equalp medium-list (mpk:decode (mpk:encode medium-list))))
(is (equalp long-list (mpk:decode (mpk:encode long-list))))))))
(defun make-map (size)
(let ((result (make-hash-table :test #'equalp)))
(loop
for i from 1 to size
do (setf (gethash i result) (- i)))
result))
(test decoding-maps
"Test that (equalp (decode (encode data)) data) holds for hash
tables that have #'equalp as test."
(let ((small-map (make-map 10))
(medium-map (make-map 1000))
(big-map (make-map 70000)))
(is (equalp small-map (mpk:decode (mpk:encode small-map))))
(is (equalp medium-map (mpk:decode (mpk:encode medium-map))))
(is (equalp big-map (mpk:decode (mpk:encode big-map))))))
(test extension-types-decoding
"Test that extended types are decoded properly."
(dolist (test '((:byte-array #(100 200 250 5) #(100 14 1 200))
(:numeric 27 100)))
(destructuring-bind (encoding-type id1 id2) test
(let ((mpk:*extended-types*
(mpk:define-extension-types
`(,encoding-type 120 typea typeb)))
(mpk:*lookup-table* (mpk:make-lookup-table)))
(let* ((a1 (make-instance 'typea 'messagepack-sym:id id1))
(a2 (make-instance 'typea 'messagepack-sym:id id2))
(b1 (make-instance 'typeb 'messagepack-sym:id id1)))
(is (eq (mpk:decode (mpk:encode a1)) a1))
(is (eq (mpk:decode (mpk:encode a2)) a2))
(is (eq (mpk:decode (mpk:encode b1)) b1))
(is (eq (mpk:decode (mpk:encode a1)) (mpk:decode (mpk:encode a1))))
(is (eq (mpk:decode (mpk:encode a2)) (mpk:decode (mpk:encode a2))))
(is (not (eq (mpk:decode (mpk:encode a1)) (mpk:decode (mpk:encode a2)))))
(is (not (eq (mpk:decode (mpk:encode b1)) (mpk:decode (mpk:encode a1)))))
(is (not (eq (mpk:decode (mpk:encode b1)) (mpk:decode (mpk:encode a2))))))))))
(test decoding-maps-to-alists
"Tests that decoding maps to alists works as expected."
(let ((mpk:*decoder-prefers-alists* t))
(is (equal '((1 . -1) (2 . -2) (3 . -3))
(mpk:decode (mpk:encode (make-map 3)))))))