~bigbes/tarantool

tarantool-protobuf

ref: 53840866a6ef3349d42bf589a6646aaad6869386 tarantool-protobuf/test/c_runtime_encode_test.lua -rw-r--r-- 22.9 KiB
53840866 — Eugene Blikh codegen: emit <Msg>_decode_unsafe for trusted-source decoding (6bb) 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
-- Test for bd-y1n / ra6 3b: C-side scalar encode.
--
-- Only runs when PB_ENABLE_C=1 is set in the environment AND the C
-- runtime module is loadable. Otherwise the group is skipped, which
-- keeps `just test` green on hosts without the C module built.
--
-- Acceptance per bd-y1n:
--   Person encode for {name='x', age=42, balance=-7, user_id=...,
--   weight_kg=3.14} byte-equal to mode=full pure-Lua output.

local t = require('luatest')
local ffi = require('ffi')

local pb = require('pb')
local c_runtime = pb.c_runtime

local function skip_if_no_c()
    if c_runtime == nil then
        t.skip('PB_ENABLE_C not set or pb.c_runtime not available')
    end
end

-- The mode=full reference is the byte-equality target; the test runs
-- against both codegen modes' descriptors (the C plan is mode-agnostic).
local full_hello

for _, mode in ipairs({'full', 'runtime'}) do
    local g = t.group('c_runtime_encode.' .. mode)
    local hello

    g.before_all(function()
        skip_if_no_c()
        hello = require(mode .. '.hello.hello_pb')
        full_hello = require('full.hello.hello_pb')
    end)

    g.before_each(skip_if_no_c)

    -- ---------- Acceptance per bd-y1n ----------

    function g.test_acceptance_person_scalar_subset()
        local msg = {
            name = 'x',
            age = 42,
            balance = -7,
            user_id = 0xDEADBEEFCAFEBABEULL,
            weight_kg = 3.14,
        }
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local c_bytes = c_runtime.encode(plan, msg)
        local lua_bytes = full_hello.Person_encode(msg)
        t.assert_equals(c_bytes, lua_bytes,
            'C encode matches mode=full pure-Lua encode byte-for-byte')
    end

    -- ---------- Per-kind coverage ----------

    function g.test_empty_message_produces_empty_string()
        local plan = c_runtime.compile_plan(hello.Address_descriptor)
        t.assert_equals(c_runtime.encode(plan, {}), '')
    end

    function g.test_address_strings_and_int32()
        local plan = c_runtime.compile_plan(hello.Address_descriptor)
        local msg = {street = 'Main', city = 'Springfield', zip = 12345}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Address_encode(msg))
    end

    function g.test_proto3_optional_emits_empty_string()
        -- Address.apartment is proto3-optional; presence beats default.
        local plan = c_runtime.compile_plan(hello.Address_descriptor)
        local msg = {apartment = ''}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Address_encode(msg))
    end

    function g.test_double_negative_zero_emits()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        -- +0.0 -> skip; -0.0 -> emit (sign bit preserved by wire spec).
        local m_pos = {weight_kg = 0.0}
        local m_neg = {weight_kg = -0.0}
        t.assert_equals(c_runtime.encode(plan, m_pos),
                        full_hello.Person_encode(m_pos))
        t.assert_equals(c_runtime.encode(plan, m_neg),
                        full_hello.Person_encode(m_neg))
    end

    function g.test_enum_as_number()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {status = 2}  -- ERROR
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_enum_as_string_lookup()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {status = 'OK'}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_enum_zero_value_suppressed()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {status = 'UNKNOWN'}  -- = 0; default-suppressed
        t.assert_equals(c_runtime.encode(plan, msg), '')
    end

    function g.test_enum_unknown_string_errors()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        t.assert_error_msg_contains("unknown enum value 'NOPE'", function()
            c_runtime.encode(plan, {status = 'NOPE'})
        end)
    end

    function g.test_fixed64_cdata_round_trip()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {user_id = ffi.new('uint64_t', 0x123456789ABCDEF0)}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_sint32_negative_and_zero()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        t.assert_equals(c_runtime.encode(plan, {balance = 0}), '')
        t.assert_equals(
            c_runtime.encode(plan, {balance = -1}),
            full_hello.Person_encode({balance = -1}))
        t.assert_equals(
            c_runtime.encode(plan, {balance = 0x7fffffff}),
            full_hello.Person_encode({balance = 0x7fffffff}))
    end

    function g.test_int32_zero_suppressed()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        t.assert_equals(c_runtime.encode(plan, {age = 0}), '')
    end

    function g.test_bytes_field()
        -- Person.avatar is bytes @8
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {avatar = '\x00\x01\xff\xfe'}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    -- ---------- Map encode (bd-asz / ra6 3h) ----------

    function g.test_map_string_to_int32_single_key()
        -- Single-key fixtures lock down byte-for-byte equality. Multi-key
        -- map encode order is hash-determined per Lua's pairs() and won't
        -- match mode=full's pairs() order in general, so we exercise
        -- multi-key behavior via round-trip below.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {ages_by_nickname = {alice = 30}}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_map_int32_to_string_single_key()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {nickname_by_age = {[30] = 'alice'}}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_map_string_to_message_single_key()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {addresses_by_label = {
            home = {street = 'Main', city = 'X', zip = 1},
        }}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_map_empty_emits_nothing()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        t.assert_equals(c_runtime.encode(plan, {ages_by_nickname = {}}), '')
    end

    function g.test_map_default_key_and_value_round_trip()
        -- Empty-string key + zero value: per proto3 wire spec both bytes
        -- are elided but the entry itself is still emitted (presence of
        -- the key/value pair is meaningful even when both default).
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {ages_by_nickname = {[''] = 0}}
        local c_bytes = c_runtime.encode(plan, msg)
        t.assert_equals(c_bytes, full_hello.Person_encode(msg))
        local dec = full_hello.Person_decode(c_bytes)
        t.assert_equals(dec.ages_by_nickname[''], 0)
    end

    function g.test_map_multi_key_round_trip()
        -- Multi-key encode byte order is hash-determined; we only assert
        -- that mode=full can decode our bytes back to the same table.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {ages_by_nickname = {alice = 30, bob = 25, carol = 40}}
        local dec = full_hello.Person_decode(c_runtime.encode(plan, msg))
        t.assert_equals(dec.ages_by_nickname, msg.ages_by_nickname)
    end

    function g.test_map_message_value_with_other_fields()
        -- Ensure map<,message> sub-plan resolution doesn't disturb the
        -- enclosing message's field-walk.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {
            name = 'x',
            addresses_by_label = {home = {street = 'Main', zip = 7}},
            emails = {'a@b'},
        }
        local dec = full_hello.Person_decode(c_runtime.encode(plan, msg))
        t.assert_equals(dec.name, 'x')
        t.assert_equals(dec.emails, {'a@b'})
        t.assert_equals(dec.addresses_by_label.home.street, 'Main')
        t.assert_equals(dec.addresses_by_label.home.zip, 7)
    end

    -- ---------- Sub-message encode (bd-hwe / ra6 3d) ----------

    function g.test_singular_submessage_round_trip()
        -- Person.address (one-level singular sub-message) round-trip.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {
            name = 'x',
            address = {street = 'Main', city = 'Springfield', zip = 100},
        }
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_empty_submessage_emits_tag_with_zero_length()
        -- Singular sub-message with an empty table — proto3 presence
        -- semantics require emitting tag + length(0). The Lua codec
        -- does this; the C codec must match.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {name = 'x', address = {}}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_submessage_with_proto3_optional_field()
        -- Address.apartment is proto3-optional. Encode through the
        -- sub-message must surface the empty string just like the
        -- top-level Address codec does.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {address = {apartment = ''}}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_submessage_non_table_value_errors()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        t.assert_error_msg_contains('table', function()
            c_runtime.encode(plan, {address = 'not a table'})
        end)
    end

    function g.test_nested_5_levels()
        -- 5-level chain of singular sub-messages. Exercises the
        -- recursion depth and the cycle-breaking c_plan stash.
        local cn = require(mode .. '.c_nested.c_nested_pb')
        local full_cn = require('full.c_nested.c_nested_pb')
        local msg = {
            v = 1,
            next = {v = 2, next = {v = 3, next = {v = 4,
                next = {v = 5}}}},
        }
        local plan = c_runtime.compile_plan(cn.L1_descriptor)
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_cn.L1_encode(msg))
    end

    function g.test_large_submessage_triggers_parent_grow()
        -- Parent buffer starts at 4KB stack-backed; force a parent
        -- regrow path during the post-recursion ebuf_reserve by
        -- packing a >4KB string into the sub-message.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {
            name = 'x',
            address = {street = string.rep('s', 8192)},
        }
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_long_string_grows_buffer()
        -- Stack buffer is 4KB; force the heap-promotion path with a
        -- string that pushes past it.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {name = string.rep('a', 8192)}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_wkt_override_passthrough()
        -- bd-rmf / ra6 3k: has_override plans now dispatch to
        -- desc.encode(value) and return its bytes verbatim.
        local plan = c_runtime.compile_plan(pb.wkt.Timestamp_descriptor)
        local v = {seconds = 1, nanos = 2}
        t.assert_equals(c_runtime.encode(plan, v),
                        pb.wkt.Timestamp_encode(v))
    end

    -- ---------- Repeated + packed (bd-jc9 / ra6 3e) ----------

    -- Acceptance per bd-jc9: Person.lucky_numbers (packed int32) round-
    -- trips byte-equal, and a fixture mixing packed + unpacked at 10/
    -- 100/1000 elements matches mode=full byte-for-byte.

    function g.test_acceptance_lucky_numbers_packed_int32()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {lucky_numbers = {1, 2, 3, 4, 5, -1, 0x7fffffff}}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_repeated_empty_array_omits_field()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        -- Empty repeated must be wire-equivalent to absent.
        t.assert_equals(c_runtime.encode(plan, {lucky_numbers = {}}), '')
        t.assert_equals(c_runtime.encode(plan, {emails = {}}), '')
    end

    function g.test_repeated_emits_zero_elements_no_suppression()
        -- Unlike singular scalars, repeated elements are NOT zero-
        -- suppressed — every element reaches the wire.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {lucky_numbers = {0, 0, 0}}
        local bytes = c_runtime.encode(plan, msg)
        t.assert_equals(bytes, full_hello.Person_encode(msg))
        t.assert(#bytes > 0, 'zero elements still emit')
    end

    function g.test_repeated_string_per_element_tag()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {emails = {'a@b', 'c@d', '', 'long' .. string.rep('x', 200)}}
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_repeated_message_self_reference()
        -- Person.friends is `repeated Person` — a self-referencing
        -- sub-message exercising recursive plan dispatch via the
        -- c_plan stash for cycle-breaking.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local msg = {
            name = 'root',
            friends = {
                {name = 'alice', age = 30},
                {name = 'bob', friends = {{name = 'carol'}}},
                {},  -- empty friend → tag + len(0) per proto3 presence
            },
        }
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_hello.Person_encode(msg))
    end

    function g.test_repeated_non_table_value_errors()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        t.assert_error_msg_contains('repeated', function()
            c_runtime.encode(plan, {lucky_numbers = 'not an array'})
        end)
    end

    -- ---------- c_repeated fixture: every dispatch branch ----------

    local function counts() return {10, 100, 1000} end

    function g.test_fixture_packed_int32_at_counts()
        local cr = require(mode .. '.c_repeated.c_repeated_pb')
        local full_cr = require('full.c_repeated.c_repeated_pb')
        local plan = c_runtime.compile_plan(cr.Holder_descriptor)
        for _, n in ipairs(counts()) do
            local arr = {}
            for i = 1, n do arr[i] = i end
            local msg = {packed_int32 = arr}
            t.assert_equals(c_runtime.encode(plan, msg),
                            full_cr.Holder_encode(msg),
                            ('packed_int32 n=%d'):format(n))
        end
    end

    function g.test_fixture_packed_all_numeric_kinds()
        local cr = require(mode .. '.c_repeated.c_repeated_pb')
        local full_cr = require('full.c_repeated.c_repeated_pb')
        local plan = c_runtime.compile_plan(cr.Holder_descriptor)
        local msg = {
            packed_int32   = {1, -1, 0x7fffffff},
            packed_int64   = {ffi.new('int64_t', 1), ffi.new('int64_t', -1)},
            packed_sint32  = {-3, 0, 3},
            packed_uint32  = {7, 8, 9},
            packed_fixed32 = {100, 200},
            packed_fixed64 = {ffi.new('uint64_t', 0x1234567890ABCDEFULL)},
            packed_double  = {1.5, -2.25, 0},
            packed_float   = {0.5, -0.25},
            packed_bool    = {true, false, true, true, false},
        }
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_cr.Holder_encode(msg))
    end

    function g.test_fixture_unpacked_scalars_at_counts()
        local cr = require(mode .. '.c_repeated.c_repeated_pb')
        local full_cr = require('full.c_repeated.c_repeated_pb')
        local plan = c_runtime.compile_plan(cr.Holder_descriptor)
        for _, n in ipairs(counts()) do
            local i32, sx, fx = {}, {}, {}
            for i = 1, n do
                i32[i] = i
                sx[i] = -i
                fx[i] = ffi.new('uint64_t', i)
            end
            local msg = {
                unpacked_int32 = i32,
                unpacked_sint32 = sx,
                unpacked_fixed64 = fx,
            }
            t.assert_equals(c_runtime.encode(plan, msg),
                            full_cr.Holder_encode(msg),
                            ('unpacked scalars n=%d'):format(n))
        end
    end

    function g.test_fixture_repeated_strings_and_bytes()
        local cr = require(mode .. '.c_repeated.c_repeated_pb')
        local full_cr = require('full.c_repeated.c_repeated_pb')
        local plan = c_runtime.compile_plan(cr.Holder_descriptor)
        for _, n in ipairs(counts()) do
            local s, b = {}, {}
            for i = 1, n do
                s[i] = 'str' .. tostring(i)
                b[i] = string.char(i % 256) .. '\0\xff'
            end
            local msg = {strings = s, blobs = b}
            t.assert_equals(c_runtime.encode(plan, msg),
                            full_cr.Holder_encode(msg),
                            ('strings/blobs n=%d'):format(n))
        end
    end

    function g.test_fixture_repeated_messages_at_counts()
        local cr = require(mode .. '.c_repeated.c_repeated_pb')
        local full_cr = require('full.c_repeated.c_repeated_pb')
        local plan = c_runtime.compile_plan(cr.Holder_descriptor)
        for _, n in ipairs(counts()) do
            local arr = {}
            for i = 1, n do arr[i] = {v = i, s = 'name' .. i} end
            local msg = {messages = arr}
            t.assert_equals(c_runtime.encode(plan, msg),
                            full_cr.Holder_encode(msg),
                            ('messages n=%d'):format(n))
        end
    end

    -- ---------- Acceptance per bd-exy / ra6 3f ----------
    --
    -- Person.emails (repeated string) and Person.friends (repeated
    -- Person, self-reference) round-trip byte-equal to mode=full at
    -- 1KB, 10KB, 100KB wire sizes. The repeated dispatch landed
    -- with 3e (encode_repeated_field handles MESSAGE + string/bytes
    -- branches alongside the scalars); this acceptance pins it under
    -- the cached-stack-idx pattern at scale.
    --
    -- Sizing chosen to hit the named targets without per-test math
    -- in the loop:
    --   emails  @  50 elements ≈   1KB,  ≈ 10KB,  ≈ 100KB
    --   friends @  60 elements ≈   1KB,  ≈ 10KB,  ≈ 100KB
    --
    -- Bytes are checked at runtime against the named target band so
    -- a future schema or encoding shift surfaces as a test failure
    -- rather than silently moving off-target.

    local function size_band(actual, target)
        -- Accept anything within 30% of the named size — generous
        -- enough to survive small wire-format shifts, tight enough
        -- to flag a regression to the wrong order of magnitude.
        return actual >= target * 0.7 and actual <= target * 1.3
    end

    function g.test_acceptance_repeated_strings_at_size_targets()
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local cases = {
            {label = '1KB',   n =   50, elem_len = 18, target = 1024},
            {label = '10KB',  n =  500, elem_len = 18, target = 10240},
            {label = '100KB', n = 5000, elem_len = 18, target = 102400},
        }
        for _, c in ipairs(cases) do
            local emails = {}
            for i = 1, c.n do
                emails[i] = string.rep('e', c.elem_len - 2)
                          .. string.format('%02d', i % 100)
            end
            local msg = {emails = emails}
            local c_bytes = c_runtime.encode(plan, msg)
            local lua_bytes = full_hello.Person_encode(msg)
            t.assert_equals(c_bytes, lua_bytes,
                ('emails %s byte-equal'):format(c.label))
            t.assert(size_band(#c_bytes, c.target),
                ('emails %s actual=%dB target=%dB'):format(
                    c.label, #c_bytes, c.target))
        end
    end

    function g.test_acceptance_repeated_messages_self_ref_at_size_targets()
        -- Friend payload shape: {name = '<12-char>', age = N}. Each
        -- emitted friend is around 18 bytes: parent tag (1) + body
        -- length varint (1) + inner name tag (1) + name len (1) +
        -- 12 chars + age tag (1) + age varint (1-2). 56 → ~1KB,
        -- 560 → ~10KB, 5600 → ~100KB.
        local plan = c_runtime.compile_plan(hello.Person_descriptor)
        local cases = {
            {label = '1KB',   n =   56, target = 1024},
            {label = '10KB',  n =  560, target = 10240},
            {label = '100KB', n = 5600, target = 102400},
        }
        local pad = 'xxxxxxx'  -- 7 chars; combined with 'fNNNN' → 12
        for _, c in ipairs(cases) do
            local friends = {}
            for i = 1, c.n do
                friends[i] = {
                    name = pad .. string.format('f%04d', i),
                    age = i,
                }
            end
            local msg = {friends = friends}
            local c_bytes = c_runtime.encode(plan, msg)
            local lua_bytes = full_hello.Person_encode(msg)
            t.assert_equals(c_bytes, lua_bytes,
                ('friends %s byte-equal'):format(c.label))
            t.assert(size_band(#c_bytes, c.target),
                ('friends %s actual=%dB target=%dB'):format(
                    c.label, #c_bytes, c.target))
        end
    end

    function g.test_fixture_mixed_packed_and_unpacked()
        -- All branches simultaneously: packed + unpacked + string/bytes
        -- + message, on the same message instance.
        local cr = require(mode .. '.c_repeated.c_repeated_pb')
        local full_cr = require('full.c_repeated.c_repeated_pb')
        local plan = c_runtime.compile_plan(cr.Holder_descriptor)
        local p, u, s, m = {}, {}, {}, {}
        for i = 1, 100 do
            p[i] = i
            u[i] = -i
            s[i] = 'k' .. i
            m[i] = {v = i, s = 's' .. i}
        end
        local msg = {
            packed_int32 = p,
            unpacked_int32 = u,
            strings = s,
            messages = m,
        }
        t.assert_equals(c_runtime.encode(plan, msg),
                        full_cr.Holder_encode(msg))
    end
end