hdf5-pure 0.21.1

Pure-Rust HDF5 library: read, write, and edit files in place (WASM-compatible, no C dependencies)
Documentation
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! Whole-file repack (issue #21): compaction, object dropping, fidelity of
//! survivors, and fail-loud refusal of features that cannot be reproduced.

use hdf5_pure::{
    AttrValue, Datatype, DatatypeByteOrder, FileBuilder, FileSpaceStrategy, RepackOptions,
    ScaleOffset, repack,
};

fn tmp(name: &str) -> std::path::PathBuf {
    std::env::temp_dir().join(name)
}

#[test]
fn drops_object_and_shrinks_file() {
    let src = tmp("hdf5_pure_repack_drop_src.h5");
    let dst = tmp("hdf5_pure_repack_drop_dst.h5");
    let mut b = FileBuilder::new();
    b.create_dataset("keep").with_i32_data(&[1, 2, 3]);
    b.create_dataset("bulk").with_f64_data(&vec![9.0; 4096]);
    b.write(&src).unwrap();
    let src_size = std::fs::metadata(&src).unwrap().len();

    repack(&src, &dst, &RepackOptions::new().drop_path("bulk")).unwrap();

    let dst_size = std::fs::metadata(&dst).unwrap().len();
    assert!(
        dst_size < src_size,
        "dropping the bulk dataset should shrink the file (src {src_size}, dst {dst_size})"
    );

    let f = hdf5_pure::File::open(&dst).unwrap();
    assert_eq!(f.root().datasets().unwrap(), vec!["keep".to_string()]);
    assert_eq!(
        f.dataset("keep").unwrap().read_i32().unwrap(),
        vec![1, 2, 3]
    );
    assert!(f.dataset("bulk").is_err());
    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn pure_compaction_preserves_everything() {
    let src = tmp("hdf5_pure_repack_compact_src.h5");
    let dst = tmp("hdf5_pure_repack_compact_dst.h5");
    let mut b = FileBuilder::new();
    b.create_dataset("ints").with_i64_data(&[10, 20, 30, 40]);
    b.create_dataset("floats").with_f64_data(&[1.5, 2.5, 3.5]);
    b.set_attr("title", AttrValue::String("experiment".to_string()));
    let mut g = b.create_group("grp");
    g.create_dataset("inner").with_f32_data(&[0.25, 0.5]);
    g.set_attr("units", AttrValue::AsciiString("m/s".to_string()));
    b.add_group(g.finish());
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    assert_eq!(
        f.dataset("ints").unwrap().read_i64().unwrap(),
        vec![10, 20, 30, 40]
    );
    assert_eq!(
        f.dataset("floats").unwrap().read_f64().unwrap(),
        vec![1.5, 2.5, 3.5]
    );
    assert_eq!(
        f.dataset("grp/inner").unwrap().read_f32().unwrap(),
        vec![0.25, 0.5]
    );
    // Attributes carried over.
    let root_attrs = f.root().attrs().unwrap();
    assert_eq!(
        root_attrs.get("title"),
        Some(&AttrValue::String("experiment".to_string()))
    );
    let grp_attrs = f.group("grp").unwrap().attrs().unwrap();
    // The value survives. Note: the reader reports a fixed-width ASCII string as
    // AttrValue::String (it does not preserve the ASCII-vs-UTF-8 charset
    // distinction on read), so repack round-trips it as String — verified to be
    // pre-existing reader behavior, independent of repack.
    assert_eq!(
        grp_attrs.get("units"),
        Some(&AttrValue::String("m/s".to_string()))
    );
    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn repacks_v1_symbol_table_source_with_attributes() {
    // `attrs.h5` is an older-format file (v0 superblock => v1 symbol-table
    // groups) carrying compact attributes. Repack now opens the source via the
    // streaming backend, so this drives v1 group traversal and compact attribute
    // reads end to end through the repack entry point (issues #82 / #27).
    let dst = tmp("hdf5_pure_repack_v1_attrs_dst.h5");
    let src = "tests/fixtures/attrs.h5";

    let source = hdf5_pure::File::open(src).unwrap();
    let src_data = source.dataset("data").unwrap().read_f64().unwrap();
    let src_data_attrs = source.dataset("data").unwrap().attrs().unwrap();
    let src_root_attrs = source.root().attrs().unwrap();
    assert!(!src_data_attrs.is_empty() && !src_root_attrs.is_empty());

    repack(src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    assert_eq!(f.dataset("data").unwrap().read_f64().unwrap(), src_data);
    assert_eq!(f.dataset("data").unwrap().attrs().unwrap(), src_data_attrs);
    assert_eq!(f.root().attrs().unwrap(), src_root_attrs);

    std::fs::remove_file(&dst).ok();
}

#[test]
fn repacks_v1_nested_symbol_table_groups() {
    // `two_groups.h5` has v1 symbol-table groups nested under the root. Repacking
    // it exercises the streaming v1 B-tree/local-heap/SNOD traversal across
    // multiple groups and preserves the full subtree.
    let dst = tmp("hdf5_pure_repack_v1_groups_dst.h5");
    let src = "tests/fixtures/two_groups.h5";

    repack(src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    let mut groups = f.root().groups().unwrap();
    groups.sort();
    assert_eq!(groups, vec!["group1".to_string(), "group2".to_string()]);
    assert_eq!(
        f.dataset("group1/values").unwrap().read_i32().unwrap(),
        vec![10, 20, 30]
    );
    assert_eq!(
        f.group("group2").unwrap().datasets().unwrap(),
        vec!["temps".to_string()]
    );

    std::fs::remove_file(&dst).ok();
}

#[test]
fn carries_dataset_attributes() {
    let src = tmp("hdf5_pure_repack_dsattr_src.h5");
    let dst = tmp("hdf5_pure_repack_dsattr_dst.h5");
    let mut b = FileBuilder::new();
    let ds = b.create_dataset("signal");
    ds.with_f64_data(&[1.0, 2.0, 3.0]);
    ds.set_attr("sample_rate", AttrValue::F64(44100.0));
    ds.set_attr("label", AttrValue::String("voltage".to_string()));
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    let attrs = f.dataset("signal").unwrap().attrs().unwrap();
    assert_eq!(attrs.get("sample_rate"), Some(&AttrValue::F64(44100.0)));
    assert_eq!(
        attrs.get("label"),
        Some(&AttrValue::String("voltage".to_string()))
    );
    assert_eq!(
        f.dataset("signal").unwrap().read_f64().unwrap(),
        vec![1.0, 2.0, 3.0]
    );
    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn drops_whole_group_subtree() {
    let src = tmp("hdf5_pure_repack_dropgrp_src.h5");
    let dst = tmp("hdf5_pure_repack_dropgrp_dst.h5");
    let mut b = FileBuilder::new();
    b.create_dataset("root_ds").with_i32_data(&[1]);
    let mut g = b.create_group("doomed");
    g.create_dataset("a").with_i32_data(&[1, 2]);
    g.create_dataset("b").with_i32_data(&[3, 4]);
    b.add_group(g.finish());
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new().drop_path("/doomed")).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    assert_eq!(f.root().datasets().unwrap(), vec!["root_ds".to_string()]);
    assert!(f.group("doomed").is_err());
    assert!(f.dataset("doomed/a").is_err());
    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn preserves_chunked_and_compressed_dataset() {
    let src = tmp("hdf5_pure_repack_chunk_src.h5");
    let dst = tmp("hdf5_pure_repack_chunk_dst.h5");
    let data: Vec<f64> = (0..2048).map(|i| i as f64 * 0.5).collect();
    let mut b = FileBuilder::new();
    b.create_dataset("keep").with_i32_data(&[7]);
    b.create_dataset("comp")
        .with_f64_data(&data)
        .with_chunks(&[256])
        .with_shuffle()
        .with_deflate(6);
    b.write(&src).unwrap();

    // Drop nothing; just compact. The chunked+filtered dataset must survive with
    // byte-exact values.
    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    assert_eq!(f.dataset("comp").unwrap().read_f64().unwrap(), data);
    assert_eq!(f.dataset("keep").unwrap().read_i32().unwrap(), vec![7]);
    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn preserves_multidim_and_maxshape() {
    let src = tmp("hdf5_pure_repack_md_src.h5");
    let dst = tmp("hdf5_pure_repack_md_dst.h5");
    let mut b = FileBuilder::new();
    b.create_dataset("grid")
        .with_f64_data(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
        .with_shape(&[2, 3])
        .with_maxshape(&[u64::MAX, 3])
        .with_chunks(&[1, 3]);
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    let ds = f.dataset("grid").unwrap();
    assert_eq!(ds.shape().unwrap(), vec![2, 3]);
    assert_eq!(ds.read_f64().unwrap(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn roundtrips_integer_scale_offset() {
    // Integer scale-offset is lossless, so repack re-applies it (decompress +
    // recompress reconstructs the exact bytes). Highly compressible data (a tiny
    // value range) makes the filter's survival observable from the file size.
    let data: Vec<i32> = (0..4096).map(|i| i % 8).collect();

    let so_src = tmp("hdf5_pure_repack_so_src.h5");
    let so_dst = tmp("hdf5_pure_repack_so_dst.h5");
    let plain_src = tmp("hdf5_pure_repack_soplain_src.h5");
    let plain_dst = tmp("hdf5_pure_repack_soplain_dst.h5");

    let mut b = FileBuilder::new();
    b.create_dataset("vals")
        .with_i32_data(&data)
        .with_chunks(&[512])
        .with_scale_offset(ScaleOffset::Integer(0));
    b.write(&so_src).unwrap();

    // The same data, chunked but unfiltered, as a baseline for the size check.
    let mut p = FileBuilder::new();
    p.create_dataset("vals")
        .with_i32_data(&data)
        .with_chunks(&[512]);
    p.write(&plain_src).unwrap();

    repack(&so_src, &so_dst, &RepackOptions::new()).unwrap();
    repack(&plain_src, &plain_dst, &RepackOptions::new()).unwrap();

    // Values survive byte-exact.
    let f = hdf5_pure::File::open(&so_dst).unwrap();
    assert_eq!(f.dataset("vals").unwrap().read_i32().unwrap(), data);

    // The filter survived the repack: had it been dropped, the scale-offset copy
    // would be no smaller than the unfiltered one.
    let so_size = std::fs::metadata(&so_dst).unwrap().len();
    let plain_size = std::fs::metadata(&plain_dst).unwrap().len();
    assert!(
        so_size < plain_size,
        "repacked scale-offset file ({so_size}) should be smaller than the unfiltered repack ({plain_size}), proving the filter was re-applied"
    );

    for p in [so_src, so_dst, plain_src, plain_dst] {
        std::fs::remove_file(p).ok();
    }
}

#[test]
fn roundtrips_lossy_float_scale_offset_verbatim() {
    // Float D-scale scale-offset is lossy, but a CHUNKED dataset's compressed
    // chunks are copied verbatim (never decoded), so repack reproduces the data
    // byte-exact instead of refusing. The values read back from the repacked file
    // must equal the values read back from the source (the lossy rounding is
    // baked into the stored bytes and carried through unchanged).
    let src = tmp("hdf5_pure_repack_fso_src.h5");
    let dst = tmp("hdf5_pure_repack_fso_dst.h5");
    let data: Vec<f64> = (0..1024).map(|i| (i as f64) * 0.01).collect();
    let mut b = FileBuilder::new();
    b.create_dataset("vals")
        .with_f64_data(&data)
        .with_chunks(&[256])
        .with_scale_offset(ScaleOffset::FloatDScale(3));
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let src_vals = hdf5_pure::File::open(&src)
        .unwrap()
        .dataset("vals")
        .unwrap()
        .read_f64()
        .unwrap();
    let dst_f = hdf5_pure::File::open(&dst).unwrap();
    let dst_ds = dst_f.dataset("vals").unwrap();
    assert_eq!(
        dst_ds.read_f64().unwrap(),
        src_vals,
        "verbatim chunk copy must reproduce the (lossy-rounded) values exactly"
    );
    // The dataset is still chunked + filtered: a read loads the chunk index into
    // the per-dataset chunk cache (a contiguous dataset never would), and the
    // filter shrinks the file well below the raw 1024*8 bytes of element data.
    assert!(
        dst_ds.chunk_cache_stats().index_loaded(),
        "repacked dataset should still be chunked"
    );
    assert!(
        std::fs::metadata(&dst).unwrap().len() < 1024 * 8,
        "scale-offset filter should keep the repacked file below the raw data size"
    );

    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn roundtrips_opaque_and_bitfield_datatypes() {
    // Opaque and bit-field datatypes now serialize losslessly, so repack carries
    // them through byte-for-byte instead of refusing.
    let src = tmp("hdf5_pure_repack_dt_src.h5");
    let dst = tmp("hdf5_pure_repack_dt_dst.h5");

    let opaque_dt = Datatype::Opaque {
        size: 4,
        tag: b"rgba".to_vec(),
    };
    let opaque_raw: Vec<u8> = vec![0xDE, 0xAD, 0xBE, 0xEF, 1, 2, 3, 4, 9, 8, 7, 6];
    let bitfield_dt = Datatype::BitField {
        size: 2,
        byte_order: DatatypeByteOrder::LittleEndian,
        bit_offset: 0,
        bit_precision: 12,
    };
    let bitfield_raw: Vec<u8> = vec![0x34, 0x12, 0xFF, 0x0F];

    let mut b = FileBuilder::new();
    b.create_dataset("blob")
        .with_raw_data(opaque_dt.clone(), opaque_raw.clone(), 3);
    b.create_dataset("flags")
        .with_raw_data(bitfield_dt.clone(), bitfield_raw.clone(), 2);
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    let blob = f.dataset("blob").unwrap();
    assert_eq!(blob.datatype().unwrap(), opaque_dt);
    assert_eq!(blob.read_raw().unwrap(), opaque_raw);
    let flags = f.dataset("flags").unwrap();
    assert_eq!(flags.datatype().unwrap(), bitfield_dt);
    assert_eq!(flags.read_raw().unwrap(), bitfield_raw);

    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn preserves_file_space_strategy() {
    let src = tmp("hdf5_pure_repack_fss_src.h5");
    let dst = tmp("hdf5_pure_repack_fss_dst.h5");
    let mut b = FileBuilder::new();
    b.create_dataset("keep").with_i32_data(&[1, 2, 3]);
    b.create_dataset("drop_me").with_f64_data(&vec![0.0; 1000]);
    b.with_file_space_strategy(FileSpaceStrategy::Page, false, 4)
        .with_file_space_page_size(8192);
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new().drop_path("drop_me")).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    // The strategy (and its page size / threshold) carries forward; persist is
    // reset to false since the compact output has no free space to persist.
    assert_eq!(f.file_space_strategy(), Some(FileSpaceStrategy::Page));
    let info = f.file_space_info().unwrap();
    assert_eq!(info.page_size, 8192);
    assert_eq!(info.threshold, 4);
    assert!(!info.persist);
    assert_eq!(
        f.dataset("keep").unwrap().read_i32().unwrap(),
        vec![1, 2, 3]
    );
    assert!(f.dataset("drop_me").is_err());
    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn rejects_nonexistent_drop_path() {
    let src = tmp("hdf5_pure_repack_baddrop_src.h5");
    let dst = tmp("hdf5_pure_repack_baddrop_dst.h5");
    let mut b = FileBuilder::new();
    b.create_dataset("present").with_i32_data(&[1]);
    b.write(&src).unwrap();

    let err = repack(&src, &dst, &RepackOptions::new().drop_path("absent")).unwrap_err();
    assert!(
        matches!(err, hdf5_pure::Error::RepackUnsupported(_)),
        "expected RepackUnsupported, got {err:?}"
    );
    // Nothing should have been written to dst.
    assert!(
        !dst.exists(),
        "dst must not be created when the repack fails"
    );
    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn verbatim_chunk_copy_preserves_compressed_bytes() {
    // A chunked + deflate dataset: repack copies its compressed chunks verbatim,
    // so the values read back are byte-identical and the dataset stays chunked +
    // compressed (the file remains far smaller than the raw element bytes).
    let src = tmp("hdf5_pure_repack_verbatim_src.h5");
    let dst = tmp("hdf5_pure_repack_verbatim_dst.h5");
    // Highly compressible data so the filter's survival is observable by size.
    let data: Vec<i32> = (0..4096).map(|i| i % 4).collect();
    let mut b = FileBuilder::new();
    b.create_dataset("vals")
        .with_i32_data(&data)
        .with_chunks(&[512])
        .with_shuffle()
        .with_deflate(6);
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    let ds = f.dataset("vals").unwrap();
    assert_eq!(
        ds.read_i32().unwrap(),
        data,
        "values must round-trip exactly"
    );
    assert!(
        ds.chunk_cache_stats().index_loaded(),
        "repacked dataset must still be chunked"
    );
    assert!(
        std::fs::metadata(&dst).unwrap().len() < 4096 * 4,
        "deflate filter must survive (file smaller than raw element bytes)"
    );

    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn repacks_multichunk_2d_fixed_array() {
    // A 2D dataset chunked into a 2x2 grid uses a v4 Fixed Array index. Repack's
    // verbatim path must lay the four chunks back in dense grid order so the
    // values round-trip exactly.
    let src = tmp("hdf5_pure_repack_fa_src.h5");
    let dst = tmp("hdf5_pure_repack_fa_dst.h5");
    // 4x4 grid, chunk 2x2 -> a 2x2 chunk grid (four chunks).
    let data: Vec<f64> = (0..16).map(|i| i as f64 * 1.5).collect();
    let mut b = FileBuilder::new();
    b.create_dataset("grid")
        .with_f64_data(&data)
        .with_shape(&[4, 4])
        .with_chunks(&[2, 2])
        .with_deflate(4);
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    let ds = f.dataset("grid").unwrap();
    assert_eq!(ds.shape().unwrap(), vec![4, 4]);
    assert_eq!(ds.read_f64().unwrap(), data);
    assert!(ds.chunk_cache_stats().index_loaded());

    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn repacks_resizable_extensible_array() {
    // An unlimited-maxshape dataset uses a v4 Extensible Array index. Repack must
    // carry the maxshape through and reproduce the values exactly.
    let src = tmp("hdf5_pure_repack_ea_src.h5");
    let dst = tmp("hdf5_pure_repack_ea_dst.h5");
    let data: Vec<i64> = (0..1000).collect();
    let mut b = FileBuilder::new();
    b.create_dataset("series")
        .with_i64_data(&data)
        .with_shape(&[1000])
        .with_maxshape(&[u64::MAX])
        .with_chunks(&[128])
        .with_deflate(3);
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    let ds = f.dataset("series").unwrap();
    assert_eq!(ds.read_i64().unwrap(), data);
    // Maxshape (resizability) is carried through.
    assert_eq!(ds.shape().unwrap(), vec![1000]);
    assert!(ds.chunk_cache_stats().index_loaded());

    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[cfg(feature = "zfp")]
#[test]
fn roundtrips_zfp_verbatim() {
    // ZFP is a lossy filter this crate's read-raw path would refuse to re-encode,
    // but a chunked dataset's compressed chunks are copied verbatim, so repack
    // reproduces the stored (lossy-compressed) values byte-exact. The values read
    // back from the repacked file must equal those read back from the source.
    let src = tmp("hdf5_pure_repack_zfp_src.h5");
    let dst = tmp("hdf5_pure_repack_zfp_dst.h5");
    let data: Vec<f64> = (0..1024).map(|i| (i as f64).sin()).collect();
    let mut b = FileBuilder::new();
    b.create_dataset("vals")
        .with_f64_data(&data)
        .with_chunks(&[256])
        .with_zfp(32.0);
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let src_vals = hdf5_pure::File::open(&src)
        .unwrap()
        .dataset("vals")
        .unwrap()
        .read_f64()
        .unwrap();
    let dst_f = hdf5_pure::File::open(&dst).unwrap();
    let dst_ds = dst_f.dataset("vals").unwrap();
    assert_eq!(
        dst_ds.read_f64().unwrap(),
        src_vals,
        "verbatim ZFP chunk copy must reproduce the stored values exactly"
    );
    assert!(
        dst_ds.chunk_cache_stats().index_loaded(),
        "repacked ZFP dataset must still be chunked"
    );

    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn repacks_single_chunk_filtered_verbatim() {
    // A dataset whose single chunk covers the whole dataset uses the v4
    // single-chunk index. The verbatim path must carry the chunk's real filter
    // mask into that index and reproduce the values exactly.
    let src = tmp("hdf5_pure_repack_single_src.h5");
    let dst = tmp("hdf5_pure_repack_single_dst.h5");
    let data: Vec<i32> = (0..256).map(|i| i % 5).collect();
    let mut b = FileBuilder::new();
    b.create_dataset("vals")
        .with_i32_data(&data)
        .with_chunks(&[256]) // one chunk covers all 256 elements
        .with_deflate(6);
    b.write(&src).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    let ds = f.dataset("vals").unwrap();
    assert_eq!(ds.read_i32().unwrap(), data);
    assert!(ds.chunk_cache_stats().index_loaded());

    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}

#[test]
fn repacks_chunked_dataset_from_a_userblock_file() {
    // The source carries a userblock (non-zero base address), so its chunk index
    // and chunk data are stored base-relative. Repack reads each chunk verbatim
    // from the source; it must apply the base address, or it reads the wrong bytes
    // and produces a corrupt copy.
    let src = tmp("hdf5_pure_repack_ub_src.h5");
    let dst = tmp("hdf5_pure_repack_ub_dst.h5");
    let data: Vec<f64> = (0..1000).map(|i| i as f64 * 0.25).collect();
    let mut b = FileBuilder::new();
    b.with_userblock(512);
    b.create_dataset("chk")
        .with_f64_data(&data)
        .with_shape(&[1000])
        .with_deflate(6);
    b.create_dataset("plain").with_f64_data(&[1.0, 2.0, 3.0]);
    std::fs::write(&src, b.finish().unwrap()).unwrap();

    repack(&src, &dst, &RepackOptions::new()).unwrap();

    let f = hdf5_pure::File::open(&dst).unwrap();
    assert_eq!(f.dataset("chk").unwrap().read_f64().unwrap(), data);
    assert_eq!(
        f.dataset("plain").unwrap().read_f64().unwrap(),
        vec![1.0, 2.0, 3.0]
    );

    std::fs::remove_file(&src).ok();
    std::fs::remove_file(&dst).ok();
}