Skip to content

Commit 16bdc06

Browse files
Merge pull request #5 from dpk/master
Test suite and bug fixes
2 parents 2a40d9a + f85da59 commit 16bdc06

File tree

11 files changed

+2004
-33
lines changed

11 files changed

+2004
-33
lines changed

.github/workflows/chez.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
on: [push]
2+
jobs:
3+
test-chez:
4+
runs-on: ubuntu-latest
5+
container:
6+
image: schemers/chezscheme:latest
7+
steps:
8+
- uses: actions/checkout@v5
9+
- name: 'Install chez-srfi'
10+
run: |
11+
apt-get update
12+
apt-get install -yy git
13+
git clone https://github.com/arcfide/chez-srfi.git
14+
cd chez-srfi
15+
./install.chezscheme.sps ~/chezlib
16+
- name: 'Install (chibi test)'
17+
run: |
18+
git clone https://codeberg.org/dpk/chibi-lib.git ~/chezlib/chibi
19+
- name: 'Run tests'
20+
run: |
21+
env CHEZSCHEMELIBDIRS="$HOME/chezlib:" scheme --program test-on-r6rs.sps

.github/workflows/chibi.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
on: [push]
2+
jobs:
3+
test-chibi:
4+
runs-on: ubuntu-latest
5+
container:
6+
image: schemers/chibi:latest
7+
steps:
8+
- uses: actions/checkout@v5
9+
- name: 'Grab newer version of (chibi test)'
10+
run: |
11+
apt-get update
12+
apt-get install -yy wget
13+
mkdir -p ~/chibilib/chibi
14+
cd ~/chibilib/chibi
15+
wget https://raw.githubusercontent.com/ashinn/chibi-scheme/3ca9e57d1e2a7199ea84c775296843ca5f08c024/lib/chibi/test.sld
16+
wget https://raw.githubusercontent.com/ashinn/chibi-scheme/3ca9e57d1e2a7199ea84c775296843ca5f08c024/lib/chibi/test.scm
17+
- name: 'Run tests'
18+
run: |
19+
env TEST_GROUP_REMOVE='Stress tests: building' chibi-scheme -I ~/chibilib -I . test-on-r7rs.scm

srfi/250.sld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878

