< Summary

Information
Line coverage
0%
Covered lines: 0
Uncovered lines: 24
Coverable lines: 24
Total lines: 66
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Enqueue(...)0%660%
TryDequeue(...)0%440%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\ValueQueue.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Collections.Generic;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Runtime.InteropServices;
 8
 9namespace System.Text.Json
 10{
 11    /// <summary>
 12    /// A struct variant of <see cref="Queue{T}"/> that only allocates for Counts > 1.
 13    /// </summary>
 14    [StructLayout(LayoutKind.Auto)]
 15    internal struct ValueQueue<T>
 16    {
 17        private byte _state; // 0 = empty, 1 = single, 2 = multiple
 18        private T? _single;
 19        private Queue<T>? _multiple;
 20
 21        public readonly int Count => _state < 2 ? _state : _multiple!.Count;
 22
 23        public void Enqueue(T value)
 024        {
 025            switch (_state)
 26            {
 27                case 0:
 028                    _single = value;
 029                    _state = 1;
 030                    break;
 31
 32                case 1:
 33                    // Once a queue gets allocated the struct will always remain in the multiple state.
 034                    (_multiple ??= new()).Enqueue(_single!);
 035                    _single = default;
 036                    _state = 2;
 037                    goto default;
 38
 39                default:
 040                    Debug.Assert(_multiple != null);
 041                    _multiple.Enqueue(value);
 042                    break;
 43            }
 044        }
 45
 46        public bool TryDequeue([MaybeNullWhen(false)] out T? value)
 047        {
 048            switch (_state)
 49            {
 50                case 0:
 051                    value = default;
 052                    return false;
 53
 54                case 1:
 055                    value = _single;
 056                    _single = default;
 057                    _state = 0;
 058                    return true;
 59
 60                default:
 061                    Debug.Assert(_multiple != null);
 062                    return _multiple.TryDequeue(out value);
 63            }
 064        }
 65    }
 66}

Methods/Properties

Enqueue(T)
TryDequeue(T&)