summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2021-01-03 18:23:50 +0100
committerpukkamustard <pukkamustard@posteo.net>2021-01-03 18:23:50 +0100
commit15be0f473382cd287633e187681bcbc47fb0383e (patch)
tree5e8d4e64d5a06702ca4d04c8fb0c30b0b054e926
parent9c6711a2af82ab4111bf0e36f332c6a7520f2136 (diff)
(cbor tests): initial tests using examples from specificatoin
-rw-r--r--Makefile.am2
-rw-r--r--README.org18
-rw-r--r--cbor.scm5
-rw-r--r--cbor/encode.scm4
-rw-r--r--guix.scm13
-rw-r--r--hall.scm2
-rw-r--r--tests/cbor.scm52
7 files changed, 82 insertions, 14 deletions
diff --git a/Makefile.am b/Makefile.am
index b0c9687..016fa25 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -39,7 +39,7 @@ SOURCES = cbor.scm \
cbor/encode.scm \
cbor/tag.scm
-TESTS =
+TESTS = tests/cbor.scm
TEST_EXTENSIONS = .scm
SCM_LOG_DRIVER = \
diff --git a/README.org b/README.org
index 4f78234..8a1e1f2 100644
--- a/README.org
+++ b/README.org
@@ -19,15 +19,27 @@ guile-cbor is a Guile implementation of CBOR (Concise Binary Object Representati
| alist | map (major type 5) | |
| <cbor-tag> | tagged data item (major type 6) | |
| - | IEEE 754 Half-Precision Float (major type 7) | Can not be parsed (FIXME) |
-| real | IEEE 754 Double-Precision Float (major type 7) | Scheme reals are alwyas encoded as CBOR double precision floats |
-| real | IEEE 754 Single-Precision Float (major type 7) | CBOR single precision floats are parsed as Schme real numbers |
+| real | IEEE 754 Double-Precision Float (major type 7) | Scheme reals are always encoded as CBOR double precision floats |
+| real | IEEE 754 Single-Precision Float (major type 7) | CBOR single precision floats are parsed as Scheme real numbers |
| #f | false (major type 7, simple value 20) | |
| #t | true (major type 7, simple value 21) | |
| #nil | null (major type 7, simple value 22) | |
-
* Todos
+** TODO Extended Generic Data Models
+
+Currently not the full "extended generic data model" is supported (only simple values ~#f~, ~#t~ and ~#nil~ are supported).
+
+It might be nice to support the full extended generic data model (bignums, decimal fractions, bigfloats and date/time tags).
+
+** TODO Indefinite Lenght *
+
+Currently no support.
+
** TODO Tests
+
+Some examples from the specification have been added.
+
** TODO texinfo documentation
* License
diff --git a/cbor.scm b/cbor.scm
index 2b35e09..fa75ea1 100644
--- a/cbor.scm
+++ b/cbor.scm
@@ -3,7 +3,10 @@
#:use-module (rnrs io ports)
#:use-module (cbor encode)
- #:use-module (cbor decode))
+ #:use-module (cbor decode)
+
+ #:export (scm->cbor
+ cbor->scm))
(define (scm->cbor native)
(call-with-values open-bytevector-output-port
diff --git a/cbor/encode.scm b/cbor/encode.scm
index 064926b..04c827d 100644
--- a/cbor/encode.scm
+++ b/cbor/encode.scm
@@ -51,11 +51,11 @@
(cond
;; unsigned integer
- ((and (integer? value) (<= 0 value))
+ ((and (exact-integer? value) (<= 0 value))
(put-cbor-integer port 0 value))
;; negative integer
- ((and (integer? value) (> 0 value))
+ ((and (exact-integer? value) (> 0 value))
(put-cbor-integer port 1 (- -1 value)))
((bytevector? value)
diff --git a/guix.scm b/guix.scm
index e6cefe1..02ebcfb 100644
--- a/guix.scm
+++ b/guix.scm
@@ -17,16 +17,17 @@
(build-system gnu-build-system)
(arguments `())
(native-inputs
- `(("autoconf" ,autoconf)
- ("automake" ,automake)
- ("pkg-config" ,pkg-config)
- ("texinfo" ,texinfo)))
+ `(("guile-hall" ,guile-hall)
+ ("autoconf" ,autoconf)
+ ("automake" ,automake)
+ ("pkg-config" ,pkg-config)
+ ("texinfo" ,texinfo)))
(inputs `(("guile" ,guile-3.0)))
(propagated-inputs `())
(synopsis "Guile implementation of CBOR")
(description
- "This package provides a Guile implementation of the Concise Binary\nObject Representation (CBOR) as defined by RFC 8949.")
+ "This package provides a Guile implementation of the Concise Binary\nObject Representation (CBOR) as defined by RFC 8949.")
(home-page
- "https://inqlab.net/git/guile-cbor.git")
+ "https://inqlab.net/git/guile-cbor.git")
(license license:gpl3+))
diff --git a/hall.scm b/hall.scm
index 63fbe8a..f0f6720 100644
--- a/hall.scm
+++ b/hall.scm
@@ -16,7 +16,7 @@ Object Representation (CBOR) as defined by RFC 8949.")
(directory "cbor" ((scheme-file "decode")
(scheme-file "encode")
(scheme-file "tag")))))
- (tests ((directory "tests" ())))
+ (tests ((directory "tests" ((scheme-file "cbor")))))
(programs ((directory "scripts" ())))
(documentation
((org-file "README")
diff --git a/tests/cbor.scm b/tests/cbor.scm
new file mode 100644
index 0000000..1a0cc73
--- /dev/null
+++ b/tests/cbor.scm
@@ -0,0 +1,52 @@
+(define-module (tests cbor)
+ #:use-module (cbor)
+
+ #:use-module (rnrs bytevectors)
+
+ #:use-module (srfi srfi-64))
+
+(define specification-examples
+ '((0 . (#x00))
+ (1 . (#x01))
+ (10 . (#x0a))
+ (23 . (#x17))
+ (24 . (#x18 #x18))
+ (25 . (#x18 #x19))
+ (100 . (#x18 #x64))
+ (1000 . (#x19 #x03 #xe8))
+ (1000000 . (#x1a #x00 #x0f #x42 #x40))
+ (1000000000000 . (#x1b #x00 #x00 #x00 #xe8 #xd4 #xa5 #x10 #x00))
+ (18446744073709551615 . (#x1b #xff #xff #xff #xff #xff #xff #xff #xff))
+ ;; requires bignums
+ ;; (18446744073709551616 . (#xc2 #x49 #x01 #x00 #x00 #x00 #x00 #x00 #x00 #x00 #x00))
+ (-18446744073709551616 . (#x3b #xff #xff #xff #xff #xff #xff #xff #xff))
+ ;; requires bignums
+ ;; (-18446744073709551617 . (#xc3 #x49 #x01 #x00 #x00 #x00 #x00 #x00 #x00 #x00 #x00))
+ (-1 . (#x20))
+ (-10 . (#x29))
+ (-100 . (#x38 #x63))
+ ;; requires support for IEE 754 half precission
+ ;; (0.0 . (#xf9 #x00 #x00))
+ ;; (-0.0 . (#xf9 #x80 #x00))
+ ;; (1.0 . (#xf9 #x3c #x00))
+ (1.1 . (#xfb #x3f #xf1 #x99 #x99 #x99 #x99 #x99 #x9a))
+ ;; requires support for IEE 754 half precission
+ ;; (1.5 . (#xf9 #x3e #x00))
+
+ )
+ )
+
+(scm->cbor 1.1)
+
+(scm->cbor 18446744073709551616)
+(1+ 18446744073709551616)
+
+(test-begin "Examples from specification")
+(for-each
+ (lambda (example)
+ (let* ((native (car example))
+ (encoded (u8-list->bytevector (cdr example))))
+ (test-assert (bytevector=? (scm->cbor native) encoded))
+ (test-equal (cbor->scm encoded) native)))
+ specification-examples)
+(test-end "Examples from specification")