Skip to content

test/cmocka: Characterize the util/darray.h operations the interleaver uses - #1596

Merged
zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:test/darray-ops-characterization
Oct 21, 2026
Merged

zackees merged 1 commit into
obs-rust:masterfrom
rdoupe:test/darray-ops-characterization

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Refs #1594 (step 3, part 1 of 2: the characterization; the Rust darray operations in obs-util follow in their own PR)

What and why

The interleaver port (#1594, step 4) works on darrays that obs-output.c also owns and frees: interleaved_packets, encoder_packet_times[] and keyframe_group_tracking. It needs the C semantics, in place on a C-owned darray, of the operations obs-output-interleave.c.h calls: da_insert, da_erase_range, da_pop_front, da_push_back_new, da_erase and da_free. The Rust equivalent of util/darray.h in obs-util has free, reserve, ensure_capacity, clear, resize, push_back_array, erase and pop_back. Insert, erase range, pop front and push back new are missing.

The testing policy says a header-inline unit gets a layout test, a 1:1 Tier 1 port of its C test and Tier 3 parity against oracle wrappers. test_darray has one case, which covers only da_push_back_array, so Tier 1 would have nothing to port for the new operations. This PR adds the characterization test first, as #1548 (test_deque) did for util/deque.h. It contains no Rust and does not change libobs. test_darray is unchanged.

New test test_darray_ops (tests.d/ fragment, all platforms, plus the Cargo C-test manifest rust/obs-build/manifests/c-tests-linux.json). It links libobs only for bmalloc / bfree. 14 cases:

  • Layout: struct darray is three pointer-sized words (array, num, capacity, in that order) with pointer alignment, and the DARRAY(type) union's typed fields alias .da.
  • Insert: da_insert at the front, middle and end (idx == num takes the push-back path), into an array zeroed with memset the way resort_interleaved_packets does it, and on a 16-byte record. Growth follows the darray_ensure_capacity rule: the first allocation takes the new size, then the capacity doubles, or becomes the new size when that is larger (1 → 2 → 4 → 8 → 16; 3 → 6 from a reserve). A middle insert into a full array grows it and keeps every item.
  • Erase: da_erase at the front, middle and end, down to empty. da_erase_range from the front (as discard_to_idx does), the middle and the end, a range of one (the darray_erase path) and the whole array (only num changes).
  • Pop front: da_pop_front down to empty.
  • Push back new: da_push_back_new returns the new last item, zeroed, after any growth and on a reused slot. With GCC and Clang it is a macro of its own, so the darray_push_back_new function that other compilers use is called directly as well.
  • Memory: removal never shrinks or frees the buffer. The bmalloc accounting: the first insert allocates, growth replaces the block (the count stays the same), removal neither allocates nor frees, da_free releases it and zeroes the struct, and freeing an array that never allocated is fine.
  • The interleaver's patterns: the resort (move .da out, memset the owner, insert every old item in sorted position, free the old buffer) and the packet-time arrays (records appended with da_push_back_new, the oldest dropped with da_pop_front, a matching one erased from the middle), over 40 rounds.

Characterized, not endorsed: removal moves the tail down and lowers num, but the slots past the new num keep their old values. Nothing may read them. The test pins this only so that the Rust equivalent, which works in place on the same C buffer, is known to leave the same bytes.

Every call meets the header's asserts (idx <= num, end > start, a non-empty array for pop_front), so the results do not depend on NDEBUG. The functions are static inline, so the test exercises the C header in both libobs builds and cannot see a Rust equivalent. In the follow-up PR, Tier 3 parity against oracle_* wrappers of these inlines checks the Rust side.

RED → GREEN

  • No RED commit: this is a characterization test of unchanged C, and it passes on master's header. Commit: f2f2a3da6 test/cmocka: Characterize the util/darray.h operations the interleaver uses.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON). In each, test_darray_ops, test_darray and test_deque ran against libobs and against libobs-with-rust (loaded in its place), headless:
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 14/14 14/14 IDENTICAL (1766, same as master)
Rust (ENABLE_RUST_LIBOBS=ON) 14/14 14/14 IDENTICAL (1766)
  • test_darray (1/1) and test_deque (16/16) also pass in all four runs, and the export lists are identical across the two lanes and both libraries.
  • The test also passes 14/14 under AddressSanitizer and UndefinedBehaviorSanitizer.
  • Deliberate-break check, on a copy of libobs/util/ placed first on the include path (the header in the tree is unchanged):
    • darray_insert writing the new item at idx + 1 instead of idx: 4 of 14 cases fail;
    • darray_erase_range lowering num by count - 1: 3 of 14 fail;
    • darray_pop_front erasing the last item instead of the first: 3 of 14 fail;
    • darray_push_back_new without its memset: 1 of 14 fails, and the same for the GCC/Clang da_push_back_new macro;
    • the growth rule changed from capacity * 2 to capacity * 2 + 1: 9 of 14 fail;
    • with the header restored, 14/14 pass.
  • ci/local_gate.py:
    • --lane lint (clang-format, gersemi, rustfmt, clippy -D warnings, Dylints, ifdef ratchet, test-binary cap): passed.
    • --lane cross: passed.
    • --lane cross-release: passed.
    • --lane rust-test (soldr cargo test --workspace --locked, plus a --no-run link for x86_64-pc-windows-gnu): passed, 1732 tests, none failed, 1 ignored. No Rust test binary was added.
  • Real app: not applicable (no behavior change).

Windows/macOS: not built there. The test is portable C with no host conditionals and links only OBS::libobs and cmocka. It uses no assert_in_range, so it builds on cmocka 1.x and 2.x.

Checklist

  • Base is master
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (this PR is the characterization; RED and GREEN are the follow-up)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): not applicable (no port here)

