// 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() { FixedPoint(); Console.WriteLine("PASS fixed-point"); } private static void FixedPoint() { var layout = new CStruct("struct root { fixed16_16> revision; ufixed8_8< volume; };", aligned: false); byte[] bytes = layout.Serialize("root", new Dictionary { ["revision"] = -1.5, ["volume"] = 0.5 }); SequenceEqual([255, 254, 128, 0, 128, 0], bytes); Equal(-1.5, layout.ReadValue(bytes.AsSpan(), "root.revision")); Equal(0.5, layout.ReadValue(bytes.AsSpan(), "root.volume")); using var stream = new MemoryStream(bytes); Throws(() => layout.Update(stream, "root.volume", 0.1)); 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}."); } }