コード例 #1
0
        public void CreateConstantCStringTest( )
        {
            using var context = new Context( );
            string            str   = "hello world";
            ConstantDataArray value = context.CreateConstantString(str);

            Assert.IsNotNull(value);
            Assert.IsTrue(value.IsString);   // Has terminating null, so it is a C String
            Assert.IsTrue(value.IsI8Sequence);

            Assert.IsFalse(value.IsNull);
            Assert.IsFalse(value.IsUndefined);
            Assert.IsFalse(value.IsInstruction);
            Assert.IsFalse(value.IsFunction);
            Assert.IsFalse(value.IsCallSite);
            Assert.IsFalse(value.IsZeroValue);

            Assert.AreSame(string.Empty, value.Name);
            Assert.IsNotNull(value.NativeType);

            var arrayType = value.NativeType as IArrayType;

            Assert.IsNotNull(arrayType);
            Assert.AreSame(context, arrayType !.Context);
            Assert.AreSame(context.Int8Type, arrayType.ElementType);
            Assert.AreEqual(( uint )str.Length + 1, arrayType.Length);    // +1 for terminating \0
            string valueStr = value.ExtractAsString( );

            Assert.IsFalse(string.IsNullOrWhiteSpace(valueStr));
            Assert.AreEqual(str, valueStr);

            var span = value.RawData;

            byte[] strBytes = System.Text.Encoding.ASCII.GetBytes(str);

            Assert.AreEqual(strBytes.Length + 1, span.Length);   // +1 for terminating \0
            for (int i = 0; i < strBytes.Length; ++i)
            {
                Assert.AreEqual(strBytes[i], span[i], $"At index {i}");
            }

            Assert.AreEqual(0, span[^ 1]);
コード例 #2
0
ファイル: ContextTests.cs プロジェクト: nbsn2/Llvm.NET
        public void CreateConstantStringTest( )
        {
            using (var context = new Context())
            {
                string            str   = "hello world";
                ConstantDataArray value = context.CreateConstantString(str);
                Assert.IsNotNull(value);
                Assert.IsTrue(value.IsString);
                Assert.IsFalse(value.IsNull);
                Assert.IsFalse(value.IsUndefined);
                Assert.AreSame(string.Empty, value.Name);
                Assert.IsNotNull(value.NativeType);

                var arrayType = value.NativeType as IArrayType;
                Assert.IsNotNull(arrayType);
                Assert.AreSame(context, arrayType.Context);
                Assert.AreSame(context.Int8Type, arrayType.ElementType);
                Assert.AreEqual(( uint )str.Length, arrayType.Length);
                string valueStr = value.ExtractAsString( );
                Assert.IsFalse(string.IsNullOrWhiteSpace(valueStr));
                Assert.AreEqual(str, valueStr);
            }
        }