Ejemplo n.º 1
0
        public unsafe void TestDefinitionHashCodes()
        {
            var wr = new Writer.MetadataWriter();

            wr.ScopeDefinitions.Add(BuildSimpleTestDefinitionData());
            var ms = new MemoryStream();

            wr.Write(ms);

            fixed(byte *pBuffer = ms.ToArray())
            {
                var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length);

                Reader.ScopeDefinitionHandle scopeHandle        = rd.ScopeDefinitions.Single();
                Reader.ScopeDefinition       systemRuntimeScope = scopeHandle.GetScopeDefinition(rd);

                // Validate root type hash code
                Reader.NamespaceDefinition  rootNamespace    = systemRuntimeScope.RootNamespaceDefinition.GetNamespaceDefinition(rd);
                Reader.TypeDefinitionHandle moduleTypeHandle = rootNamespace.TypeDefinitions.Single();
                Assert.Equal(TypeHashingAlgorithms.ComputeNameHashCode("<Module>"), MetadataTypeHashingAlgorithms.ComputeHashCode(moduleTypeHandle, rd));

                // Validate namespace type hashcode
                Reader.NamespaceDefinition  systemNamespace  = rootNamespace.NamespaceDefinitions.Single().GetNamespaceDefinition(rd);
                Reader.TypeDefinitionHandle objectTypeHandle = systemNamespace.TypeDefinitions.Single();
                int objectHashCode = TypeHashingAlgorithms.ComputeNameHashCode("System.Object");

                Assert.Equal(objectHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(objectTypeHandle, rd));

                // Validate nested type hashcode
                Reader.TypeDefinitionHandle nestedTypeHandle = objectTypeHandle.GetTypeDefinition(rd).NestedTypes.Single();
                int nestedHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(objectHashCode, TypeHashingAlgorithms.ComputeNameHashCode("Nested"));

                Assert.Equal(nestedHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(nestedTypeHandle, rd));

                // Validate really nested type hashcode
                Reader.TypeDefinitionHandle reallyNestedTypeHandle = nestedTypeHandle.GetTypeDefinition(rd).NestedTypes.Single();
                int reallyNestedHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(nestedHashCode, TypeHashingAlgorithms.ComputeNameHashCode("ReallyNested"));

                Assert.Equal(reallyNestedHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(reallyNestedTypeHandle, rd));
            }
        }
Ejemplo n.º 2
0
        public unsafe static void TestCommonTailOptimization()
        {
            var wr = new Writer.MetadataWriter();

            wr.ScopeDefinitions.Add(BuildSimpleTestData());
            var ms = new MemoryStream();

            wr.Write(ms);

            fixed(byte *pBuffer = ms.ToArray())
            {
                var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length);

                Reader.ScopeDefinitionHandle scopeHandle        = rd.ScopeDefinitions.Single();
                Reader.ScopeDefinition       systemRuntimeScope = scopeHandle.GetScopeDefinition(rd);
                Reader.NamespaceDefinition   rootNamespace      = systemRuntimeScope.RootNamespaceDefinition.GetNamespaceDefinition(rd);
                Reader.NamespaceDefinition   systemNamespace    =
                    rootNamespace.NamespaceDefinitions.Single(
                        ns => ns.GetNamespaceDefinition(rd).Name.StringEquals("System", rd)
                        ).GetNamespaceDefinition(rd);

                // This validates the common tail optimization.
                // Since both System.Object and System.String define a default constructor and the
                // records are structurally equivalent, there should only be one metadata record
                // representing a default .ctor in the blob.
                Reader.TypeDefinition objectType = systemNamespace.TypeDefinitions.Single(
                    t => t.GetTypeDefinition(rd).Name.StringEquals("Object", rd)
                    ).GetTypeDefinition(rd);
                Reader.TypeDefinition stringType = systemNamespace.TypeDefinitions.Single(
                    t => t.GetTypeDefinition(rd).Name.StringEquals("String", rd)
                    ).GetTypeDefinition(rd);

                Reader.MethodHandle objectCtor = objectType.Methods.Single();
                Reader.MethodHandle stringCtor = stringType.Methods.Single();

                Assert.True(objectCtor.Equals(stringCtor));
            }
        }