@rdoupe
rdoupe requested a review from zackees as a code owner October 21, 2026 02:02
…r uses

The output interleaver port (obs-rust#1594) has to insert into, erase ranges
from, pop the front of and append zeroed items to darrays that C in
obs-output.c also owns and frees, so the Rust equivalent of the
header-inline util/darray.h in obs-util needs those operations with the
C semantics. The testing policy has Tier 1 port the unit's C test 1:1,
but test_darray covers only da_push_back_array, so this pins the
current C behaviour first, as test_deque did for util/deque.h.

test_darray_ops (14 cases, all platforms, plus the Cargo C-test
manifest) covers the struct darray layout and the DARRAY union; da_insert
at the front, middle and end, into a zeroed array, and through growth
(the ensure_capacity rule, including from a reserve that is not a power
of two), on a multi-word element; da_erase and da_erase_range from the
front, middle and end, a range of one, and the whole array; da_pop_front;
da_push_back_new (the GCC and Clang macro, and the darray_push_back_new
function other compilers use) returning the zeroed new last item after
growth and on a reused slot; that removal never shrinks or frees the buffer; the
bmalloc accounting; and the interleaver's resort and packet-time
patterns. Characterized, not endorsed: removal leaves the old values in
the slots past the new num.

The functions are static inline, so the test exercises the C header in
both libobs builds; the Rust equivalent will be checked against oracle
wrappers of them. Every call satisfies the header's asserts, so the
results do not depend on NDEBUG. No change to libobs.

Refs obs-rust#1594
@zackees
zackees force-pushed the test/darray-ops-characterization branch from f2f2a3d to f7661f7 Compare October 21, 2026 02:12
@zackees
zackees enabled auto-merge October 21, 2026 02:12
@zackees

zackees commented Oct 21, 2026

Copy link
Copy Markdown
Member

Coordinator: rebased onto master. The only conflict was c-tests-linux.json, where test_output_interleave landed at the same spot; kept both entries. Auto-merge on.

@zackees
zackees added this pull request to the merge queue Oct 21, 2026
Merged via the queue into obs-rust:master with commit fc698ca Oct 21, 2026
25 checks passed
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 21, 2026
…1600)

* obs-util: Add Tier 1 tests for the darray.h interleaver operations

