Skip to content

Commit c8472c7

Browse files
committed
More direct array tests
1 parent f08ad4b commit c8472c7

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

gel-db-protocol/src/arrays.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,93 @@ mod tests {
286286
assert_eq!(rest_array.get(4), Some(5));
287287
assert_eq!(rest_array.get(5), None); // Out of bounds
288288
}
289+
290+
#[test]
291+
fn test_array_u32() {
292+
let data = vec![
293+
0x00, 0x00, 0x00, 0x03, // Length prefix
294+
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
295+
];
296+
297+
let mut buf = &data[..];
298+
let array = Array::<u32, u32>::decode_for(&mut buf).unwrap();
299+
300+
assert_eq!(array.len(), 3);
301+
assert!(!array.is_empty());
302+
assert_eq!(buf.len(), 0);
303+
304+
let collected: Vec<u32> = array.into_iter().collect();
305+
assert_eq!(collected, vec![1, 2, 3]);
306+
}
307+
308+
#[test]
309+
fn test_array_invalid_length() {
310+
let data = vec![
311+
0xFF, 0xFF, 0xFF, 0xFF, // Invalid length
312+
0x00, 0x00, 0x00, 0x01,
313+
];
314+
315+
let mut buf = &data[..];
316+
let result = Array::<u32, u32>::decode_for(&mut buf);
317+
assert!(result.is_err());
318+
}
319+
320+
#[test]
321+
fn test_zt_array() {
322+
let data = vec![
323+
0x01, 0x02, 0x03, 0x00, // Zero-terminated array
324+
];
325+
326+
let mut buf = &data[..];
327+
let array = ZTArray::<u8>::decode_for(&mut buf).unwrap();
328+
329+
assert_eq!(array.len(), 3);
330+
assert!(!array.is_empty());
331+
assert_eq!(buf.len(), 0);
332+
333+
let collected: Vec<u8> = array.into_iter().collect();
334+
assert_eq!(collected, vec![1, 2, 3]);
335+
}
336+
337+
#[test]
338+
fn test_zt_array_string() {
339+
let data = vec![
340+
b'h', b'e', b'l', b'l', b'o', b'\0', b'w', b'o', b'r', b'l', b'd', b'\0',
341+
b'\0', // Zero-terminated array
342+
];
343+
344+
let mut buf = &data[..];
345+
let array = ZTArray::<ZTString>::decode_for(&mut buf).unwrap();
346+
347+
assert_eq!(array.len(), 2);
348+
assert!(!array.is_empty());
349+
assert_eq!(buf.len(), 0);
350+
351+
let collected: Vec<_> = array.into_iter().collect();
352+
assert_eq!(collected, vec!["hello", "world"]);
353+
}
354+
355+
#[test]
356+
fn test_zt_array_missing_terminator() {
357+
let data = vec![0x01, 0x02, 0x03]; // No zero terminator
358+
359+
let mut buf = &data[..];
360+
let result = ZTArray::<u8>::decode_for(&mut buf);
361+
assert!(result.is_err());
362+
}
363+
364+
#[test]
365+
fn test_zt_array_empty() {
366+
let data = vec![0x00]; // Just terminator
367+
368+
let mut buf = &data[..];
369+
let array = ZTArray::<u8>::decode_for(&mut buf).unwrap();
370+
371+
assert_eq!(array.len(), 0);
372+
assert!(array.is_empty());
373+
assert_eq!(buf.len(), 0);
374+
375+
let collected: Vec<u8> = array.into_iter().collect();
376+
assert_eq!(collected, vec![]);
377+
}
289378
}

0 commit comments

Comments
 (0)