Ejemplo n.º 1
0
 public static MappedAccessor <MockObject> CreateAccessor(MemoryMap map, long sizeInBytes)
 {
     return(map.CreateVariable <MockObject>(sizeInBytes, new MemoryMap.ReadFromDelegate <MockObject>(
                                                (Stream stream, long position, ref MockObject value) =>
     {
         var payload = string.Empty;
         var size = MemoryMapDelegates.ReadFromString(stream, position, ref payload);
         value = new MockObject(payload);
         return size;
     }),
                                            new MemoryMap.WriteToDelegate <MockObject>(
                                                (Stream stream, long position, ref MockObject value) =>
     {
         var payload = value.Payload;
         return MemoryMapDelegates.WriteToString(stream, position, ref payload);
     })));
 }
Ejemplo n.º 2
0
        public void TestTinyAccessors()
        {
            using (var map = new MemoryMapStream())
            {
                using (var tempStream = new MemoryStream(new byte[1024]))
                {
                    var index = new Index <string>(map, 32);

                    var element = "Ben";
                    var id1     = index.Add(element);
                    Assert.AreEqual(0, id1);
                    var id2Ref = MemoryMapDelegates.WriteToString(tempStream, id1, ref element);

                    element = "Abelshausen";
                    var id2 = index.Add(element);
                    Assert.AreEqual(id2Ref, id2);
                    var id3Ref = MemoryMapDelegates.WriteToString(tempStream, id2, ref element) + id2;

                    element = "is";
                    var id3 = index.Add(element);
                    Assert.AreEqual(id3Ref, id3);
                    var id4Ref = MemoryMapDelegates.WriteToString(tempStream, id3, ref element) + id3;

                    element = "the";
                    var id4 = index.Add(element);
                    Assert.AreEqual(id4Ref, id4);
                    var id5Ref = MemoryMapDelegates.WriteToString(tempStream, id4, ref element) + id4;

                    element = "author";
                    var id5 = index.Add(element);
                    Assert.AreEqual(id5Ref, id5);
                    var id6Ref = MemoryMapDelegates.WriteToString(tempStream, id5, ref element) + id5;

                    element = "of";
                    var id6 = index.Add(element);
                    Assert.AreEqual(id6Ref, id6);
                    var id7Ref = MemoryMapDelegates.WriteToString(tempStream, id6, ref element) + id6;

                    element = "this";
                    var id7 = index.Add(element);
                    Assert.AreEqual(id7Ref, id7);
                    var id8Ref = MemoryMapDelegates.WriteToString(tempStream, id7, ref element) + id7;

                    element = "library";
                    var id8 = index.Add(element);
                    Assert.AreEqual(id8Ref, id8);
                    var id9Ref = MemoryMapDelegates.WriteToString(tempStream, id8, ref element) + id8;

                    element = "and";
                    var id9 = index.Add(element);
                    Assert.AreEqual(id9Ref, id9);
                    var id10Ref = MemoryMapDelegates.WriteToString(tempStream, id9, ref element) + id9;

                    element = "this";
                    var id10 = index.Add(element);
                    Assert.AreEqual(id10Ref, id10);
                    var id11Ref = MemoryMapDelegates.WriteToString(tempStream, id10, ref element) + id10;

                    element = "test!";
                    var id11 = index.Add("test!");
                    Assert.AreEqual(id11Ref, id11);
                    var id12Ref = MemoryMapDelegates.WriteToString(tempStream, id11, ref element) + id11;

                    Assert.AreEqual(id12Ref, index.SizeInBytes);

                    Assert.AreEqual("Ben", index.Get(id1));
                    Assert.AreEqual("Abelshausen", index.Get(id2));
                    Assert.AreEqual("is", index.Get(id3));
                    Assert.AreEqual("the", index.Get(id4));
                    Assert.AreEqual("author", index.Get(id5));
                    Assert.AreEqual("of", index.Get(id6));
                    Assert.AreEqual("this", index.Get(id7));
                    Assert.AreEqual("library", index.Get(id8));
                    Assert.AreEqual("and", index.Get(id9));
                    Assert.AreEqual("this", index.Get(id10));
                    Assert.AreEqual("test!", index.Get(id11));
                }
            }
        }
Ejemplo n.º 3
0
        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]);
                        }
                    }
                }
            }
        }