comparison tests/automated/base64-tests.el @ 4855:189fb67ca31a

Create Assert-eq, Assert-equal, etc. These are equivalent to (Assert (eq ...)) but display both the actual value and the expected value of the comparison. Use them throughout the test suite.
author Ben Wing <ben@xemacs.org>
date Thu, 14 Jan 2010 02:18:03 -0600
parents abe6d1db359e
children 0f66906b6e37
comparison
equal deleted inserted replaced
4854:95c4ced5c07c 4855:189fb67ca31a
50 (with-current-buffer bt-test-buffer 50 (with-current-buffer bt-test-buffer
51 ;; the whole buffer 51 ;; the whole buffer
52 (erase-buffer) 52 (erase-buffer)
53 (insert string) 53 (insert string)
54 (setq length (base64-encode-region (point-min) (point-max) no-line-break)) 54 (setq length (base64-encode-region (point-min) (point-max) no-line-break))
55 (Assert (eq length (- (point-max) (point-min)))) 55 (Assert-eq length (- (point-max) (point-min)))
56 (Assert (equal (buffer-string) string-result)) 56 (Assert-equal (buffer-string) string-result)
57 ;; partial 57 ;; partial
58 (erase-buffer) 58 (erase-buffer)
59 (insert "random junk........\0\0';'eqwrkw[erpqf") 59 (insert "random junk........\0\0';'eqwrkw[erpqf")
60 (let ((p1 (point)) p2) 60 (let ((p1 (point)) p2)
61 (insert string) 61 (insert string)
62 (setq p2 (point-marker)) 62 (setq p2 (point-marker))
63 (insert "...more random junk.q,f3/.qrm314.r,m2typ' 2436T@W$^@$#^T@") 63 (insert "...more random junk.q,f3/.qrm314.r,m2typ' 2436T@W$^@$#^T@")
64 (setq length (base64-encode-region p1 p2 no-line-break)) 64 (setq length (base64-encode-region p1 p2 no-line-break))
65 (Assert (eq length (- p2 p1))) 65 (Assert-eq length (- p2 p1))
66 (Assert (equal (buffer-substring p1 p2) string-result)))) 66 (Assert-equal (buffer-substring p1 p2) string-result)))
67 string-result)) 67 string-result))
68 68
69 (defun bt-base64-decode-string (string) 69 (defun bt-base64-decode-string (string)
70 (let ((string-result (base64-decode-string string)) 70 (let ((string-result (base64-decode-string string))
71 length) 71 length)
73 ;; the whole buffer 73 ;; the whole buffer
74 (erase-buffer) 74 (erase-buffer)
75 (insert string) 75 (insert string)
76 (setq length (base64-decode-region (point-min) (point-max))) 76 (setq length (base64-decode-region (point-min) (point-max)))
77 (cond (string-result 77 (cond (string-result
78 (Assert (eq length (- (point-max) (point-min)))) 78 (Assert-eq length (- (point-max) (point-min)))
79 (Assert (equal (buffer-string) string-result))) 79 (Assert-equal (buffer-string) string-result))
80 (t 80 (t
81 (Assert (null length)) 81 (Assert (null length))
82 ;; The buffer should not have been modified. 82 ;; The buffer should not have been modified.
83 (Assert (equal (buffer-string) string)))) 83 (Assert-equal (buffer-string) string)))
84 ;; partial 84 ;; partial
85 (erase-buffer) 85 (erase-buffer)
86 (insert "random junk........\0\0';'eqwrkw[erpqf") 86 (insert "random junk........\0\0';'eqwrkw[erpqf")
87 (let ((p1 (point)) p2) 87 (let ((p1 (point)) p2)
88 (insert string) 88 (insert string)
89 (setq p2 (point-marker)) 89 (setq p2 (point-marker))
90 (insert "...more random junk.q,f3/.qrm314.\0\0r,m2typ' 2436T@W$^@$#T@") 90 (insert "...more random junk.q,f3/.qrm314.\0\0r,m2typ' 2436T@W$^@$#T@")
91 (setq length (base64-decode-region p1 p2)) 91 (setq length (base64-decode-region p1 p2))
92 (cond (string-result 92 (cond (string-result
93 (Assert (eq length (- p2 p1))) 93 (Assert-eq length (- p2 p1))
94 (Assert (equal (buffer-substring p1 p2) string-result))) 94 (Assert-equal (buffer-substring p1 p2) string-result))
95 (t 95 (t
96 (Assert (null length)) 96 (Assert (null length))
97 ;; The buffer should not have been modified. 97 ;; The buffer should not have been modified.
98 (Assert (equal (buffer-substring p1 p2) string)))))) 98 (Assert-equal (buffer-substring p1 p2) string)))))
99 string-result)) 99 string-result))
100 100
101 (defun bt-remove-newlines (str) 101 (defun bt-remove-newlines (str)
102 (apply #'string (delete ?\n (mapcar #'identity str)))) 102 (apply #'string (delete ?\n (mapcar #'identity str))))
103 103
124 ;;----------------------------------------------------- 124 ;;-----------------------------------------------------
125 ;; Encoding base64 125 ;; Encoding base64
126 ;;----------------------------------------------------- 126 ;;-----------------------------------------------------
127 127
128 (loop for (raw encoded) in bt-test-strings do 128 (loop for (raw encoded) in bt-test-strings do
129 (Assert (equal (bt-base64-encode-string raw) encoded)) 129 (Assert-equal (bt-base64-encode-string raw) encoded)
130 ;; test the NO-LINE-BREAK flag 130 ;; test the NO-LINE-BREAK flag
131 (Assert (equal (bt-base64-encode-string raw t) (bt-remove-newlines encoded)))) 131 (Assert-equal (bt-base64-encode-string raw t) (bt-remove-newlines encoded)))
132 132
133 ;; When Mule is around, Lisp programmers should make sure that the 133 ;; When Mule is around, Lisp programmers should make sure that the
134 ;; buffer contains only characters whose `char-int' is in the [0, 256) 134 ;; buffer contains only characters whose `char-int' is in the [0, 256)
135 ;; range. If this condition is not satisfied for any character, an 135 ;; range. If this condition is not satisfied for any character, an
136 ;; error is signaled. 136 ;; error is signaled.
148 ;;----------------------------------------------------- 148 ;;-----------------------------------------------------
149 ;; Decoding base64 149 ;; Decoding base64
150 ;;----------------------------------------------------- 150 ;;-----------------------------------------------------
151 151
152 (loop for (raw encoded) in bt-test-strings do 152 (loop for (raw encoded) in bt-test-strings do
153 (Assert (equal (bt-base64-decode-string encoded) raw)) 153 (Assert-equal (bt-base64-decode-string encoded) raw)
154 (Assert (equal (bt-base64-decode-string (bt-remove-newlines encoded)) raw))) 154 (Assert-equal (bt-base64-decode-string (bt-remove-newlines encoded)) raw))
155 155
156 ;; Test errors 156 ;; Test errors
157 (dolist (str `("foo" "AAC" "foo\0bar" "====" "Zm=9v" ,bt-allchars)) 157 (dolist (str `("foo" "AAC" "foo\0bar" "====" "Zm=9v" ,bt-allchars))
158 (Check-Error error (base64-decode-string str))) 158 (Check-Error error (base64-decode-string str)))
159 159
180 (left (substring encoded 0 middlepos)) 180 (left (substring encoded 0 middlepos))
181 (right (substring encoded middlepos))) 181 (right (substring encoded middlepos)))
182 ;; Whitespace at the beginning, end, and middle. 182 ;; Whitespace at the beginning, end, and middle.
183 (let ((mangled (concat bt-nonbase64-chars left bt-nonbase64-chars right 183 (let ((mangled (concat bt-nonbase64-chars left bt-nonbase64-chars right
184 bt-nonbase64-chars))) 184 bt-nonbase64-chars)))
185 (Assert (equal (bt-base64-decode-string mangled) raw))) 185 (Assert-equal (bt-base64-decode-string mangled) raw))
186 186
187 ;; Whitespace between every char. 187 ;; Whitespace between every char.
188 (let ((mangled (concat bt-nonbase64-chars 188 (let ((mangled (concat bt-nonbase64-chars
189 ;; ENCODED with bt-nonbase64-chars 189 ;; ENCODED with bt-nonbase64-chars
190 ;; between every character. 190 ;; between every character.
191 (mapconcat #'char-to-string encoded 191 (mapconcat #'char-to-string encoded
192 (apply #'string bt-nonbase64-chars)) 192 (apply #'string bt-nonbase64-chars))
193 bt-nonbase64-chars))) 193 bt-nonbase64-chars)))
194 (Assert (equal (bt-base64-decode-string mangled) raw)))))) 194 (Assert-equal (bt-base64-decode-string mangled) raw)))))
195 195
196 ;;----------------------------------------------------- 196 ;;-----------------------------------------------------
197 ;; Mixed... 197 ;; Mixed...
198 ;;----------------------------------------------------- 198 ;;-----------------------------------------------------
199 199
203 ;; (base64-decode-string (base64-decode-string FOO)) equals FOO for 203 ;; (base64-decode-string (base64-decode-string FOO)) equals FOO for
204 ;; any FOO we can think of. The following stunts stress-test 204 ;; any FOO we can think of. The following stunts stress-test
205 ;; practically all aspects of the encoding and decoding process. 205 ;; practically all aspects of the encoding and decoding process.
206 206
207 (loop for (raw ignored) in bt-test-strings do 207 (loop for (raw ignored) in bt-test-strings do
208 (Assert (equal (bt-base64-decode-string 208 (Assert-equal (bt-base64-decode-string
209 (bt-base64-encode-string raw)) 209 (bt-base64-encode-string raw))
210 raw)) 210 raw)
211 (Assert (equal (bt-base64-decode-string 211 (Assert-equal (bt-base64-decode-string
212 (bt-base64-decode-string 212 (bt-base64-decode-string
213 (bt-base64-encode-string 213 (bt-base64-encode-string
214 (bt-base64-encode-string raw)))) 214 (bt-base64-encode-string raw))))
215 raw)) 215 raw)
216 (Assert (equal (bt-base64-decode-string 216 (Assert-equal (bt-base64-decode-string
217 (bt-base64-decode-string 217 (bt-base64-decode-string
218 (bt-base64-decode-string 218 (bt-base64-decode-string
219 (bt-base64-encode-string 219 (bt-base64-encode-string
220 (bt-base64-encode-string 220 (bt-base64-encode-string
221 (bt-base64-encode-string raw)))))) 221 (bt-base64-encode-string raw))))))
222 raw)) 222 raw)
223 (Assert (equal (bt-base64-decode-string 223 (Assert-equal (bt-base64-decode-string
224 (bt-base64-decode-string 224 (bt-base64-decode-string
225 (bt-base64-decode-string 225 (bt-base64-decode-string
226 (bt-base64-decode-string 226 (bt-base64-decode-string
227 (bt-base64-encode-string 227 (bt-base64-encode-string
228 (bt-base64-encode-string 228 (bt-base64-encode-string
229 (bt-base64-encode-string 229 (bt-base64-encode-string
230 (bt-base64-encode-string raw)))))))) 230 (bt-base64-encode-string raw))))))))
231 raw)) 231 raw)
232 (Assert (equal (bt-base64-decode-string 232 (Assert-equal (bt-base64-decode-string
233 (bt-base64-decode-string 233 (bt-base64-decode-string
234 (bt-base64-decode-string 234 (bt-base64-decode-string
235 (bt-base64-decode-string 235 (bt-base64-decode-string
236 (bt-base64-decode-string 236 (bt-base64-decode-string
237 (bt-base64-encode-string 237 (bt-base64-encode-string
238 (bt-base64-encode-string 238 (bt-base64-encode-string
239 (bt-base64-encode-string 239 (bt-base64-encode-string
240 (bt-base64-encode-string 240 (bt-base64-encode-string
241 (bt-base64-encode-string raw)))))))))) 241 (bt-base64-encode-string raw))))))))))
242 raw))) 242 raw))