1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; Free Software published under an MIT-like license. See LICENSE ;;;
5 ;;; Copyright (c) 2012 Google, Inc. All rights reserved. ;;;
7 ;;; Original author: Scott McKay ;;;
8 ;;; Based on original work by Robert Brown ;;;
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 (in-package "PROTO-TEST")
17 (qtest:define-test length32-test ()
18 (qtest:assert-equal (length32 0) 1)
19 (qtest:assert-equal (length32 1) 1)
20 (qtest:assert-equal (length32 127) 1)
21 (qtest:assert-equal (length32 128) 2)
22 (qtest:assert-equal (length32 16383) 2)
23 (qtest:assert-equal (length32 16384) 3)
24 (qtest:assert-equal (length32 (ash 1 31)) 5))
26 (qtest:define-test length64-test ()
27 (qtest:assert-equal (length64 0) 1)
28 (qtest:assert-equal (length64 1) 1)
29 (qtest:assert-equal (length64 127) 1)
30 (qtest:assert-equal (length64 128) 2)
31 (qtest:assert-equal (length64 16383) 2)
32 (qtest:assert-equal (length64 16384) 3)
33 (qtest:assert-equal (length64 (- (ash 1 21) 1)) 3)
34 (qtest:assert-equal (length64 (ash 1 21)) 4)
35 (qtest:assert-equal (length64 (ash 1 63)) 10))
38 (qtest:define-test uint32-test ()
39 (let* ((val #xE499867)
40 (encoding #(#xE7 #xB0 #xA6 #x72))
41 (len (length encoding))
42 (buf (make-byte-vector len)))
43 (let ((idx (encode-uint32 val buf 0)))
44 (qtest:assert-equal idx len)
45 (qtest:assert-true (equalp buf encoding))
46 (multiple-value-bind (nval nidx)
48 (qtest:assert-equal nidx len)
49 (qtest:assert-equal nval val)))))
51 (qtest:define-test uint64-test ()
52 (let* ((val #xE4998679470D98D)
53 (encoding #(#x8D #xB3 #xC3 #xA3 #xF9 #x8C #xE6 #xA4 #x0E))
54 (len (length encoding))
55 (buf (make-byte-vector len)))
56 (let ((idx (encode-uint64 val buf 0)))
57 (qtest:assert-equal idx len)
58 (qtest:assert-true (equalp buf encoding))
59 (multiple-value-bind (nval nidx)
61 (qtest:assert-equal nidx len)
62 (qtest:assert-equal nval val)))))
65 (defvar $max-bytes-32 5)
66 (defvar $max-bytes-64 10)
68 (qtest:define-test powers-varint-test ()
69 (let ((buffer (make-byte-vector (* 128 $max-bytes-64)))
73 ;; Smaller powers are encoded as both 32-bit and 64-bit varints
76 (setq index (encode-uint32 (ash 1 p) buffer index)))
77 (setq index (encode-uint64 (ash 1 p) buffer index)))
84 (multiple-value-bind (val idx)
85 (decode-uint32 buffer index)
86 (qtest:assert-equal val (ash 1 p))
88 (multiple-value-bind (val idx)
89 (decode-uint64 buffer index)
90 (qtest:assert-equal val (ash 1 p))
92 (qtest:assert-equal index length)
98 (setq index (skip-element buffer index $wire-type-varint)))
99 (setq index (skip-element buffer index $wire-type-varint)))
100 (qtest:assert-equal index length)))
102 (qtest:define-test random-varint-test ()
103 ;; Encode 1000 random numbers as both 32-bit and 64-bit varints
105 (buf32 (make-byte-vector (* count $max-bytes-32)))
106 (buf64 (make-byte-vector (* count $max-bytes-64)))
107 (vals32 (make-array count))
108 (vals64 (make-array count))
112 (let* ((val64 (random (ash 1 64)))
113 (val32 (ldb (byte 32 0) val64)))
114 (setf (aref vals32 i) val32)
115 (setf (aref vals64 i) val64)
116 (setq index32 (encode-uint32 val32 buf32 index32))
117 (setq index64 (encode-uint64 val64 buf64 index64))))
119 ;; Test the decodings
123 (multiple-value-bind (val32 idx)
124 (decode-uint32 buf32 index32)
125 (qtest:assert-equal val32 (aref vals32 i))
127 (multiple-value-bind (val64 idx)
128 (decode-uint64 buf64 index64)
129 (qtest:assert-equal val64 (aref vals64 i))
130 (setq index64 idx)))))
133 (qtest:define-test-suite varint-tests ()
141 (qtest:register-test 'varint-tests)