gnunet-svn
[Top][All Lists]
Advanced

[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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]