本文整理汇总了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);
}
}
}
示例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]);
}
}
}
}
}