|
| 1 | +import pytest |
| 2 | + |
1 | 3 | from pyarinc.arinc717.aligned import AlignedStream |
2 | 4 | from pyarinc.arinc717.bitstream import BitstreamScanner |
3 | 5 |
|
@@ -66,3 +68,66 @@ def test_missing_frame_insertion(): |
66 | 68 | frames = list(aligned.iter_frames()) |
67 | 69 | # we expect two frames extracted sequentially |
68 | 70 | assert len(frames) >= 2 |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "sync_pat, sync_len, bitstream, expected_pos", |
| 75 | + [ |
| 76 | + (0b11, 2, "001100", 2), # Simple short sync pattern |
| 77 | + ( |
| 78 | + 0x257, |
| 79 | + 12, |
| 80 | + "000000000000" + "001001010111" + "1111", |
| 81 | + 12, |
| 82 | + ), # Standard 717 pattern |
| 83 | + (0xF, 4, "1111", 0), # Sync at the very beginning |
| 84 | + (0xF, 4, "000000001111", 8), # Sync at the very end |
| 85 | + ], |
| 86 | +) |
| 87 | +def test_bitstream_scanner_parametrized(sync_pat, sync_len, bitstream, expected_pos): |
| 88 | + data = bits_to_bytes(bitstream) |
| 89 | + scanner = BitstreamScanner(sync_pat, sync_len) |
| 90 | + positions = scanner.find_sync_positions(data) |
| 91 | + assert positions |
| 92 | + assert expected_pos in positions |
| 93 | + |
| 94 | + |
| 95 | +def test_bitstream_scanner_edge_cases(): |
| 96 | + # Sync length longer than data |
| 97 | + scanner = BitstreamScanner(0b1010, 16) |
| 98 | + assert scanner.find_sync_positions(b"\x0A") == [] |
| 99 | + |
| 100 | + # Zero or negative sync length |
| 101 | + scanner_zero = BitstreamScanner(0b1010, 0) |
| 102 | + assert scanner_zero.find_sync_positions(b"\x0A\x0B") == [] |
| 103 | + |
| 104 | + # Empty data |
| 105 | + scanner_empty = BitstreamScanner(0b10, 2) |
| 106 | + assert scanner_empty.find_sync_positions(b"") == [] |
| 107 | + |
| 108 | + |
| 109 | +def test_aligned_stream_truncated_tail(): |
| 110 | + # Setup data where a full frame requires 4 bytes total, but we leave only 2 bytes at the end |
| 111 | + sync = 0b11110000 |
| 112 | + sync_len = 8 |
| 113 | + # Frame size configuration: 2 subframes * 1 word per subframe * 8 bits = 2 bytes per frame (+ sync) |
| 114 | + # Let's make frame size 4 bytes by setting words_per_subframe=2, subframes_per_frame=2 |
| 115 | + # Total frame size = 2 * 2 * 8 bits = 32 bits = 4 bytes. |
| 116 | + frame1 = bytes([0xAA, 0xBB, 0xCC, 0xDD]) # Full 4-byte frame |
| 117 | + truncated_tail = bytes([0x11, 0x22]) # Only 2 bytes left (incomplete frame) |
| 118 | + |
| 119 | + data = bytes([sync]) + frame1 + truncated_tail |
| 120 | + |
| 121 | + aligned = AlignedStream.from_bitstream( |
| 122 | + data, |
| 123 | + sync_pattern=sync, |
| 124 | + sync_length=sync_len, |
| 125 | + word_bits=8, |
| 126 | + words_per_subframe=2, |
| 127 | + subframes_per_frame=2, # Requires 4 bytes per frame |
| 128 | + ) |
| 129 | + frames = list(aligned.iter_frames()) |
| 130 | + |
| 131 | + # Should safely drop the truncated 2-byte tail and only return the 1 valid frame |
| 132 | + assert len(frames) == 1 |
| 133 | + assert frames[0].frame_index == 0 |
0 commit comments