Example #1
0
        public void IsNullOrEmptyTest()
        {
            Assert.True(ArrayUtils.IsNullOrEmpty <int>(null));
            Assert.True(ArrayUtils.IsNullOrEmpty(ArrayUtils.Empty <int>()));
            Assert.False(ArrayUtils.IsNullOrEmpty(new[] { 1 }));

            Assert.True(ArrayUtils.IsNullOrEmpty(null));
            Assert.True(ArrayUtils.IsNullOrEmpty((Array)ArrayUtils.Empty <int>()));
            Assert.False(ArrayUtils.IsNullOrEmpty((Array) new[] { 1 }));
        }
        public void GenerateTest()
        {
            var builder = new AssemblyNameBuilder();

            Assert.Empty(builder.ToString());

            // name only
            builder.Name = "mscorlib";
            Assert.Equal("mscorlib", builder.ToString());

            // full name
            builder = new AssemblyNameBuilder
            {
                Name           = "mscorlib",
                Version        = new Version(4, 0, 0, 0),
                CultureName    = "neutral",
                PublicKeyToken = new byte[] { 0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89 }
            };
            Assert.Equal("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", builder.ToString());

            // missing attributes
            builder.Version        = null;
            builder.CultureName    = null;
            builder.PublicKeyToken = ArrayUtils.Empty <byte>();
            Assert.Equal("mscorlib, PublicKeyToken=null", builder.ToString());

            // all (with defaults)
            builder.Name                  = "lib";
            builder.VersionString         = "1.0.0.0";
            builder.CultureName           = "neutral";
            builder.PublicKeyString       = "null";
            builder.PublicKeyTokenString  = null;
            builder.ProcessorArchitecture = ProcessorArchitecture.X86;
            builder.Retargetable          = true;
            builder.ContentType           = AssemblyContentType.WindowsRuntime;
            builder.Custom                = new byte[0];
            Assert.Equal("lib, Version=1.0.0.0, Culture=neutral, PublicKey=null, ProcessorArchitecture=X86, Retargetable=yes, ContentType=WindowsRuntime, Custom=null", builder.ToString());

            // all
            builder.Name                  = "lib";
            builder.VersionString         = "1.0.0.0";
            builder.CultureName           = "en";
            builder.PublicKeyString       = "0123456789abcdef";
            builder.ProcessorArchitecture = ProcessorArchitecture.None;
            builder.Retargetable          = false;
            builder.ContentType           = AssemblyContentType.Default;
            builder.Custom                = new byte[] { 0, 1 };
            Assert.Equal("lib, Version=1.0.0.0, Culture=en, PublicKey=0123456789abcdef, Custom=0001", builder.ToString());

            // remove attributes

            builder.RemoveAttributes();
            Assert.Equal("lib", builder.ToString());
        }
Example #3
0
        public void ContentEqualsTest()
        {
            Assert.True(ArrayUtils.ContentEquals <int>(null, null));
            Assert.False(ArrayUtils.ContentEquals(ArrayUtils.Empty <int>(), null));
            Assert.False(ArrayUtils.ContentEquals(null, ArrayUtils.Empty <int>()));

            var array = new int[10];

            array.Fill(CachedDelegates.Identity <int> .Func);
            Assert.False(ArrayUtils.ContentEquals(array, ArrayUtils.Empty <int>()));

            var otherArray = (int[])array.Clone();

            Assert.True(ArrayUtils.ContentEquals(array, otherArray));
            otherArray[otherArray.Length - 1] = 0;
            Assert.False(ArrayUtils.ContentEquals(array, otherArray));

            var stringArray = new[] { "a", "b", "c" };

            Assert.False(ArrayUtils.ContentEquals(new[] { "a", "B", "c" }, stringArray));
            Assert.True(ArrayUtils.ContentEquals(new[] { "a", "B", "c" }, stringArray, StringComparer.OrdinalIgnoreCase));
        }
