// Generated from executable documentation examples. Edit the source region, then regenerate. using System; using System.IO; using System.IO.Pipelines; using System.Linq; using System.Buffers; using System.Collections.Generic; using System.Dynamic; using System.Globalization; using System.Numerics; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using CStructSharp; using CStructSharp.Codecs; using CStructSharp.Diagnostics; using CStructSharp.Introspection; using CStructSharp.Values; internal static partial class Program { public static void Main() { Integers24(); Console.WriteLine("PASS integers-24"); } private static void Integers24() { var layout = new CStruct("struct root { uint24< size; int24< delta; uint8 tail; };", aligned: true); byte[] bytes = layout.Serialize("root", new Dictionary { ["size"] = 16777215U, ["delta"] = -2, ["tail"] = 99 }); SequenceEqual([255, 255, 255, 254, 255, 255, 99], bytes); Equal(7, layout.GetStructSizeInBytes("root")); Equal(-2, layout.ReadValue(bytes.AsSpan(), "root.delta")); using var stream = new MemoryStream(bytes); Equal(3L, layout.ResolveAddress(stream, "root.delta")); Throws(() => layout.Update(stream, "root.size", 16777216U)); SequenceEqual(bytes, stream.ToArray()); } private static void Equal(T expected, T actual) { if (!EqualityComparer.Default.Equals(expected, actual)) { throw new InvalidOperationException($"Expected '{expected}', received '{actual}'."); } } private static void SequenceEqual(byte[] expected, byte[] actual) { if (!expected.AsSpan().SequenceEqual(actual)) { throw new InvalidOperationException( $"Expected {Convert.ToHexString(expected)}, received {Convert.ToHexString(actual)}."); } } private static void Throws(Action action) where TException : Exception { try { action(); } catch (TException) { return; } throw new InvalidOperationException($"Expected {typeof(TException).Name}."); } }