// Copyright 2025 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // This file defines several datatypes used for testing our parser/deparser // implementation. For each, we will validate that it corresponds to the // expected MojomType and packs to the right MojomWireType, and that values of // that type can be correctly converted to/from MojomValues. // The corresponding tests can be found in //mojo/public/rust/mojom_parser/test module parser_unittests.mojom; struct Empty {}; // Packing: a [pad 8] b c d struct FourInts { int8 a; int16 b; int32 c; int64 d; }; // Packing: d c b a [pad 8] struct FourIntsReversed { int64 d; int32 c; int16 b; int8 a; }; // Packing: a d c b struct FourIntsIntermixed { int8 a; int32 b; int16 c; int8 d; }; // Packing: f1 a b [pad 8] c f2 f3 struct OnceNested { FourInts f1; uint32 a; int8 b; FourIntsReversed f2; FourIntsIntermixed f3; uint16 c; }; // Packing: o a b [pad 16] f c [pad 32] struct TwiceNested { OnceNested o; int16 a; FourInts f; int32 b; int32 c; }; // Test that bools get packed into bytes as expected // Packing: [b0..b7] n1 [b8,b9,0..] struct TenBoolsAndAByte { bool b0; bool b1; bool b2; bool b3; bool b4; uint8 n1; bool b5; bool b6; bool b7; bool b8; bool b9; }; // Packing: [b0..b7] [b8,b9,0..] n1 struct TenBoolsAndTwoBytes { bool b0; bool b1; bool b2; bool b3; bool b4; uint16 n1; bool b5; bool b6; bool b7; bool b8; bool b9; }; enum TestEnum { Zero, Three = 3, Four, Seven = 7, }; enum TestEnum2 { Eleven = 11, Twelve, Four = 4, FourtyTwo = 42 }; // Packing: e1 e2 n1 struct SomeEnums { TestEnum e1; uint64 n1; TestEnum2 e2; }; enum TestEnumWithNegativeDiscriminants { NegVal = -1, PosVal = 1, ZeroVal = 0, }; union BaseUnion { int8 n1; uint64 u1; TestEnum e1; bool b1; bool b2; Empty em1; FourInts f1; float fl; }; union NestedUnion { int32 n; BaseUnion u; // Represented as a pointer }; // Packing: n1 u n2 [pad 32] struct WithNestedUnion { int64 n1; NestedUnion u; // Represented as itself int32 n2; }; union NestederUnion { bool b; int8 n; NestedUnion u; WithNestedUnion w; }; // This is extra complicated to make sure nested union pointers work from // various positions in the struct // Packing: u1 i1 i2 [pad 24] u2 d1 u3 u4 i3 struct WithManyUnions { NestedUnion u1; int8 i1; NestederUnion u2; double d1; BaseUnion u3; NestederUnion u4; int32 i2; int32 i3; }; struct Arrays { array ints; array ints_sized; array bools; // Will be packed as bitfields array bool_sized; // Will still be packed as bitfields array floats; array enums; array unions; array unions_nested; array fourints; array> nested; array, 3> nested_sized; }; struct Maps { map eights; map bools; map enums; map to_struct; map to_union; map> to_map; map float_map; }; struct Strings { string str; array arr; map to_str; map from_str; }; // Now that we have arrays, maps, and strings, we should // make sure a union can hold all of them properly. union HoldsComplexTypes { string str; array arr; map m; }; struct ComplexUnionHolder { HoldsComplexTypes u; }; struct StructWithNestedEnum { enum Type { ODD = 1, EVEN = 2, }; Type even_or_odd = Type.ODD; }; ////// TESTS FOR NULLABLES ////// // Packing: [b, nullable tags] n2 n1 e empty fourints f1 [pad 32] f2 struct NullableBasics { bool? b; uint16? n1; int8? n2; Empty? empty; TestEnum? e; FourInts? fourints; float? f1; double? f2; }; struct ArraysOfNullables { array bools; array empties; array enums; array unions; }; struct NullableArrays { array? null_arr; array? double_null_arr; }; union UnionWithNullables { // Nullable primitives aren't allowed in unions Empty? e; string? str; BaseUnion? u; }; struct NullableOthers { UnionWithNullables? u; map? m; string? str; }; ////// TESTS FOR HANDLES ////// // Handle tests are separated out from the other types because we can't compare // handles for equality (at least, not usefully), so we use a different testing // function for these that runs more relaxed checks. struct Handles { handle h1; handle? h2; handle h3; handle? h4; }; union WithHandles { handle? h1; handle h2; uint8 n; }; // Packing: h1 h2 arr wh h3 struct NestedHandles { handle h1; array arr; handle h2; WithHandles wh; handle h3; }; interface DummyInterface {}; struct WithPendingTypes { pending_receiver rec; pending_remote rem; pending_receiver rec2; pending_remote rem2; }; struct TypemappedStruct { int32 a; }; struct WithPendingAssociatedTypes { pending_associated_receiver? rec; pending_associated_remote? rem; pending_associated_receiver? rec2; pending_associated_remote? rem2; };