Skip to main content

ionic/ion/decoder/decode/
arrays.rs

1use super::*;
2
3#[derive(Debug, Clone, Copy, Default)]
4pub(crate) struct ArrayAddress {
5    pub(crate) block_id: u32,
6    pub(crate) element_offset: u64,
7    pub(crate) element_count: u64,
8    pub(crate) array_type: u32,
9    pub(crate) dtype: u8,
10    pub(crate) array_filter: u8,
11    pub(crate) encoded_len: u32,
12    pub(crate) continues_previous_segment: u8,
13    pub(crate) array_cv_code: u8,
14}
15
16#[cfg(test)]
17impl ArrayAddress {
18    pub(crate) fn array_type(&self) -> u32 {
19        self.array_type
20    }
21
22    pub(crate) fn dtype(&self) -> u8 {
23        self.dtype
24    }
25
26    pub(crate) fn array_filter(&self) -> u8 {
27        self.array_filter
28    }
29}
30
31#[derive(Debug, Clone)]
32pub(crate) struct ArrayGroup {
33    pub(crate) array_type: u32,
34    pub(crate) array_cv_code: u8,
35    pub(crate) dtype: u8,
36    pub(crate) array_filter: u8,
37    pub(crate) refs: Vec<ArrayAddress>,
38}
39
40#[derive(Clone)]
41pub(crate) struct ArrayAddressList {
42    pub(crate) len: usize,
43    pub(crate) inline: [ArrayAddress; INLINE_ARRAY_ADDRESS_CAP],
44    pub(crate) heap: Option<Vec<ArrayAddress>>,
45}
46
47impl ArrayAddressList {
48    #[inline]
49    pub(crate) fn with_capacity(capacity: usize) -> Self {
50        Self {
51            len: 0,
52            inline: [ArrayAddress::default(); INLINE_ARRAY_ADDRESS_CAP],
53            heap: (capacity > INLINE_ARRAY_ADDRESS_CAP).then(|| Vec::with_capacity(capacity)),
54        }
55    }
56
57    #[inline]
58    pub(crate) fn push(&mut self, value: ArrayAddress) {
59        if let Some(heap) = self.heap.as_mut() {
60            heap.push(value);
61            self.len = heap.len();
62            return;
63        }
64        self.inline[self.len] = value;
65        self.len += 1;
66    }
67
68    #[inline]
69    pub(crate) fn is_empty(&self) -> bool {
70        self.len == 0
71    }
72
73    #[inline]
74    pub(crate) fn as_slice(&self) -> &[ArrayAddress] {
75        match self.heap.as_deref() {
76            Some(heap) => heap,
77            None => &self.inline[..self.len],
78        }
79    }
80
81    #[inline]
82    pub(crate) fn into_vec(self) -> Vec<ArrayAddress> {
83        self.heap
84            .unwrap_or_else(|| self.inline[..self.len].to_vec())
85    }
86}
87
88#[inline]
89pub(crate) fn address_read_params(array_address: &ArrayAddress) -> (u64, u64, usize) {
90    if array_address.encoded_len > 0 {
91        (
92            array_address.element_offset,
93            array_address.encoded_len as u64,
94            1,
95        )
96    } else {
97        (
98            array_address.element_offset,
99            array_address.element_count,
100            dtype_stride(array_address.dtype),
101        )
102    }
103}
104
105#[inline]
106pub(crate) fn parse_array_address(bytes: &[u8]) -> ArrayAddress {
107    ArrayAddress {
108        element_offset: u64::from_le_bytes(bytes[0..8].try_into().unwrap()),
109        element_count: u64::from_le_bytes(bytes[8..16].try_into().unwrap()),
110        block_id: u32::from_le_bytes(bytes[16..20].try_into().unwrap()),
111        array_type: u32::from_le_bytes(bytes[20..24].try_into().unwrap()),
112        dtype: bytes[24],
113        array_filter: bytes[25],
114        encoded_len: u32::from_le_bytes(bytes[26..30].try_into().unwrap()),
115        continues_previous_segment: bytes[30],
116        array_cv_code: bytes[31],
117    }
118}
119
120#[inline]
121pub(crate) fn read_array_addresses_from_buffers(
122    entries_buf: &[u8],
123    array_addresses_buf: &[u8],
124    index: usize,
125) -> Option<ArrayAddressList> {
126    let entry_offset = index.checked_mul(INDEX_ENTRY_BYTES)?;
127    let entry_end = entry_offset.checked_add(INDEX_ENTRY_BYTES)?;
128    let entry = entries_buf.get(entry_offset..entry_end)?;
129    let ref_start = usize::try_from(u64::from_le_bytes(entry[0..8].try_into().unwrap())).ok()?;
130    let address_count =
131        usize::try_from(u64::from_le_bytes(entry[8..16].try_into().unwrap())).ok()?;
132    let max_refs = array_addresses_buf.len() / ARRAY_ADDRESS_BYTES;
133    if address_count > max_refs {
134        return None;
135    }
136    let mut refs = ArrayAddressList::with_capacity(address_count);
137    for offset in 0..address_count {
138        let pos = ref_start
139            .checked_add(offset)?
140            .checked_mul(ARRAY_ADDRESS_BYTES)?;
141        let end = pos.checked_add(ARRAY_ADDRESS_BYTES)?;
142        refs.push(parse_array_address(array_addresses_buf.get(pos..end)?));
143    }
144    Some(refs)
145}
146
147pub(crate) fn group_arrays(refs: &[ArrayAddress]) -> IonResult<Vec<ArrayGroup>> {
148    if refs.is_empty() {
149        return Ok(Vec::new());
150    }
151
152    if refs[0].continues_previous_segment != 0 {
153        return Err("array grouping: first ref must have continues_previous_segment = 0".into());
154    }
155
156    let mut groups = Vec::new();
157    let mut current_group_refs = Vec::new();
158    let mut current_type = refs[0].array_type;
159    let mut current_cv_code = refs[0].array_cv_code;
160    let mut current_dtype = refs[0].dtype;
161    let mut current_filter = refs[0].array_filter;
162
163    for address in refs {
164        if address.continues_previous_segment != 0 && address.continues_previous_segment != 1 {
165            return Err(format!(
166                "array grouping: invalid continues_previous_segment value {}, must be 0 or 1",
167                address.continues_previous_segment
168            )
169            .into());
170        }
171
172        if address.continues_previous_segment == 0 {
173            if !current_group_refs.is_empty() {
174                groups.push(ArrayGroup {
175                    array_type: current_type,
176                    array_cv_code: current_cv_code,
177                    dtype: current_dtype,
178                    array_filter: current_filter,
179                    refs: current_group_refs,
180                });
181                current_group_refs = Vec::new();
182            }
183            current_type = address.array_type;
184            current_cv_code = address.array_cv_code;
185            current_dtype = address.dtype;
186            current_filter = address.array_filter;
187        } else if address.array_type != current_type
188            || address.dtype != current_dtype
189            || address.array_filter != current_filter
190        {
191            return Err(
192                "array grouping: continuation ref has different array_type, dtype, or filter"
193                    .into(),
194            );
195        }
196
197        current_group_refs.push(*address);
198    }
199
200    if !current_group_refs.is_empty() {
201        groups.push(ArrayGroup {
202            array_type: current_type,
203            array_cv_code: current_cv_code,
204            dtype: current_dtype,
205            array_filter: current_filter,
206            refs: current_group_refs,
207        });
208    }
209
210    for group in &groups {
211        if group.refs.len() > 1 {
212            for address in &group.refs {
213                if address.encoded_len > 0 {
214                    return Err(
215                        "array grouping: multi-ref group cannot contain variable-length arrays"
216                            .into(),
217                    );
218                }
219            }
220        }
221    }
222
223    Ok(groups)
224}
225
226#[inline]
227pub(crate) fn dtype_stride(dtype: u8) -> usize {
228    match dtype {
229        FILE_DTYPE_F64 | FILE_DTYPE_I64 => 8,
230        FILE_DTYPE_F32 | FILE_DTYPE_I32 => 4,
231        FILE_DTYPE_F16 | FILE_DTYPE_I16 => 2,
232        _ => 1,
233    }
234}
235
236pub(crate) fn unfilter_array_bytes(
237    raw: &[u8],
238    dtype: u8,
239    array_filter: u8,
240) -> IonResult<std::borrow::Cow<'_, [u8]>> {
241    let pk_id = PackingId::from_byte(array_filter)?;
242    match pk_id {
243        PackingId::Raw => Ok(std::borrow::Cow::Borrowed(raw)),
244        PackingId::ByteShuffle => match crate::ion::packing::Dtype::from_byte(dtype) {
245            Ok(
246                dtype_enum @ (crate::ion::packing::Dtype::F64 | crate::ion::packing::Dtype::F32),
247            ) => {
248                let mut out = Vec::new();
249                crate::ion::packing::packing_by_id(PackingId::ByteShuffle)
250                    .decode(raw, dtype_enum, &mut out)?;
251                Ok(std::borrow::Cow::Owned(out))
252            }
253            _ => Err(format!("array filter {array_filter} is not valid for dtype {dtype}").into()),
254        },
255        PackingId::DeltaShuffle => match crate::ion::packing::Dtype::from_byte(dtype) {
256            Ok(
257                dtype_enum @ (crate::ion::packing::Dtype::F64 | crate::ion::packing::Dtype::F32),
258            ) => {
259                let mut out = Vec::new();
260                crate::ion::packing::packing_by_id(PackingId::DeltaShuffle)
261                    .decode(raw, dtype_enum, &mut out)?;
262                Ok(std::borrow::Cow::Owned(out))
263            }
264            _ => Err(format!("array filter {array_filter} is not valid for dtype {dtype}").into()),
265        },
266    }
267}
268
269pub(crate) fn decode_into(
270    buf: &mut Vec<f64>,
271    raw: &[u8],
272    dtype: u8,
273    array_filter: u8,
274) -> IonResult<()> {
275    buf.clear();
276    let bytes = unfilter_array_bytes(raw, dtype, array_filter)?;
277    match dtype {
278        FILE_DTYPE_F64 => {
279            buf.reserve(bytes.len() / 8);
280            buf.extend(bytes.as_chunks::<8>().0.iter().map(|c| f64::from_le_bytes(*c)));
281        }
282        FILE_DTYPE_F32 => {
283            buf.reserve(bytes.len() / 4);
284            buf.extend(
285                bytes
286                    .as_chunks::<4>()
287                    .0
288                    .iter()
289                    .map(|c| f32::from_le_bytes(*c) as f64),
290            );
291        }
292        FILE_DTYPE_F16 => {
293            buf.reserve(bytes.len() / 2);
294            buf.extend(
295                bytes
296                    .as_chunks::<2>()
297                    .0
298                    .iter()
299                    .map(|c| f16_bits_to_f64(u16::from_le_bytes(*c))),
300            );
301        }
302        FILE_DTYPE_I16 => {
303            buf.reserve(bytes.len() / 2);
304            buf.extend(
305                bytes
306                    .as_chunks::<2>()
307                    .0
308                    .iter()
309                    .map(|c| i16::from_le_bytes(*c) as f64),
310            );
311        }
312        FILE_DTYPE_I32 => {
313            buf.reserve(bytes.len() / 4);
314            buf.extend(
315                bytes
316                    .as_chunks::<4>()
317                    .0
318                    .iter()
319                    .map(|c| i32::from_le_bytes(*c) as f64),
320            );
321        }
322        FILE_DTYPE_I64 => {
323            buf.reserve(bytes.len() / 8);
324            buf.extend(
325                bytes
326                    .as_chunks::<8>()
327                    .0
328                    .iter()
329                    .map(|c| i64::from_le_bytes(*c) as f64),
330            );
331        }
332        _ => {
333            return Err(IonError::BadDtype {
334                dtype,
335                kind: "decode array dtype",
336            });
337        }
338    }
339    Ok(())
340}
341
342fn read_group_values(
343    refs: &[ArrayAddress],
344    out: &mut Vec<f64>,
345    mut read_one: impl FnMut(&ArrayAddress, &mut Vec<f64>) -> IonResult<()>,
346) -> IonResult<()> {
347    let Some((first, rest)) = refs.split_first() else {
348        out.clear();
349        return Ok(());
350    };
351    read_one(first, out)?;
352    let mut window = Vec::new();
353    for array_address in rest {
354        read_one(array_address, &mut window)?;
355        out.extend_from_slice(&window);
356    }
357    Ok(())
358}
359
360impl IonReader {
361    pub(crate) fn spectrum_array_addresses(&self, index: usize) -> Option<Vec<ArrayAddress>> {
362        if index >= self.header.spectrum_count as usize {
363            return None;
364        }
365        read_array_addresses_from_buffers(&self.spec_entries_buf, &self.spec_array_addresses, index)
366            .map(ArrayAddressList::into_vec)
367    }
368
369    pub(crate) fn read_spectrum_values(
370        &mut self,
371        array_address: &ArrayAddress,
372        out: &mut Vec<f64>,
373    ) -> IonResult<()> {
374        let (element_offset, count, stride) = address_read_params(array_address);
375        let raw = self.spec_container.get_array_bytes_from_block(
376            array_address.block_id,
377            element_offset,
378            count,
379            stride,
380            "read_array",
381        )?;
382        decode_into(out, raw, array_address.dtype, array_address.array_filter)
383    }
384
385    pub(crate) fn read_spectrum_array(&mut self, array_address: &ArrayAddress) -> IonResult<NumericArray> {
386        let (element_offset, count, stride) = address_read_params(array_address);
387        let raw = self.spec_container.get_array_bytes_from_block(
388            array_address.block_id,
389            element_offset,
390            count,
391            stride,
392            "read_spectrum_array",
393        )?;
394        let values = unfilter_array_bytes(raw, array_address.dtype, array_address.array_filter)?;
395        super::to_mzml::decoded_bytes_to_binary_data(&values, array_address.dtype)
396    }
397
398    pub(crate) fn read_spectrum_group_values(
399        &mut self,
400        group: &ArrayGroup,
401        out: &mut Vec<f64>,
402    ) -> IonResult<()> {
403        read_group_values(&group.refs, out, |address, buf| {
404            self.read_spectrum_values(address, buf)
405        })
406    }
407
408    pub fn spectrum_array(
409        &mut self,
410        spectrum_index: usize,
411        array_type: impl Into<u32>,
412    ) -> IonResult<Vec<f64>> {
413        let mut values = Vec::new();
414        self.spectrum_array_into(spectrum_index, array_type, &mut values)?;
415        Ok(values)
416    }
417
418    pub fn spectrum_array_into(
419        &mut self,
420        spectrum_index: usize,
421        array_type: impl Into<u32>,
422        out: &mut Vec<f64>,
423    ) -> IonResult<()> {
424        out.clear();
425        let array_type = array_type.into();
426        if spectrum_index >= self.header.spectrum_count as usize {
427            return Err(IonError::OutOfRange {
428                index: spectrum_index,
429                count: self.header.spectrum_count,
430            });
431        }
432
433        let Some(array_addresses) = read_array_addresses_from_buffers(
434            &self.spec_entries_buf,
435            &self.spec_array_addresses,
436            spectrum_index,
437        ) else {
438            return Ok(());
439        };
440
441        let groups = group_arrays(array_addresses.as_slice())?;
442
443        for group in groups {
444            if group.array_type != array_type {
445                continue;
446            }
447            self.read_spectrum_group_values(&group, out)?;
448            return Ok(());
449        }
450
451        Ok(())
452    }
453
454    pub(crate) fn read_chromatogram_values(
455        &mut self,
456        array_address: &ArrayAddress,
457        out: &mut Vec<f64>,
458    ) -> IonResult<()> {
459        let container = self
460            .chrom_container
461            .as_mut()
462            .ok_or_else(|| IonError::from("no chromatogram container"))?;
463        let (element_offset, count, stride) = address_read_params(array_address);
464        let raw = container.get_array_bytes_from_block(
465            array_address.block_id,
466            element_offset,
467            count,
468            stride,
469            "read_chromatogram_values",
470        )?;
471        decode_into(out, raw, array_address.dtype, array_address.array_filter)
472    }
473
474    pub(crate) fn read_chromatogram_group_values(
475        &mut self,
476        group: &ArrayGroup,
477        out: &mut Vec<f64>,
478    ) -> IonResult<()> {
479        read_group_values(&group.refs, out, |address, buf| {
480            self.read_chromatogram_values(address, buf)
481        })
482    }
483
484    pub fn chromatogram_array(
485        &mut self,
486        chromatogram_index: usize,
487        array_type: impl Into<u32>,
488    ) -> IonResult<Vec<f64>> {
489        let mut values = Vec::new();
490        self.chromatogram_array_into(chromatogram_index, array_type, &mut values)?;
491        Ok(values)
492    }
493
494    pub fn chromatogram_array_into(
495        &mut self,
496        chromatogram_index: usize,
497        array_type: impl Into<u32>,
498        out: &mut Vec<f64>,
499    ) -> IonResult<()> {
500        out.clear();
501        let array_type = array_type.into();
502        if chromatogram_index >= self.header.chrom_count as usize {
503            return Err(IonError::OutOfRange {
504                index: chromatogram_index,
505                count: self.header.chrom_count,
506            });
507        }
508
509        let Some(array_addresses) = read_array_addresses_from_buffers(
510            &self.chrom_entries_buf,
511            &self.chrom_array_addresses,
512            chromatogram_index,
513        ) else {
514            return Ok(());
515        };
516
517        let groups = group_arrays(array_addresses.as_slice())?;
518
519        for group in groups {
520            if group.array_type != array_type {
521                continue;
522            }
523            self.read_chromatogram_group_values(&group, out)?;
524            return Ok(());
525        }
526
527        Ok(())
528    }
529}
530
531#[cfg(test)]
532impl IonReader {
533    pub(crate) fn chromatogram_array_addresses(&self, index: usize) -> Option<Vec<ArrayAddress>> {
534        if index >= self.header.chrom_count as usize {
535            return None;
536        }
537        read_array_addresses_from_buffers(
538            &self.chrom_entries_buf,
539            &self.chrom_array_addresses,
540            index,
541        )
542        .map(ArrayAddressList::into_vec)
543    }
544
545    pub(crate) fn read_chromatogram_array(
546        &mut self,
547        array_address: &ArrayAddress,
548    ) -> IonResult<NumericArray> {
549        let container = self
550            .chrom_container
551            .as_mut()
552            .ok_or_else(|| IonError::from("no chromatogram container"))?;
553        let (element_offset, count, stride) = address_read_params(array_address);
554        let raw = container.get_array_bytes_from_block(
555            array_address.block_id,
556            element_offset,
557            count,
558            stride,
559            "read_chromatogram_array",
560        )?;
561        let values = unfilter_array_bytes(raw, array_address.dtype, array_address.array_filter)?;
562        super::to_mzml::decoded_bytes_to_binary_data(&values, array_address.dtype)
563    }
564}
565
566#[cfg(test)]
567mod tests {
568    use super::*;
569
570    #[test]
571    fn unfilter_array_bytes_delta_shuffle_f64_matches_cumulative_sum() {
572        let deltas: [u64; 4] = [100u64, 5, 3, 10];
573        let raw: Vec<u8> = deltas.iter().flat_map(|w| w.to_le_bytes()).collect();
574
575        let unfiltered =
576            unfilter_array_bytes(&raw, FILE_DTYPE_F64, PackingId::DeltaShuffle as u8).unwrap();
577
578        let mut expected_prev: u64 = 0;
579        let mut expected = Vec::new();
580        for delta in deltas {
581            expected_prev = expected_prev.wrapping_add(delta);
582            expected.extend_from_slice(&expected_prev.to_le_bytes());
583        }
584
585        assert_eq!(unfiltered.as_ref(), expected.as_slice());
586    }
587
588    #[test]
589    fn unfilter_array_bytes_delta_shuffle_rejects_misaligned_f64_input() {
590        let raw = [0u8; 7];
591        let err = unfilter_array_bytes(&raw, FILE_DTYPE_F64, PackingId::DeltaShuffle as u8)
592            .expect_err("7 bytes is not a multiple of the f64 word size");
593        assert!(err.contains("not a multiple of the word size"));
594    }
595
596    #[test]
597    fn unfilter_array_bytes_byte_shuffle_f64_round_trips_71() {
598        use crate::ion::packing::{PackingInput, packing_by_id};
599
600        let data: Vec<f64> = (0..600)
601            .map(|i| 250.0 + (i as f64) * 0.0137 + ((i * 5 % 11) as f64) * 0.0011)
602            .collect();
603        let raw: Vec<u8> = data.iter().flat_map(|v| v.to_le_bytes()).collect();
604
605        let mut shuffled = Vec::new();
606        packing_by_id(PackingId::ByteShuffle)
607            .encode(PackingInput::F64(&data), &mut shuffled)
608            .unwrap();
609
610        assert_ne!(
611            shuffled, raw,
612            "shuffled layout must differ from raw for varying data"
613        );
614
615        let unfiltered =
616            unfilter_array_bytes(&shuffled, FILE_DTYPE_F64, PackingId::ByteShuffle as u8).unwrap();
617
618        assert_eq!(
619            unfiltered.as_ref(),
620            raw.as_slice(),
621            "decode of ByteShuffle-tagged bytes must unshuffle back to raw"
622        );
623
624        let out: Vec<f64> = unfiltered
625            .as_chunks::<8>()
626            .0
627            .iter()
628            .map(|c| f64::from_le_bytes(*c))
629            .collect();
630        assert_eq!(out.len(), data.len());
631        for (a, b) in out.iter().zip(data.iter()) {
632            assert_eq!(a.to_bits(), b.to_bits());
633        }
634    }
635
636    #[test]
637    fn unfilter_array_bytes_delta_shuffle_rejects_unsupported_dtype() {
638        let raw = [1u8, 2, 3, 4];
639        let err = unfilter_array_bytes(&raw, FILE_DTYPE_I16, PackingId::DeltaShuffle as u8)
640            .expect_err("delta shuffle is only valid for f32/f64");
641        assert!(err.contains("is not valid for dtype"));
642    }
643
644    #[test]
645    fn shuffle_filter_on_integer_dtype_is_rejected_4() {
646        let raw = [0u8; 8];
647
648        let err = unfilter_array_bytes(&raw, FILE_DTYPE_I32, PackingId::ByteShuffle as u8)
649            .expect_err("byte shuffle is only valid for f32/f64, not i32");
650        assert!(err.contains("is not valid for dtype"));
651
652        let ok = unfilter_array_bytes(&raw, FILE_DTYPE_F64, PackingId::Raw as u8)
653            .expect("raw filter must still work for any dtype");
654        assert_eq!(ok.as_ref(), raw.as_slice());
655    }
656
657    mod array_roundtrips {
658        use crate::{
659            IonReader, Range, ReadOptions, ScanQuery, Select, WriteOptions,
660            format::{CURRENT_VERSION, HEADER_FORMAT_VERSION_OFFSET},
661            ion::encoder::ion_writer::write_mzml_to_ion,
662            mzml::structs::{
663                BinaryDataArray, BinaryDataArrayList, Chromatogram, CvParam, FileContent,
664                FileDescription, MzML, NumericArray, NumericType, Run, Scan, ScanList,
665                SourceFileList, Spectrum, SpectrumList,
666            },
667        };
668        use proptest::prelude::*;
669
670        fn synthetic_ms_cv(accession: &str, value: Option<&str>) -> CvParam {
671            CvParam {
672                cv_ref: Some("MS".to_string()),
673                accession: Some(accession.to_string()),
674                name: accession.to_string(),
675                value: value.map(ToString::to_string),
676                ..Default::default()
677            }
678        }
679
680        fn precision_accession(numeric_type: NumericType) -> &'static str {
681            match numeric_type {
682                NumericType::Float64 => "MS:1000523",
683                NumericType::Float32 => "MS:1000521",
684                NumericType::Float16 => "MS:1000520",
685                NumericType::Int64 => "MS:1000522",
686                NumericType::Int32 => "MS:1000519",
687                NumericType::Int16 => "MS:1000518",
688            }
689        }
690
691        fn synthetic_binary_data_array(
692            role_accession: &str,
693            numeric_type: NumericType,
694            binary: NumericArray,
695            declared_length: Option<usize>,
696        ) -> BinaryDataArray {
697            BinaryDataArray {
698                array_length: declared_length,
699                cv_params: vec![
700                    synthetic_ms_cv(role_accession, None),
701                    synthetic_ms_cv(precision_accession(numeric_type), None),
702                    synthetic_ms_cv("MS:1000576", None),
703                ],
704                numeric_type: Some(numeric_type),
705                binary: Some(binary),
706                ..Default::default()
707            }
708        }
709
710        fn minimal_file_description() -> FileDescription {
711            FileDescription {
712                file_content: FileContent::default(),
713                source_file_list: SourceFileList {
714                    count: Some(0),
715                    source_file: Vec::new(),
716                },
717                contacts: Vec::new(),
718            }
719        }
720
721        fn mzml_with_single_array(
722            numeric_type: NumericType,
723            binary: NumericArray,
724            len: usize,
725        ) -> MzML {
726            MzML {
727                file_description: Some(minimal_file_description()),
728                run: Run {
729                    id: format!("array-test-{numeric_type:?}"),
730                    spectrum_list: Some(SpectrumList {
731                        count: Some(1),
732                        spectra: vec![Spectrum {
733                            id: "scan=1".to_string(),
734                            index: Some(0),
735                            default_array_length: Some(len),
736                            binary_data_array_list: Some(BinaryDataArrayList {
737                                count: Some(1),
738                                binary_data_arrays: vec![synthetic_binary_data_array(
739                                    "MS:1000515",
740                                    numeric_type,
741                                    binary,
742                                    Some(len),
743                                )],
744                            }),
745                            ..Default::default()
746                        }],
747                        ..Default::default()
748                    }),
749                    ..Default::default()
750                },
751                ..Default::default()
752            }
753        }
754
755        fn build_mzml(spectra: Vec<Spectrum>, chromatograms: Vec<Chromatogram>) -> MzML {
756            MzML {
757                file_description: Some(minimal_file_description()),
758                run: Run {
759                    id: "test-run".to_string(),
760                    spectrum_list: if spectra.is_empty() {
761                        None
762                    } else {
763                        Some(SpectrumList {
764                            count: Some(spectra.len()),
765                            spectra,
766                            ..Default::default()
767                        })
768                    },
769                    chromatogram_list: if chromatograms.is_empty() {
770                        None
771                    } else {
772                        Some(crate::mzml::structs::ChromatogramList {
773                            count: Some(chromatograms.len()),
774                            chromatograms,
775                            ..Default::default()
776                        })
777                    },
778                    ..Default::default()
779                },
780                ..Default::default()
781            }
782        }
783
784        fn make_spectrum_f64(id: &str, mz: Vec<f64>, intensity: Vec<f64>) -> Spectrum {
785            let len = mz.len();
786            Spectrum {
787                id: id.to_string(),
788                index: Some(0),
789                default_array_length: Some(len),
790                binary_data_array_list: Some(BinaryDataArrayList {
791                    count: Some(2),
792                    binary_data_arrays: vec![
793                        synthetic_binary_data_array(
794                            "MS:1000514",
795                            NumericType::Float64,
796                            NumericArray::F64(mz),
797                            Some(len),
798                        ),
799                        synthetic_binary_data_array(
800                            "MS:1000515",
801                            NumericType::Float64,
802                            NumericArray::F64(intensity),
803                            Some(len),
804                        ),
805                    ],
806                }),
807                ..Default::default()
808            }
809        }
810
811        fn make_chromatogram_f64(id: &str, time: Vec<f64>, intensity: Vec<f64>) -> Chromatogram {
812            let len = time.len();
813            Chromatogram {
814                id: id.to_string(),
815                index: Some(0),
816                default_array_length: Some(len),
817                binary_data_array_list: Some(BinaryDataArrayList {
818                    count: Some(2),
819                    binary_data_arrays: vec![
820                        synthetic_binary_data_array(
821                            "MS:1000595",
822                            NumericType::Float64,
823                            NumericArray::F64(time),
824                            Some(len),
825                        ),
826                        synthetic_binary_data_array(
827                            "MS:1000515",
828                            NumericType::Float64,
829                            NumericArray::F64(intensity),
830                            Some(len),
831                        ),
832                    ],
833                }),
834                ..Default::default()
835            }
836        }
837
838        fn encode_to_ion(mzml: &MzML, compression_level: u8, force_f32: bool) -> Vec<u8> {
839            let mut out = Vec::new();
840            write_mzml_to_ion(
841                mzml,
842                &WriteOptions {
843                    compression_level,
844                    force_f32,
845                    ..Default::default()
846                },
847                &mut out,
848            )
849            .expect("encode should succeed");
850            out
851        }
852
853        fn decode_ion(bytes: &[u8]) -> crate::IonResult<MzML> {
854            let mut decoder = IonReader::from_bytes(bytes, &ReadOptions::default())?;
855            decoder.to_mzml()
856        }
857
858        fn roundtrip(mzml: &MzML) -> MzML {
859            decode_ion(&encode_to_ion(mzml, 0, false)).expect("decode should succeed")
860        }
861
862        fn first_spectrum_binary(mzml: &MzML) -> Option<&NumericArray> {
863            mzml.run
864                .spectrum_list
865                .as_ref()?
866                .spectra
867                .first()?
868                .binary_data_array_list
869                .as_ref()?
870                .binary_data_arrays
871                .first()?
872                .binary
873                .as_ref()
874        }
875
876        fn first_chrom_array_values_by_accession(c: &Chromatogram, accession: &str) -> Vec<f64> {
877            let bda = c
878                .binary_data_array_list
879                .as_ref()
880                .expect("binary data array list present")
881                .binary_data_arrays
882                .iter()
883                .find(|a| {
884                    a.cv_params
885                        .iter()
886                        .any(|p| p.accession.as_deref() == Some(accession))
887                })
888                .expect("array with accession present");
889            bda.binary.as_ref().expect("binary payload present").to_f64()
890        }
891
892        #[test]
893        fn roundtrip_f64_array() {
894            let values = vec![1.0_f64, -2.5, 0.0, f64::MAX, f64::MIN, std::f64::consts::PI];
895            let len = values.len();
896            let mzml =
897                mzml_with_single_array(NumericType::Float64, NumericArray::F64(values.clone()), len);
898            let out = roundtrip(&mzml);
899            let bin = first_spectrum_binary(&out).expect("should have binary data");
900            let got = bin.to_f64();
901            assert_eq!(got, values);
902        }
903
904        #[test]
905        fn roundtrip_f32_array() {
906            let values = vec![1.0_f32, -2.5, 0.0, f32::MAX, f32::MIN, std::f32::consts::PI];
907            let len = values.len();
908            let mzml =
909                mzml_with_single_array(NumericType::Float32, NumericArray::F32(values.clone()), len);
910            let out = roundtrip(&mzml);
911            let bin = first_spectrum_binary(&out).expect("should have binary data");
912            let got = bin.to_f64();
913            let expected: Vec<f64> = values.iter().map(|v| *v as f64).collect();
914            assert_eq!(got, expected);
915        }
916
917        #[test]
918        fn array_filter_label_matches_applied_transform() {
919            let raw_filter = 0u8;
920            let f64_dtype = 1u8;
921            let f32_dtype = 2u8;
922
923            let f32_values = vec![10.0_f32, 11.0, 12.0, 13.0];
924            let f32_len = f32_values.len();
925            let f32_mzml =
926                mzml_with_single_array(NumericType::Float32, NumericArray::F32(f32_values), f32_len);
927
928            let f64_values = vec![100.0_f64, 100.5, 101.0, 101.5];
929            let f64_len = f64_values.len();
930            let f64_mzml =
931                mzml_with_single_array(NumericType::Float64, NumericArray::F64(f64_values), f64_len);
932
933            let f32_bytes = encode_to_ion(&f32_mzml, 9, false);
934            let f32_decoder =
935                IonReader::from_bytes(&f32_bytes, &ReadOptions::default()).expect("open f32 ion");
936            let f32_refs = f32_decoder
937                .spectrum_array_addresses(0)
938                .expect("f32 array refs");
939
940            let f64_bytes = encode_to_ion(&f64_mzml, 9, false);
941            let f64_decoder =
942                IonReader::from_bytes(&f64_bytes, &ReadOptions::default()).expect("open f64 ion");
943            let f64_refs = f64_decoder
944                .spectrum_array_addresses(0)
945                .expect("f64 array refs");
946
947            assert!(
948                f32_refs
949                    .iter()
950                    .any(|a| a.dtype() == f32_dtype && a.array_filter() == raw_filter),
951                "f32 intensity must be tagged raw"
952            );
953            assert!(
954                f64_refs
955                    .iter()
956                    .any(|a| a.dtype() == f64_dtype && a.array_filter() == raw_filter),
957                "f64 intensity must be tagged raw (intensity is never delta-shuffled)"
958            );
959        }
960
961        #[test]
962        fn roundtrip_i64_array() {
963            let values = vec![0_i64, 1, -1, i64::MAX, i64::MIN, 42];
964            let len = values.len();
965            let mzml =
966                mzml_with_single_array(NumericType::Int64, NumericArray::I64(values.clone()), len);
967            let out = roundtrip(&mzml);
968            let bin = first_spectrum_binary(&out).expect("should have binary data");
969            match bin {
970                NumericArray::I64(got) => assert_eq!(got, &values),
971                other => {
972                    let got = other.to_f64();
973                    let expected: Vec<f64> = values.iter().map(|v| *v as f64).collect();
974                    assert_eq!(
975                        got, expected,
976                        "i64 roundtrip values differ (via f64 conversion)"
977                    );
978                }
979            }
980        }
981
982        #[test]
983        fn roundtrip_i32_array() {
984            let values = vec![0_i32, 1, -1, i32::MAX, i32::MIN, 42];
985            let len = values.len();
986            let mzml =
987                mzml_with_single_array(NumericType::Int32, NumericArray::I32(values.clone()), len);
988            let out = roundtrip(&mzml);
989            let bin = first_spectrum_binary(&out).expect("should have binary data");
990            match bin {
991                NumericArray::I32(got) => assert_eq!(got, &values),
992                other => {
993                    let got = other.to_f64();
994                    let expected: Vec<f64> = values.iter().map(|v| *v as f64).collect();
995                    assert_eq!(got, expected, "i32 roundtrip values differ");
996                }
997            }
998        }
999
1000        #[test]
1001        fn roundtrip_i16_array() {
1002            let values = vec![0_i16, 1, -1, i16::MAX, i16::MIN, 42];
1003            let len = values.len();
1004            let mzml =
1005                mzml_with_single_array(NumericType::Int16, NumericArray::I16(values.clone()), len);
1006            let out = roundtrip(&mzml);
1007            let bin = first_spectrum_binary(&out).expect("should have binary data");
1008            match bin {
1009                NumericArray::I16(got) => assert_eq!(got, &values),
1010                other => {
1011                    let got = other.to_f64();
1012                    let expected: Vec<f64> = values.iter().map(|v| *v as f64).collect();
1013                    assert_eq!(got, expected, "i16 roundtrip values differ");
1014                }
1015            }
1016        }
1017
1018        #[test]
1019        fn roundtrip_single_element_per_type() {
1020            let cases: Vec<(NumericType, NumericArray)> = vec![
1021                (NumericType::Float64, NumericArray::F64(vec![42.0])),
1022                (NumericType::Float32, NumericArray::F32(vec![42.0])),
1023                (NumericType::Int64, NumericArray::I64(vec![42])),
1024                (NumericType::Int32, NumericArray::I32(vec![42])),
1025                (NumericType::Int16, NumericArray::I16(vec![42])),
1026            ];
1027
1028            for (nt, bin) in cases {
1029                let mzml = mzml_with_single_array(nt, bin.clone(), 1);
1030                let out = roundtrip(&mzml);
1031                let got = first_spectrum_binary(&out).expect("should have binary data");
1032                let expected_f64 = bin.to_f64();
1033                let got_f64 = got.to_f64();
1034                assert_eq!(
1035                    got_f64, expected_f64,
1036                    "single-element roundtrip failed for {nt:?}"
1037                );
1038            }
1039        }
1040
1041        #[test]
1042        fn roundtrip_at_compression_levels() {
1043            let values = vec![100.0_f64, 200.0, 300.0, 400.0, 500.0];
1044            let len = values.len();
1045            let mzml =
1046                mzml_with_single_array(NumericType::Float64, NumericArray::F64(values.clone()), len);
1047
1048            for level in [0, 3, 10, 22] {
1049                let mut buf = Vec::new();
1050                write_mzml_to_ion(
1051                    &mzml,
1052                    &WriteOptions {
1053                        compression_level: level,
1054                        force_f32: false,
1055                        ..Default::default()
1056                    },
1057                    &mut buf,
1058                )
1059                .unwrap_or_else(|e| panic!("encode at level {level} failed: {e}"));
1060                let mut decoder = IonReader::from_bytes(&buf, &ReadOptions::default())
1061                    .unwrap_or_else(|e| panic!("decoder open at level {level} failed: {e}"));
1062                let decoded = decoder
1063                    .to_mzml()
1064                    .unwrap_or_else(|e| panic!("to_mzml at level {level} failed: {e}"));
1065                let got = first_spectrum_binary(&decoded).expect("should have binary data");
1066                assert_eq!(
1067                    got.to_f64(),
1068                    values,
1069                    "values differ at compression level {level}"
1070                );
1071            }
1072        }
1073
1074        #[test]
1075        fn roundtrip_force_f32_downcasts() {
1076            let values = vec![100.0_f64, 200.0, 300.0];
1077            let len = values.len();
1078            let mzml =
1079                mzml_with_single_array(NumericType::Float64, NumericArray::F64(values.clone()), len);
1080
1081            let mut buf = Vec::new();
1082            write_mzml_to_ion(
1083                &mzml,
1084                &WriteOptions {
1085                    compression_level: 0,
1086                    force_f32: true,
1087                    ..Default::default()
1088                },
1089                &mut buf,
1090            )
1091            .expect("encode with force_f32");
1092            let mut decoder =
1093                IonReader::from_bytes(&buf, &ReadOptions::default()).expect("decoder open");
1094            let decoded = decoder.to_mzml().expect("to_mzml");
1095
1096            let bin = first_spectrum_binary(&decoded).expect("should have binary data");
1097            let got = bin.to_f64();
1098            for (i, (g, e)) in got.iter().zip(values.iter()).enumerate() {
1099                let expected_f32 = *e as f32 as f64;
1100                assert!(
1101                    (g - expected_f32).abs() < 1e-6,
1102                    "index {i}: force_f32 value mismatch: {g} vs {expected_f32}"
1103                );
1104            }
1105        }
1106
1107        fn encode_decode_mz_compressed(mz: Vec<f64>) -> Vec<f64> {
1108            let len = mz.len();
1109            let mzml = mzml_with_single_array(NumericType::Float64, NumericArray::F64(mz), len);
1110            let buf = encode_to_ion(&mzml, 3, false);
1111            let decoded = decode_ion(&buf).unwrap();
1112            first_spectrum_binary(&decoded).unwrap().to_f64()
1113        }
1114
1115        #[test]
1116        fn delta_mz_single_element_is_bit_exact() {
1117            let input = vec![503.42f64];
1118            let got = encode_decode_mz_compressed(input.clone());
1119            assert_eq!(got[0].to_bits(), input[0].to_bits());
1120        }
1121
1122        #[test]
1123        fn delta_mz_two_elements_are_bit_exact() {
1124            let input = vec![100.0f64, 200.5];
1125            let got = encode_decode_mz_compressed(input.clone());
1126            for (a, b) in got.iter().zip(input.iter()) {
1127                assert_eq!(a.to_bits(), b.to_bits());
1128            }
1129        }
1130
1131        #[test]
1132        fn delta_mz_monotonic_array_is_bit_exact() {
1133            let input: Vec<f64> = (0..10_000).map(|i| 100.0 + i as f64 * 0.01).collect();
1134            let got = encode_decode_mz_compressed(input.clone());
1135            for (a, b) in got.iter().zip(input.iter()) {
1136                assert_eq!(a.to_bits(), b.to_bits());
1137            }
1138        }
1139
1140        #[test]
1141        fn delta_mz_special_values_are_bit_exact() {
1142            let input = vec![f64::NAN, f64::INFINITY, f64::NEG_INFINITY, -0.0f64, 0.0f64];
1143            let got = encode_decode_mz_compressed(input.clone());
1144            for (a, b) in got.iter().zip(input.iter()) {
1145                assert_eq!(a.to_bits(), b.to_bits());
1146            }
1147        }
1148
1149        #[test]
1150        fn delta_mz_via_for_each_scan_is_bit_exact() {
1151            let input: Vec<f64> = (0..500).map(|i| 100.0 + i as f64 * 0.05).collect();
1152            let intensity: Vec<f64> = vec![1.0; input.len()];
1153            let mut spectrum = make_spectrum_f64("scan=1", input.clone(), intensity);
1154            spectrum.scan_list = Some(ScanList {
1155                count: Some(1),
1156                scans: vec![Scan {
1157                    cv_params: vec![CvParam {
1158                        cv_ref: Some("MS".to_string()),
1159                        accession: Some("MS:1000016".to_string()),
1160                        name: "scan start time".to_string(),
1161                        value: Some("1.0".to_string()),
1162                        unit_accession: Some("UO:0000031".to_string()),
1163                        ..Default::default()
1164                    }],
1165                    ..Default::default()
1166                }],
1167                ..Default::default()
1168            });
1169            let mzml = build_mzml(vec![spectrum], vec![]);
1170            let buf = encode_to_ion(&mzml, 3, false);
1171            let mut ion = IonReader::from_bytes(&buf, &ReadOptions::default()).unwrap();
1172            let mut got_mz: Vec<f64> = Vec::new();
1173            let query = ScanQuery {
1174                mz: Range {
1175                    from: 0.0,
1176                    to: f64::MAX,
1177                },
1178                select: Select::All,
1179                ms_level: None,
1180            };
1181            ion.scans_in(&query, |window| {
1182                got_mz = window.mz.to_vec();
1183            })
1184            .unwrap();
1185            assert_eq!(got_mz.len(), input.len());
1186            for (a, b) in got_mz.iter().zip(input.iter()) {
1187                assert_eq!(a.to_bits(), b.to_bits());
1188            }
1189        }
1190
1191        #[test]
1192        fn delta_on_mz_raw_on_intensity() {
1193            let mz: Vec<f64> = (0..100).map(|i| 100.0 + i as f64).collect();
1194            let intensity: Vec<f64> = (0..100).map(|i| (i * 10) as f64).collect();
1195            let mzml = build_mzml(
1196                vec![make_spectrum_f64("scan=1", mz.clone(), intensity.clone())],
1197                vec![],
1198            );
1199            let buf = encode_to_ion(&mzml, 3, false);
1200            let mut decoder = IonReader::from_bytes(&buf, &ReadOptions::default()).unwrap();
1201            let refs = decoder.spectrum_array_addresses(0).unwrap();
1202            let mz_address = refs.iter().find(|r| r.array_type() == 1_000_514).unwrap();
1203            let int_ref = refs.iter().find(|r| r.array_type() == 1_000_515).unwrap();
1204            assert_eq!(
1205                mz_address.array_filter(),
1206                2,
1207                "m/z must use DeltaShuffle filter"
1208            );
1209            assert_eq!(
1210                int_ref.array_filter(),
1211                0,
1212                "intensity must use raw filter (intensity is never delta-shuffled)"
1213            );
1214            let mut got_mz = Vec::new();
1215            decoder
1216                .read_spectrum_values(mz_address, &mut got_mz)
1217                .unwrap();
1218            let mut got_int = Vec::new();
1219            decoder.read_spectrum_values(int_ref, &mut got_int).unwrap();
1220            for (a, b) in got_mz.iter().zip(mz.iter()) {
1221                assert_eq!(a.to_bits(), b.to_bits());
1222            }
1223            for (a, b) in got_int.iter().zip(intensity.iter()) {
1224                assert_eq!(a.to_bits(), b.to_bits());
1225            }
1226        }
1227
1228        #[test]
1229        fn format_version_always_matches_current() {
1230            let values = vec![1.0_f64, 2.0, 3.0, 4.0, 5.0];
1231            let len = values.len();
1232            let mzml =
1233                mzml_with_single_array(NumericType::Float64, NumericArray::F64(values.clone()), len);
1234            for level in [0u8, 3, 22] {
1235                let buf = encode_to_ion(&mzml, level, false);
1236                let format_version = u16::from_le_bytes(
1237                    buf[HEADER_FORMAT_VERSION_OFFSET..HEADER_FORMAT_VERSION_OFFSET + 2]
1238                        .try_into()
1239                        .unwrap(),
1240                );
1241                assert_eq!(
1242                    format_version, CURRENT_VERSION,
1243                    "format_version must match CURRENT_VERSION at compression level {level}"
1244                );
1245            }
1246            let out = roundtrip(&mzml);
1247            let bin = first_spectrum_binary(&out).expect("binary data");
1248            for (a, b) in bin.to_f64().iter().zip(values.iter()) {
1249                assert_eq!(a.to_bits(), b.to_bits());
1250            }
1251        }
1252
1253        #[test]
1254        fn delta_not_applied_without_compression() {
1255            let mz: Vec<f64> = (0..10).map(|i| 100.0 + i as f64).collect();
1256            let intensity = vec![1.0_f64; mz.len()];
1257            let mzml = build_mzml(
1258                vec![make_spectrum_f64("scan=1", mz.clone(), intensity)],
1259                vec![],
1260            );
1261            let buf = encode_to_ion(&mzml, 0, false);
1262            let mut decoder = IonReader::from_bytes(&buf, &ReadOptions::default()).unwrap();
1263            let refs = decoder.spectrum_array_addresses(0).unwrap();
1264            let mz_address = refs.iter().find(|r| r.array_type() == 1_000_514).unwrap();
1265            assert_eq!(mz_address.array_filter(), 0, "no delta without compression");
1266            let mut got = Vec::new();
1267            decoder.read_spectrum_values(mz_address, &mut got).unwrap();
1268            for (a, b) in got.iter().zip(mz.iter()) {
1269                assert_eq!(a.to_bits(), b.to_bits());
1270            }
1271        }
1272
1273        #[test]
1274        fn delta_shuffle_applied_to_time_array() {
1275            let time: Vec<f64> = (0..100).map(|i| i as f64 * 0.1).collect();
1276            let intensity: Vec<f64> = vec![1.0; time.len()];
1277            let mzml = build_mzml(
1278                vec![],
1279                vec![make_chromatogram_f64("tic", time.clone(), intensity)],
1280            );
1281            let buf = encode_to_ion(&mzml, 3, false);
1282            let mut decoder = IonReader::from_bytes(&buf, &ReadOptions::default()).unwrap();
1283            let refs = decoder.chromatogram_array_addresses(0).unwrap();
1284            let time_ref = refs.iter().find(|r| r.array_type() == 1_000_595).unwrap();
1285            assert_eq!(
1286                time_ref.array_filter(),
1287                2,
1288                "time must use DeltaShuffle filter"
1289            );
1290            let mut got = Vec::new();
1291            decoder
1292                .read_chromatogram_values(time_ref, &mut got)
1293                .unwrap();
1294            for (a, b) in got.iter().zip(time.iter()) {
1295                assert_eq!(a.to_bits(), b.to_bits());
1296            }
1297        }
1298
1299        #[test]
1300        fn read_chromatogram_array_keeps_native_f32_width_33() {
1301            let time: Vec<f64> = (0..64).map(|i| i as f64 * 0.5).collect();
1302            let intensity: Vec<f64> = (0..64).map(|i| i as f64).collect();
1303            let mzml = build_mzml(vec![], vec![make_chromatogram_f64("tic", time, intensity)]);
1304            let buf = encode_to_ion(&mzml, 3, true);
1305            let mut decoder = IonReader::from_bytes(&buf, &ReadOptions::default()).unwrap();
1306            let refs = decoder.chromatogram_array_addresses(0).unwrap();
1307            let intensity_ref = refs.iter().find(|r| r.array_type() == 1_000_515).unwrap();
1308            let native = decoder.read_chromatogram_array(intensity_ref).unwrap();
1309            assert!(
1310                matches!(native, NumericArray::F32(_)),
1311                "read_chromatogram_array must keep the stored native f32 width"
1312            );
1313            let mut widened = Vec::new();
1314            decoder
1315                .read_chromatogram_values(intensity_ref, &mut widened)
1316                .unwrap();
1317            assert_eq!(
1318                widened.len(),
1319                64,
1320                "read_chromatogram_values must still widen to f64"
1321            );
1322        }
1323
1324        proptest! {
1325            #![proptest_config(ProptestConfig::with_cases(32))]
1326
1327            #[test]
1328            fn proptest_arbitrary_f64_roundtrip(
1329                values in prop::collection::vec(prop::num::f64::ANY, 1..64)
1330            ) {
1331                let len = values.len();
1332                let mzml = mzml_with_single_array(
1333                    NumericType::Float64,
1334                    NumericArray::F64(values.clone()),
1335                    len,
1336                );
1337                let out = roundtrip(&mzml);
1338                let bin = first_spectrum_binary(&out).expect("should have binary data");
1339                let got = bin.to_f64();
1340                prop_assert_eq!(got.len(), values.len());
1341                for (i, (g, e)) in got.iter().zip(values.iter()).enumerate() {
1342                    if e.is_nan() {
1343                        prop_assert!(g.is_nan(), "index {}: expected NaN", i);
1344                    } else {
1345                        prop_assert_eq!(g.to_bits(), e.to_bits(), "index {}: bit mismatch", i);
1346                    }
1347                }
1348            }
1349
1350            #[test]
1351            fn proptest_delta2vbyte_mz_roundtrip(
1352                mz in prop::collection::vec(0.0f64..100_000.0, 3..256)
1353            ) {
1354                let mut sorted_mz = mz.clone();
1355                sorted_mz.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
1356                let intensity = vec![1.0f64; sorted_mz.len()];
1357                let mzml = build_mzml(
1358                    vec![make_spectrum_f64("scan=1", sorted_mz.clone(), intensity)],
1359                    vec![],
1360                );
1361                let buf = encode_to_ion(&mzml, 3, false);
1362                let got = decode_ion(&buf).unwrap();
1363                let bin = first_spectrum_binary(&got).expect("should have binary data");
1364                let decoded = bin.to_f64();
1365                prop_assert_eq!(decoded.len(), sorted_mz.len());
1366                for (i, (g, e)) in decoded.iter().zip(sorted_mz.iter()).enumerate() {
1367                    if e.is_nan() {
1368                        prop_assert!(g.is_nan(), "index {}: expected NaN", i);
1369                    } else {
1370                        prop_assert_eq!(g.to_bits(), e.to_bits(), "index {}: bit mismatch", i);
1371                    }
1372                }
1373            }
1374
1375            #[test]
1376            fn proptest_chimp_time_roundtrip(
1377                time in prop::collection::vec(prop::num::f64::ANY, 2..256)
1378            ) {
1379                let intensity = vec![1.0f64; time.len()];
1380                let mzml = build_mzml(
1381                    vec![],
1382                    vec![make_chromatogram_f64("tic", time.clone(), intensity)],
1383                );
1384                let buf = encode_to_ion(&mzml, 3, false);
1385                let got = decode_ion(&buf).unwrap();
1386                let chrom_list = got.run.chromatogram_list.unwrap();
1387                let decoded = first_chrom_array_values_by_accession(&chrom_list.chromatograms[0], "MS:1000595");
1388                prop_assert_eq!(decoded.len(), time.len());
1389                for (i, (g, e)) in decoded.iter().zip(time.iter()).enumerate() {
1390                    if e.is_nan() {
1391                        prop_assert!(g.is_nan(), "index {}: expected NaN", i);
1392                    } else {
1393                        prop_assert_eq!(g.to_bits(), e.to_bits(), "index {}: bit mismatch", i);
1394                    }
1395                }
1396            }
1397
1398            #[test]
1399            fn proptest_arbitrary_i32_roundtrip(
1400                values in prop::collection::vec(prop::num::i32::ANY, 1..64)
1401            ) {
1402                let len = values.len();
1403                let mzml = mzml_with_single_array(
1404                    NumericType::Int32,
1405                    NumericArray::I32(values.clone()),
1406                    len,
1407                );
1408                let out = roundtrip(&mzml);
1409                let bin = first_spectrum_binary(&out).expect("should have binary data");
1410                match bin {
1411                    NumericArray::I32(got) => prop_assert_eq!(got, &values),
1412                    other => {
1413                        let got = other.to_f64();
1414                        let expected: Vec<f64> = values.iter().map(|v| *v as f64).collect();
1415                        prop_assert_eq!(got, expected);
1416                    }
1417                }
1418            }
1419
1420            #[test]
1421            fn proptest_arbitrary_i16_roundtrip(
1422                values in prop::collection::vec(prop::num::i16::ANY, 1..64)
1423            ) {
1424                let len = values.len();
1425                let mzml = mzml_with_single_array(
1426                    NumericType::Int16,
1427                    NumericArray::I16(values.clone()),
1428                    len,
1429                );
1430                let out = roundtrip(&mzml);
1431                let bin = first_spectrum_binary(&out).expect("should have binary data");
1432                match bin {
1433                    NumericArray::I16(got) => prop_assert_eq!(got, &values),
1434                    other => {
1435                        let got = other.to_f64();
1436                        let expected: Vec<f64> = values.iter().map(|v| *v as f64).collect();
1437                        prop_assert_eq!(got, expected);
1438                    }
1439                }
1440            }
1441        }
1442    }
1443}