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

afxres / binary / 13345991469

15 Feb 2025 02:36PM UTC coverage: 99.941% (-0.04%) from 99.98%
13345991469

push

github

afxres
Fix named object decoder with duplicate keys

25 of 26 new or added lines in 2 files covered. (96.15%)

1 existing line in 1 file now uncovered.

5123 of 5126 relevant lines covered (99.94%)

303120.95 hits per line

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

96.55
/code/Binary/External/BinaryObject.cs
1
namespace Mikodev.Binary.External;
2

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

12
internal static class BinaryObject
13
{
14
    internal static ByteViewList? Create(ImmutableArray<ReadOnlyMemory<byte>> items, out int error)
15
    {
546✔
16
        Debug.Assert(items.Any());
546✔
17
        if (items.Length <= BinaryDefine.LongDataListItemCountLimits && items.All(x => x.Length <= BinaryDefine.LongDataListItemBytesLimits))
1,700✔
18
            return CreateLongDataList(items, out error);
524✔
19
        else
20
            return CreateHashCodeList(items, out error);
22✔
21
    }
546✔
22

23
    private static LongDataList? CreateLongDataList(ImmutableArray<ReadOnlyMemory<byte>> items, out int error)
24
    {
550✔
25
        error = -1;
550✔
26
        if (items.Length > BinaryDefine.LongDataListItemCountLimits)
550✔
UNCOV
27
            return null;
×
28
        var records = new List<LongDataSlot>();
550✔
29
        for (var i = 0; i < items.Length; i++)
3,488✔
30
        {
1,202✔
31
            var span = items[i].Span;
1,202✔
32
            if (span.Length > BinaryDefine.LongDataListItemBytesLimits)
1,202✔
NEW
33
                return null;
×
34
            var slot = BinaryModule.GetLongData(ref MemoryMarshal.GetReference(span), span.Length);
1,202✔
35
            if (records.Any(x => x.Head == slot.Head && x.Tail == slot.Tail))
2,328✔
36
            {
8✔
37
                error = i;
8✔
38
                return null;
8✔
39
            }
40
            records.Add(slot);
1,194✔
41
        }
1,194✔
42
        return new LongDataList([.. records]);
542✔
43
    }
550✔
44

45
    private static HashCodeList? CreateHashCodeList(ImmutableArray<ReadOnlyMemory<byte>> items, out int error)
46
    {
42✔
47
        error = -1;
42✔
48
        var records = new HashCodeSlot[items.Length];
42✔
49
        var buckets = new int[DetectHashCodeListBucketLength(records.Length)];
42✔
50
        Array.Fill(buckets, -1);
42✔
51

52
        for (var i = 0; i < items.Length; i++)
91,896✔
53
        {
45,912✔
54
            var buffer = items[i].ToArray();
45,912✔
55
            var length = buffer.Length;
45,912✔
56
            ref var source = ref MemoryMarshal.GetArrayDataReference(buffer);
45,912✔
57
            var hash = BinaryModule.GetHashCode(ref source, length);
45,912✔
58
            ref var next = ref buckets[(int)(hash % (uint)buckets.Length)];
45,912✔
59
            while ((uint)next < (uint)records.Length)
66,742✔
60
            {
20,836✔
61
                ref var slot = ref records[next];
20,836✔
62
                if (hash == slot.Hash && BinaryModule.GetEquality(ref source, length, slot.Head))
20,836✔
63
                {
6✔
64
                    error = i;
6✔
65
                    return null;
6✔
66
                }
67
                next = ref slot.Next;
20,830✔
68
            }
20,830✔
69
            records[i] = new HashCodeSlot { Head = buffer, Hash = hash, Next = -1 };
45,906✔
70
            next = i;
45,906✔
71
        }
45,906✔
72

73
        Debug.Assert(records.Select(x => x.Next).All(next => next is -1 || (uint)next < (uint)records.Length));
91,820✔
74
        return new HashCodeList(buckets, records);
36✔
75
    }
42✔
76

77
    private static int DetectHashCodeListBucketLength(int capacity)
78
    {
196✔
79
        var result = BinaryDefine.HashCodeListPrimes.FirstOrDefault(x => x >= capacity);
5,868✔
80
        if (result is 0)
196✔
81
            ThrowHelper.ThrowMaxCapacityOverflow();
2✔
82
        return result;
194✔
83
    }
194✔
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