Example #4
0
        private void Parse(string input)
        {
            var parts = input.Split(new[] { ',' }, StringSplitOptions.None);

            var value = parts[0].Trim();

            if (value.Length == 0)
            {
                throw new FormatException();
            }
            Name = value;

            const int versionAttribute               = 0x1;
            const int cultureOrLanguageAttribute     = 0x2;
            const int publicKeyOrTokenAttribute      = 0x4;
            const int processorArchitectureAttribute = 0x8;
            const int retargetableAttribute          = 0x10;
            const int contentTypeAttribute           = 0x20;
            const int customAttribute = -0x8000_0000;

            int attributesParsed = 0;

            string part;
            int    index;

            for (int i = 1, n = parts.Length; i < n; i++)
            {
                if ((index = (part = parts[i]).IndexOf('=')) > 0)
                {
                    value = part.Remove(0, index + 1).Trim();
                    if (value.Length == 0)
                    {
                        continue;
                    }

                    var key = part.Substring(0, index).Trim();

                    if (ShouldParseAttribute(ref attributesParsed, versionAttribute) &&
                        VersionKey.Equals(key, StringComparison.OrdinalIgnoreCase))
                    {
                        attributesParsed |= versionAttribute;

                        if (Version.TryParse(value, out Version version))
                        {
                            Version = version;
                        }
                    }
                    else if (ShouldParseAttribute(ref attributesParsed, cultureOrLanguageAttribute) &&
                             CultureKey.Equals(key, StringComparison.OrdinalIgnoreCase) || LanguageKey.Equals(key, StringComparison.OrdinalIgnoreCase))
                    {
                        attributesParsed |= cultureOrLanguageAttribute;

                        try { CultureName = value; }
                        catch (CultureNotFoundException) { }
                    }
                    else if (ShouldParseAttribute(ref attributesParsed, publicKeyOrTokenAttribute) &&
                             PublicKeyTokenKey.Equals(key, StringComparison.OrdinalIgnoreCase))
                    {
                        attributesParsed |= publicKeyOrTokenAttribute;

                        if (value.Length == PublicKeyTokenLength << 1)
                        {
                            try { PublicKeyTokenString = value; }
                            catch (FormatException) { }
                        }
                    }
                    else if (ShouldParseAttribute(ref attributesParsed, publicKeyOrTokenAttribute) &&
                             PublicKeyKey.Equals(key, StringComparison.OrdinalIgnoreCase))
                    {
                        attributesParsed |= publicKeyOrTokenAttribute;

                        if (value.Length <= PublicKeyMaxLength << 1)
                        {
                            try { PublicKeyString = value; }
                            catch (FormatException) { }
                        }
                    }
                    else if (ShouldParseAttribute(ref attributesParsed, processorArchitectureAttribute) &&
                             ProcessorArchitectureKey.Equals(key, StringComparison.OrdinalIgnoreCase))
                    {
                        attributesParsed |= processorArchitectureAttribute;

                        if (Enum.TryParse(value, ignoreCase: true, out _ProcessorArchitecture processorArchitecture) &&
                            processorArchitecture != _ProcessorArchitecture.None)
                        {
                            ProcessorArchitecture = processorArchitecture;
                        }
                    }
                    else if (ShouldParseAttribute(ref attributesParsed, retargetableAttribute) &&
                             RetargetableKey.Equals(key, StringComparison.OrdinalIgnoreCase))
                    {
                        attributesParsed |= retargetableAttribute;

                        if (YesValue.Equals(value, StringComparison.OrdinalIgnoreCase))
                        {
                            Retargetable = true;
                        }
                    }
                    else if (ShouldParseAttribute(ref attributesParsed, contentTypeAttribute) &&
                             ContentTypeKey.Equals(key, StringComparison.OrdinalIgnoreCase))
                    {
                        attributesParsed |= contentTypeAttribute;

                        if (Enum.TryParse(value, ignoreCase: true, out AssemblyContentType contentType) &&
                            contentType != AssemblyContentType.Default)
                        {
                            ContentType = contentType;
                        }
                    }
                    else if (ShouldParseAttribute(ref attributesParsed, customAttribute) &&
                             CustomKey.Equals(key, StringComparison.OrdinalIgnoreCase))
                    {
                        attributesParsed |= customAttribute;

                        if (NullValue.Equals(value, StringComparison.OrdinalIgnoreCase))
                        {
                            Custom = ArrayUtils.Empty <byte>();
                        }
                        else if (value.Length <= PublicKeyMaxLength << 1)
                        {
                            try { Custom = StringUtils.BytesFromHexString(value); }
                            catch (FormatException) { }
                        }
                    }
                }
            }

            bool ShouldParseAttribute(ref int attrsParsed, int attr)
            {
                return((attrsParsed & attr) == 0);
            }
        }
Example #5
0
 public void EmptyTest()
 {
     Assert.Equal(0, ArrayUtils.Empty <int>().Length);
     Assert.Same(ArrayUtils.Empty <int>(), ArrayUtils.Empty <int>());
 }