当前位置: 首页>>代码示例>>C#>>正文


C# Index.CopyToWithSize方法代码示例

本文整理汇总了C#中Index.CopyToWithSize方法的典型用法代码示例。如果您正苦于以下问题:C# Index.CopyToWithSize方法的具体用法?C# Index.CopyToWithSize怎么用?C# Index.CopyToWithSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Index的用法示例。


在下文中一共展示了Index.CopyToWithSize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestCreateFromAndCopyTo

        /// <summary>
        /// Tests create from and copy to in a row.
        /// </summary>
        public void TestCreateFromAndCopyTo()
        {
            byte[] data = null;
            var indexDictionary = new System.Collections.Generic.Dictionary<long, string>();

            using (var indexStream = new MemoryStream())
            {
                using (var map = new MemoryMapStream())
                {
                    // write to index and to a stream.
                    var index = new Index<string>(map, 32);

                    var element = "Ben";
                    var id = index.Add(element);
                    indexDictionary[id] = element;
                    element = "Abelshausen";
                    id = index.Add(element);
                    indexDictionary[id] = element;
                    element = "is";
                    id = index.Add(element);
                    indexDictionary[id] = element;
                    element = "the";
                    id = index.Add(element);
                    indexDictionary[id] = element;
                    element = "author";
                    id = index.Add(element);
                    indexDictionary[id] = element;
                    element = "of";
                    id = index.Add(element);
                    indexDictionary[id] = element;
                    element = "this";
                    id = index.Add(element);
                    indexDictionary[id] = element;
                    element = "library";
                    id = index.Add(element);
                    indexDictionary[id] = element;
                    element = "and";
                    id = index.Add(element);
                    indexDictionary[id] = element;
                    element = "this";
                    id = index.Add(element);
                    indexDictionary[id] = element;
                    element = "test!";
                    id = index.Add("test!");
                    indexDictionary[id] = element;

                    index.CopyToWithSize(indexStream);
                    data = indexStream.ToArray();
                }
            }

            using (var indexStream = new MemoryStream(data))
            {
                var index = Index<string>.CreateFromWithSize(indexStream);

                foreach (var refIndexElement in indexDictionary)
                {
                    var value = index.Get(refIndexElement.Key);
                    Assert.AreEqual(refIndexElement.Value, value);
                }

                using(var outputData = new MemoryStream())
                {
                    var size = index.CopyToWithSize(outputData);
                    Assert.AreEqual(data.Length, size);
                }
            }
        }
开发者ID:cmberryau,项目名称:reminiscence,代码行数:71,代码来源:IndexTests.cs

示例2: TestCopyTo

        /// <summary>
        /// Tests copying the data to a stream.
        /// </summary>
        public void TestCopyTo()
        {
            using (var map = new MemoryMapStream())
            {
                using (var refStream = new MemoryStream(new byte[1024]))
                {
                    // write to index and to a stream.
                    var index = new Index<string>(map, 32);

                    var element = "Ben";
                    var id = index.Add(element);
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    element = "Abelshausen";
                    id = index.Add(element);
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    element = "is";
                    id = index.Add(element);
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    element = "the";
                    id = index.Add(element);
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    element = "author";
                    id = index.Add(element);
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    element = "of";
                    id = index.Add(element);
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    element = "this";
                    id = index.Add(element);
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    element = "library";
                    id = index.Add(element);
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    element = "and";
                    id = index.Add(element);
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    element = "this";
                    id = index.Add(element);
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    element = "test!";
                    id = index.Add("test!");
                    MemoryMapDelegates.WriteToString(refStream, id, ref element);

                    refStream.SetLength(refStream.Position);

                    using(var indexStream = new MemoryStream((int)index.SizeInBytes))
                    {
                        var refBytes = refStream.ToArray();

                        Assert.AreEqual(refBytes.Length, index.CopyTo(indexStream));
                        var bytes = indexStream.ToArray();

                        Assert.AreEqual(index.SizeInBytes, bytes.Length);
                        Assert.AreEqual(index.SizeInBytes, refBytes.Length);

                        for(var i = 0; i < bytes.Length; i++)
                        {
                            Assert.AreEqual(refBytes[i], bytes[i]);
                        }
                    }

                    using (var indexStream = new MemoryStream((int)index.SizeInBytes + 8))
                    {
                        var refBytes = refStream.ToArray();

                        Assert.AreEqual(refBytes.Length + 8, index.CopyToWithSize(indexStream));
                        var bytes = indexStream.ToArray();

                        Assert.AreEqual(index.SizeInBytes, bytes.Length - 8);
                        Assert.AreEqual(index.SizeInBytes, refBytes.Length);

                        for (var i = 0; i < refBytes.Length; i++)
                        {
                            Assert.AreEqual(refBytes[i], bytes[i + 8]);
                        }
                    }
                }
            }
        }
开发者ID:cmberryau,项目名称:reminiscence,代码行数:92,代码来源:IndexTests.cs


注:本文中的Index.CopyToWithSize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。