< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.ServiceModelDictionary
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/ServiceModelDictionary.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 54
Coverable lines: 54
Total lines: 146
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 34
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
.ctor(...)100%110%
CreateString(...)100%110%
TryLookup(...)0%880%
TryLookup(...)0%14140%
TryLookup(...)0%12120%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/ServiceModelDictionary.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.Xml;
 6
 7namespace CoreWCF
 8{
 9    internal class ServiceModelDictionary : IXmlDictionary
 10    {
 011        public static readonly ServiceModelDictionary Version1 = new ServiceModelDictionary(new ServiceModelStringsVersi
 12        private readonly ServiceModelStrings _strings;
 13        private readonly int _count;
 14        private XmlDictionaryString[] _dictionaryStrings1;
 15        private XmlDictionaryString[] _dictionaryStrings2;
 16        private Dictionary<string, int> _dictionary;
 17        private XmlDictionaryString[] _versionedDictionaryStrings;
 18
 019        public ServiceModelDictionary(ServiceModelStrings strings)
 20        {
 021            _strings = strings;
 022            _count = strings.Count;
 023        }
 24
 25        public static ServiceModelDictionary CurrentVersion
 26        {
 27            get
 28            {
 029                return Version1;
 30            }
 31        }
 32
 33        public XmlDictionaryString CreateString(string value, int key)
 34        {
 035            return new XmlDictionaryString(this, value, key);
 36        }
 37
 38        public bool TryLookup(string key, out XmlDictionaryString value)
 39        {
 040            if (key == null)
 41            {
 042                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key));
 43            }
 44
 045            if (_dictionary == null)
 46            {
 047                Dictionary<string, int> dictionary = new Dictionary<string, int>(_count);
 048                for (int i = 0; i < _count; i++)
 49                {
 050                    dictionary.Add(_strings[i], i);
 51                }
 52
 053                _dictionary = dictionary;
 54            }
 55
 056            if (_dictionary.TryGetValue(key, out int id))
 57            {
 058                return TryLookup(id, out value);
 59            }
 60
 061            value = null;
 062            return false;
 63        }
 64
 65        public bool TryLookup(int key, out XmlDictionaryString value)
 66        {
 67            const int keyThreshold = 32;
 068            if (key < 0 || key >= _count)
 69            {
 070                value = null;
 071                return false;
 72            }
 73
 74            XmlDictionaryString s;
 075            if (key < keyThreshold)
 76            {
 077                if (_dictionaryStrings1 == null)
 78                {
 079                    _dictionaryStrings1 = new XmlDictionaryString[keyThreshold];
 80                }
 81
 082                s = _dictionaryStrings1[key];
 083                if (s == null)
 84                {
 085                    s = CreateString(_strings[key], key);
 086                    _dictionaryStrings1[key] = s;
 87                }
 88            }
 89            else
 90            {
 091                if (_dictionaryStrings2 == null)
 92                {
 093                    _dictionaryStrings2 = new XmlDictionaryString[_count - keyThreshold];
 94                }
 95
 096                s = _dictionaryStrings2[key - keyThreshold];
 097                if (s == null)
 98                {
 099                    s = CreateString(_strings[key], key);
 0100                    _dictionaryStrings2[key - keyThreshold] = s;
 101                }
 102            }
 103
 0104            value = s;
 0105            return true;
 106        }
 107
 108        public bool TryLookup(XmlDictionaryString key, out XmlDictionaryString value)
 109        {
 0110            if (key == null)
 111            {
 0112                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(key));
 113            }
 114
 0115            if (key.Dictionary == this)
 116            {
 0117                value = key;
 0118                return true;
 119            }
 120
 0121            if (key.Dictionary == CurrentVersion)
 122            {
 0123                if (_versionedDictionaryStrings == null)
 124                {
 0125                    _versionedDictionaryStrings = new XmlDictionaryString[CurrentVersion._count];
 126                }
 127
 0128                XmlDictionaryString s = _versionedDictionaryStrings[key.Key];
 0129                if (s == null)
 130                {
 0131                    if (!TryLookup(key.Value, out s))
 132                    {
 0133                        value = null;
 0134                        return false;
 135                    }
 0136                    _versionedDictionaryStrings[key.Key] = s;
 137                }
 0138                value = s;
 0139                return true;
 140            }
 141
 0142            value = null;
 0143            return false;
 144        }
 145    }
 146}