|
| 1 | +pub use crate::byte_array::{ |
| 2 | + Span as ByteArraySpan, SpanTrait as ByteArraySpanTrait, ToSpanTrait as ByteArrayToSpanTrait, |
| 3 | +}; |
1 | 4 | use crate::test::test_utils::{assert_eq, assert_ne};
|
2 | 5 |
|
3 | 6 | #[test]
|
@@ -463,6 +466,50 @@ fn test_from_collect() {
|
463 | 466 | assert_eq!(ba, "hello");
|
464 | 467 | }
|
465 | 468 |
|
| 469 | +#[test] |
| 470 | +fn test_span_len() { |
| 471 | + // Test simple happy flow --- value is included in the last word. |
| 472 | + // TODO(giladchase): add short string test here once supported cast into span. |
| 473 | + let ba: ByteArray = "A"; |
| 474 | + let span = ba.span(); |
| 475 | + assert_eq(@span.len(), @1, 'wrong span len'); |
| 476 | + assert!(!span.is_empty()); |
| 477 | + |
| 478 | + // Test empty. |
| 479 | + let empty_ba: ByteArray = ""; |
| 480 | + let empty_span = empty_ba.span(); |
| 481 | + assert_eq(@empty_span.len(), @0, 'empty span len != 0'); |
| 482 | + assert!(empty_span.is_empty()); |
| 483 | + |
| 484 | + // First word in the array + start offset, second in last word. |
| 485 | + let two_byte31: ByteArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg"; |
| 486 | + let mut single_span = two_byte31.span(); |
| 487 | + // TODO(giladchase): use slice once supported. |
| 488 | + single_span.first_char_start_offset = 1; |
| 489 | + |
| 490 | + assert_eq(@single_span.len(), @32, 'len error with start offset'); |
| 491 | + assert!(!single_span.is_empty()); |
| 492 | + |
| 493 | + // First word in the array + start offset, second in the array, third in last word. |
| 494 | + let three_bytes31: ByteArray = |
| 495 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789#$"; // 64 chars. |
| 496 | + let mut three_span = three_bytes31.span(); |
| 497 | + three_span.first_char_start_offset = 1; |
| 498 | + assert_eq(@three_span.len(), @63, 'len error with size-3 bytearray'); |
| 499 | + assert!(!three_span.is_empty()); |
| 500 | +} |
| 501 | + |
| 502 | +#[test] |
| 503 | +fn test_span_copy() { |
| 504 | + let ba: ByteArray = "12"; |
| 505 | + let span = ba.span(); |
| 506 | + assert_eq(@span.len(), @2, 'wrong span len'); |
| 507 | + |
| 508 | + let other_span = span; |
| 509 | + assert_eq(@other_span.len(), @2, 'span len is equal to Copy'); |
| 510 | + assert_eq(@other_span.len(), @span.len(), 'original span still usable'); |
| 511 | +} |
| 512 | + |
466 | 513 | // ========= Test helper functions =========
|
467 | 514 |
|
468 | 515 | fn compare_spans<T, +crate::fmt::Debug<T>, +PartialEq<T>, +Copy<T>, +Drop<T>>(
|
|
0 commit comments