7979
(define-record-type Hash-Table
8080
(%make-hash-table type-test-function hash-function same?-function
81-
size next-entry compact-index
81+
size next-entry compact-index compact-index-max-fill
8282
keys-vector values-vector mutable?)
8383
hash-table?
8484
(type-test-function hash-table-type-test-function)

srfi/250/hash-tables.scm

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
;; -*- eldoc-documentation-function: eldoc-documentation-default -*-
22
;; scheme-complete eldoc is bizarrely slow in this buffer
3+
(begin ; meze only supports one expression per file
34

45
(define *nice-n-buckets*
56
'#(2 2 3 5 5 7 7 11 11 13 13 17 17 19 19 23 23 23 23 29 29 31 31 31 31
@@ -162,8 +163,7 @@
162163
(let loop ((from-idx 0)
163164
(to-idx 0))
164165
;;(display from-idx) (newline) (display to-idx) (newline) (newline)
165-
(cond ((or (>= from-idx (hash-table-next-entry ht))
166-
(unfilled? (vector-ref (hash-table-keys-vector ht) from-idx)))
166+
(cond ((>= from-idx (hash-table-next-entry ht))
167167
(vector-fill! (hash-table-keys-vector ht)
168168
*unfilled*
169169
(hash-table-size ht)
@@ -175,8 +175,35 @@
175175
(hash-table-next-entry-set! ht (hash-table-size ht)))
176176
((deletion? (vector-ref (hash-table-keys-vector ht) from-idx))
177177
(unless fast?
178-
(compact-array-delete! (hash-table-compact-index ht)
179-
(vector-ref (hash-table-values-vector ht) from-idx)))
178+
(let ((deleted-bucket (vector-ref (hash-table-values-vector ht) from-idx)))
179+
(compact-array-delete! (hash-table-compact-index ht)
180+
deleted-bucket)
181+
(let loop ((deleted-bucket deleted-bucket)
182+
(examine-bucket (+ deleted-bucket 1)))
183+
(let ((examine-bucket
184+
(modulo examine-bucket
185+
(compact-array-length
186+
(hash-table-compact-index ht)))))
187+
(let ((collision-idx
188+
(compact-array-ref (hash-table-compact-index ht)
189+
examine-bucket)))
190+
(when collision-idx
191+
(let* ((key (vector-ref (hash-table-keys-vector ht)
192+
collision-idx))
193+
(new-bucket
194+
(if (deletion? key)
195+
(vector-ref (hash-table-values-vector ht)
196+
collision-idx)
197+
(hash-table-bucket-for-key ht key))))
198+
(if (eqv? new-bucket deleted-bucket)
199+
(begin
200+
(compact-array-set! (hash-table-compact-index ht)
201+
deleted-bucket
202+
collision-idx)
203+
(compact-array-delete! (hash-table-compact-index ht)
204+
examine-bucket)
205+
(loop examine-bucket (+ examine-bucket 1)))
206+
(loop deleted-bucket (+ examine-bucket 1))))))))))
180207
(loop (+ from-idx 1) to-idx))
181208
((eqv? from-idx to-idx) (loop (+ from-idx 1) (+ to-idx 1)))
182209
(else
@@ -233,9 +260,13 @@
233260

234261
;; add to the entries arrays, setting the bucket in the compact index
235262
(define (hash-table-add-entry! ht bucket key value)
236-
(if (>= (hash-table-next-entry ht)
237-
(vector-length (hash-table-keys-vector ht)))
238-
(hash-table-grow-entries! ht))
263+
(when (>= (hash-table-next-entry ht)
264+
(vector-length (hash-table-keys-vector ht)))
265+
(if (eqv? (hash-table-size ht) (hash-table-next-entry ht))
266+
(hash-table-grow-entries! ht)
267+
(begin
268+
(hash-table-grow-entries! ht)
269+
(set! bucket (hash-table-bucket-for-key ht key)))))
239270
(when (hash-table-compact-index-must-grow? ht)
240271
(hash-table-grow-compact-index! ht)
241272
(set! bucket (hash-table-bucket-for-key ht key)))
@@ -416,7 +447,7 @@
416447

417448
(define (hash-table-pop! ht)
418449
(unless (hash-table-mutable? ht)
419-
(assertion-violation 'hash-table-delete!
450+
(assertion-violation 'hash-table-pop!
420451
"hash table is immutable"
421452
ht))
422453
(when (hash-table-empty? ht)
@@ -425,11 +456,12 @@
425456
ht))
426457
(let* ((idx (- (hash-table-next-entry ht) 1))
427458
(key (vector-ref (hash-table-keys-vector ht) idx))
428-
(value (vector-ref (hash-table-values-vector ht) idx)))
429-
(vector-set! (hash-table-keys-vector ht) idx *unfilled*)
430-
(vector-set! (hash-table-values-vector ht) idx *unfilled*)
459+
(value (vector-ref (hash-table-values-vector ht) idx))
460+
(bucket (hash-table-bucket-for-key ht key)))
461+
(vector-set! (hash-table-keys-vector ht) idx *deletion*)
462+
(vector-set! (hash-table-values-vector ht) idx bucket)
431463
(hash-table-size-set! ht (- (hash-table-size ht) 1))
432-
(hash-table-next-entry-set! ht idx)
464+
(hash-table-prune-dead-entries-at-end! ht)
433465
(values key value)))
434466

435467
(define (hash-table-clear! ht)
@@ -635,23 +667,25 @@
635667
ht))
636668

637669
(define (hash-table-prune! proc ht)
670+
(define original-size (hash-table-size ht))
638671
(unless (hash-table-mutable? ht)
639672
(assertion-violation 'hash-table-prune!
640673
"hash table is immutable"
641674
ht))
642675
(let loop ((cur (hash-table-cursor-first ht)) (n-deleted 0))
643676
(if (hash-table-cursor-at-end? ht cur)
644677
(begin
645-
(hash-table-size-set! ht (- (hash-table-size ht) n-deleted))
646678
(hash-table-prune-dead-entries-at-end! ht)
647-
(when (> (- (hash-table-next-entry ht) (hash-table-size ht))
679+
(when (> (- (hash-table-next-entry ht) original-size)
648680
(* 1/3 (hash-table-size ht)))
649681
(hash-table-prune-dead-entries! ht #f))
650682
n-deleted)
651683
(let-values (((k v) (hash-table-cursor-key+value ht cur)))
652684
(if (and (proc k v)
653685
(hash-table-delete-one! ht k))
654-
(loop (hash-table-cursor-next ht cur) (+ n-deleted 1))
686+
(begin
687+
(hash-table-size-set! ht (- (hash-table-size ht) 1))
688+
(loop (hash-table-cursor-next ht cur) (+ n-deleted 1)))
655689
(loop (hash-table-cursor-next ht cur) n-deleted))))))
656690

657691
(define (hash-table-copy ht mutable?)
@@ -679,7 +713,7 @@
679713
(hash-table-for-each
680714
(lambda (k v)
681715
(unless (hash-table-contains? ht_1 k)
682-
(hash-table-set! ht_2 k v)))
716+
(hash-table-set! ht_1 k v)))
683717
ht_2)
684718
ht_1)
685719

@@ -771,3 +805,5 @@
771805
"hash table is immutable"
772806
ht))
773807
(hash-table-set! ht key (updater (hash-table-ref/default ht key default))))
808+
809+
)

srfi/250/internal/srfi-compact-arrays.scm

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
(define (make-compact-array size)
2-
(cond ((< size #xFF) (make-bytevector size #xFF))
2+
(cond ((< size #xFF) (make-u8vector size #xFF))
33
((< size #xFFFF) (make-u16vector size #xFFFF))
44
((< size #xFFFFFFFF) (make-u32vector size #xFFFFFFFF))
55
(else (make-u64vector size #xFFFFFFFFFFFFFFFF))))
66

77
(define (compact-array-ref sa idx)
88
(define (max-to n) (lambda (x) (if (eqv? x n) #f x)))
9-
(cond ((and (bytevector? sa) (bytevector-u8-ref sa idx))
9+
(cond ((and (u8vector? sa) (u8vector-ref sa idx))
1010
=> (max-to #xFF))
1111
((and (u16vector? sa) (u16vector-ref sa idx))
1212
=> (max-to #xFFFF))
@@ -19,14 +19,14 @@
1919
(not (not (compact-array-ref sa idx))))
2020

2121
(define (compact-array-set! sa idx val)
22-
(cond ((bytevector? sa) (bytevector-u8-set! sa idx val))
22+
(cond ((u8vector? sa) (u8vector-set! sa idx val))
2323
((u16vector? sa) (u16vector-set! sa idx val))
2424
((u32vector? sa) (u32vector-set! sa idx val))
2525
((u64vector? sa) (u64vector-set! sa idx val))))
2626

2727
(define (compact-array-delete! sa idx)
28-
(cond ((bytevector? sa)
29-
(bytevector-u8-set! sa idx #xFF))
28+
(cond ((u8vector? sa)
29+
(u8vector-set! sa idx #xFF))
3030
((u16vector? sa)
3131
(u16vector-set! sa idx #xFFFF))
3232
((u32vector? sa)
@@ -36,10 +36,10 @@
3636

3737
(define (compact-array-clear! sa)
3838
(define len (compact-array-length sa))
39-
(cond ((bytevector? sa)
39+
(cond ((u8vector? sa)
4040
(let loop ((idx 0))
4141
(when (< idx len)
42-
(bytevector-u8-set! sa idx #xFF)
42+
(u8vector-set! sa idx #xFF)
4343
(loop (+ idx 1)))))
4444
((u16vector? sa)
4545
(let loop ((idx 0))
@@ -59,26 +59,38 @@
5959

6060
(define (compact-array-copy sa)
6161
(define len (compact-array-length sa))
62-
(cond ((bytevector? sa) (bytevector-copy sa))
62+
(cond ((u8vector? sa)
63+
(let ((out (make-u8vector len)))
64+
(let loop ((idx 0))
65+
(when (< idx len)
66+
(u8vector-set! out idx (u8vector-ref sa idx))
67+
(loop (+ idx 1))))
68+
out))
6369
((u16vector? sa)
6470
(let ((out (make-u16vector len)))
6571
(let loop ((idx 0))
6672
(when (< idx len)
67-
(u16vector-set! out idx (u16vector-ref sa idx))))))
73+
(u16vector-set! out idx (u16vector-ref sa idx))
74+
(loop (+ idx 1))))
75+
out))
6876
((u32vector? sa)
6977
(let ((out (make-u32vector len)))
7078
(let loop ((idx 0))
7179
(when (< idx len)
72-
(u32vector-set! out idx (u32vector-ref sa idx))))))
80+
(u32vector-set! out idx (u32vector-ref sa idx))
81+
(loop (+ idx 1))))
82+
out))
7383
((u64vector? sa)
7484
(let ((out (make-u64vector len)))
7585
(let loop ((idx 0))
7686
(when (< idx len)
77-
(u64vector-set! out idx (u64vector-ref sa idx))))))))
87+
(u64vector-set! out idx (u64vector-ref sa idx))
88+
(loop (+ idx 1))))
89+
out))))
7890

7991
(define (compact-array-length sa)
80-
(cond ((bytevector? sa)
81-
(bytevector-length sa))
92+
(cond ((u8vector? sa)
93+
(u8vector-length sa))
8294
((u16vector? sa)
8395
(u16vector-length sa))
8496
((u32vector? sa)

srfi/:250/hash-tables.sls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@
9393
(syntax-rules ()
9494
((_ name pred)
9595
(begin
96-
(define-record-type the-sentinel-type)
97-
(define name (make-the-sentinel-type))
96+
(define-record-type the-sentinel-type (fields sentinel-name))
97+
(define name (make-the-sentinel-type 'name))
9898
(define (pred obj) (eq? obj name))))))
9999
(define-sentinel *unfilled* unfilled?)
100100
(define-sentinel *deletion* deletion?)

srfi/srfi-250.scm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#:use-module ((guile) #:select (include
1212
procedure-name))
1313
#:use-module (ice-9 format)
14+
#:use-module (srfi srfi-4)
1415
#:use-module ((srfi srfi-9 gnu) #:select (set-record-type-printer!))
1516
#:use-module (srfi srfi-128) ; https://codeberg.org/pukkamustard/guile-srfi-128
1617
#:duplicates (last)
@@ -113,7 +114,7 @@
113114
(define (hash-table-immutablize! ht)
114115
(hash-table-mutable?-set! ht #f))
115116

116-
(include "250/internal/r6rs-compact-arrays.scm")
117+
(include "250/internal/srfi-compact-arrays.scm")
117118
(include "250/hash-tables.scm")
118119

119120
(set-record-type-printer! (record-type-descriptor hash-table)

test-on-guile.scm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(import (rnrs)
2+
(rnrs r5rs)
3+
(chibi test)
4+
(only (srfi :1) list-tabulate)
5+
(srfi :6)
6+
(srfi :27)
7+
(srfi :128)
8+
(srfi :250)
9+
(only (guile) include))
10+
11+
(define (exact-integer? x)
12+
(and (integer? x) (exact? x)))
13+
14+
(test-begin "SRFI 250")
15+
(include "test-srfi-250.scm")
16+
(test-end "SRFI 250")
17+
(test-exit)

test-on-r6rs.sps

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(import (rnrs)
2+
(rnrs r5rs)
3+
(chibi test)
4+
(only (srfi :1) list-tabulate)
5+
(srfi :6)
6+
(srfi :27)
7+
(srfi :128)
8+
(srfi :250)
9+
(srfi :250 internal include))
10+
11+
(define (exact-integer? x)
12+
(and (integer? x) (exact? x)))
13+
14+
(test-begin "SRFI 250")
15+
(include "test-srfi-250.scm")
16+
(test-end "SRFI 250")
17+
(test-exit)

test-on-r7rs.scm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(import (scheme base)
2+
(scheme char)
3+
(scheme write)
4+
(chibi test)
5+
(only (srfi 1) list-tabulate)
6+
(srfi 27)
7+
(srfi 250)
8+
(rename (srfi 128)
9+
(default-hash equal-hash)))
10+
11+
(define-syntax assert
12+
(syntax-rules ()
13+
((_ what) (unless what (error "assertion failed")))))
14+
(define (assertion-violation who msg . rest)
15+
(apply error msg rest))
16+
(define assertion-violation? error-object?)
17+
18+
(test-begin "SRFI 250")
19+
(include "test-srfi-250.scm")
20+
(test-end "SRFI 250")
21+
(test-exit)

0 commit comments

Comments
 (0)