Ejemplo n.º 3
0
        public static unsafe void TestCommonTailOptimization()
        {
            var wr = new Writer.MetadataWriter();
            wr.ScopeDefinitions.Add(BuildSimpleTestData());
            var ms = new MemoryStream();
            wr.Write(ms);

            fixed (byte* pBuffer = ms.ToArray())
            {
                var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length);

                Reader.ScopeDefinitionHandle scopeHandle = rd.ScopeDefinitions.Single();
                Reader.ScopeDefinition systemRuntimeScope = scopeHandle.GetScopeDefinition(rd);
                Reader.NamespaceDefinition rootNamespace = systemRuntimeScope.RootNamespaceDefinition.GetNamespaceDefinition(rd);
                Reader.NamespaceDefinition systemNamespace =
                    rootNamespace.NamespaceDefinitions.Single(
                        ns => ns.GetNamespaceDefinition(rd).Name.StringEquals("System", rd)
                        ).GetNamespaceDefinition(rd);

                // This validates the common tail optimization.
                // Since both System.Object and System.String define a default constructor and the
                // records are structurally equivalent, there should only be one metadata record
                // representing a default .ctor in the blob.
                Reader.TypeDefinition objectType = systemNamespace.TypeDefinitions.Single(
                    t => t.GetTypeDefinition(rd).Name.StringEquals("Object", rd)
                    ).GetTypeDefinition(rd);
                Reader.TypeDefinition stringType = systemNamespace.TypeDefinitions.Single(
                    t => t.GetTypeDefinition(rd).Name.StringEquals("String", rd)
                    ).GetTypeDefinition(rd);

                Reader.MethodHandle objectCtor = objectType.Methods.Single();
                Reader.MethodHandle stringCtor = stringType.Methods.Single();

                Assert.True(objectCtor.Equals(stringCtor));
            }
        }
Ejemplo n.º 4
0
        public unsafe void TestDefinitionHashCodes()
        {
            var wr = new Writer.MetadataWriter();
            wr.ScopeDefinitions.Add(BuildSimpleTestDefinitionData());
            var ms = new MemoryStream();
            wr.Write(ms);

            fixed (byte* pBuffer = ms.ToArray())
            {
                var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length);

                Reader.ScopeDefinitionHandle scopeHandle = rd.ScopeDefinitions.Single();
                Reader.ScopeDefinition systemRuntimeScope = scopeHandle.GetScopeDefinition(rd);

                // Validate root type hash code
                Reader.NamespaceDefinition rootNamespace = systemRuntimeScope.RootNamespaceDefinition.GetNamespaceDefinition(rd);
                Reader.TypeDefinitionHandle moduleTypeHandle = rootNamespace.TypeDefinitions.Single();
                Assert.Equal(TypeHashingAlgorithms.ComputeNameHashCode("<Module>"), MetadataTypeHashingAlgorithms.ComputeHashCode(moduleTypeHandle, rd));

                // Validate namespace type hashcode
                Reader.NamespaceDefinition systemNamespace = rootNamespace.NamespaceDefinitions.Single().GetNamespaceDefinition(rd);
                Reader.TypeDefinitionHandle objectTypeHandle = systemNamespace.TypeDefinitions.Single();
                int objectHashCode = TypeHashingAlgorithms.ComputeNameHashCode("System.Object");
                Assert.Equal(objectHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(objectTypeHandle, rd));

                // Validate nested type hashcode
                Reader.TypeDefinitionHandle nestedTypeHandle = objectTypeHandle.GetTypeDefinition(rd).NestedTypes.Single();
                int nestedHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(objectHashCode, TypeHashingAlgorithms.ComputeNameHashCode("Nested"));
                Assert.Equal(nestedHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(nestedTypeHandle, rd));

                // Validate really nested type hashcode
                Reader.TypeDefinitionHandle reallyNestedTypeHandle = nestedTypeHandle.GetTypeDefinition(rd).NestedTypes.Single();
                int reallyNestedHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(nestedHashCode, TypeHashingAlgorithms.ComputeNameHashCode("ReallyNested"));
                Assert.Equal(reallyNestedHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(reallyNestedTypeHandle, rd));
            }
        }
