aboutsummaryrefslogtreecommitdiff
path: root/tests/bv-slice.scm
blob: 44c45fbd622fdfd2dac8b2cbed5ce3d5a9e4b91a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
;; This file is part of scheme-GNUnet.
;; Copyright (C) 2021 GNUnet e.V.
;;
;; 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