-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.lisp
157 lines (126 loc) · 4.03 KB
/
tests.lisp
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
(in-package #:common-lisp-user)
(defpackage #:simpbin.test
(:use :cl #:simpbin)
(:import-from #:parachute
#:define-test
#:is
#:true #:false)
(:export #:test-all))
(in-package #:simpbin.test)
;;; integer
(define-test write-integer
(is equalp
(flexi-streams:with-output-to-sequence (output)
(write-integer 42 output))
#(42 0 0 0)))
(define-test read-integer
(is = 42
(flexi-streams:with-input-from-sequence (input #(42 0 0 0))
(read-integer input))))
;;; varint
(define-test write-varint
(is equalp
(flexi-streams:with-output-to-sequence (output)
(write-varint 42 output))
#(42))
(is equalp
(flexi-streams:with-output-to-sequence (output)
(write-varint 800 output))
#(160 6))
(loop :for i :below 128
:collect
(is = `#(,i)
(flexi-streams:with-output-to-sequence (output)
(write-varint i output)))))
(define-test read-varint
(is = 42
(flexi-streams:with-input-from-sequence (input #(42))
(read-varint input)))
(is = 800
(flexi-streams:with-input-from-sequence (input #(160 6))
(read-varint input))))
(define-test varint-roundtrip
(loop :for i :below 100000
:do (assert
(= i (flexi-streams:with-input-from-sequence
(input
(flexi-streams:with-output-to-sequence (output)
(write-varint i output)))
(read-varint input)))
(i))))
;;; header
(define-test write-header
(is equalp
(flexi-streams:with-output-to-sequence (output)
(write-header output))
#(66 73 78 83 0 0 0 0))
(is =
(length
(flexi-streams:with-output-to-sequence (output)
(write-header output)))
8))
(define-test read-header
(true
(zerop
(flexi-streams:with-input-from-sequence (input #(66 73 78 83 0 0 0 0))
(read-header input))))
(parachute:fail
;; This fails because it reaches EOF while reading.
(flexi-streams:with-input-from-sequence (input #())
(read-header input))))
;;; octets
(define-test write-octets
(is equalp
(flexi-streams:with-output-to-sequence (output)
(write-octets (make-array
4
:initial-contents '(1 2 3 4)
:element-type 'nibbles:octet)
output))
#(4 0 0 0 1 2 3 4)))
(define-test read-octets
(is equalp
(flexi-streams:with-input-from-sequence (input #(4 0 0 0 1 2 3 4))
(read-octets input))
#(1 2 3 4)))
;;; string
(define-test write-binary-string
(is equalp
(flexi-streams:with-output-to-sequence (output)
(write-binary-string "hi" output))
#(2 0 0 0 104 105))
(is equalp
(flexi-streams:with-output-to-sequence (output)
(write-binary-string
"this is a string with şömé unicode"
output))
#(37 0 0 0 116 104 105 115 32 105 115 32 97 32 115 116 114 105 110 103 32 119 105 116 104 32 197 159 195 182 109 195 169 32 117 110 105 99 111 100 101)))
(define-test read-binary-string
(is equalp
(flexi-streams:with-input-from-sequence (input #(2 0 0 0 104 105))
(read-binary-string input))
"hi")
(is equalp
(flexi-streams:with-input-from-sequence (input #(37 0 0 0 116 104 105 115 32 105 115 32 97 32 115 116 114 105 110 103 32 119 105 116 104 32 197 159 195 182 109 195 169 32 117 110 105 99 111 100 101))
(read-binary-string input))
"this is a string with şömé unicode"))
;;; with-macros
;; These tests are not part of the unit tests because they interact
;; with the file system.
#|
(with-output-to-binary-file (output "/tmp/test.bins"
:if-exists :overwrite
:if-does-not-exist :create)
(write-header output)
(write-binary-string "Hello" output))
(with-input-from-binary-file (input "/tmp/test.bins")
(read-header input)
(read-binary-string input))
;; => "Hello"
(with-input-from-binary-file (input "/tmp/test.bins")
(read-header input)
(read-binary-string input)
(peek-char nil input nil))
|#
(defun test-all ()
(parachute:test #.*package*))