/// <summary> /// This test checks a score array (array of doubles) against a standard or benchmark previously stored. /// </summary> public static void CompareArrayWithBenchmark(string testName, double[] scoreArray, FileInfo scoreFile) { LoggedConsole.WriteLine("# TESTING: Starting benchmark test for " + testName + ":"); LoggedConsole.WriteLine("# Comparing passed array of double with content of file <" + scoreFile.Name + ">"); bool allOk = true; var scoreLines = FileTools.ReadTextFile(scoreFile.FullName); if (scoreArray.Length != scoreLines.Count) { LoggedConsole.WriteWarnLine(" FAIL! SCORE ARRAY not same length as Benchmark."); return; } for (int i = 0; i < scoreLines.Count; i++) { var str = scoreArray[i].ToString(CultureInfo.InvariantCulture); if (!scoreLines[i].Equals(str)) { LoggedConsole.WriteWarnLine($"Line {i}: {str} NOT= benchmark <{scoreLines[i]}>"); allOk = false; } } if (allOk) { LoggedConsole.WriteSuccessLine(" SUCCESS! Passed the SCORE ARRAY TEST."); } else { LoggedConsole.WriteWarnLine(" FAILED THE SCORE ARRAY TEST"); } LoggedConsole.WriteLine("Completed benchmark test."); }
/// <summary> /// Returns the requested column of data from a CSV file and also returns the column header. /// </summary> public static double[] ReadColumnOfCsvFile(string fileName, int colNumber, out string header) { List <string> lines = FileTools.ReadTextFile(fileName); string[] words = lines[0].Split(','); header = words[colNumber]; // -1 because ignore header double[] array = new double[lines.Count - 1]; // read csv data into arrays. Ignore first line = header. for (int i = 1; i < lines.Count; i++) { words = lines[i].Split(','); if (words.Length <= colNumber) { array[i - 1] = 0.0; LoggedConsole.WriteErrorLine("WARNING: Error while reading line " + i + "of CSV file."); } else { array[i - 1] = double.TryParse(words[colNumber], out var value) ? value : 0.0; } } return(array); }
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); } } for (int i = 1; i < listA.Count; i++) //skip first line { 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"); }
} // DataTable2CSV() /// <summary> /// returns a list of the column values in a csv file plus the column headings /// ASSUMED to be doubles /// returns as lists of type double /// TODO Anthony to use the new U-BEAUT csv file reader. /// </summary> public static Tuple <List <string>, List <double[]> > ReadCSVFile(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 List <double[]> 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++) { double value = 0.0; if (double.TryParse(words[c], out value)) { values[c][r - 1] = value; } else { values[c][r - 1] = 0.0; } } } return(Tuple.Create(headers, values)); }
/// <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."); }
/// <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); }