[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnunet-scheme] 91/324: Define slice-copy! and slice-zero!.
From: |
gnunet |
Subject: |
[gnunet-scheme] 91/324: Define slice-copy! and slice-zero!. |
Date: |
Tue, 21 Sep 2021 13:22:11 +0200 |
This is an automated email from the git hooks/post-receive script.
maxime-devos pushed a commit to branch master
in repository gnunet-scheme.
commit 82826b558a1a50239e55c533574aeb1491f1086e
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Tue Mar 16 21:29:48 2021 +0100
Define slice-copy! and slice-zero!.
These will be used for the ancillary message parsing code,
which will be used to generate more detailed network error
messages than what can be interferenced from the errno.
* tests/bv-slice.scm: New test file, testing slice-copy!
and slice-zero!
* gnu/gnunet/utils/bv-slice.scm (slice-zero!, slice-copy!):
New procedures. Also comment the module more.
* Makefile.am (SCM_TESTS): Register tests/bv-slice.scm.
---
Makefile.am | 3 +-
gnu/gnunet/utils/bv-slice.scm | 46 +++++++++++++++++----
tests/bv-slice.scm | 94 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 134 insertions(+), 9 deletions(-)
diff --git a/Makefile.am b/Makefile.am
index 98d624e..9d236a4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -92,7 +92,8 @@ SCM_TESTS = \
tests/envelope.scm \
tests/message-handler.scm \
tests/update.scm \
- tests/message-io.scm
+ tests/message-io.scm \
+ tests/bv-slice.scm
SCM_TESTS_ENVIRONMENT = \
GUILE_AUTO_COMPILE=0 \
diff --git a/gnu/gnunet/utils/bv-slice.scm b/gnu/gnunet/utils/bv-slice.scm
index 4dfbc2c..a4156b9 100644
--- a/gnu/gnunet/utils/bv-slice.scm
+++ b/gnu/gnunet/utils/bv-slice.scm
@@ -1,5 +1,5 @@
;; This file is part of scheme-GNUnet, a partial Scheme port of GNUnet.
-;; Copyright (C) 2020 Maxime Devos <maxime.devos@student.kuleuven.be>
+;; Copyright (C) 2020, 2021 Maxime Devos <maximedevos@telenet.be>
;;
;; scheme-GNUnet is free software: you can redistribute it and/or modify it
;; under the terms of the GNU Affero General Public License as published
@@ -15,11 +15,6 @@
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;
;; SPDX-License-Identifier: AGPL-3.0-or-later
-;;
-;; As a special exception to the GNU Affero General Public License,
-;; the file may be relicensed under any license used for
-;; most source code of GNUnet 0.13.1, or later versions, as published by
-;; GNUnet e.V.
;; Author: Maxime Devos
;; Source: gnu/gnunet/utils/bv-slice.scm
@@ -28,6 +23,7 @@
(library (gnu gnunet utils bv-slice)
(export slice?
+ ;; Slicing
slice-bv
slice-offset
slice-length
@@ -39,6 +35,7 @@
slice/read-only
slice/write-only
slice/read-write
+ ;; Small operations
slice-u8-ref
slice-u16-ref
slice-u32-ref
@@ -46,13 +43,18 @@
slice-u8-set!
slice-u16-set!
slice-u32-set!
- slice-u64-set!)
+ slice-u64-set!
+ ;; Large operations
+ slice-copy!
+ slice-zero!)
(import (rnrs arithmetic bitwise)
(rnrs base)
(rnrs bytevectors)
(rnrs control)
(rnrs records syntactic)
(srfi srfi-31))
+
+ ;; Slicing
(define-record-type (<slice> %make-slice slice?)
;; TODO: perhaps use pointer->bytevector
@@ -150,6 +152,9 @@ the bytevector in place."
(define slice/read-write
(make-select-capabilities (bitwise-ior CAP_READ CAP_WRITE)))
+
+ ;; ‘Small’ operations
+
(define (wrap-rnrs-ref rnrs-ref ok? size)
(lambda (slice index . rest)
(assert (and (exact? index)
@@ -177,4 +182,29 @@ the bytevector in place."
(define slice-u32-set!
(wrap-rnrs-ref bytevector-u32-set! slice-writable? 4))
(define slice-u64-set!
- (wrap-rnrs-ref bytevector-u64-set! slice-writable? 8)))
+ (wrap-rnrs-ref bytevector-u64-set! slice-writable? 8))
+
+
+ ;; ‘Large’ operations.
+
+ (define (slice-zero! slice)
+ "Zero out the writable slice @var{slice}."
+ (assert (slice-writable? slice))
+ ;; TODO optimise this and/or optimise guile's compiler
+ ;; w.r.t. bytevectors, structs and type inference.
+ (let loop ((i 0))
+ (when (< i (slice-length slice))
+ (slice-u8-set! slice i 0)
+ (loop (+ i 1))))
+ (values))
+
+ (define (slice-copy! from to)
+ "Copy the contents of the readable slice @var{from} to
+the writable slice @var{slice}. The slices may overlap."
+ (assert (slice-readable? from))
+ (assert (slice-writable? to))
+ (assert (= (slice-length from) (slice-length to)))
+ (bytevector-copy! (slice-bv from) (slice-offset from)
+ (slice-bv to) (slice-offset to)
+ (slice-length from))
+ (values)))
diff --git a/tests/bv-slice.scm b/tests/bv-slice.scm
new file mode 100644
index 0000000..516f9db
--- /dev/null
+++ b/tests/bv-slice.scm
@@ -0,0 +1,94 @@
+;; This file is part of scheme-GNUnet.
+;; Copyright (C) 2021 Maxime Devos
+;;
+;; scheme-GNUnet is free software: you can redistribute it and/or modify it
+;; under the terms of the GNU Affero General Public License as published
+;; by the Free Software Foundation, either version 3 of the License,
+;; or (at your option) any later version.
+;;
+;; scheme-GNUnet is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+;; Affero General Public License for more details.
+;;
+;; You should have received a copy of the GNU Affero General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+;;
+;; SPDX-License-Identifier: AGPL3.0-or-later
+
+(import (gnu gnunet utils bv-slice)
+ (srfi srfi-26)
+ (rnrs conditions)
+ (rnrs bytevectors))
+
+(test-begin "bv-slice")
+
+
+;; slice-copy!
+
+;; TODO maybe more specific conditions
+(test-error "destination of slice-copy! must be writable"
+ &assertion
+ (slice-copy! (make-slice/read-write 9)
+ (slice/read-only (make-slice/read-write 9))))
+
+(test-error "source of slice-copy! must be readable"
+ &assertion
+ (slice-copy! (slice/write-only (make-slice/read-write 9))
+ (make-slice/read-write 9)))
+
+(test-error "lengths must match (1)"
+ &assertion
+ (slice-copy! (make-slice/read-write 9)
+ (make-slice/read-write 0)))
+
+(test-error "lengths must match (2)"
+ &assertion
+ (slice-copy! (make-slice/read-write 0)
+ (make-slice/read-write 9)))
+
+(test-equal "slice-copy! copies"
+ #vu8(0 1 2 3)
+ (let ((source (bv-slice/read-write #vu8(0 1 2 3)))
+ (dest (make-slice/read-write 4)))
+ (slice-copy! source dest)
+ (slice-bv dest)))
+
+(test-equal "also if there's an offset in the source"
+ #vu8(0 1 2 3)
+ (let ((source (slice-slice (bv-slice/read-write #vu8(0 0 1 2 3)) 1))
+ (dest (make-slice/read-write 4)))
+ (slice-copy! source dest)
+ (slice-bv dest)))
+
+(test-equal "also if the destination bv is long"
+ #vu8(9 8 0 1 2 3)
+ (let ((source (bv-slice/read-write #vu8(8 0 1 2)))
+ (dest (slice-slice
+ (bv-slice/read-write (bytevector-copy #vu8(9 7 7 7 7 3)))
+ 1 4)))
+ (slice-copy! source dest)
+ (slice-bv dest)))
+
+
+
+(test-equal "slice-zero! writes zeros"
+ #vu8(1 2 0 0 5 6 7 8)
+ (let ((dest
+ (slice-slice
+ (bv-slice/read-write (bytevector-copy #vu8(1 2 3 4 5 6 7 8)))
+ 2 2)))
+ (slice-zero! dest)
+ (slice-bv dest)))
+
+(test-error "slice-zero! requires writability"
+ &assertion
+ (slice-zero! (slice/write-only (make-slice/read-write 9))))
+
+(test-error "even if the length is zero"
+ &assertion
+ (slice-zero! (slice/write-only (make-slice/read-write 0))))
+
+(test-end "bv-slice")
+
+;; ^ TODO: test other procedures
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [gnunet-scheme] 65/324: download-store: prepare supporting the nar output format, (continued)
- [gnunet-scheme] 65/324: download-store: prepare supporting the nar output format, gnunet, 2021/09/21
- [gnunet-scheme] 64/324: scripts: download-store: use SRFI-39 parameters for configuration, gnunet, 2021/09/21
- [gnunet-scheme] 75/324: build: correct propagated-inputs fields, gnunet, 2021/09/21
- [gnunet-scheme] 79/324: vc: add most build artifacts to .gitignore, gnunet, 2021/09/21
- [gnunet-scheme] 80/324: Define various common network structures, gnunet, 2021/09/21
- [gnunet-scheme] 86/324: doc: Update the roadmap on Guix + GNUnet., gnunet, 2021/09/21
- [gnunet-scheme] 84/324: utils: correct definition of unsigned integers, gnunet, 2021/09/21
- [gnunet-scheme] 76/324: scripts: download-store: make dependency on guix optional, gnunet, 2021/09/21
- [gnunet-scheme] 83/324: Document the current state of message queues., gnunet, 2021/09/21
- [gnunet-scheme] 85/324: mq: Do not include callbacks in envelopes., gnunet, 2021/09/21
- [gnunet-scheme] 91/324: Define slice-copy! and slice-zero!.,
gnunet <=
- [gnunet-scheme] 90/324: icmp: Define some packet types and error codes., gnunet, 2021/09/21
- [gnunet-scheme] 100/324: config: parser: Define return types for expansion parser., gnunet, 2021/09/21
- [gnunet-scheme] 108/324: tets: message-handler: Correct imports., gnunet, 2021/09/21
- [gnunet-scheme] 110/324: config: Define a quaject for quering and modifying a configuration., gnunet, 2021/09/21
- [gnunet-scheme] 71/324: doc: Document maintainer quirk, gnunet, 2021/09/21
- [gnunet-scheme] 77/324: util: add missing import, gnunet, 2021/09/21
- [gnunet-scheme] 82/324: Implement self-documenting ‘network structures’, gnunet, 2021/09/21
- [gnunet-scheme] 88/324: mq: Move message queue modules to (gnu gnunet mq SOMETHING)., gnunet, 2021/09/21
- [gnunet-scheme] 101/324: utils: hat-let: Add <--, a variant on <-., gnunet, 2021/09/21
- [gnunet-scheme] 89/324: doc: Document current list of defined GNUnet network structures., gnunet, 2021/09/21