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