• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

afxres / binary / 13353308356

16 Feb 2025 08:29AM UTC coverage: 99.961% (-0.02%) from 99.98%
13353308356

push

github

afxres
Fix internal exception thrown

7 of 8 new or added lines in 2 files covered. (87.5%)

1 existing line in 1 file now uncovered.

5118 of 5120 relevant lines covered (99.96%)

303471.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.96
/code/Binary/Internal.Contexts.Decoders/NamedObjectDecoder.cs
1
namespace Mikodev.Binary.Internal.Contexts.Decoders;
2

3
using Mikodev.Binary.External;
4
using Mikodev.Binary.Internal;
5
using System;
6
using System.Collections.Immutable;
7
using System.Diagnostics;
8
using System.Diagnostics.CodeAnalysis;
9
using System.Runtime.CompilerServices;
10
using System.Runtime.InteropServices;
11

12
internal sealed class NamedObjectDecoder
13
{
14
    private readonly Type type;
15

16
    private readonly ByteViewList view;
17

18
    private readonly bool[] optional;
19

20
    private readonly string[] names;
21

22
    public int Length => this.optional.Length;
404,340✔
23

24
    public NamedObjectDecoder(ImmutableArray<ImmutableArray<byte>> headers, ImmutableArray<string> names, ImmutableArray<bool> optional, Type type)
556✔
25
    {
556✔
26
        if (headers.Length is 0 || names.Length is 0 || optional.Length is 0)
556✔
27
            throw new ArgumentException($"Sequence is null or empty.");
12✔
28
        if (headers.Length != names.Length || headers.Length != optional.Length)
544✔
29
            throw new ArgumentException($"Sequence lengths not match.");
6✔
30
        this.type = type;
538✔
31
        this.names = [.. names];
538✔
32
        var view = BinaryObject.Create(headers, out var error);
538✔
33
        Debug.Assert(view is null || error is -1);
538✔
34
        if (view is null)
538✔
35
            ExceptKeyFound(error);
2✔
36
        this.view = view;
536✔
37
        this.optional = [.. optional];
536✔
38
    }
536✔
39

40
    [DebuggerStepThrough, DoesNotReturn]
41
    private void ExceptKeyFound(int cursor)
42
    {
4✔
43
        throw new ArgumentException($"Named key '{this.names[cursor]}' already exists, type: {this.type}");
4✔
44
    }
45

46
    [DebuggerStepThrough, DoesNotReturn]
47
    private void ExceptNotFound(int cursor)
48
    {
8✔
49
        throw new ArgumentException($"Named key '{this.names[cursor]}' does not exist, type: {this.type}");
8✔
50
    }
51

52
    public void Invoke(ReadOnlySpan<byte> span, Span<long> slices)
53
    {
404,340✔
54
        var optional = this.optional;
404,340✔
55
        var record = this.view;
404,340✔
56
        ref var source = ref MemoryMarshal.GetReference(span);
404,340✔
57

58
        var limits = span.Length;
404,340✔
59
        var offset = 0;
404,340✔
60
        var length = 0;
404,340✔
61
        while (limits - offset != length)
1,213,206✔
62
        {
808,878✔
63
            offset += length;
808,878✔
64
            length = NumberModule.DecodeEnsureBuffer(ref source, ref offset, limits);
808,878✔
65
            var cursor = record.Invoke(ref Unsafe.Add(ref source, offset), length);
808,874✔
66
            Debug.Assert(cursor is -1 || (uint)cursor < (uint)slices.Length);
808,874✔
67
            offset += length;
808,874✔
68
            length = NumberModule.DecodeEnsureBuffer(ref source, ref offset, limits);
808,874✔
69
            if ((uint)cursor >= (uint)slices.Length)
808,868✔
70
                continue;
6✔
71
            ref var handle = ref slices[cursor];
808,862✔
72
            if (handle is not 0)
808,862✔
73
                ExceptKeyFound(cursor);
2✔
74
            handle = (long)((ulong)(uint)offset << 32 | (uint)length);
808,860✔
75
        }
808,860✔
76

77
        for (var i = 0; i < slices.Length; i++)
2,426,412✔
78
        {
808,886✔
79
            if (slices[i] is not 0 || optional[i])
808,886✔
80
                continue;
808,878✔
81
            ExceptNotFound(i);
8✔
UNCOV
82
        }
×
83
    }
404,320✔
84
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc