upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/xma_decoder/tests
diff options
context:
space:
mode:
authorRafael <rafaelalvesp2005@gmail.com>2026-11-23 15:36:26 -0300
committerRafael <rafaelalvesp2005@gmail.com>2026-11-23 15:36:26 -0300
commit63f0e0dea3a9d44201db408c4e2c3f2f5b2b94d6 (patch)
tree86f0dcad8070e2a3a693ef4897aa83950cfeb224 /runtime/xma_decoder/tests
parentf78f7d6a2a9364cb128c03ac539fe540d5c70df1 (diff)
parent6dda0630bd6fc0514baadb94fb98e36bd7d64c0b (diff)
Merge main into fix/walk-bink_names
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/xma_decoder/tests')
-rw-r--r--runtime/xma_decoder/tests/ffi_contract.rs227
-rw-r--r--runtime/xma_decoder/tests/ffi_cpp_smoke.cpp87
2 files changed, 314 insertions, 0 deletions
diff --git a/runtime/xma_decoder/tests/ffi_contract.rs b/runtime/xma_decoder/tests/ffi_contract.rs
new file mode 100644
index 000000000..0eb8e5cf4
--- /dev/null
+++ b/runtime/xma_decoder/tests/ffi_contract.rs
@@ -0,0 +1,227 @@
1use leonida_xma2_decoder::*;
2use std::ptr;
3use xma::bits::BitWriter;
4use xma::Xma2Stream;
5
6const PACKET_BYTES: usize = 2048;
7
8fn synthetic_silent_packet(channels: usize) -> [u8; PACKET_BYTES] {
9 assert!(channels == 1 || channels == 2);
10
11 let mut frame = BitWriter::default();
12 frame.put(1, 1); // Fixed-size tile header.
13 frame.put(1, 0); // One full-length subframe.
14 if channels == 2 {
15 frame.put(1, 0); // No optional stereo decorrelation matrix.
16 }
17 frame.put(8, 0); // No dynamic range adjustment.
18 frame.put(1, 0); // No trim metadata.
19 frame.put(1, 0); // No fill data.
20 frame.put(1, 0); // No corrupt-subframe marker.
21 if channels == 2 {
22 frame.put(1, 0); // No stereo transform error.
23 frame.put(1, 1); // Two-channel group without decorrelation.
24 frame.put(1, 0);
25 }
26 for _ in 0..channels {
27 frame.put(1, 0); // No transmitted coefficients: silence.
28 }
29 frame.put(1, 0); // Align to the next frame boundary.
30 frame.put(1, 0); // This packet contains one frame.
31
32 let frame_bits = 15 + frame.bits;
33 let mut packet_writer = BitWriter::default();
34 packet_writer.put(6, 1);
35 packet_writer.put(15, frame_bits as u32);
36 packet_writer.put(3, 1);
37 packet_writer.put(8, 0);
38 packet_writer.put(15, frame_bits as u32);
39 packet_writer.copy_from(&frame.buf, 0, frame.bits);
40
41 let mut packet = [0u8; PACKET_BYTES];
42 packet[..packet_writer.buf.len()].copy_from_slice(&packet_writer.buf);
43 packet
44}
45
46fn make_decoder(rate: u32, channels: u32) -> *mut LeonidaXma2Decoder {
47 let mut decoder = ptr::null_mut();
48 let result = unsafe { leonida_xma2_decoder_create(rate, channels, &mut decoder) };
49 assert_eq!(result, LEONIDA_XMA2_OK);
50 assert!(!decoder.is_null());
51 decoder
52}
53
54#[test]
55fn creation_rejects_invalid_outputs_and_formats() {
56 assert_eq!(
57 unsafe { leonida_xma2_decoder_create(48_000, 2, ptr::null_mut()) },
58 LEONIDA_XMA2_INVALID_ARGUMENT
59 );
60
61 let mut decoder = ptr::dangling_mut::<LeonidaXma2Decoder>();
62 assert_eq!(
63 unsafe { leonida_xma2_decoder_create(48_000, 3, &mut decoder) },
64 LEONIDA_XMA2_INVALID_FORMAT
65 );
66 assert!(decoder.is_null());
67
68 assert_eq!(
69 unsafe { leonida_xma2_decoder_create(0, 1, &mut decoder) },
70 LEONIDA_XMA2_INVALID_FORMAT
71 );
72 assert!(decoder.is_null());
73}
74
75#[test]
76fn synthetic_silent_packet_returns_owned_pcm_block_after_decoder_destruction() {
77 let decoder = make_decoder(48_000, 2);
78 let packet = synthetic_silent_packet(2);
79 let mut input = [0u8; PACKET_BYTES * 3];
80 input[PACKET_BYTES..PACKET_BYTES * 2].copy_from_slice(&packet);
81 input[PACKET_BYTES * 2..].copy_from_slice(&packet);
82 let mut block = ptr::null_mut();
83
84 assert_eq!(
85 unsafe { leonida_xma2_decode_buffer(decoder, input.as_ptr(), input.len(), &mut block,) },
86 LEONIDA_XMA2_OK
87 );
88 assert!(!block.is_null());
89
90 let samples = unsafe { leonida_xma2_pcm_sample_count(block) };
91 assert_eq!(samples % 2, 0);
92 assert!(samples > 0);
93 let pcm = unsafe { leonida_xma2_pcm_data(block) };
94 assert!(!pcm.is_null());
95 let decoded = unsafe { std::slice::from_raw_parts(pcm, samples) };
96 assert!(decoded.iter().all(|sample| *sample == 0));
97
98 unsafe { leonida_xma2_decoder_destroy(decoder) };
99 assert_eq!(unsafe { leonida_xma2_pcm_sample_count(block) }, samples);
100 assert_eq!(
101 unsafe { std::slice::from_raw_parts(leonida_xma2_pcm_data(block), samples) },
102 decoded
103 );
104 unsafe { leonida_xma2_pcm_destroy(block) };
105}
106
107#[test]
108fn synthetic_silent_mono_stream_decodes_to_silent_pcm() {
109 let decoder = make_decoder(44_100, 1);
110 let packet = synthetic_silent_packet(1);
111 let mut input = [0u8; PACKET_BYTES * 3];
112 input[PACKET_BYTES..PACKET_BYTES * 2].copy_from_slice(&packet);
113 input[PACKET_BYTES * 2..].copy_from_slice(&packet);
114 let mut block = ptr::null_mut();
115
116 assert_eq!(
117 unsafe { leonida_xma2_decode_buffer(decoder, input.as_ptr(), input.len(), &mut block) },
118 LEONIDA_XMA2_OK
119 );
120 assert!(!block.is_null());
121 let samples = unsafe { leonida_xma2_pcm_sample_count(block) };
122 assert!(samples > 0);
123 let pcm = unsafe { leonida_xma2_pcm_data(block) };
124 assert!(!pcm.is_null());
125 assert!(unsafe { std::slice::from_raw_parts(pcm, samples) }
126 .iter()
127 .all(|sample| *sample == 0));
128
129 unsafe {
130 leonida_xma2_pcm_destroy(block);
131 leonida_xma2_decoder_destroy(decoder);
132 }
133}
134
135#[test]
136fn decode_rejects_nulls_and_non_packet_aligned_buffers() {
137 let decoder = make_decoder(44_100, 1);
138 let packet = [0u8; PACKET_BYTES];
139 let mut block = ptr::dangling_mut::<LeonidaXma2PcmBlock>();
140
141 assert_eq!(
142 unsafe { leonida_xma2_decode_buffer(decoder, packet.as_ptr(), 0, &mut block) },
143 LEONIDA_XMA2_BAD_PACKET_SIZE
144 );
145 assert!(block.is_null());
146
147 assert_eq!(
148 unsafe {
149 leonida_xma2_decode_buffer(decoder, packet.as_ptr(), PACKET_BYTES - 1, &mut block)
150 },
151 LEONIDA_XMA2_BAD_PACKET_SIZE
152 );
153 assert!(block.is_null());
154
155 assert_eq!(
156 unsafe { leonida_xma2_decode_buffer(decoder, ptr::null(), PACKET_BYTES, &mut block) },
157 LEONIDA_XMA2_INVALID_ARGUMENT
158 );
159 assert!(block.is_null());
160
161 unsafe { leonida_xma2_decoder_destroy(decoder) };
162}
163
164#[test]
165fn deterministic_synthetic_packets_do_not_unwind_across_ffi() {
166 let decoder = make_decoder(48_000, 2);
167 for seed in 1..17u32 {
168 let mut state = seed.wrapping_mul(2_654_435_761).wrapping_add(1);
169 let mut packet = [0u8; PACKET_BYTES];
170 for byte in &mut packet {
171 state ^= state << 13;
172 state ^= state >> 17;
173 state ^= state << 5;
174 *byte = state as u8;
175 }
176
177 let mut block = ptr::null_mut();
178 let result = unsafe {
179 leonida_xma2_decode_buffer(decoder, packet.as_ptr(), packet.len(), &mut block)
180 };
181 assert!(
182 result == LEONIDA_XMA2_OK
183 || result == LEONIDA_XMA2_DECODER_FAILURE
184 || result == LEONIDA_XMA2_PANIC,
185 "unexpected status {result}"
186 );
187 if !block.is_null() {
188 unsafe { leonida_xma2_pcm_destroy(block) };
189 }
190 }
191
192 unsafe { leonida_xma2_decoder_destroy(decoder) };
193}
194
195#[test]
196fn multiple_packet_buffer_matches_serial_decoder_calls() {
197 let decoder = make_decoder(48_000, 2);
198 let packet = synthetic_silent_packet(2);
199 let mut input = [0u8; PACKET_BYTES * 3];
200 input[PACKET_BYTES..PACKET_BYTES * 2].copy_from_slice(&packet);
201 input[PACKET_BYTES * 2..].copy_from_slice(&packet);
202
203 let mut reference = Xma2Stream::new(48_000, 2).unwrap();
204 let mut expected = Vec::new();
205 for packet in input.as_chunks::<PACKET_BYTES>().0 {
206 reference.decode_packet(packet, &mut expected);
207 }
208
209 let mut block = ptr::null_mut();
210 assert_eq!(
211 unsafe { leonida_xma2_decode_buffer(decoder, input.as_ptr(), input.len(), &mut block) },
212 LEONIDA_XMA2_OK
213 );
214 assert!(!block.is_null());
215 let samples = unsafe { leonida_xma2_pcm_sample_count(block) };
216 assert!(samples > 0);
217 assert_eq!(samples, expected.len());
218 assert_eq!(samples % 2, 0);
219 let pcm = unsafe { leonida_xma2_pcm_data(block) };
220 let actual = unsafe { std::slice::from_raw_parts(pcm, samples) };
221 assert_eq!(actual, expected.as_slice());
222
223 unsafe {
224 leonida_xma2_pcm_destroy(block);
225 leonida_xma2_decoder_destroy(decoder);
226 }
227}
diff --git a/runtime/xma_decoder/tests/ffi_cpp_smoke.cpp b/runtime/xma_decoder/tests/ffi_cpp_smoke.cpp
new file mode 100644
index 000000000..cb41c81f5
--- /dev/null
+++ b/runtime/xma_decoder/tests/ffi_cpp_smoke.cpp
@@ -0,0 +1,87 @@
1#include "../leonida_xma2_decoder.h"
2
3#include <algorithm>
4#include <array>
5#include <cstdint>
6#include <cstdio>
7#include <cstdlib>
8
9void check(bool condition, const char* expression, int line) {
10 if (!condition) {
11 std::fprintf(stderr, "ffi_cpp_smoke.cpp:%d: %s\n", line, expression);
12 std::exit(EXIT_FAILURE);
13 }
14}
15
16#define CHECK(condition) check((condition), #condition, __LINE__)
17
18template <size_t Size>
19void put_bits(std::array<uint8_t, Size>& bytes, size_t& position, uint32_t width,
20 uint32_t value) {
21 for (uint32_t bit = width; bit > 0; --bit, ++position) {
22 if (((value >> (bit - 1)) & 1u) != 0) {
23 bytes[position / 8] |= static_cast<uint8_t>(1u << (7 - (position % 8)));
24 }
25 }
26}
27
28std::array<uint8_t, LEONIDA_XMA2_PACKET_BYTES> make_silent_stereo_packet() {
29 std::array<uint8_t, LEONIDA_XMA2_PACKET_BYTES> packet{};
30 constexpr uint32_t frame_bits = 36;
31 size_t position = 0;
32 put_bits(packet, position, 6, 1);
33 put_bits(packet, position, 15, frame_bits);
34 put_bits(packet, position, 3, 1);
35 put_bits(packet, position, 8, 0);
36 put_bits(packet, position, 15, frame_bits);
37 put_bits(packet, position, 1, 1); // Fixed-size tile header.
38 put_bits(packet, position, 1, 0); // One full-length subframe.
39 put_bits(packet, position, 1, 0); // No optional stereo decorrelation matrix.
40 put_bits(packet, position, 8, 0); // No dynamic range adjustment.
41 put_bits(packet, position, 1, 0); // No trim metadata.
42 put_bits(packet, position, 1, 0); // No fill data.
43 put_bits(packet, position, 1, 0); // No corrupt-subframe marker.
44 put_bits(packet, position, 1, 0); // No stereo transform error.
45 put_bits(packet, position, 1, 1); // Two-channel group without decorrelation.
46 put_bits(packet, position, 1, 0);
47 put_bits(packet, position, 2, 0); // No transmitted coefficients: silence.
48 put_bits(packet, position, 1, 0); // Frame alignment bit.
49 put_bits(packet, position, 1, 0); // No additional frame in this packet.
50 CHECK(position == 32 + frame_bits);
51 return packet;
52}
53
54int main() {
55 LeonidaXma2Decoder* decoder = nullptr;
56 CHECK(leonida_xma2_decoder_create(48000, 2, &decoder) == LEONIDA_XMA2_OK);
57 CHECK(decoder != nullptr);
58
59 const auto packet = make_silent_stereo_packet();
60 std::array<uint8_t, LEONIDA_XMA2_PACKET_BYTES * 3> input{};
61 std::copy(packet.begin(), packet.end(), input.begin() + LEONIDA_XMA2_PACKET_BYTES);
62 std::copy(packet.begin(), packet.end(), input.begin() + LEONIDA_XMA2_PACKET_BYTES * 2);
63 LeonidaXma2PcmBlock* pcm = nullptr;
64 CHECK(leonida_xma2_decode_buffer(decoder, input.data(), input.size(), &pcm) ==
65 LEONIDA_XMA2_OK);
66 CHECK(pcm != nullptr);
67 const size_t samples = leonida_xma2_pcm_sample_count(pcm);
68 CHECK(samples > 0);
69 CHECK(samples % 2 == 0);
70 const int16_t* data = leonida_xma2_pcm_data(pcm);
71 CHECK(data != nullptr);
72
73 LeonidaXma2PcmBlock* invalid = reinterpret_cast<LeonidaXma2PcmBlock*>(1);
74 CHECK(leonida_xma2_decode_buffer(decoder, input.data(), input.size() - 1, &invalid) ==
75 LEONIDA_XMA2_BAD_PACKET_SIZE);
76 CHECK(invalid == nullptr);
77
78 leonida_xma2_decoder_destroy(decoder);
79 CHECK(leonida_xma2_pcm_sample_count(pcm) == samples);
80 CHECK(leonida_xma2_pcm_data(pcm) == data);
81 for (size_t i = 0; i < samples; ++i) {
82 CHECK(data[i] == 0);
83 }
84 leonida_xma2_pcm_destroy(pcm);
85 leonida_xma2_decoder_destroy(nullptr);
86 return 0;
87}