public void TestBinaryStorage() { Point[] pts = new Point[10000000]; String fileName = GetTempFileName(); Stopwatch watch = Stopwatch.StartNew(); BinaryFileStorage<Point> stor = new BinaryFileStorage<Point>(fileName, pts); watch.Stop(); Trace.WriteLine(String.Format("Time for writing {0} points: {1} milliseconds", pts.Length, watch.ElapsedMilliseconds)); watch.Reset(); watch.Start(); int counter = 0; foreach (Point p in stor) { counter++; } watch.Stop(); Trace.WriteLine(String.Format("Time for reading {0} points: {1} milliseconds", pts.Length, watch.ElapsedMilliseconds)); File.Delete(fileName); }
private static void Main(string[] args) { Console.WriteLine("=== Test Equals ==="); Book romAndJul = new Book("Romeo and Juliet", "William Shakespeare", "tradegy", 171); Book romAndJulCopy = new Book("Romeo and Juliet", "William Shakespeare", "tradegy", 171); Console.WriteLine(romAndJul); Console.WriteLine(romAndJulCopy); Console.WriteLine("Are equals? {0}", romAndJul.Equals(romAndJulCopy) ? "Yes" : "No"); Console.WriteLine(); Book almostRomAndJul = new Book("Almost Romeo and Juliet", "William Shakespeare", "tradegy", 170); Console.WriteLine(romAndJul); Console.WriteLine(almostRomAndJul); Console.WriteLine("Are equals? {0}", romAndJul.Equals(almostRomAndJul) ? "Yes" : "No"); Console.WriteLine("==== end Test Equals ===="); Console.WriteLine(); Console.WriteLine("=== Test GetHashCode ==="); Console.WriteLine("Different books:"); Console.WriteLine($"{romAndJul.GetHashCode()} and {almostRomAndJul.GetHashCode()}"); Console.WriteLine("The same books:"); Console.WriteLine($"{romAndJul.GetHashCode()} and {romAndJulCopy.GetHashCode()}"); Console.WriteLine("=== end Test GetHashCode ==="); Console.WriteLine(); Console.WriteLine("===== Test BookListStorage ====="); Book myBook = new Book("Death Note", "Andrei Khotko", "Detective", 100); Book parasyte = new Book("Parasyte", "Hey Layla", "Horror", 1021); Book elfenLied = new Book("Elfen Lied", "Hayavo Miodzaki", "Comedy", 94); BookListService service = new BookListService(); try { service.AddBook(myBook); service.AddBook(parasyte); service.AddBook(elfenLied); service.AddBook(almostRomAndJul); service.AddBook(romAndJul); service.AddBook(romAndJulCopy); // Exception here } catch (DuplicateBookException ex) { Console.WriteLine($"Error : {ex.Message}"); // temporary substitution of logging, SO SORRY FOR THAT :( //Do logging } Book result = service.FindBookByTag(b => b.Name == "Elfen Lied"); Console.WriteLine($"Result of FindBookByTag Name = ElfnLied : {result}"); PrintService(service, "Before sort"); service.SortBooksByTag(new NameComparer()); PrintService(service, "After sort"); Console.WriteLine("=== end Test BookListStorage ==="); string fileName = "storage.bin"; Console.WriteLine($"Saving storage to file {fileName}..."); IBookListStorage storage = new BinaryFileStorage(fileName); service.SaveStorage(storage); var newService = new BookListService();; newService.LoadStorage(storage); newService.RemoveBook(myBook); PrintService(newService, "New Service"); Console.ReadLine(); }
public void TestBinaryStorage() { //generate some randome points PointF[] pts = new PointF[120]; GCHandle handle = GCHandle.Alloc(pts, GCHandleType.Pinned); using (Matrix<float> ptsMat = new Matrix<float>(pts.Length, 2, handle.AddrOfPinnedObject(), Marshal.SizeOf(typeof(float)) * 2)) { ptsMat.SetRandNormal(new MCvScalar(), new MCvScalar(100)); } handle.Free(); String fileName = Path.Combine(Path.GetTempPath(), "tmp.dat"); Stopwatch watch = Stopwatch.StartNew(); BinaryFileStorage<PointF> stor = new BinaryFileStorage<PointF>(fileName, pts); //BinaryFileStorage<PointF> stor = new BinaryFileStorage<PointF>("abc.data", pts); watch.Stop(); EmguAssert.WriteLine(String.Format("Time for writing {0} points: {1} milliseconds", pts.Length, watch.ElapsedMilliseconds)); int estimatedSize = stor.EstimateSize(); //EmguAssert.IsTrue(pts.Length == estimatedSize); watch.Reset(); watch.Start(); PointF[] pts2 = stor.ToArray(); watch.Stop(); EmguAssert.WriteLine(String.Format("Time for reading {0} points: {1} milliseconds", pts.Length, watch.ElapsedMilliseconds)); if (File.Exists(fileName)) File.Delete(fileName); //EmguAssert.IsTrue(pts.Length == pts2.Length); //Check for equality for (int i = 0; i < pts.Length; i++) { //EmguAssert.IsTrue(pts[i] == pts2[i]); } }
static void Main(string[] args) { Book twelveChairs = new Book("978 - 0810114845", "Ilya Ilf, Evgeny Petrov", "The Twelve Chairs", "Northwestern University Press", 1997, 395, 41.93m); Book gameOfThrones = new Book("978 - 0553593716", "George R. R. Martin", "A Game of Thrones", "Bantam", 2011, 864, 9.49m); Book designPatterns = new Book("978-0596007126", "Eric Freeman, Elisabeth Robson, Bert Bates, Kathy Sierra", "Head First Design Patterns", "O'Reilly Media", 2004, 694, 32.35m); Book danceWithDragons = new Book("978-0553582017", "George R. R. Martin", "A Dance with Dragons", "Bantam", 2013, 1152, 7.59m); Book orwell1984 = new Book("1328869334", "George Orwell", "1984", "Houghton Mifflin Harcourt", 2017, 304, 15.81m); Console.WriteLine("Create service with memory storage"); IStorage memoryStorage = new MemoryStorage(); BookListService bookListServiceMemoryStorage = new BookListService(memoryStorage); Console.WriteLine("Add book \"The Twelve Chairs\""); bookListServiceMemoryStorage.AddBook(twelveChairs); Console.WriteLine("Add book \"A Game of Thrones\""); bookListServiceMemoryStorage.AddBook(gameOfThrones); Console.WriteLine(); Console.WriteLine("Show all books in storage:\n"); bookListServiceMemoryStorage.ShowAll(); Console.WriteLine("Try to add \"The Twelve Chairs\" again"); try { bookListServiceMemoryStorage.AddBook(twelveChairs); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("\n\nCreate service with binary file storage"); IStorage binaryFileStorage = new BinaryFileStorage("books.dat"); BookListService bookListServiceBinaryFileStorage = new BookListService(binaryFileStorage); Console.WriteLine("Add book \"A Game of Thrones\""); bookListServiceBinaryFileStorage.AddBook(gameOfThrones); Console.WriteLine("Add book \"A Dance with Dragons\""); bookListServiceBinaryFileStorage.AddBook(danceWithDragons); Console.WriteLine("Add book \"1984\""); bookListServiceBinaryFileStorage.AddBook(orwell1984); Console.WriteLine("Add book \"Head First Design Patterns\""); bookListServiceBinaryFileStorage.AddBook(designPatterns); Console.WriteLine("\nList of books in binary file storage:\n"); bookListServiceBinaryFileStorage.ShowAll(); Console.WriteLine("\nFind books whose authors contains \"george\"\n"); var list = bookListServiceBinaryFileStorage.FindBookByTag(Tags.Author, "george"); foreach (var book in list) { Console.WriteLine(book); Console.WriteLine(); } Console.WriteLine("\nSort books by number of pages"); bookListServiceBinaryFileStorage.SortBooksByTag(Tags.NumberOfPages); Console.WriteLine("\nSorted list of books:\n"); bookListServiceBinaryFileStorage.ShowAll(); Console.WriteLine("\nRemove \"A Game of Thrones\""); bookListServiceBinaryFileStorage.RemoveBook(gameOfThrones); Console.WriteLine("\nRemove \"A Dance with Dragons\""); bookListServiceBinaryFileStorage.RemoveBook(danceWithDragons); Console.WriteLine("\nList of books:\n"); bookListServiceBinaryFileStorage.ShowAll(); Console.WriteLine("\nRemove \"1984\""); bookListServiceBinaryFileStorage.RemoveBook(orwell1984); Console.WriteLine("\nRemove \"Head First Design Patterns\""); bookListServiceBinaryFileStorage.RemoveBook(designPatterns); Console.ReadLine(); }