1
1
import { describe , expect , it } from "vitest" ;
2
2
3
3
import * as zarr from "../../src/index.js" ;
4
+ import { get , set , slice } from "../../src/index.js" ;
4
5
import { IndexError } from "../../src/indexing/indexer.js" ;
5
- import * as ops from "../../src/indexing/ops.js" ;
6
- import { slice } from "../../src/indexing/util.js" ;
7
6
8
7
const DATA = {
9
8
// biome-ignore format: the array should not be formatted
@@ -12,126 +11,120 @@ const DATA = {
12
11
stride : [ 12 , 4 , 1 ] ,
13
12
} ;
14
13
15
- export function run_suite ( name : string , getter : unknown ) {
16
- let get = getter as typeof ops . get ;
17
-
18
- describe ( name , async ( ) => {
19
- let arr = await zarr . create ( new Map ( ) , {
20
- shape : [ 2 , 3 , 4 ] ,
21
- data_type : "int32" ,
22
- chunk_shape : [ 1 , 2 , 2 ] ,
23
- } ) ;
24
- await ops . set ( arr , null , DATA ) ;
25
-
26
- it . each ( [
27
- undefined ,
28
- null ,
29
- [ null , null , null ] ,
30
- [ slice ( null ) , slice ( null ) , slice ( null ) ] ,
31
- [ null , slice ( null ) , slice ( null ) ] ,
32
- ] ) ( "Reads entire array: selection = %j" , async ( sel ) => {
33
- let { data, shape, stride } = await get ( arr , sel ) ;
34
- expect ( { data, shape, stride } ) . toStrictEqual ( DATA ) ;
35
- } ) ;
14
+ describe ( "slice" , async ( ) => {
15
+ let arr = await zarr . create ( new Map ( ) , {
16
+ shape : [ 2 , 3 , 4 ] ,
17
+ data_type : "int32" ,
18
+ chunk_shape : [ 1 , 2 , 2 ] ,
19
+ } ) ;
20
+ await set ( arr , null , DATA ) ;
36
21
37
- it . each ( [
38
- [
39
- [ 1 , slice ( 1 , 3 ) , null ] ,
40
- {
41
- data : new Int32Array ( [ 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 ] ) ,
42
- shape : [ 2 , 4 ] ,
43
- stride : [ 4 , 1 ] ,
44
- } ,
45
- ] ,
46
- [
47
- [ null , slice ( 1 , 3 ) , slice ( 2 ) ] ,
48
- {
49
- data : new Int32Array ( [ 4 , 5 , 8 , 9 , 16 , 17 , 20 , 21 ] ) ,
50
- shape : [ 2 , 2 , 2 ] ,
51
- stride : [ 4 , 2 , 1 ] ,
52
- } ,
53
- ] ,
54
- [
55
- [ null , null , 0 ] ,
56
- {
57
- data : new Int32Array ( [ 0 , 4 , 8 , 12 , 16 , 20 ] ) ,
58
- shape : [ 2 , 3 ] ,
59
- stride : [ 3 , 1 ] ,
60
- } ,
61
- ] ,
62
- [
63
- [ slice ( 3 ) , slice ( 2 ) , slice ( 2 ) ] ,
64
- {
65
- data : new Int32Array ( [ 0 , 1 , 4 , 5 , 12 , 13 , 16 , 17 ] ) ,
66
- shape : [ 2 , 2 , 2 ] ,
67
- stride : [ 4 , 2 , 1 ] ,
68
- } ,
69
- ] ,
70
- [
71
- [ 1 , null , null ] ,
72
- {
73
- // biome-ignore format: the array should not be formatted
74
- data : new Int32Array ( [ 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 ] ) ,
75
- shape : [ 3 , 4 ] ,
76
- stride : [ 4 , 1 ] ,
77
- } ,
78
- ] ,
79
- [
80
- [ 0 , slice ( null , null , 2 ) , slice ( null , null , 3 ) ] ,
81
- {
82
- data : new Int32Array ( [ 0 , 3 , 8 , 11 ] ) ,
83
- shape : [ 2 , 2 ] ,
84
- stride : [ 2 , 1 ] ,
85
- } ,
86
- ] ,
87
- [
88
- [ null , null , slice ( null , null , 4 ) ] ,
89
- {
90
- data : new Int32Array ( [ 0 , 4 , 8 , 12 , 16 , 20 ] ) ,
91
- shape : [ 2 , 3 , 1 ] ,
92
- stride : [ 3 , 1 , 1 ] ,
93
- } ,
94
- ] ,
95
- [
96
- [ 1 , null , slice ( null , 3 , 2 ) ] ,
97
- {
98
- data : new Int32Array ( [ 12 , 14 , 16 , 18 , 20 , 22 ] ) ,
99
- shape : [ 3 , 2 ] ,
100
- stride : [ 2 , 1 ] ,
101
- } ,
102
- ] ,
103
- [
104
- [ null , 1 , null ] ,
105
- {
106
- data : new Int32Array ( [ 4 , 5 , 6 , 7 , 16 , 17 , 18 , 19 ] ) ,
107
- shape : [ 2 , 4 ] ,
108
- stride : [ 4 , 1 ] ,
109
- } ,
110
- ] ,
111
- [
112
- [ 1 , 2 , null ] ,
113
- {
114
- data : new Int32Array ( [ 20 , 21 , 22 , 23 ] ) ,
115
- shape : [ 4 ] ,
116
- stride : [ 1 ] ,
117
- } ,
118
- ] ,
119
- ] ) ( "Reads fancy slices: selection - %j" , async ( sel , expected ) => {
120
- let { data, shape, stride } = await get ( arr , sel ) ;
121
- expect ( { data, shape, stride } ) . toStrictEqual ( expected ) ;
122
- } ) ;
22
+ it . each ( [
23
+ undefined ,
24
+ null ,
25
+ [ null , null , null ] ,
26
+ [ slice ( null ) , slice ( null ) , slice ( null ) ] ,
27
+ [ null , slice ( null ) , slice ( null ) ] ,
28
+ ] ) ( "Reads entire array: selection = %j" , async ( sel ) => {
29
+ let chunk = await get ( arr , sel ) ;
30
+ expect ( chunk ) . toStrictEqual ( DATA ) ;
31
+ } ) ;
123
32
124
- it ( "Reads a scalar" , async ( ) => {
125
- let sel = [ 1 , 1 , 1 ] ;
126
- let val = await get ( arr , sel ) ;
127
- expect ( val ) . toBe ( 17 ) ;
128
- } ) ;
33
+ it . each ( [
34
+ [
35
+ [ 1 , slice ( 1 , 3 ) , null ] ,
36
+ {
37
+ data : new Int32Array ( [ 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 ] ) ,
38
+ shape : [ 2 , 4 ] ,
39
+ stride : [ 4 , 1 ] ,
40
+ } ,
41
+ ] ,
42
+ [
43
+ [ null , slice ( 1 , 3 ) , slice ( 2 ) ] ,
44
+ {
45
+ data : new Int32Array ( [ 4 , 5 , 8 , 9 , 16 , 17 , 20 , 21 ] ) ,
46
+ shape : [ 2 , 2 , 2 ] ,
47
+ stride : [ 4 , 2 , 1 ] ,
48
+ } ,
49
+ ] ,
50
+ [
51
+ [ null , null , 0 ] ,
52
+ {
53
+ data : new Int32Array ( [ 0 , 4 , 8 , 12 , 16 , 20 ] ) ,
54
+ shape : [ 2 , 3 ] ,
55
+ stride : [ 3 , 1 ] ,
56
+ } ,
57
+ ] ,
58
+ [
59
+ [ slice ( 3 ) , slice ( 2 ) , slice ( 2 ) ] ,
60
+ {
61
+ data : new Int32Array ( [ 0 , 1 , 4 , 5 , 12 , 13 , 16 , 17 ] ) ,
62
+ shape : [ 2 , 2 , 2 ] ,
63
+ stride : [ 4 , 2 , 1 ] ,
64
+ } ,
65
+ ] ,
66
+ [
67
+ [ 1 , null , null ] ,
68
+ {
69
+ // biome-ignore format: the array should not be formatted
70
+ data : new Int32Array ( [ 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 ] ) ,
71
+ shape : [ 3 , 4 ] ,
72
+ stride : [ 4 , 1 ] ,
73
+ } ,
74
+ ] ,
75
+ [
76
+ [ 0 , slice ( null , null , 2 ) , slice ( null , null , 3 ) ] ,
77
+ {
78
+ data : new Int32Array ( [ 0 , 3 , 8 , 11 ] ) ,
79
+ shape : [ 2 , 2 ] ,
80
+ stride : [ 2 , 1 ] ,
81
+ } ,
82
+ ] ,
83
+ [
84
+ [ null , null , slice ( null , null , 4 ) ] ,
85
+ {
86
+ data : new Int32Array ( [ 0 , 4 , 8 , 12 , 16 , 20 ] ) ,
87
+ shape : [ 2 , 3 , 1 ] ,
88
+ stride : [ 3 , 1 , 1 ] ,
89
+ } ,
90
+ ] ,
91
+ [
92
+ [ 1 , null , slice ( null , 3 , 2 ) ] ,
93
+ {
94
+ data : new Int32Array ( [ 12 , 14 , 16 , 18 , 20 , 22 ] ) ,
95
+ shape : [ 3 , 2 ] ,
96
+ stride : [ 2 , 1 ] ,
97
+ } ,
98
+ ] ,
99
+ [
100
+ [ null , 1 , null ] ,
101
+ {
102
+ data : new Int32Array ( [ 4 , 5 , 6 , 7 , 16 , 17 , 18 , 19 ] ) ,
103
+ shape : [ 2 , 4 ] ,
104
+ stride : [ 4 , 1 ] ,
105
+ } ,
106
+ ] ,
107
+ [
108
+ [ 1 , 2 , null ] ,
109
+ {
110
+ data : new Int32Array ( [ 20 , 21 , 22 , 23 ] ) ,
111
+ shape : [ 4 ] ,
112
+ stride : [ 1 ] ,
113
+ } ,
114
+ ] ,
115
+ ] ) ( "Reads fancy slices: selection - %j" , async ( sel , expected ) => {
116
+ let { data, shape, stride } = await get ( arr , sel ) ;
117
+ expect ( { data, shape, stride } ) . toStrictEqual ( expected ) ;
118
+ } ) ;
129
119
130
- it ( "Does not support negative indices " , async ( ) => {
131
- let sel = [ 0 , slice ( null , null , - 2 ) , slice ( null , null , 3 ) ] ;
132
- await expect ( get ( arr , sel ) ) . rejects . toThrowError ( IndexError ) ;
133
- } ) ;
120
+ it ( "Reads a scalar " , async ( ) => {
121
+ let sel = [ 1 , 1 , 1 ] ;
122
+ let val = await get ( arr , sel ) ;
123
+ expect ( val ) . toBe ( 17 ) ;
134
124
} ) ;
135
- }
136
125
137
- run_suite ( "builtins" , ops . get ) ;
126
+ it ( "Does not support negative indices" , async ( ) => {
127
+ let sel = [ 0 , slice ( null , null , - 2 ) , slice ( null , null , 3 ) ] ;
128
+ await expect ( get ( arr , sel ) ) . rejects . toThrowError ( IndexError ) ;
129
+ } ) ;
130
+ } ) ;
0 commit comments