public void CompareToArrayTest() { var randomGenerator = new System.Random(66707770); // make this deterministic using (var map = new MemoryMapStream()) { using (var array = new Array <uint>(map, 1000)) { var arrayExpected = new uint[1000]; for (uint i = 0; i < 1000; i++) { if (randomGenerator.Next(4) >= 2) { // add data. arrayExpected[i] = i; array[i] = i; } else { arrayExpected[i] = int.MaxValue; array[i] = int.MaxValue; } Assert.AreEqual(arrayExpected[i], array[i]); } for (var i = 0; i < 1000; i++) { Assert.AreEqual(arrayExpected[i], array[i]); } } } }
/// <summary> /// Tests adding random keys. /// </summary> public static void TestRandom() { using (var mapStream = new FileInfo(Global.FileName).Open( FileMode.Create, FileAccess.ReadWrite)) { using (var map = new MemoryMapStream(mapStream)) { var random = new Random(); var dictionary = new Dictionary <int, long>(map); var count = 65536 * 2; var perf = new PerformanceInfoConsumer( string.Format("Write Dictionary Random"), 1000); perf.Start(); for (var i = 0; i < count; i++) { var r = random.Next(); dictionary[r] = (long)r * 2; if (Global.Verbose && i % (count / 100) == 0) { perf.Report("Writing... {0}%", i, count - 1); } } perf.Stop(); } } }
public void TestAdd() { using (var map = new MemoryMapStream()) { var dictionary = new Dictionary<string, string>(map); dictionary.Add("Ben", "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary["Ben"]); Assert.Catch<ArgumentException>(() => dictionary.Add("Ben", "Not Abelshausen")); } MockObject.RegisterAccessorCreateFunc(); using (var map = new MemoryMapStream()) { var dictionary = new Dictionary<MockObject, string>(map); dictionary.Add(new MockObject("Ben"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben")]); Assert.Catch<ArgumentException>(() => dictionary.Add(new MockObject("Ben"), "Not Abelshausen")); dictionary.Add(new MockObject("Ben1"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben1")]); dictionary.Add(new MockObject("Ben2"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben2")]); dictionary.Add(new MockObject("Ben3"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben3")]); dictionary.Add(new MockObject("Ben4"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben4")]); dictionary.Add(new MockObject("Ben5"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben5")]); } }
/// <summary> /// Deserializes from the given stream, returns an optimized index. /// </summary> public static MappedAttributesIndex Deserialize(Stream stream, MappedAttributesIndexProfile profile) { var version = stream.ReadByte(); if (version > 1) { throw new Exception(string.Format("Cannot deserialize mapped attributes index: Invalid version #: {0}, upgrade Itinero.", version)); } var bytes = new byte[4]; stream.Read(bytes, 0, 4); var length = BitConverter.ToUInt32(bytes, 0); ArrayBase <uint> data; if (profile == null || profile.DataProfile == null) { data = Context.ArrayFactory.CreateMemoryBackedArray <uint>(length); data.CopyFrom(stream); } else { var position = stream.Position; var map = new MemoryMapStream(new CappedStream(stream, position, length * 4)); data = new Array <uint>(map.CreateUInt32(length), profile.DataProfile); stream.Seek(length * 4, SeekOrigin.Current); } var attributes = AttributesIndex.Deserialize(new LimitedStream(stream, stream.Position), true); return(new MappedAttributesIndex(data, attributes)); }
public void TestAdd() { using (var map = new MemoryMapStream()) { var list = new List<int>(map); list.Add(100); Assert.AreEqual(1, list.Count); Assert.AreEqual(100, list[0]); Assert.AreEqual(1024, list.Capacity); for(var i = 0; i < 1023; i++) { list.Add(i + 101); } Assert.AreEqual(1024, list.Count); Assert.AreEqual(100, list[0]); Assert.AreEqual(1024, list.Capacity); list.Add(1002); Assert.AreEqual(1025, list.Count); Assert.AreEqual(100, list[0]); Assert.AreEqual(1049600, list.Capacity); } }
public void TestAdd() { using (var map = new MemoryMapStream()) { var dictionary = new Dictionary <string, string>(map); dictionary.Add("Ben", "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary["Ben"]); Assert.Catch <ArgumentException>(() => dictionary.Add("Ben", "Not Abelshausen")); } MockObject.RegisterAccessorCreateFunc(); using (var map = new MemoryMapStream()) { var dictionary = new Dictionary <MockObject, string>(map); dictionary.Add(new MockObject("Ben"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben")]); Assert.Catch <ArgumentException>(() => dictionary.Add(new MockObject("Ben"), "Not Abelshausen")); dictionary.Add(new MockObject("Ben1"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben1")]); dictionary.Add(new MockObject("Ben2"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben2")]); dictionary.Add(new MockObject("Ben3"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben3")]); dictionary.Add(new MockObject("Ben4"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben4")]); dictionary.Add(new MockObject("Ben5"), "Abelshausen"); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben5")]); } }
/// <summary> /// Tests adding random keys. /// </summary> public static void TestRandom() { using (var mapStream = new FileInfo(Global.FileName).Open( FileMode.Create, FileAccess.ReadWrite)) { using (var map = new MemoryMapStream(mapStream)) { var random = new Random(); var dictionary = new Dictionary<int, long>(map); var count = 65536 * 2; var perf = new PerformanceInfoConsumer( string.Format("Write Dictionary Random"), 1000); perf.Start(); for (var i = 0; i < count; i++) { var r = random.Next(); dictionary[r] = (long)r * 2; if (Global.Verbose && i % (count / 100) == 0) { perf.Report("Writing... {0}%", i, count - 1); } } perf.Stop(); } } }
public void ArgumentTest() { using (var map = new MemoryMapStream()) { using (var array = new VariableArray <string>(map, 1024, 10)) { Assert.AreEqual(10, array.Length); Assert.Catch <ArgumentOutOfRangeException>(() => { array[1001] = 10.ToString(); }); Assert.Catch <ArgumentOutOfRangeException>(() => { array[-1] = 10.ToString(); }); string value; Assert.Catch <ArgumentOutOfRangeException>(() => { value = array[1001]; }); Assert.Catch <ArgumentOutOfRangeException>(() => { value = array[-1]; }); } } }
/// <summary> /// Tests writing to an array. /// </summary> public static void TestWrite(ArrayProfile profile) { using (var mapStream = new FileInfo(Global.FileName).Open( FileMode.Create, FileAccess.ReadWrite)) { using (var map = new MemoryMapStream(mapStream)) { var array = new Array<int>(map, Global.ArrayTestLength, profile); var perf = new PerformanceInfoConsumer( string.Format("Write Array: {0}", profile.ToString()), 1000); perf.Start(); for (var i = 0; i < array.Length; i++) { array[i] = i * 2; if (Global.Verbose && i % (array.Length / 100) == 0) { perf.Report("Writing... {0}%", i, array.Length - 1); } } perf.Stop(); } } }
public void ArgumentTest() { using (var map = new MemoryMapStream()) { using (var array = new Array<uint>(map, 1000)) { Assert.AreEqual(1000, array.Length); Assert.Catch<ArgumentOutOfRangeException>(() => { array[1001] = 10; }); Assert.Catch<ArgumentOutOfRangeException>(() => { array[-1] = 10; }); uint value; Assert.Catch<ArgumentOutOfRangeException>(() => { value = array[1001]; }); Assert.Catch<ArgumentOutOfRangeException>(() => { value = array[-1]; }); } } }
public void TestCopyTo() { using (var map = new MemoryMapStream()) { var list = new List <int>(map); for (var i = 0; i < 10; i++) { list.Add(i); } var array = new int[20]; list.CopyTo(array, 0); for (var i = 0; i < 10; i++) { Assert.AreEqual(i, array[i]); } list.CopyTo(array, 10); for (var i = 10; i < 20; i++) { Assert.AreEqual(i - 10, array[i]); } } }
public void TestClear() { using (var map = new MemoryMapStream()) { var list = new List <int>(map); list.Add(100); list.Clear(); Assert.AreEqual(0, list.Count); Assert.AreEqual(1024, list.Capacity); for (var i = 0; i < 1024; i++) { list.Add(i + 101); } list.Clear(); Assert.AreEqual(1024, list.Capacity); for (var i = 0; i < 1025; i++) { list.Add(i + 101); } list.Clear(); Assert.AreEqual(1049600, list.Capacity); } }
public void ArgumentTest() { using (var map = new MemoryMapStream()) { using (var array = new Array <uint>(map, 1000)) { Assert.AreEqual(1000, array.Length); Assert.Catch <ArgumentOutOfRangeException>(() => { array[1001] = 10; }); Assert.Catch <ArgumentOutOfRangeException>(() => { array[-1] = 10; }); uint value; Assert.Catch <ArgumentOutOfRangeException>(() => { value = array[1001]; }); Assert.Catch <ArgumentOutOfRangeException>(() => { value = array[-1]; }); } } }
public void TestRemoveAt() { using (var map = new MemoryMapStream()) { var list = new List <int>(map); for (var i = 0; i < 10; i++) { list.Add(i); } list.Insert(2, 101); list.Insert(3, 104); list.Insert(4, 501); list.Insert(5, 201); list.RemoveAt(2); list.RemoveAt(2); list.RemoveAt(2); list.RemoveAt(2); for (var i = 0; i < 10; i++) { Assert.AreEqual(i, list[i]); } } }
/// <summary> /// Tests read from an array. /// </summary> public static void TestReadRandom(ArrayProfile profile) { using (var mapStream = new FileInfo(Global.FileName).Open( FileMode.Open, FileAccess.ReadWrite)) { using (var map = new MemoryMapStream(mapStream)) { var array = new Array <int>(map.CreateInt32(mapStream.Length / 4), profile); var size = 1000000; var perf = new PerformanceInfoConsumer( string.Format("Read Random Array: {0} {1}", size, profile.ToString()), 1000); perf.Start(); var rand = new Random(); for (var i = 0; i < size; i++) { var ran = (long)rand.Next((int)array.Length); var val = array[ran]; if (val != ran * 2) { // oeps, something went wrong here! throw new System.Exception(); } if (Global.Verbose && i % (size / 100) == 0) { perf.Report("Reading... {0}%", i, size); } } perf.Stop(); } } }
public void ArgumentTest() { using (var map = new MemoryMapStream()) { using (var array = new VariableArray<string>(map, 1024, 10)) { Assert.AreEqual(10, array.Length); Assert.Catch<ArgumentOutOfRangeException>(() => { array[1001] = 10.ToString(); }); Assert.Catch<ArgumentOutOfRangeException>(() => { array[-1] = 10.ToString(); }); string value; Assert.Catch<ArgumentOutOfRangeException>(() => { value = array[1001]; }); Assert.Catch<ArgumentOutOfRangeException>(() => { value = array[-1]; }); } } }
public void TestBasics() { using (var map = new MemoryMapStream()) { var accessor = map.CreateUInt16(123456); Assert.AreEqual(true, accessor.CanWrite); Assert.AreEqual(123456 * 2, accessor.Capacity); Assert.AreEqual(123456, accessor.CapacityElements); Assert.AreEqual(2, accessor.ElementSize); Assert.AreEqual(true, accessor.ElementSizeFixed); var data = new ushort[1024]; for (ushort i = 0; i < 1024; i++) { data[i] = (ushort)(i ^ 6983); } accessor.WriteArray(15214, data, 0, 1024); var readData = new ushort[1024]; accessor.ReadArray(15214, readData, 0, 1024); for (ushort i = 0; i < 1024; i++) { Assert.AreEqual(data[i], readData[i]); } } }
/// <summary> /// Tests writing to an array. /// </summary> public static void TestWrite(ArrayProfile profile) { using (var mapStream = new FileInfo(Global.FileName).Open( FileMode.Create, FileAccess.ReadWrite)) { using (var map = new MemoryMapStream(mapStream)) { var array = new Array <int>(map, Global.ArrayTestLength, profile); var perf = new PerformanceInfoConsumer( string.Format("Write Array: {0}", profile.ToString()), 1000); perf.Start(); for (var i = 0; i < array.Length; i++) { array[i] = i * 2; if (Global.Verbose && i % (array.Length / 100) == 0) { perf.Report("Writing... {0}%", i, array.Length - 1); } } perf.Stop(); } } }
public void TestSetItem() { using (var map = new MemoryMapStream()) { var list = new List <int>(map); for (var i = 0; i < 10; i++) { list.Add(i + 100); } for (var i = 0; i < 10; i++) { list[i] = i; } for (var i = 0; i < 10; i++) { Assert.AreEqual(i, list[i]); } Assert.Catch <ArgumentOutOfRangeException>(() => { list[11] = 0; }); Assert.Catch <ArgumentOutOfRangeException>(() => { list[-1] = 0; }); Assert.Catch <ArgumentOutOfRangeException>(() => { var t = list[11]; }); Assert.Catch <ArgumentOutOfRangeException>(() => { var t = list[-1]; }); } }
public void TestContainsKey() { using (var map = new MemoryMapStream()) { var dictionary = new Dictionary <string, string>(map); dictionary.Add("Ben", "Abelshausen"); Assert.IsTrue(dictionary.ContainsKey("Ben")); } MockObject.RegisterAccessorCreateFunc(); using (var map = new MemoryMapStream()) { var dictionary = new Dictionary <MockObject, string>(map); Assert.IsFalse(dictionary.ContainsKey(new MockObject("Ben"))); Assert.IsFalse(dictionary.ContainsKey(new MockObject("Ben1"))); dictionary.Add(new MockObject("Ben"), "Abelshausen"); dictionary.Add(new MockObject("Ben1"), "Abelshausen"); dictionary.Add(new MockObject("Ben2"), "Abelshausen"); Assert.IsFalse(dictionary.ContainsKey(new MockObject("Ben3"))); dictionary.Add(new MockObject("Ben3"), "Abelshausen"); dictionary.Add(new MockObject("Ben4"), "Abelshausen"); dictionary.Add(new MockObject("Ben5"), "Abelshausen"); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben"))); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben1"))); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben2"))); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben3"))); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben4"))); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben5"))); } }
/// <summary> /// Deserializes a graph from the given stream. /// </summary> /// <returns></returns> public static Graph Deserialize(System.IO.Stream stream, GraphProfile profile) { var initialPosition = stream.Position; // read sizes. long size = 1; var version = stream.ReadByte(); if (version != 1) { throw new Exception(string.Format("Cannot deserialize graph: Invalid version #: {0}.", version)); } var bytes = new byte[8]; stream.Read(bytes, 0, 8); size = size + 8; var vertexLength = BitConverter.ToInt64(bytes, 0); stream.Read(bytes, 0, 8); size = size + 8; var edgeLength = BitConverter.ToInt64(bytes, 0); stream.Read(bytes, 0, 4); size = size + 4; var vertexSize = BitConverter.ToInt32(bytes, 0); stream.Read(bytes, 0, 4); size = size + 4; var edgeSize = BitConverter.ToInt32(bytes, 0); ArrayBase <uint> vertices; ArrayBase <uint> edges; if (profile == null) { // just create arrays and read the data. vertices = new MemoryArray <uint>(vertexLength * vertexSize); vertices.CopyFrom(stream); size += vertexLength * vertexSize * 4; edges = new MemoryArray <uint>(edgeLength * edgeSize); edges.CopyFrom(stream); size += edgeLength * edgeSize * 4; } else { // create accessors over the exact part of the stream that represents vertices/edges. var position = stream.Position; var map1 = new MemoryMapStream(new CappedStream(stream, position, vertexLength * vertexSize * 4)); vertices = new Array <uint>(map1.CreateUInt32(vertexLength * vertexSize), profile.VertexProfile); size += vertexLength * vertexSize * 4; var map2 = new MemoryMapStream(new CappedStream(stream, position + vertexLength * vertexSize * 4, edgeLength * edgeSize * 4)); edges = new Array <uint>(map2.CreateUInt32(edgeLength * edgeSize), profile.EdgeProfile); size += edgeLength * edgeSize * 4; } // make sure stream is positioned at the correct location. stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin); return(new Graph(edgeSize - MINIMUM_EDGE_SIZE, vertices, edges)); }
public void TestIsReadonly() { using (var map = new MemoryMapStream()) { var list = new List <int>(map); Assert.IsFalse(list.IsReadOnly); } }
/// <summary> /// Deserializes an shapes index from the given stream. /// </summary> public static ShapesArray CreateFrom(Stream stream, bool copy, out long size, bool hasElevation = false) { var initialPosition = stream.Position; size = 0; var longBytes = new byte[8]; stream.Read(longBytes, 0, 8); size += 8; var indexLength = BitConverter.ToInt64(longBytes, 0); stream.Read(longBytes, 0, 8); size += 8; var coordinatesLength = BitConverter.ToInt64(longBytes, 0); ArrayBase <ulong> index; ArrayBase <float> coordinates; ArrayBase <short> elevation = null; if (copy) { // just create arrays and read the data. index = Context.ArrayFactory.CreateMemoryBackedArray <ulong>(indexLength); index.CopyFrom(stream); size += indexLength * 8; coordinates = Context.ArrayFactory.CreateMemoryBackedArray <float>(coordinatesLength); size += coordinatesLength * 4; coordinates.CopyFrom(stream); if (hasElevation) { elevation = Context.ArrayFactory.CreateMemoryBackedArray <short>(coordinatesLength / 2); size += coordinatesLength; elevation.CopyFrom(stream); } } else { // create accessors over the exact part of the stream that represents vertices/edges. var position = stream.Position; var map1 = new MemoryMapStream(new CappedStream(stream, position, indexLength * 8)); index = new Array <ulong>(map1.CreateUInt64(indexLength)); size += indexLength * 8; var map2 = new MemoryMapStream(new CappedStream(stream, position + indexLength * 8, coordinatesLength * 4)); coordinates = new Array <float>(map2.CreateSingle(coordinatesLength)); size += coordinatesLength * 4; if (hasElevation) { var map3 = new MemoryMapStream(new CappedStream(stream, position + indexLength * 8 + coordinatesLength * 4, coordinatesLength)); elevation = new Array <short>(map3.CreateInt16(coordinatesLength / 2)); size += coordinatesLength; } } // make stream is positioned correctly. stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin); return(new ShapesArray(index, coordinates, elevation)); }
public void TestCreate() { using (var map = new MemoryMapStream()) { var dictionary = new Dictionary <string, string>(map); Assert.AreEqual(0, dictionary.Count); } }
public void TestCreate() { using(var map = new MemoryMapStream()) { var dictionary = new Dictionary<string, string>(map); Assert.AreEqual(0, dictionary.Count); } }
public void TestAddWithElevation() { using (var map = new MemoryMapStream()) { var array = new ShapesArray(map, 1024); array.Set(0, new Coordinate(0, 0.1f, 10), new Coordinate(1, 1.1f, 11)); var shape = array[0]; Assert.IsNotNull(shape); Assert.AreEqual(2, shape.Count); Assert.AreEqual(0, shape[0].Latitude, .00001); Assert.AreEqual(0.1, shape[0].Longitude, .00001); Assert.AreEqual(10, shape[0].Elevation); Assert.AreEqual(1, shape[1].Latitude, .00001); Assert.AreEqual(1.1, shape[1].Longitude, .00001); Assert.AreEqual(11, shape[1].Elevation); } using (var map = new MemoryMapStream()) { var box = new Box( new Coordinate(-90, -180), new Coordinate(90, 180)); var refArray = new ShapeBase[1024]; var array = new ShapesArray(map, 1024); var rand = new Randomizer(1587341311); for (var i = 0; i < 1024; i++) { var count = rand.Next(10); var newShape = new List <Coordinate>(count); for (var j = 0; j < count; j++) { newShape.Add(box.GenerateRandomIn()); } var shape = new ShapeEnumerable(newShape); refArray[i] = shape; array[i] = shape; } for (var i = 0; i < refArray.Length; i++) { var refShape = refArray[i]; var shape = array[i]; Assert.IsNotNull(shape); for (var j = 0; j < shape.Count; j++) { Assert.AreEqual(refShape[j].Latitude, shape[j].Latitude); Assert.AreEqual(refShape[j].Longitude, shape[j].Longitude); Assert.AreEqual(refShape[j].Elevation, shape[j].Elevation); } } } }
public void TestOneElement() { using(var map = new MemoryMapStream()) { var index = new Index<string>(map); var id = index.Add("Ben"); Assert.AreEqual("Ben", index.Get(id)); } }
public void TestOneElement() { using (var map = new MemoryMapStream()) { var index = new Index <string>(map); var id = index.Add("Ben"); Assert.AreEqual("Ben", index.Get(id)); } }
public void TestWriteToAndReadFrom() { using (var map = new MemoryMapStream()) { using (var memoryStream = new MemoryStream()) { using (var array = new Array <int>(map, 10)) { for (var i = 0; i < array.Length; i++) { array[i] = i + 100; } array.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); using (var array1 = new Array <int>(map, array.Length)) { array1.CopyFrom(memoryStream); for (var i = 0; i < array.Length; i++) { Assert.AreEqual(array[i], array1[i]); } } } } } using (var map = new MemoryMapStream()) { using (var memoryStream = new MemoryStream()) { using (var array = new Array <int>(map, 10000, 32, 32, 2)) { for (var i = 0; i < array.Length; i++) { array[i] = i + 100; } array.CopyFrom(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); using (var array1 = new Array <int>(map, array.Length)) { array.CopyFrom(memoryStream); for (var i = 0; i < array.Length; i++) { Assert.AreEqual(array[i], array1[i]); } } } } } }
/// <summary> /// Tests read from an array. /// </summary> public static void TestRead(ArrayProfile profile) { using (var mapStream = new FileInfo(Global.FileName).Open( FileMode.Open, FileAccess.ReadWrite)) { using (var map = new MemoryMapStream(mapStream)) { var array = new Array<int>(map.CreateInt32(mapStream.Length / 4), profile); var perf = new PerformanceInfoConsumer( string.Format("Read Array: {0}", profile.ToString()), 1000); perf.Start(); for (var i = 0; i < array.Length; i++) { var val = array[i]; if (val != i * 2) { // oeps, something went wrong here! throw new System.Exception(); } if (Global.Verbose && i % (array.Length / 100) == 0) { perf.Report("Reading... {0}%", i, array.Length - 1); } } perf.Stop(); //var size = 1000000; //perf = new PerformanceInfoConsumer( // string.Format("Read Random Array: {0} {1}", size, profile.ToString()), // 1000); //perf.Start(); //var rand = new Random(); //for (var i = 0; i < size; i++) //{ // var ran = (long)rand.Next((int)array.Length); // var val = array[ran]; // if (val != ran * 2) // { // oeps, something went wrong here! // throw new System.Exception(); // } // if (Global.Verbose && i % (size / 100) == 0) // { // perf.Report("Reading... {0}%", i, size); // } //} //perf.Stop(); } } }
public void TestItemGetOrSet() { using (var map = new MemoryMapStream()) { var dictionary = new Dictionary <string, string>(map); dictionary["Ben"] = "Abelshausen"; Assert.Catch <System.Collections.Generic.KeyNotFoundException>(() => { var t = dictionary["Ben2"]; }); Assert.AreEqual("Abelshausen", dictionary["Ben"]); } MockObject.RegisterAccessorCreateFunc(); using (var map = new MemoryMapStream()) { var dictionary = new Dictionary <MockObject, string>(map); dictionary[new MockObject("Ben")] = "Abelshausen"; dictionary[new MockObject("Ben1")] = "Abelshausen1"; dictionary[new MockObject("Ben2")] = "Abelshausen2"; dictionary[new MockObject("Ben3")] = "Abelshausen3"; dictionary[new MockObject("Ben4")] = "Abelshausen4"; dictionary[new MockObject("Ben5")] = "Abelshausen5"; Assert.Catch <System.Collections.Generic.KeyNotFoundException>(() => { var t = dictionary[new MockObject("Ben6")]; }); Assert.AreEqual("Abelshausen", dictionary[new MockObject("Ben")]); Assert.AreEqual("Abelshausen1", dictionary[new MockObject("Ben1")]); Assert.AreEqual("Abelshausen2", dictionary[new MockObject("Ben2")]); Assert.AreEqual("Abelshausen3", dictionary[new MockObject("Ben3")]); Assert.AreEqual("Abelshausen4", dictionary[new MockObject("Ben4")]); Assert.AreEqual("Abelshausen5", dictionary[new MockObject("Ben5")]); dictionary[new MockObject("Ben")] = "Abelshausen1"; dictionary[new MockObject("Ben1")] = "Abelshausen11"; dictionary[new MockObject("Ben2")] = "Abelshausen12"; dictionary[new MockObject("Ben3")] = "Abelshausen13"; Assert.AreEqual("Abelshausen1", dictionary[new MockObject("Ben")]); Assert.AreEqual("Abelshausen11", dictionary[new MockObject("Ben1")]); Assert.AreEqual("Abelshausen12", dictionary[new MockObject("Ben2")]); Assert.AreEqual("Abelshausen13", dictionary[new MockObject("Ben3")]); Assert.AreEqual("Abelshausen4", dictionary[new MockObject("Ben4")]); Assert.AreEqual("Abelshausen5", dictionary[new MockObject("Ben5")]); } }
public void TestCreateNew() { using(var map = new MemoryMapStream()) { var list = new List<int>(map); Assert.AreEqual(0, list.Count); Assert.AreEqual(1024, list.Capacity); list = new List<int>(map, 10); Assert.AreEqual(0, list.Count); Assert.AreEqual(10, list.Capacity); } }
public void TestCreateNew() { using (var map = new MemoryMapStream()) { var list = new List <int>(map); Assert.AreEqual(0, list.Count); Assert.AreEqual(1024, list.Capacity); list = new List <int>(map, 10); Assert.AreEqual(0, list.Count); Assert.AreEqual(10, list.Capacity); } }
public void TestCopyTo() { using (var map = new MemoryMapStream()) { var array = new ShapesArray(map, 1); array.Set(0, new Coordinate(0, 0.1f), new Coordinate(1, 1.1f)); using (var stream = new MemoryStream()) { Assert.AreEqual(16 + 8 + (4 * 4), array.CopyTo(stream)); } } }
/// <summary> /// Deserializes from a stream. /// </summary> public static RoutingNetwork Deserialize(Stream stream, RoutingNetworkProfile profile) { var version = stream.ReadByte(); if (version > 2) { throw new Exception(string.Format("Cannot deserialize routing network: Invalid version #: {0}, upgrade Itinero.", version)); } var position = stream.Position; var initialPosition = stream.Position; // read maxEdgeDistance if version # = 2. var maxEdgeDistance = Constants.DefaultMaxEdgeDistance; if (version == 2) { var bytes = new byte[4]; stream.Read(bytes, 0, 4); maxEdgeDistance = BitConverter.ToSingle(bytes, 0); } // deserialize graph. var graph = GeometricGraph.Deserialize(stream, profile == null ? null : profile.GeometricGraphProfile); var size = stream.Position - position; var edgeLength = graph.EdgeCount; var edgeSize = 1; ArrayBase <uint> edgeData; if (profile == null) { // just create arrays and read the data. edgeData = Context.ArrayFactory.CreateMemoryBackedArray <uint>(edgeLength * edgeSize); edgeData.CopyFrom(stream); size += edgeLength * edgeSize * 4; } else { // create accessors over the exact part of the stream that represents vertices/edges. position = stream.Position; var map = new MemoryMapStream(new CappedStream(stream, position, edgeLength * edgeSize * 4)); edgeData = new Array <uint>(map.CreateUInt32(edgeLength * edgeSize), profile.EdgeDataProfile); size += edgeLength * edgeSize * 4; } // make stream is positioned correctly. stream.Seek(initialPosition + size, System.IO.SeekOrigin.Begin); return(new RoutingNetwork(graph, edgeData, maxEdgeDistance)); }
/// <summary> /// Creates a new dictionary. /// </summary> public Dictionary(int hashes, Func <TKey, int> keyGetHashCode, Func <TKey, TKey, bool> keyEquals) { var map = new MemoryMapStream(new System.IO.MemoryStream()); _hashedPointers = new MemoryArray <uint>(hashes); _keyValueList = new List <uint>(); _keys = new Indexes.Index <TKey>(map, 1024 * 1024 * 4); _values = new Indexes.Index <TValue>(map, 1024 * 1024 * 4); _keyGetHashCode = keyGetHashCode; _keysEqual = keyEquals; _keyValueList.Add(0); // zero cannot be used. }
public void ZeroSizeTest() { using (var map = new MemoryMapStream()) { using (var array = new VariableArray <string>(map, 1024, 0)) { Assert.AreEqual(0, array.Length); } using (var array = new VariableArray <string>(map, 1024, 100)) { array.Resize(0); Assert.AreEqual(0, array.Length); } } }
public void TestCreate() { var array = new ShapesArray(1024); Assert.IsNull(array[0]); Assert.IsNull(array[1000]); using (var map = new MemoryMapStream()) { array = new ShapesArray(map, 1024); Assert.IsNull(array[0]); Assert.IsNull(array[1000]); } }
public void TestCreate() { var index = new AttributesIndex(); Assert.IsFalse(index.IsReadonly); using (var map = new MemoryMapStream()) { index = new AttributesIndex(map); Assert.IsFalse(index.IsReadonly); index = new AttributesIndex(map, AttributesIndexMode.IncreaseOne); Assert.IsFalse(index.IsReadonly); } }
public void ZeroSizeTest() { using (var map = new MemoryMapStream()) { using (var array = new Array <uint>(map, 0)) { Assert.AreEqual(0, array.Length); } using (var array = new Array <uint>(map, 100)) { array.Resize(0); Assert.AreEqual(0, array.Length); } } }
public void ZeroSizeTest() { using (var map = new MemoryMapStream()) { using (var array = new Array<uint>(map, 0)) { Assert.AreEqual(0, array.Length); } using (var array = new Array<uint>(map, 100)) { array.Resize(0); Assert.AreEqual(0, array.Length); } } }
public void ZeroSizeTest() { using (var map = new MemoryMapStream()) { using (var array = new VariableArray<string>(map, 1024, 0)) { Assert.AreEqual(0, array.Length); } using (var array = new VariableArray<string>(map, 1024, 100)) { array.Resize(0); Assert.AreEqual(0, array.Length); } } }
public void TestWriteToAndReadFrom() { using (var map = new MemoryMapStream()) { using (var memoryStream = new MemoryStream()) { using (var array = new Array<int>(map, 10)) { for (var i = 0; i < array.Length; i++) { array[i] = i + 100; } array.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); using (var array1 = new Array<int>(map, array.Length)) { array1.CopyFrom(memoryStream); for (var i = 0; i < array.Length; i++) { Assert.AreEqual(array[i], array1[i]); } } } } } using (var map = new MemoryMapStream()) { using (var memoryStream = new MemoryStream()) { using (var array = new Array<int>(map, 10000, 32, 32, 2)) { for (var i = 0; i < array.Length; i++) { array[i] = i + 100; } array.CopyFrom(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); using (var array1 = new Array<int>(map, array.Length)) { array.CopyFrom(memoryStream); for (var i = 0; i < array.Length; i++) { Assert.AreEqual(array[i], array1[i]); } } } } } }
public void TestArraySmallerThanBuffer() { var data = new byte[16]; // room for 4 int's using(var map = new MemoryMapStream(new MemoryStream(data))) { // create a fixed-length array with one accessor. var array = new Array<int>(map.CreateInt32(4)); Assert.AreEqual(4, array.Length); array[0] = 0; array[1] = 1; array[2] = 2; array[3] = 3; Assert.AreEqual(0, array[0]); Assert.AreEqual(1, array[1]); Assert.AreEqual(2, array[2]); Assert.AreEqual(3, array[3]); } }
public void ResizeTests() { var randomGenerator = new System.Random(66707770); // make this deterministic using (var map = new MemoryMapStream()) { using (var array = new VariableArray<string>(map, 1024, 1000)) { var arrayExepected = new string[1000]; for (uint i = 0; i < 1000; i++) { if (randomGenerator.Next(4) >= 2) { // add data. arrayExepected[i] = i.ToString(); array[i] = i.ToString(); } else { arrayExepected[i] = int.MaxValue.ToString(); array[i] = int.MaxValue.ToString(); } Assert.AreEqual(arrayExepected[i], array[i]); } Array.Resize<string>(ref arrayExepected, 335); array.Resize(335); Assert.AreEqual(arrayExepected.Length, array.Length); for (int i = 0; i < arrayExepected.Length; i++) { Assert.AreEqual(arrayExepected[i], array[i]); } } using (var array = new VariableArray<string>(map, 1024, 1000)) { var arrayExpected = new string[1000]; for (uint i = 0; i < 1000; i++) { if (randomGenerator.Next(4) >= 1) { // add data. arrayExpected[i] = i.ToString(); array[i] = i.ToString(); } else { arrayExpected[i] = int.MaxValue.ToString(); array[i] = int.MaxValue.ToString(); } Assert.AreEqual(arrayExpected[i], array[i]); } Array.Resize<string>(ref arrayExpected, 1235); var oldSize = array.Length; array.Resize(1235); Assert.AreEqual(arrayExpected.Length, array.Length); for (int i = 0; i < arrayExpected.Length; i++) { Assert.AreEqual(arrayExpected[i], array[i], string.Format("Array element not equal at index: {0}. Expected {1}, found {2}", i, array[i], arrayExpected[i])); } } } }
public void TestSetItem() { using (var map = new MemoryMapStream()) { var list = new List<int>(map); for (var i = 0; i < 10; i++) { list.Add(i + 100); } for (var i = 0; i < 10; i++) { list[i] = i; } for (var i = 0; i < 10; i++) { Assert.AreEqual(i, list[i]); } Assert.Catch<ArgumentOutOfRangeException>(() => { list[11] = 0; }); Assert.Catch<ArgumentOutOfRangeException>(() => { list[-1] = 0; }); Assert.Catch<ArgumentOutOfRangeException>(() => { var t = list[11]; }); Assert.Catch<ArgumentOutOfRangeException>(() => { var t = list[-1]; }); } }
public void TestContainsKey() { using (var map = new MemoryMapStream()) { var dictionary = new Dictionary<string, string>(map); dictionary.Add("Ben", "Abelshausen"); Assert.IsTrue(dictionary.ContainsKey("Ben")); } MockObject.RegisterAccessorCreateFunc(); using (var map = new MemoryMapStream()) { var dictionary = new Dictionary<MockObject, string>(map); Assert.IsFalse(dictionary.ContainsKey(new MockObject("Ben"))); Assert.IsFalse(dictionary.ContainsKey(new MockObject("Ben1"))); dictionary.Add(new MockObject("Ben"), "Abelshausen"); dictionary.Add(new MockObject("Ben1"), "Abelshausen"); dictionary.Add(new MockObject("Ben2"), "Abelshausen"); Assert.IsFalse(dictionary.ContainsKey(new MockObject("Ben3"))); dictionary.Add(new MockObject("Ben3"), "Abelshausen"); dictionary.Add(new MockObject("Ben4"), "Abelshausen"); dictionary.Add(new MockObject("Ben5"), "Abelshausen"); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben"))); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben1"))); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben2"))); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben3"))); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben4"))); Assert.IsTrue(dictionary.ContainsKey(new MockObject("Ben5"))); } }
/// <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]); } } } } }
/// <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); } } }
public void TestContains() { using (var map = new MemoryMapStream()) { var list = new List<int>(map); for (var i = 0; i < 1024; i++) { list.Add(i); } for (var i = 0; i < 1024; i++) { Assert.IsTrue(list.Contains(i)); } Assert.IsFalse(list.Contains(2048)); } }
public void TestCopyTo() { using (var map = new MemoryMapStream()) { var list = new List<int>(map); for (var i = 0; i < 10; i++) { list.Add(i); } var array = new int[20]; list.CopyTo(array, 0); for (var i = 0; i < 10; i++) { Assert.AreEqual(i, array[i]); } list.CopyTo(array, 10); for (var i = 10; i < 20; i++) { Assert.AreEqual(i - 10, array[i]); } } }
public void TestInsert() { using (var map = new MemoryMapStream()) { var list = new List<int>(map); for (var i = 1; i < 10; i++) { list.Add(i); } list.Insert(0, 0); for (var i = 0; i < 10; i++) { Assert.AreEqual(i, list[i]); } } }
public void CompareToArrayWithNoCacheTest() { var randomGenerator = new System.Random(66707770); // make this deterministic using (var map = new MemoryMapStream()) { using (var array = new Array<uint>(map, 1000, ArrayProfile.NoCache)) { var arrayExpected = new uint[1000]; for (uint i = 0; i < 1000; i++) { if (randomGenerator.Next(4) >= 2) { // add data. arrayExpected[i] = i; array[i] = i; } else { arrayExpected[i] = int.MaxValue; array[i] = int.MaxValue; } Assert.AreEqual(arrayExpected[i], array[i]); } for (var i = 0; i < 1000; i++) { Assert.AreEqual(arrayExpected[i], array[i]); } } } }
public void TestRemoveAt() { using (var map = new MemoryMapStream()) { var list = new List<int>(map); for (var i = 0; i < 10; i++) { list.Add(i); } list.Insert(2, 101); list.Insert(3, 104); list.Insert(4, 501); list.Insert(5, 201); list.RemoveAt(2); list.RemoveAt(2); list.RemoveAt(2); list.RemoveAt(2); for (var i = 0; i < 10; i++) { Assert.AreEqual(i, list[i]); } } }
public void CompareToArrayTest() { var randomGenerator = new System.Random(66707770); // make this deterministic using (var map = new MemoryMapStream()) { using (var array = new VariableArray<string>(map, 1024, 1000)) { var arrayExpected = new string[1000]; for (uint i = 0; i < 1000; i++) { if (randomGenerator.Next(4) >= 2) { // add data. arrayExpected[i] = i.ToString(); array[i] = i.ToString(); } else { arrayExpected[i] = int.MaxValue.ToString(); array[i] = int.MaxValue.ToString(); } Assert.AreEqual(arrayExpected[i], array[i]); } for (var i = 0; i < 1000; i++) { Assert.AreEqual(arrayExpected[i], array[i]); } } } }
public void TestEnumerator() { using (var map = new MemoryMapStream()) { var list = new List<int>(map); for (var i = 0; i < 10; i++) { list.Add(i); } var otherList = new System.Collections.Generic.List<int>( list.Where((i) => true)); for (var i = 0; i < 10; i++) { Assert.AreEqual(list[i], otherList[i]); } } }
public void TestIndexOf() { using (var map = new MemoryMapStream()) { var list = new List<int>(map); for (var i = 0; i < 1024; i++) { list.Add(i); } for(var i = 0; i < 1024; i++) { Assert.AreEqual(i, list.IndexOf(i)); } Assert.AreEqual(-1, list.IndexOf(2048)); } }
public void TestIsReadonly() { using (var map = new MemoryMapStream()) { var list = new List<int>(map); Assert.IsFalse(list.IsReadOnly); } }
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)); } } }
public void TestEnumerators() { using (var map = new MemoryMapStream()) { var dictionary = new Dictionary<string, string>(map); dictionary["Ben"] = "Abelshausen"; // use the enumerator to create a new dictionary. var refDictionary = new System.Collections.Generic.Dictionary<string, string>( dictionary); Assert.AreEqual(1, refDictionary.Count); Assert.AreEqual("Abelshausen", dictionary["Ben"]); } MockObject.RegisterAccessorCreateFunc(); using (var map = new MemoryMapStream()) { var dictionary = new Dictionary<string, string>(map); dictionary["Ben1"] = "Abelshausen1"; dictionary["Ben2"] = "Abelshausen2"; dictionary["Ben3"] = "Abelshausen3"; dictionary["Ben4"] = "Abelshausen4"; dictionary["Ben5"] = "Abelshausen5"; // use the enumerator to create a new dictionary. var refDictionary = new System.Collections.Generic.Dictionary<string, string>( dictionary); Assert.AreEqual(5, refDictionary.Count); Assert.AreEqual("Abelshausen1", dictionary["Ben1"]); Assert.AreEqual("Abelshausen2", dictionary["Ben2"]); Assert.AreEqual("Abelshausen3", dictionary["Ben3"]); Assert.AreEqual("Abelshausen4", dictionary["Ben4"]); Assert.AreEqual("Abelshausen5", dictionary["Ben5"]); } }