本文整理汇总了C#中Index.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# Index.CopyTo方法的具体用法?C# Index.CopyTo怎么用?C# Index.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Index
的用法示例。
在下文中一共展示了Index.CopyTo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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]);
}
}
}
}
}