PageRenderTime 33ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Bio.TestAutomation/Matrix/DenseAnsiBvtTestCases.cs

#
C# | 478 lines | 314 code | 77 blank | 87 comment | 17 complexity | 106280c6366859140a4832c19143b57d MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CPL-1.0
  1. // *****************************************************************
  2. // Copyright (c) Microsoft. All rights reserved.
  3. // This code is licensed under the Apache License, Version 2.0.
  4. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  5. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  6. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  7. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  8. // *****************************************************************
  9. using System;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. using System.Collections.Generic;
  14. using Bio.Util;
  15. using Bio.Util.Logging;
  16. using Bio.Matrix;
  17. using Bio.TestAutomation.Util;
  18. using Microsoft.VisualStudio.TestTools.UnitTesting;
  19. namespace Bio.TestAutomation.Matrix
  20. {
  21. /// <summary>
  22. /// Bvt test cases to confirm the features of Dense Matrix
  23. /// </summary>
  24. [TestClass]
  25. public class DenseAnsiBvtTestCases
  26. {
  27. #region Global Variables
  28. Utility utilityObj = new Utility(@"TestUtils\MatrixTestsConfig.xml");
  29. #endregion Global Variables
  30. #region Constructor
  31. /// <summary>
  32. /// Static constructor to open log and make other settings needed for test
  33. /// </summary>
  34. static DenseAnsiBvtTestCases()
  35. {
  36. Trace.Set(Trace.SeqWarnings);
  37. if (!ApplicationLog.Ready)
  38. {
  39. ApplicationLog.Open("bio.automation.log");
  40. }
  41. }
  42. #endregion
  43. #region Test Cases
  44. /// <summary>
  45. /// Validates ColKeysInFile method
  46. /// Input : Valid values for DenseAnsi
  47. /// Validation : Col Keys In File
  48. /// </summary>
  49. [TestMethod]
  50. [Priority(0)]
  51. [TestCategory("Priority0")]
  52. public void ValidateDenseAnsiColKeysInFile()
  53. {
  54. DenseMatrix<string, string, double> denseMatObj =
  55. GetDenseMatrix();
  56. ParallelOptions parOptObj = new ParallelOptions();
  57. denseMatObj.WriteDenseAnsi(Constants.FastQTempTxtFileName, parOptObj);
  58. string[] colkey =
  59. DenseAnsi.ColKeysInFile(Constants.FastQTempTxtFileName);
  60. for (int i = 0; i < colkey.Length; i++)
  61. {
  62. Assert.AreEqual(denseMatObj.ColKeys[i], colkey[i]);
  63. }
  64. if (File.Exists(Constants.FastQTempTxtFileName))
  65. File.Delete(Constants.FastQTempTxtFileName);
  66. Console.WriteLine(
  67. "DenseAnsi BVT : Validation of ColKeysInFile() method successful");
  68. ApplicationLog.WriteLine(
  69. "DenseAnsi BVT : Validation of ColKeysInFile() method successful");
  70. }
  71. /// <summary>
  72. /// Validates CreateEmptyInstance method
  73. /// Input : Valid values for DenseAnsi
  74. /// Validation : create empty instance method
  75. /// </summary>
  76. [TestMethod]
  77. [Priority(0)]
  78. [TestCategory("Priority0")]
  79. public void ValidateDenseAnsiCreateEmptyInstance()
  80. {
  81. DenseAnsi dpaObj =
  82. DenseAnsi.CreateEmptyInstance(
  83. new string[] { "R0", "R1", "R2" },
  84. new string[] { "C0", "C1", "C2", "C3" },
  85. '?');
  86. Assert.IsNotNull(dpaObj);
  87. Console.WriteLine(
  88. "DenseAnsi BVT : Validation of CreateEmptyInstance() method successful");
  89. ApplicationLog.WriteLine(
  90. "DenseAnsi BVT : Validation of CreateEmptyInstance() method successful");
  91. }
  92. /// <summary>
  93. /// Validates GetInstance method
  94. /// Input : Valid values for DenseAnsi
  95. /// Validation : gets instance
  96. /// </summary>
  97. [TestMethod]
  98. [Priority(0)]
  99. [TestCategory("Priority0")]
  100. public void ValidateDenseAnsiGetInstance()
  101. {
  102. DenseMatrix<string, string, double> denseMatObj =
  103. GetDenseMatrix();
  104. ParallelOptions parOptObj = new ParallelOptions();
  105. denseMatObj.WriteDenseAnsi(Constants.FastQTempTxtFileName, parOptObj);
  106. DenseAnsi dpaObj =
  107. DenseAnsi.GetInstance(Constants.FastQTempTxtFileName, parOptObj);
  108. Assert.AreEqual(denseMatObj.ColCount, dpaObj.ColCount);
  109. Assert.AreEqual(denseMatObj.RowCount, dpaObj.RowCount);
  110. Assert.AreEqual(denseMatObj.RowKeys.Count, dpaObj.RowKeys.Count);
  111. Assert.AreEqual(denseMatObj.ColKeys.Count, dpaObj.ColKeys.Count);
  112. if (File.Exists(Constants.FastQTempTxtFileName))
  113. File.Delete(Constants.FastQTempTxtFileName);
  114. Console.WriteLine(
  115. "DenseAnsi BVT : Validation of GetInstance() method successful");
  116. ApplicationLog.WriteLine(
  117. "DenseAnsi BVT : Validation of GetInstance() method successful");
  118. }
  119. /// <summary>
  120. /// Validates GetInstanceFromSparse method
  121. /// Input : Valid values for DenseAnsi
  122. /// Validation : gets instance from sparse
  123. /// </summary>
  124. [TestMethod]
  125. [Priority(0)]
  126. [TestCategory("Priority0")]
  127. public void ValidateDenseAnsiGetInstanceFromSparse()
  128. {
  129. DenseMatrix<string, string, double> denseMatObj =
  130. CreateSimpleDenseMatrix();
  131. denseMatObj.WriteSparse(Constants.FastQTempTxtFileName);
  132. DenseAnsi dpaObj =
  133. DenseAnsi.GetInstanceFromSparse(Constants.FastQTempTxtFileName);
  134. Assert.AreEqual(denseMatObj.ColCount, dpaObj.ColCount);
  135. Assert.AreEqual(denseMatObj.RowCount, dpaObj.RowCount);
  136. Assert.AreEqual(denseMatObj.RowKeys.Count, dpaObj.RowKeys.Count);
  137. Assert.AreEqual(denseMatObj.ColKeys.Count, dpaObj.ColKeys.Count);
  138. if (File.Exists(Constants.FastQTempTxtFileName))
  139. File.Delete(Constants.FastQTempTxtFileName);
  140. Console.WriteLine(
  141. "DenseAnsi BVT : Validation of GetInstanceFromSparse() method successful");
  142. ApplicationLog.WriteLine(
  143. "DenseAnsi BVT : Validation of GetInstanceFromSparse() method successful");
  144. }
  145. /// <summary>
  146. /// Validates RowKeysInFile method
  147. /// Input : Valid values for DenseAnsi
  148. /// Validation : Row Keys In File
  149. /// </summary>
  150. [TestMethod]
  151. [Priority(0)]
  152. [TestCategory("Priority0")]
  153. public void ValidateDenseAnsiRowKeysInFile()
  154. {
  155. DenseMatrix<string, string, double> denseMatObj =
  156. GetDenseMatrix();
  157. ParallelOptions parOptObj = new ParallelOptions();
  158. denseMatObj.WriteDenseAnsi(Constants.FastQTempTxtFileName,
  159. parOptObj);
  160. IEnumerable<string> rowKeys =
  161. DenseAnsi.RowKeysInFile(Constants.FastQTempTxtFileName);
  162. int i = 0;
  163. foreach (string rowKey in rowKeys)
  164. {
  165. Assert.AreEqual(denseMatObj.RowKeys[i], rowKey);
  166. i++;
  167. }
  168. if (File.Exists(Constants.FastQTempTxtFileName))
  169. File.Delete(Constants.FastQTempTxtFileName);
  170. Console.WriteLine(
  171. "DenseAnsi BVT : Validation of RowKeysInFile() method successful");
  172. ApplicationLog.WriteLine(
  173. "DenseAnsi BVT : Validation of RowKeysInFile() method successful");
  174. }
  175. /// <summary>
  176. /// Validates TryGetInstance method
  177. /// Input : Valid values for DenseAnsi
  178. /// Validation : Try Get Instance
  179. /// </summary>
  180. [TestMethod]
  181. [Priority(0)]
  182. [TestCategory("Priority0")]
  183. public void ValidateDenseAnsiTryGetInstance()
  184. {
  185. DenseMatrix<string, string, double> denseMatObj =
  186. CreateSimpleDenseMatrix();
  187. ParallelOptions parOptObj = new ParallelOptions();
  188. Matrix<string, string, char> matObj = null;
  189. denseMatObj.WriteDenseAnsi(Constants.FastQTempTxtFileName,
  190. parOptObj);
  191. Assert.IsTrue(DenseAnsi.TryGetInstance(Constants.FastQTempTxtFileName,
  192. '?', parOptObj, out matObj));
  193. Assert.AreEqual(denseMatObj.ColCount, matObj.ColCount);
  194. Assert.AreEqual(denseMatObj.RowCount, matObj.RowCount);
  195. Assert.AreEqual(denseMatObj.RowKeys.Count, matObj.RowKeys.Count);
  196. Assert.AreEqual(denseMatObj.ColKeys.Count, matObj.ColKeys.Count);
  197. Assert.IsNotNull(DenseAnsi.StaticMissingValue);
  198. Assert.IsNotNull(DenseAnsi.StaticStoreMissingValue);
  199. if (File.Exists(Constants.FastQTempTxtFileName))
  200. File.Delete(Constants.FastQTempTxtFileName);
  201. Console.WriteLine(
  202. "DenseAnsi BVT : Validation of TryGetInstance() method successful");
  203. ApplicationLog.WriteLine(
  204. "DenseAnsi BVT : Validation of TryGetInstance() method successful");
  205. }
  206. /// <summary>
  207. /// Validates TryGetInstanceFromSparse method with Matrix
  208. /// Input : Valid values for DenseAnsi
  209. /// Validation : Try Get Instance from sparse with Matrix
  210. /// </summary>
  211. [TestMethod]
  212. [Priority(0)]
  213. [TestCategory("Priority0")]
  214. public void ValidateDenseAnsiTryGetInstanceFromSparseMatrix()
  215. {
  216. DenseMatrix<string, string, double> denseMatObj =
  217. CreateSimpleDenseMatrix();
  218. Matrix<string, string, char> matObj = null;
  219. denseMatObj.WriteSparse(Constants.FastQTempTxtFileName);
  220. Assert.IsTrue(DenseAnsi.TryGetInstanceFromSparse(
  221. Constants.FastQTempTxtFileName, out matObj));
  222. Assert.AreEqual(denseMatObj.ColCount, matObj.ColCount);
  223. Assert.AreEqual(denseMatObj.RowCount, matObj.RowCount);
  224. Assert.AreEqual(denseMatObj.RowKeys.Count, matObj.RowKeys.Count);
  225. Assert.AreEqual(denseMatObj.ColKeys.Count, matObj.ColKeys.Count);
  226. if (File.Exists(Constants.FastQTempTxtFileName))
  227. File.Delete(Constants.FastQTempTxtFileName);
  228. Console.WriteLine(
  229. "DenseAnsi BVT : Validation of TryGetInstanceFromSparse(Matrix) method successful");
  230. ApplicationLog.WriteLine(
  231. "DenseAnsi BVT : Validation of TryGetInstanceFromSparse(Matrix) method successful");
  232. }
  233. /// <summary>
  234. /// Validates TryGetInstanceFromSparse method with Ansi
  235. /// Input : Valid values for DenseAnsi
  236. /// Validation : Try Get Instance from sparse with Ansi
  237. /// </summary>
  238. [TestMethod]
  239. [Priority(0)]
  240. [TestCategory("Priority0")]
  241. public void ValidateDenseAnsiTryGetInstanceFromSparseAnsi()
  242. {
  243. DenseMatrix<string, string, double> denseMatObj =
  244. CreateSimpleDenseMatrix();
  245. DenseAnsi denseAnsiObj = null;
  246. denseMatObj.WriteSparse(Constants.FastQTempTxtFileName);
  247. Assert.IsTrue(DenseAnsi.TryGetInstanceFromSparse(
  248. Constants.FastQTempTxtFileName, out denseAnsiObj));
  249. Assert.AreEqual(denseMatObj.ColCount, denseAnsiObj.ColCount);
  250. Assert.AreEqual(denseMatObj.RowCount, denseAnsiObj.RowCount);
  251. Assert.AreEqual(denseMatObj.RowKeys.Count, denseAnsiObj.RowKeys.Count);
  252. Assert.AreEqual(denseMatObj.ColKeys.Count, denseAnsiObj.ColKeys.Count);
  253. if (File.Exists(Constants.FastQTempTxtFileName))
  254. File.Delete(Constants.FastQTempTxtFileName);
  255. Console.WriteLine(
  256. "DenseAnsi BVT : Validation of TryGetInstanceFromSparse(denseAnsi) method successful");
  257. ApplicationLog.WriteLine(
  258. "DenseAnsi BVT : Validation of TryGetInstanceFromSparse(denseAnsi) method successful");
  259. }
  260. /// <summary>
  261. /// Validates TryParseDenseAnsiFormatAsDoubleMatrix method
  262. /// Input : Valid values for DenseAnsi
  263. /// Validation : Try Parse DenseAnsi Format As Double Matrix
  264. /// </summary>
  265. [TestMethod]
  266. [Priority(0)]
  267. [TestCategory("Priority0")]
  268. public void ValidateDenseAnsiTryParseDenseAnsiFormatAsDoubleMatrix()
  269. {
  270. DenseMatrix<string, string, double> denseMatObj =
  271. CreateSimpleDenseMatrix();
  272. ParallelOptions parOptObj = new ParallelOptions();
  273. Matrix<string, string, double> matObj = null;
  274. denseMatObj.WriteDenseAnsi(Constants.FastQTempTxtFileName, parOptObj);
  275. Assert.IsTrue(DenseAnsi.TryParseDenseAnsiFormatAsDoubleMatrix(
  276. Constants.FastQTempTxtFileName, double.NaN, parOptObj, out matObj));
  277. Assert.AreEqual(denseMatObj.ColCount, matObj.ColCount);
  278. Assert.AreEqual(denseMatObj.RowCount, matObj.RowCount);
  279. Assert.AreEqual(denseMatObj.RowKeys.Count, matObj.RowKeys.Count);
  280. Assert.AreEqual(denseMatObj.ColKeys.Count, matObj.ColKeys.Count);
  281. if (File.Exists(Constants.FastQTempTxtFileName))
  282. File.Delete(Constants.FastQTempTxtFileName);
  283. Console.WriteLine(
  284. "DenseAnsi BVT : Validation of TryParseDenseAnsiFormatAsDoubleMatrix() method successful");
  285. ApplicationLog.WriteLine(
  286. "DenseAnsi BVT : Validation of TryParseDenseAnsiFormatAsDoubleMatrix() method successful");
  287. }
  288. /// <summary>
  289. /// Validates TryParseDenseAnsiFormatAsGenericMatrix method
  290. /// Input : Valid values for DenseAnsi
  291. /// Validation : Try Parse DenseAnsi Format As Generic Matrix
  292. /// </summary>
  293. [TestMethod]
  294. [Priority(0)]
  295. [TestCategory("Priority0")]
  296. public void ValidateDenseAnsiTryParseDenseAnsiFormatAsGenericMatrix()
  297. {
  298. DenseMatrix<string, string, double> denseMatObj =
  299. CreateSimpleDenseMatrix();
  300. ParallelOptions parOptObj = new ParallelOptions();
  301. Matrix<string, string, double> matObj = null;
  302. denseMatObj.WriteDenseAnsi(Constants.FastQTempTxtFileName, parOptObj);
  303. Assert.IsTrue(DenseAnsi.TryParseDenseAnsiFormatAsGenericMatrix(
  304. Constants.FastQTempTxtFileName, double.NaN, parOptObj, out matObj));
  305. Assert.AreEqual(denseMatObj.ColCount, matObj.ColCount);
  306. Assert.AreEqual(denseMatObj.RowCount, matObj.RowCount);
  307. Assert.AreEqual(denseMatObj.RowKeys.Count, matObj.RowKeys.Count);
  308. Assert.AreEqual(denseMatObj.ColKeys.Count, matObj.ColKeys.Count);
  309. if (File.Exists(Constants.FastQTempTxtFileName))
  310. File.Delete(Constants.FastQTempTxtFileName);
  311. Console.WriteLine(
  312. "DenseAnsi BVT : Validation of TryParseDenseAnsiFormatAsGenericMatrix() method successful");
  313. ApplicationLog.WriteLine(
  314. "DenseAnsi BVT : Validation of TryParseDenseAnsiFormatAsGenericMatrix() method successful");
  315. }
  316. #endregion
  317. #region Helper Methods
  318. /// <summary>
  319. /// Gets the two D array from the xml
  320. /// </summary>
  321. /// <param name="nodeName">Node Name of the xml to be parsed</param>
  322. /// <param name="maxRows">Maximum rows</param>
  323. /// <param name="maxColumns">Maximum columns</param>
  324. /// <returns>2 D Array</returns>
  325. double[,] GetTwoDArray(string nodeName, out int maxRows,
  326. out int maxColumns)
  327. {
  328. string[] rowArray = utilityObj.xmlUtil.GetTextValues(nodeName, Constants.RowsNode);
  329. // Gets the max number columns in the array
  330. maxColumns = 0;
  331. maxRows = rowArray.Length;
  332. for (int i = 0; i < maxRows; i++)
  333. {
  334. string[] colArray = rowArray[i].Split(',');
  335. if (maxColumns < colArray.Length)
  336. maxColumns = colArray.Length;
  337. }
  338. // Creates a 2 D with max row and column length
  339. double[,] twoDArray = new double[maxRows, maxColumns];
  340. for (int i = 0; i < maxRows; i++)
  341. {
  342. string[] colArray = rowArray[i].Split(',');
  343. for (int j = 0; j < colArray.Length; j++)
  344. {
  345. twoDArray[i, j] = double.Parse(colArray[j], (IFormatProvider)null);
  346. }
  347. }
  348. return twoDArray;
  349. }
  350. /// <summary>
  351. /// Creates a simple matrix for local validation
  352. /// </summary>
  353. /// <returns>Dense Matrix</returns>
  354. static DenseMatrix<string, string, double> CreateSimpleDenseMatrix()
  355. {
  356. double[,] twoDArray = new double[,] { { 1, 1, 1, 1 }, { 2, 3, 4, 5 }, { 3, 4, double.NaN, 5 } };
  357. DenseMatrix<string, string, double> denseMatObj =
  358. new DenseMatrix<string, string, double>(twoDArray,
  359. new string[] { "R0", "R1", "R2" }, new string[] { "C0", "C1", "C2", "C3" }, double.NaN);
  360. return denseMatObj;
  361. }
  362. /// <summary>
  363. /// Gets the key sequence with the max length specified
  364. /// </summary>
  365. /// <param name="maxKey">Max length of the key sequence</param>
  366. /// <param name="isRow">If Row, append R else append C</param>
  367. /// <returns>Key Sequence Array</returns>
  368. static string[] GetKeySequence(int maxKey, bool isRow)
  369. {
  370. string[] keySeq = new string[maxKey];
  371. string tempSeq = string.Empty;
  372. if (isRow)
  373. tempSeq = "R";
  374. else
  375. tempSeq = "C";
  376. for (int i = 0; i < maxKey; i++)
  377. {
  378. keySeq[i] = tempSeq + i.ToString((IFormatProvider)null);
  379. }
  380. return keySeq;
  381. }
  382. /// <summary>
  383. /// Creates a DenseAnsi instance and returns the same.
  384. /// </summary>
  385. /// <returns>DenseAnsi Instance</returns>
  386. DenseMatrix<string, string, double> GetDenseMatrix()
  387. {
  388. int maxRows = 0;
  389. int maxColumns = 0;
  390. double[,] twoDArray = GetTwoDArray(Constants.SimpleMatrixNodeName,
  391. out maxRows, out maxColumns);
  392. string[] rowKeySeq = GetKeySequence(maxRows, true);
  393. string[] colKeySeq = GetKeySequence(maxColumns, false);
  394. DenseMatrix<string, string, double> denseMatrixObj =
  395. new DenseMatrix<string, string, double>(twoDArray, rowKeySeq,
  396. colKeySeq, double.NaN);
  397. return denseMatrixObj;
  398. }
  399. #endregion;
  400. }
  401. }