From e07c58f24608806d81417f70df4d904b4c5fffe3 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 29 May 2022 17:15:04 -0400 Subject: [PATCH 1/7] ; bindat-tests (strz): Add more tests --- test/lisp/emacs-lisp/bindat-tests.el | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/lisp/emacs-lisp/bindat-tests.el b/test/lisp/emacs-lisp/bindat-tests.el index 7722cf6c02..fbe574e490 100644 --- a/test/lisp/emacs-lisp/bindat-tests.el +++ b/test/lisp/emacs-lisp/bindat-tests.el @@ -162,4 +162,69 @@ bindat-test--recursive (bindat-pack bindat-test--LEB128 n)) n))))))) +(let ((spec (bindat-type strz 2))) + (ert-deftest bindat-test--strz-fixedlen-len () + (should (equal (bindat-length spec "") 2)) + (should (equal (bindat-length spec "a") 2))) + + (ert-deftest bindat-test--strz-fixedlen-len-overflow () + (should (equal (bindat-length spec "abc") 2))) + + (ert-deftest bindat-test--strz-fixedlen-pack () + (should (equal (bindat-pack spec "") "\0\0")) + (should (equal (bindat-pack spec "a") "a\0"))) + + (ert-deftest bindat-test--strz-fixedlen-pack-overflow () + :expected-result :failed + (should (equal (bindat-pack spec "abc") "a\0"))) + + (ert-deftest bindat-test--strz-fixedlen-unpack () + ;; There are no tests for unpacking "ab" or "ab\0" because those + ;; packed strings cannot be produced from the spec (packing "ab" + ;; should produce "a\0", not "ab" or "ab\0"). + (should (equal (bindat-unpack spec "\0\0") "")) + (should (equal (bindat-unpack spec "a\0") "a")))) + +(let ((spec (bindat-type strz))) + (ert-deftest bindat-test--strz-varlen-len () + :expected-result :failed + (should (equal (bindat-length spec "") 1)) + (should (equal (bindat-length spec "abc") 4))) + + (ert-deftest bindat-test--strz-varlen-pack () + :expected-result :failed + (should (equal (bindat-pack spec "") "\0")) + (should (equal (bindat-pack spec "abc") "abc\0"))) + + (ert-deftest bindat-test--strz-varlen-unpack () + :expected-result :failed + ;; There is no test for unpacking a string without a null + ;; terminator because such packed strings cannot be produced from + ;; the spec (packing "a" should produce "a\0", not "a"). + (should (equal (bindat-unpack spec "\0") "")) + (should (equal (bindat-unpack spec "abc\0") "abc")))) + +(let ((spec '((x strz 2)))) + (ert-deftest bindat-test--strz-legacy-fixedlen-len () + (should (equal (bindat-length spec '((x . ""))) 2)) + (should (equal (bindat-length spec '((x . "a"))) 2))) + + (ert-deftest bindat-test--strz-legacy-fixedlen-len-overflow () + (should (equal (bindat-length spec '((x . "abc"))) 2))) + + (ert-deftest bindat-test--strz-legacy-fixedlen-pack () + (should (equal (bindat-pack spec '((x . ""))) "\0\0")) + (should (equal (bindat-pack spec '((x . "a"))) "a\0"))) + + (ert-deftest bindat-test--strz-legacy-fixedlen-pack-overflow () + :expected-result :failed + (should (equal (bindat-pack spec '((x . "abc"))) "a\0"))) + + (ert-deftest bindat-test--strz-legacy-fixedlen-unpack () + ;; There are no tests for unpacking "ab" or "ab\0" because those + ;; packed strings cannot be produced from the spec (packing "ab" + ;; should produce "a\0", not "ab" or "ab\0"). + (should (equal (bindat-unpack spec "\0\0") '((x . "")))) + (should (equal (bindat-unpack spec "a\0") '((x . "a")))))) + ;;; bindat-tests.el ends here -- 2.36.1