Skip to content

Commit

Permalink
have tests use hex, update error message
Browse files Browse the repository at this point in the history
  • Loading branch information
kmarker1101 committed Jul 3, 2024
1 parent a23080b commit ef2c7ad
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 44 deletions.
25 changes: 11 additions & 14 deletions exercises/practice/variable-length-quantity/.meta/example.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,34 @@
;;; Code:


(define-error 'incomplete "incomplete sequence")


(defun encode (numbers)
"Encode a list of numbers into VLQ format."
(defun encode (hex-numbers)
"Encode a list of hexadecimal numbers into VLQ format, returning a list of hexadecimal values."
(let ((encode-single (lambda (number)
(let ((byte-string (list (logand number 127)))) ; Initialize with the least significant byte
(let ((byte-string (list (logand number #x7F)))) ; Initialize with the least significant byte
(setq number (lsh number -7)) ; Shift right before the loop
(while (> number 0)
(push (logior (logand number 127) 128) byte-string)
(push (logior (logand number #x7F) #x80) byte-string)
(setq number (lsh number -7)))
byte-string))))
(apply #'append (mapcar encode-single numbers))))
(apply #'append (mapcar encode-single hex-numbers))))


(defun decode (byte-string)
"Decode a byte string from VLQ format into numbers."
(defun decode (hex-values)
"Decode a list of hexadecimal values from VLQ format into hexadecimal numbers."
(let ((values '())
(number 0)
(incomplete t))
(dolist (byte byte-string)
(dolist (byte hex-values)
(setq number (lsh number 7))
(setq number (logior number (logand byte 127)))
(if (= (logand byte 128) 0)
(setq number (logior number (logand byte #x7F)))
(if (= (logand byte #x80) 0)
(progn
(setq values (cons number values))
(setq number 0)
(setq incomplete nil))
(setq incomplete t)))
(if incomplete
(signal 'incomplete 'nil)
(error "imcomplete sequence")
(reverse values))))


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,107 +11,107 @@


(ert-deftest zero ()
(should (equal (encode '(0)) '(0))))
(should (equal (encode '(#x0)) '(#x0))))


(ert-deftest arbitrary-single-byte ()
(should (equal (encode '(64)) '(64))))
(should (equal (encode '(#x40)) '(#x40))))


(ert-deftest largest-single-byte ()
(should (equal (encode '(127)) '(127))))
(should (equal (encode '(#x7F)) '(#x7F))))


(ert-deftest smallest-double-byte ()
(should (equal (encode '(128)) '(129 0))))
(should (equal (encode '(#x80)) '(#x81 #x0))))


(ert-deftest arbitrary-double-byte ()
(should (equal (encode '(8192)) '(192 0))))
(should (equal (encode '(#x2000)) '(#xC0 #x0))))


(ert-deftest largest-double-byte ()
(should (equal (encode '(16383)) '(255 127))))
(should (equal (encode '(#x3FFF)) '(#xFF #x7F))))


(ert-deftest smallest-triple-byte ()
(should (equal (encode '(16384)) '(129 128 0))))
(should (equal (encode '(#x4000)) '(#x81 #x80 #x0))))


(ert-deftest arbitrary-triple-byte ()
(should (equal (encode '(1048576)) '(192 128 0))))
(should (equal (encode '(#x100000)) '(#xC0 #x80 #x0))))


(ert-deftest largest-triple-byte ()
(should (equal (encode '(2097151)) '(255 255 127))))
(should (equal (encode '(#x1FFFFF)) '(#xFF #xFF #x7F))))


(ert-deftest smallest-quadruple-byte ()
(should (equal (encode '(2097152)) '(129 128 128 0))))
(should (equal (encode '(#x200000)) '(#x81 #x80 #x80 #x0))))


(ert-deftest arbitrary-quadruple-byte ()
(should (equal (encode '(134217728)) '(192 128 128 0))))
(should (equal (encode '(#x8000000)) '(#xC0 #x80 #x80 #x0))))


(ert-deftest largest-quadruple-byte ()
(should (equal (encode '(268435455)) '(255 255 255 127))))
(should (equal (encode '(#xFFFFFFF)) '(#xFF #xFF #xFF #x7F))))


(ert-deftest smallest-quintuple-byte ()
(should (equal (encode '(268435456)) '(129 128 128 128 0))))
(should (equal (encode '(#x10000000)) '(#x81 #x80 #x80 #x80 #x0))))


(ert-deftest arbitrary-quintuple-byte ()
(should (equal (encode '(4278190080)) '(143 248 128 128 0))))
(should (equal (encode '(#xFF000000)) '(#x8F #xF8 #x80 #x80 #x0))))


(ert-deftest maximum-32-bit-integer-input ()
(should (equal (encode '(4294967295)) '(143 255 255 255 127))))
(should (equal (encode '(#xFFFFFFFF)) '(#x8F #xFF #xFF #xFF #x7F))))


(ert-deftest two-single-byte-values ()
(should (equal (encode '(64 127)) '(64 127))))
(should (equal (encode '(#x40 #x7F)) '(#x40 #x7F))))


(ert-deftest two-multi-byte-values ()
(should (equal (encode '(16384 1193046)) '(129 128 0 200 232 86))))
(should (equal (encode '(#x4000 #x123456)) '(#x81 #x80 #x0 #xC8 #xE8 #x56))))


(ert-deftest many-multi-byte-values ()
(should (equal (encode '(8192 1193046 268435455 0 16383 16384)) '(192 0 200 232 86 255 255 255 127 0 255 127 129 128 0))))
(should (equal (encode '(#x2000 #x123456 #xFFFFFFF #x0 #x3FFF #x4000)) '(#xC0 #x0 #xC8 #xE8 #x56 #xFF #xFF #xFF #x7F #x0 #xFF #x7F #x81 #x80 #x0))))


(ert-deftest one-byte ()
(should (equal (decode '(127)) '(127))))
(should (equal (decode '(#x7F)) '(#x7F))))


(ert-deftest two-bytes ()
(should (equal (decode '(192 0)) '(8192))))
(should (equal (decode '(#xC0 #x0)) '(#x2000))))


(ert-deftest three-bytes ()
(should (equal (decode '(255 255 127)) '(2097151))))
(should (equal (decode '(#xFF #xFF #x7F)) '(#x1FFFFF))))


(ert-deftest four-bytes ()
(should (equal (decode '(129 128 128 0)) '(2097152))))
(should (equal (decode '(#x81 #x80 #x80 #x0)) '(#x200000))))


(ert-deftest maximum-32-bit-integer ()
(should (equal (decode '(143 255 255 255 127)) '(4294967295))))
(should (equal (decode '(#x8F #xFF #xFF #xFF #x7F)) '(#xFFFFFFFF))))


(ert-deftest incomplete-sequence-causes-error ()
(should-error (decode '(255)) :type 'incomplete))
(should-error (decode '(255))))


(ert-deftest incomplete-sequence-causes-error-even-if-value-is-zero ()
(should-error (decode '(128)) :type 'incomplete))
(should-error (decode '(128))))


(ert-deftest multiple-values ()
(should (equal (decode '(192 0 200 232 86 255 255 255 127 0 255 127 129 128 0)) '(8192 1193046 268435455 0 16383 16384))))
(should (equal (decode '(#xC0 #x0 #xC8 #xE8 #x56 #xFF #xFF #xFF #x7F #x0 #xFF #x7F #x81 #x80 #x0)) '(#x2000 #x123456 #xFFFFFFF #x0 #x3FFF #x4000))))


(provide 'variable-length-quantity-test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
;;; Code:


(define-error 'incomplete
(error "Delete this S-Expression and write your own implementation"))


(defun encode (integers)
(error "Delete this S-Expression and write your own implementation"))

Expand Down

0 comments on commit ef2c7ad

Please sign in to comment.