The interleaver port (obs-rust#1594) uses the C darrays that obs-output.c resets
and frees (interleaved_packets, encoder_packet_times[] and
keyframe_group_tracking) in place, so obs-util's darray equivalent needs
the inline operations it uses and does not have yet: insert, erase
range, pop front and push back new (the function and the GCC/Clang
macro, which do the same steps). obs-rust#1596 characterized them in
test_darray_ops.

This adds those functions with todo!() bodies, to the raw API that runs
on a struct darray with bmem memory (plus darray_push_back, which insert
at num calls) and to the safe DArray, and Tier 1: every test_darray_ops
case ported 1:1 under the same names on the raw API, as the C test runs
the header, including the "characterized, not endorsed" stale slots
past num after removal. The bnum_allocs() cases run in their own
process. New edge cases: insert at num into a full array, into a
reserved empty one and with an odd item size, growth copying the stale
tail, push back new zeroing only the new item, pop front on an empty
array, the precondition panics (where C asserts), and the same
operations on the safe core.

30 of 31 fail with "not yet implemented"; layout passes, as it checks
the existing struct mirror.

Refs obs-rust#1594

* obs-util: Add the darray.h operations the interleaver uses

The interleaver port (obs-rust#1594) uses interleaved_packets,
encoder_packet_times[] and keyframe_group_tracking in place: C darrays
that obs-output.c resets and frees. darray.h is header-inline, so
nothing swaps; the Rust equivalent is a library the port calls, and the
struct darray layout is the contract.

- obs_util::ffi::darray: darray_insert, darray_erase_range,
  darray_pop_front and darray_push_back_new (the function, and the
  GCC/Clang da_push_back_new macro, which does the same steps), plus
  darray_push_back, which insert at num calls. They follow the header
  step by step on a struct darray with bmem memory, so C and Rust can
  hand one array back and forth: num grows before the capacity check,
  growth copies the whole old capacity, removal moves the tail down and
  leaves the old values past num (characterized, not endorsed), and only
  the new item of push back new is zeroed. An index or range the header
  asserts on panics; pop front on an empty array is a no-op, as the C
  body checks num after its assert.
- obs_util::darray::DArray: the same four operations on the safe core.
- Oracle: oracle_* wrappers of the five inline functions, and
  oracle_da_push_back_new, the macro on a typed DARRAY for 1, 4, 12 and
  16-byte items.
- Tier 3: darray_ops_match_c now covers every Rust function on twin
  arrays (and the safe core for one-byte items), comparing num,
  capacity, the null-ness of the buffer, the return values and every
  determined byte, including the stale slots past num.
  darray_mixed_in_place_matches_c hands one array to Rust or C at random
  against a C-only twin; interleaver_patterns_match_c runs the
  test_darray_ops packet-time and resort patterns that way, and
  bmem_accounting_matches_c checks the bnum_allocs() deltas of a hand-off
  in both directions, in its own process.
- rust/README.md: the darray row.

No intentional differences.

Refs obs-rust#1594
chriscoveries pushed a commit to chriscoveries/obsrust-fork that referenced this pull request Oct 21, 2026
* libobs: Add Tier 1 tests for the output interleaver port

RED for the port of libobs/obs-output-interleave.c.h, the encoded packet
interleaver, to obs-output's new interleave module. The pure parts work
on a slice of packets (a PacketView trait): the first and last packet of
a track, the start index, the premature prune, the insertion position,
the offset arithmetic, the streamable count and the catch-up rule, the
discard count, the caption timing and the packet-time lookup. The
functions named after the C ones are generic over the output
(InterleaveOutput): its fields, the queue and the other darrays, the
locks, the encoders and the libobs calls the C file makes, so the C ABI
shim can run them on the real struct obs_output through the hidden
accessors of obs-rust#1599, and the tests on a model. Every function is a
todo!() stub.

The first thirty cases mirror test_output_interleave 1:1 (same names,
same assertions, in order) on a model output that records what the C
test's stubs record: each sent packet with whether interleaved_mutex was
held, each packet callback call with its packet time and whether
pkt_callbacks_mutex was held, each add_caption call with whether the
track's caption mutex was held, and every held copy until it is
released. The other 23 pin the edges: the dts_usec and encoder duration
truncation, the first and last packet of a track, the start index and
the premature prune without audio, the duration lookups stopping at the
missing track, the insertion position at equal times, the opposing-type
test without video encoders, the catch-up boundaries, the discard
count, the caption timing at equal times, the first matching packet
time, single-encoder and foreign group entries, one lock per packet and
none when inactive, a delayed waiting packet released after the unlock,
a packet time offset after the start, an audio packet's time going to
the same-numbered array, one pir per callback, and a start without video
encoders.

They are a module of obs-tests-core, so no new test binary
(ci/test_binary_cap.py: 8 of 8).

soldr cargo test -p obs-data --test obs-tests-core output::interleave::
fails 53 of 53 with "not yet implemented".

Refs obs-rust#1594

* libobs: Port the output interleaver to obs-output

libobs/obs-output-interleave.c.h moves to obs_output::interleave. It is
the encoded packet callback of an output with video and audio:
interleave_packets queues the encoders' packets, starts every track
together at timestamp 0 and sends the packets in dts order, with their
encoder timing, captions and the encoder group keyframe check.
interleave_packets, get_encoder_index and free_packets are declared in
obs-internal.h without EXPORT; obs-output.c calls them, and passes
interleave_packets to the encoders by name, so the Rust symbol replaces
the C one the encoder later compares against.

The pure parts work on a slice of packets (PacketView): the first and
last packet of a track, the start index, the premature prune, the
insertion position, the offset arithmetic, the streamable count and the
catch-up rule, the discard count, the caption timing and the packet-time
lookup. The functions named after the C ones are generic over the output
(InterleaveOutput), as the delay core is over DelayOutput, and take
interleaved_mutex, pkt_callbacks_mutex and the caption mutex exactly
where the C does. The nine behaviours test_output_interleave marks
"characterized, not endorsed" are kept, among them the late-audio
keyframe drop, the last-track prune difference, the truncating encoder
duration, the track-0-only start index, the non-keyframe start, the
waiting non-keyframe discard, priming audio at offset 0 and the early
return that does not release a delayed packet.

The shim never reads struct obs_output or struct obs_encoder: it goes
through the hidden accessors and wrappers of obs-rust#1599, and reads and writes
each field where the C does. interleaved_packets, encoder_packet_times[]
and keyframe_group_tracking stay the C darrays obs-output.c resets and
frees; they are changed in place with obs_util::ffi::darray (obs-rust#1596,
obs-rust#1600). Queued packets are libobs's reference-counted copies or the
delay's. The clock is read once per packet callback, as in C; the
*_with_clock entry point and the statics module (the C file's static
functions on a live output) are Rust-mangled and are what the parity
test drives.

The file leaves libobs.core-output for its own group,
libobs.ported-output-interleave, whose PCH is obs-internal.h alone, so
the oracle and test_output_interleave compile it without the core. The
three symbols go in rust-hidden.txt, rust-exports.map and
rust-unexports-macos.txt. The exported symbol sets are unchanged.

Tier 3 (oracle/output_interleave.c) compares the layouts of the
keyframe group entry, the packet callback, the caption text, the darray
and the three accessor structs, the values the file uses, thirteen
characterization scenarios, and 1024 random runs of up to 80 steps over
one to two video and audio encoders: packets with random tracks,
keyframes, running timestamps and packet times, changes of active, the
delay, the batch limit, the captions, add_caption, the callbacks and the
encoder group, restarts, get_encoder_index, and the static functions
where their C preconditions hold. After every step it compares the
return value, every recorded call and whether each lock was held, the
log line, the held copies, the caller's packet, every queued packet,
packet time and group entry, the darray capacities and every
interleaver field. Both sides go through the same accessor and libobs
stand-ins. The oracle builds the file with NDEBUG, as libobs is built:
darray_pop_front asserts when a video packet without a packet time is
discarded, then does nothing, as the Rust darray does. No intentional
differences. test_output_interleave compiles the C into itself and is
unchanged.

Fixes obs-rust#1594
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants