public static void AssertAreEqual(FileInfo A, FileInfo B, bool throwException) { Log.WriteLine("TESTING SIMILARITY OF TWO FILES:- " + A.Name + " and " + B.Name); List <string> listA = FileTools.ReadTextFile(A.FullName); List <string> listB = FileTools.ReadTextFile(B.FullName); if (listA.Count != listB.Count) { if (throwException) { throw new Exception("Files do not have the same number of lines: " + listA.Count + " != " + listB.Count); } else { LoggedConsole.WriteLine("#### WARNING: Files do NOT have same number of lines: " + listA.Count + " != " + listB.Count); } } // skip first line for (int i = 1; i < listA.Count; i++) { if (string.Compare(listA[i], listB[i]) != 0) // if (listA[i] != listB[i]) { if (throwException) { throw new Exception("Line " + i + " of files a and b not same: " + listA[i] + " != " + listB[i]); } else { LoggedConsole.WriteLine("#### WARNING: Line " + i + " of files a and b not same: " + listA[i] + " != " + listB[i]); } } } Log.WriteLine("\t\t\t###################### PASS SIMILARITY TEST:- " + A.Name + " and " + B.Name + "\n"); }
/// <summary> /// This test checks two text/csv files to determine if they are the same. /// </summary> public static void FileEqualityTest(string testName, FileInfo testFile, FileInfo benchmarkFile) { LoggedConsole.WriteLine("# FILE EQUALITY TEST: Starting benchmark test for " + testName + ":"); LoggedConsole.WriteLine("# Comparing file <" + testFile.Name + "> with <" + benchmarkFile.Name + ">"); if (!testFile.Exists) { LoggedConsole.WriteWarnLine(" " + testName + " Test File <" + testFile.Name + "> does not exist."); return; } if (!benchmarkFile.Exists) { LoggedConsole.WriteWarnLine(" " + testName + ": the Benchmark File <" + benchmarkFile.Name + "> does not exist."); // check that the benchmark directory exists - if not create it. var benchmarkDir = benchmarkFile.Directory; if (!benchmarkDir.Exists) { LoggedConsole.WriteWarnLine(" Creating Benchmark Directory"); benchmarkDir.Create(); } LoggedConsole.WriteWarnLine(" Writing the Test File as a future Benchmark File"); File.Copy(testFile.FullName, benchmarkFile.FullName, false); return; } var lines1 = FileTools.ReadTextFile(testFile.FullName); var lines2 = FileTools.ReadTextFile(benchmarkFile.FullName); if (lines1.Count == 0) { LoggedConsole.WriteWarnLine(" " + testName + " File1 contains zero lines."); return; } if (lines2.Count == 0) { LoggedConsole.WriteWarnLine(" " + testName + " File2 contains zero lines."); return; } if (lines1.Count != lines2.Count) { LoggedConsole.WriteWarnLine(" " + testName + " The two files do not contain the same number of lines."); LoggedConsole.WriteWarnLine(" line count 1 <" + lines1.Count + "> != line count 2 <" + lines2.Count + ">"); return; } var allOk = true; for (int i = 0; i < lines2.Count; i++) { if (!lines1[i].Equals(lines2[i])) { LoggedConsole.WriteWarnLine($"Line {i}: <{lines1[i]}> != benchmark <{lines2[i]}>"); allOk = false; } } if (allOk) { LoggedConsole.WriteSuccessLine("# SUCCESS! Passed the FILE EQUALITY TEST."); } else { LoggedConsole.WriteWarnLine("# FAILED TEST! FILES ARE NOT THE SAME!"); } LoggedConsole.WriteLine("# Completed benchmark test."); }
public static void WriteData2File(List <Oblong> shapes, string shapesDataFname) { double[,] data = FeatureMatrix(shapes); FileTools.WriteMatrix2File(data, shapesDataFname); }
/// <summary> /// returns a Dictionary of the column values in a csv file with column headings as keys /// ASSUMED to be doubles /// TODO Anthony to use the new U-BEAUT csv file reader. /// </summary> public static Dictionary <string, double[]> ReadCSVFile2Dictionary(string csvFileName) { var lines = FileTools.ReadTextFile(csvFileName); int lineCount = lines.Count; string[] words = lines[0].Split(','); int columnCount = words.Length; //GET the CSV COLUMN HEADINGS var headers = new List <string>(); for (int c = 0; c < columnCount; c++) { headers.Add(words[c]); } //GET the CSV COLUMN HEADINGS //set up the matrix as List of arrays var values = new List <double[]>(); for (int c = 0; c < columnCount; c++) { double[] array = new double[lineCount - 1]; values.Add(array); } //fill the arrays for (int r = 1; r < lineCount; r++) { words = lines[r].Split(','); for (int c = 0; c < columnCount; c++) { var parsed = double.TryParse(words[c], out var d); if (parsed) { values[c][r - 1] = d; } else { parsed = TimeSpan.TryParse(words[c], out var ts); if (parsed) { values[c][r - 1] = ts.TotalSeconds; } else { values[c][r - 1] = double.NaN; } } } } Dictionary <string, double[]> dict = new Dictionary <string, double[]>(); for (int c = 0; c < columnCount; c++) { dict.Add(headers[c], values[c]); } return(dict); }