Ejemplo n.º 5
0
        public unsafe static void TestSimpleRoundTripping()
        {
            var wr = new Writer.MetadataWriter();
            wr.ScopeDefinitions.Add(BuildSimpleTestData());
            var ms = new MemoryStream();
            wr.Write(ms);
            
            fixed (byte* pBuffer = ms.ToArray())
            {
                var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length);

                // Validate the System.Runtime scope
                Reader.ScopeDefinitionHandle scopeHandle = rd.ScopeDefinitions.Single();
                Reader.ScopeDefinition systemRuntimeScope = scopeHandle.GetScopeDefinition(rd);
                Assert.Equal(4, systemRuntimeScope.MajorVersion);
                Assert.Equal("System.Runtime", systemRuntimeScope.Name.GetConstantStringValue(rd).Value);

                // Validate the root namespace and <Module> type
                Reader.NamespaceDefinition rootNamespace = systemRuntimeScope.RootNamespaceDefinition.GetNamespaceDefinition(rd);
                Assert.Equal(1, rootNamespace.TypeDefinitions.Count());
                Reader.TypeDefinition moduleType = rootNamespace.TypeDefinitions.Single().GetTypeDefinition(rd);
                Assert.Equal("<Module>", moduleType.Name.GetConstantStringValue(rd).Value);
                Assert.Equal(1, rootNamespace.NamespaceDefinitions.Count());

                // Validate the System namespace
                Reader.NamespaceDefinition systemNamespace = rootNamespace.NamespaceDefinitions.Single().GetNamespaceDefinition(rd);
                Assert.Equal(4, systemNamespace.TypeDefinitions.Count());
                foreach (var typeHandle in systemNamespace.TypeDefinitions)
                {
                    Reader.TypeDefinition type = typeHandle.GetTypeDefinition(rd);
                    string typeName = type.Name.GetConstantStringValue(rd).Value;

                    string baseTypeName = null;
                    if (!type.BaseType.IsNull(rd))
                    {
                        baseTypeName = type.BaseType.ToTypeDefinitionHandle(rd).GetTypeDefinition(rd).Name.GetConstantStringValue(rd).Value;
                    }

                    switch (typeName)
                    {
                        case "Object":
                            Assert.Null(baseTypeName);
                            Assert.Equal(1, type.Methods.Count());
                            break;
                        case "Void":
                            Assert.Equal("ValueType", baseTypeName);
                            Assert.Equal(0, type.Methods.Count());
                            break;
                        case "String":
                            Assert.Equal("Object", baseTypeName);
                            Assert.Equal(1, type.Methods.Count());
                            break;
                        case "ValueType":
                            Assert.Equal("Object", baseTypeName);
                            Assert.Equal(0, type.Methods.Count());
                            break;
                        default:
                            throw new NotImplementedException();
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public unsafe static void TestSimpleRoundTripping()
        {
            var wr = new Writer.MetadataWriter();

            wr.ScopeDefinitions.Add(BuildSimpleTestData());
            var ms = new MemoryStream();

            wr.Write(ms);

            fixed(byte *pBuffer = ms.ToArray())
            {
                var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length);

                // Validate the System.Runtime scope
                Reader.ScopeDefinitionHandle scopeHandle        = rd.ScopeDefinitions.Single();
                Reader.ScopeDefinition       systemRuntimeScope = scopeHandle.GetScopeDefinition(rd);
                Assert.Equal(4, systemRuntimeScope.MajorVersion);
                Assert.Equal("System.Runtime", systemRuntimeScope.Name.GetConstantStringValue(rd).Value);

                // Validate the root namespace and <Module> type
                Reader.NamespaceDefinition rootNamespace = systemRuntimeScope.RootNamespaceDefinition.GetNamespaceDefinition(rd);
                Assert.Equal(1, rootNamespace.TypeDefinitions.Count());
                Reader.TypeDefinition moduleType = rootNamespace.TypeDefinitions.Single().GetTypeDefinition(rd);
                Assert.Equal("<Module>", moduleType.Name.GetConstantStringValue(rd).Value);
                Assert.Equal(1, rootNamespace.NamespaceDefinitions.Count());

                // Validate the System namespace
                Reader.NamespaceDefinition systemNamespace = rootNamespace.NamespaceDefinitions.Single().GetNamespaceDefinition(rd);
                Assert.Equal(4, systemNamespace.TypeDefinitions.Count());
                foreach (var typeHandle in systemNamespace.TypeDefinitions)
                {
                    Reader.TypeDefinition type = typeHandle.GetTypeDefinition(rd);
                    string typeName            = type.Name.GetConstantStringValue(rd).Value;

                    string baseTypeName = null;
                    if (!type.BaseType.IsNull(rd))
                    {
                        baseTypeName = type.BaseType.ToTypeDefinitionHandle(rd).GetTypeDefinition(rd).Name.GetConstantStringValue(rd).Value;
                    }

                    switch (typeName)
                    {
                    case "Object":
                        Assert.Null(baseTypeName);
                        Assert.Equal(1, type.Methods.Count());
                        break;

                    case "Void":
                        Assert.Equal("ValueType", baseTypeName);
                        Assert.Equal(0, type.Methods.Count());
                        break;

                    case "String":
                        Assert.Equal("Object", baseTypeName);
                        Assert.Equal(1, type.Methods.Count());
                        break;

                    case "ValueType":
                        Assert.Equal("Object", baseTypeName);
                        Assert.Equal(0, type.Methods.Count());
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public unsafe void TestReferenceHashCodes()
        {
            var wr = new Writer.MetadataWriter();

            var systemRuntimeScopeRecord = new Writer.ScopeReference
            {
                Name         = (Writer.ConstantStringValue) "System.Runtime",
                MajorVersion = 4,
            };

            var rootNamespaceRecord = new Writer.NamespaceReference
            {
                Name = null,
                ParentScopeOrNamespace = systemRuntimeScopeRecord,
            };

            var fooTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = rootNamespaceRecord,
                TypeName = (Writer.ConstantStringValue) "Foo",
            };

            var nestedTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = fooTypeRecord,
                TypeName = (Writer.ConstantStringValue) "Nested",
            };

            var reallyNestedTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = nestedTypeRecord,
                TypeName = (Writer.ConstantStringValue) "ReallyNested",
            };

            var systemNamespaceRecord = new Writer.NamespaceReference
            {
                Name = (Writer.ConstantStringValue) "System",
                ParentScopeOrNamespace = rootNamespaceRecord,
            };

            var objectTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = systemNamespaceRecord,
                TypeName = (Writer.ConstantStringValue) "Object",
            };

            wr.AdditionalRootRecords.Add(objectTypeRecord);
            wr.AdditionalRootRecords.Add(fooTypeRecord);
            wr.AdditionalRootRecords.Add(nestedTypeRecord);
            wr.AdditionalRootRecords.Add(reallyNestedTypeRecord);
            var ms = new MemoryStream();

            wr.Write(ms);

            fixed(byte *pBuffer = ms.ToArray())
            {
                var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length);

                var fooTypeHandle   = new Reader.TypeReferenceHandle(wr.GetRecordHandle(fooTypeRecord));
                var fooTypeHashCode = TypeHashingAlgorithms.ComputeNameHashCode("Foo");

                Assert.Equal(fooTypeHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(fooTypeHandle, rd));

                var objectTypeHandle = new Reader.TypeReferenceHandle(wr.GetRecordHandle(objectTypeRecord));

                Assert.Equal(TypeHashingAlgorithms.ComputeNameHashCode("System.Object"), MetadataTypeHashingAlgorithms.ComputeHashCode(objectTypeHandle, rd));

                var nestedTypeHandle   = new Reader.TypeReferenceHandle(wr.GetRecordHandle(nestedTypeRecord));
                var nestedTypeHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(fooTypeHashCode, TypeHashingAlgorithms.ComputeNameHashCode("Nested"));

                Assert.Equal(nestedTypeHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(nestedTypeHandle, rd));

                var reallyNestedTypeHandle   = new Reader.TypeReferenceHandle(wr.GetRecordHandle(reallyNestedTypeRecord));
                var reallyNestedTypeHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(nestedTypeHashCode, TypeHashingAlgorithms.ComputeNameHashCode("ReallyNested"));

                Assert.Equal(reallyNestedTypeHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(reallyNestedTypeHandle, rd));
            }
        }
Ejemplo n.º 8
0
        public unsafe void TestReferenceHashCodes()
        {
            var wr = new Writer.MetadataWriter();

            var systemRuntimeScopeRecord = new Writer.ScopeReference
            {
                Name = (Writer.ConstantStringValue)"System.Runtime",
                MajorVersion = 4,
            };

            var rootNamespaceRecord = new Writer.NamespaceReference
            {
                Name = null,
                ParentScopeOrNamespace = systemRuntimeScopeRecord,
            };

            var fooTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = rootNamespaceRecord,
                TypeName = (Writer.ConstantStringValue)"Foo",
            };

            var nestedTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = fooTypeRecord,
                TypeName = (Writer.ConstantStringValue)"Nested",
            };

            var reallyNestedTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = nestedTypeRecord,
                TypeName = (Writer.ConstantStringValue)"ReallyNested",
            };

            var systemNamespaceRecord = new Writer.NamespaceReference
            {
                Name = (Writer.ConstantStringValue)"System",
                ParentScopeOrNamespace = rootNamespaceRecord,
            };

            var objectTypeRecord = new Writer.TypeReference
            {
                ParentNamespaceOrType = systemNamespaceRecord,
                TypeName = (Writer.ConstantStringValue)"Object",
            };

            wr.AdditionalRootRecords.Add(objectTypeRecord);
            wr.AdditionalRootRecords.Add(fooTypeRecord);
            wr.AdditionalRootRecords.Add(nestedTypeRecord);
            wr.AdditionalRootRecords.Add(reallyNestedTypeRecord);
            var ms = new MemoryStream();
            wr.Write(ms);

            fixed (byte* pBuffer = ms.ToArray())
            {
                var rd = new Reader.MetadataReader((IntPtr)pBuffer, (int)ms.Length);

                var fooTypeHandle = new Reader.TypeReferenceHandle(wr.GetRecordHandle(fooTypeRecord));
                var fooTypeHashCode = TypeHashingAlgorithms.ComputeNameHashCode("Foo");
                Assert.Equal(fooTypeHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(fooTypeHandle, rd));

                var objectTypeHandle = new Reader.TypeReferenceHandle(wr.GetRecordHandle(objectTypeRecord));
                Assert.Equal(TypeHashingAlgorithms.ComputeNameHashCode("System.Object"), MetadataTypeHashingAlgorithms.ComputeHashCode(objectTypeHandle, rd));

                var nestedTypeHandle = new Reader.TypeReferenceHandle(wr.GetRecordHandle(nestedTypeRecord));
                var nestedTypeHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(fooTypeHashCode, TypeHashingAlgorithms.ComputeNameHashCode("Nested"));
                Assert.Equal(nestedTypeHashCode , MetadataTypeHashingAlgorithms.ComputeHashCode(nestedTypeHandle, rd));

                var reallyNestedTypeHandle = new Reader.TypeReferenceHandle(wr.GetRecordHandle(reallyNestedTypeRecord));
                var reallyNestedTypeHashCode = TypeHashingAlgorithms.ComputeNestedTypeHashCode(nestedTypeHashCode, TypeHashingAlgorithms.ComputeNameHashCode("ReallyNested"));
                Assert.Equal(reallyNestedTypeHashCode, MetadataTypeHashingAlgorithms.ComputeHashCode(reallyNestedTypeHandle, rd));
            }
        }