PageRenderTime 206ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/Bio.TestAutomation/Matrix/PaddedDoubleBvtTestCases.cs

#
C# | 506 lines | 332 code | 82 blank | 92 comment | 15 complexity | 5b04b18af69bb5482fd9ce6933ca6c64 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 PaddedDoubleBvtTestCases
  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 PaddedDoubleBvtTestCases()
  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 PaddedDouble
  47. /// Validation : Col Keys In File
  48. /// </summary>
  49. [TestMethod]
  50. [Priority(0)]
  51. [TestCategory("Priority0")]
  52. public void ValidatePaddedDoubleColKeysInFile()
  53. {
  54. DenseMatrix<string, string, double> denseMatObj =
  55. GetDenseMatrix();
  56. ParallelOptions parOptObj = new ParallelOptions();
  57. denseMatObj.WritePaddedDouble(Constants.FastQTempTxtFileName,
  58. parOptObj);
  59. string[] colkey =
  60. PaddedDouble.ColKeysInFile(Constants.FastQTempTxtFileName);
  61. for (int i = 0; i < colkey.Length; i++)
  62. {
  63. Assert.AreEqual(denseMatObj.ColKeys[i], colkey[i]);
  64. }
  65. if (File.Exists(Constants.FastQTempTxtFileName))
  66. File.Delete(Constants.FastQTempTxtFileName);
  67. Console.WriteLine(
  68. "PaddedDouble BVT : Validation of ColKeysInFile() method successful");
  69. ApplicationLog.WriteLine(
  70. "PaddedDouble BVT : Validation of ColKeysInFile() method successful");
  71. }
  72. /// <summary>
  73. /// Validates CreateEmptyInstance method
  74. /// Input : Valid values for PaddedDouble
  75. /// Validation : create empty instance method
  76. /// </summary>
  77. [TestMethod]
  78. [Priority(0)]
  79. [TestCategory("Priority0")]
  80. public void ValidatePaddedDoubleCreateEmptyInstance()
  81. {
  82. PaddedDouble pdObj =
  83. PaddedDouble.CreateEmptyInstance(
  84. new string[] { "R0", "R1", "R2" },
  85. new string[] { "C0", "C1", "C2", "C3" },
  86. double.NaN);
  87. Assert.AreEqual("3", pdObj.RowCount.ToString((IFormatProvider)null));
  88. Assert.AreEqual("4", pdObj.ColCount.ToString((IFormatProvider)null));
  89. Console.WriteLine(
  90. "PaddedDouble BVT : Validation of CreateEmptyInstance() method successful");
  91. ApplicationLog.WriteLine(
  92. "PaddedDouble BVT : Validation of CreateEmptyInstance() method successful");
  93. }
  94. /// <summary>
  95. /// Validates EachSparseLine method
  96. /// Input : Valid values for PaddedDouble
  97. /// Validation : Each Sparse Line method
  98. /// </summary>
  99. [TestMethod]
  100. [Priority(0)]
  101. [TestCategory("Priority0")]
  102. public void ValidatePaddedDoubleEachSparseLine()
  103. {
  104. DenseMatrix<string, string, double> denseMatObj =
  105. GetDenseMatrix();
  106. ParallelOptions parOptObj = new ParallelOptions();
  107. denseMatObj.WritePaddedDouble(Constants.FastQTempTxtFileName,
  108. parOptObj);
  109. IEnumerable<string[]> sparseLineObjs =
  110. PaddedDouble.EachSparseLine(Constants.FastQTempTxtFileName,
  111. true, "Null File", new CounterWithMessages("Counter", 10, 20));
  112. foreach (string[] sparseLineObj in sparseLineObjs)
  113. {
  114. Assert.IsTrue(denseMatObj.RowKeys.Contains(sparseLineObj[0]));
  115. Assert.IsTrue(denseMatObj.ColKeys.Contains(sparseLineObj[1]));
  116. }
  117. Console.WriteLine(
  118. "PaddedDouble BVT : Validation of EachSparseLine() method successful");
  119. ApplicationLog.WriteLine(
  120. "PaddedDouble BVT : Validation of EachSparseLine() method successful");
  121. }
  122. /// <summary>
  123. /// Validates GetInstance method
  124. /// Input : Valid values for PaddedDouble
  125. /// Validation : gets instance
  126. /// </summary>
  127. [TestMethod]
  128. [Priority(0)]
  129. [TestCategory("Priority0")]
  130. public void ValidatePaddedDoubleGetInstance()
  131. {
  132. DenseMatrix<string, string, double> denseMatObj =
  133. GetDenseMatrix();
  134. ParallelOptions parOptObj = new ParallelOptions();
  135. denseMatObj.WritePaddedDouble(Constants.FastQTempTxtFileName, parOptObj);
  136. PaddedDouble pdObj =
  137. PaddedDouble.GetInstance(Constants.FastQTempTxtFileName, parOptObj);
  138. Assert.AreEqual(denseMatObj.ColCount, pdObj.ColCount);
  139. Assert.AreEqual(denseMatObj.RowCount, pdObj.RowCount);
  140. Assert.AreEqual(denseMatObj.RowKeys.Count, pdObj.RowKeys.Count);
  141. Assert.AreEqual(denseMatObj.ColKeys.Count, pdObj.ColKeys.Count);
  142. if (File.Exists(Constants.FastQTempTxtFileName))
  143. File.Delete(Constants.FastQTempTxtFileName);
  144. Console.WriteLine(
  145. "PaddedDouble BVT : Validation of GetInstance() method successful");
  146. ApplicationLog.WriteLine(
  147. "PaddedDouble BVT : Validation of GetInstance() method successful");
  148. }
  149. /// <summary>
  150. /// Validates GetInstanceFromSparse method
  151. /// Input : Valid values for PaddedDouble
  152. /// Validation : gets instance from sparse
  153. /// </summary>
  154. [TestMethod]
  155. [Priority(0)]
  156. [TestCategory("Priority0")]
  157. public void ValidatePaddedDoubleGetInstanceFromSparse()
  158. {
  159. SparseMatrix<string, string, double> sparseMatObj =
  160. CreateSimpleSparseMatrix();
  161. sparseMatObj.WriteSparse(Constants.FastQTempTxtFileName);
  162. PaddedDouble pdObj =
  163. PaddedDouble.GetInstanceFromSparse(Constants.FastQTempTxtFileName);
  164. Assert.AreEqual(sparseMatObj.ColCount, pdObj.ColCount);
  165. Assert.AreEqual(sparseMatObj.RowCount, pdObj.RowCount);
  166. Assert.AreEqual(sparseMatObj.RowKeys.Count, pdObj.RowKeys.Count);
  167. Assert.AreEqual(sparseMatObj.ColKeys.Count, pdObj.ColKeys.Count);
  168. if (File.Exists(Constants.FastQTempTxtFileName))
  169. File.Delete(Constants.FastQTempTxtFileName);
  170. Console.WriteLine(
  171. "PaddedDouble BVT : Validation of GetInstanceFromSparse() method successful");
  172. ApplicationLog.WriteLine(
  173. "PaddedDouble BVT : Validation of GetInstanceFromSparse() method successful");
  174. }
  175. /// <summary>
  176. /// Validates GetInstanceFromSparse method with enumerable
  177. /// Input : Valid values for PaddedDouble
  178. /// Validation : gets instance from sparse with enumerable
  179. /// </summary>
  180. [TestMethod]
  181. [Priority(0)]
  182. [TestCategory("Priority0")]
  183. public void ValidatePaddedDoubleGetInstanceFromSparseEnum()
  184. {
  185. RowKeyColKeyValue<string, string, double> rowKeyObj =
  186. new RowKeyColKeyValue<string, string, double>("R0", "C0", 2);
  187. List<RowKeyColKeyValue<string, string, double>> enumObj =
  188. new List<RowKeyColKeyValue<string, string, double>>();
  189. enumObj.Add(rowKeyObj);
  190. PaddedDouble pdObj =
  191. PaddedDouble.GetInstanceFromSparse(enumObj);
  192. Assert.AreEqual(1, pdObj.ColCount);
  193. Assert.AreEqual(1, pdObj.RowCount);
  194. Assert.AreEqual("R0", pdObj.RowKeys[0]);
  195. Assert.AreEqual("C0", pdObj.ColKeys[0]);
  196. Console.WriteLine(
  197. "PaddedDouble BVT : Validation of GetInstanceFromSparse(enum) method successful");
  198. ApplicationLog.WriteLine(
  199. "PaddedDouble BVT : Validation of GetInstanceFromSparse(enum) method successful");
  200. }
  201. /// <summary>
  202. /// Validates RowKeysInFile method
  203. /// Input : Valid values for PaddedDouble
  204. /// Validation : Row Keys In File
  205. /// </summary>
  206. [TestMethod]
  207. [Priority(0)]
  208. [TestCategory("Priority0")]
  209. public void ValidatePaddedDoubleRowKeysInFile()
  210. {
  211. DenseMatrix<string, string, double> denseMatObj =
  212. GetDenseMatrix();
  213. ParallelOptions parOptObj = new ParallelOptions();
  214. denseMatObj.WritePaddedDouble(Constants.FastQTempTxtFileName,
  215. parOptObj);
  216. IEnumerable<string> rowKeys =
  217. PaddedDouble.RowKeysInFile(Constants.FastQTempTxtFileName);
  218. int i = 0;
  219. foreach (string rowKey in rowKeys)
  220. {
  221. Assert.AreEqual(denseMatObj.RowKeys[i], rowKey);
  222. i++;
  223. }
  224. if (File.Exists(Constants.FastQTempTxtFileName))
  225. File.Delete(Constants.FastQTempTxtFileName);
  226. Console.WriteLine(
  227. "PaddedDouble BVT : Validation of RowKeysInFile() method successful");
  228. ApplicationLog.WriteLine(
  229. "PaddedDouble BVT : Validation of RowKeysInFile() method successful");
  230. }
  231. /// <summary>
  232. /// Validates StoreToSparseVal method
  233. /// Input : Valid values for PaddedDouble
  234. /// Validation : Store To Sparse Val
  235. /// </summary>
  236. [TestMethod]
  237. [Priority(0)]
  238. [TestCategory("Priority0")]
  239. public void ValidatePaddedDoubleStoreToSparseVal()
  240. {
  241. string sparseString = PaddedDouble.StoreToSparseVal(2);
  242. Assert.AreEqual(" 2", sparseString);
  243. Console.WriteLine(
  244. "PaddedDouble BVT : Validation of StoreToSparseVal() method successful");
  245. ApplicationLog.WriteLine(
  246. "PaddedDouble BVT : Validation of StoreToSparseVal() method successful");
  247. }
  248. /// <summary>
  249. /// Validates TryGetInstance method
  250. /// Input : Valid values for PaddedDouble
  251. /// Validation : Try Get Instance
  252. /// </summary>
  253. [TestMethod]
  254. [Priority(0)]
  255. [TestCategory("Priority0")]
  256. public void ValidatePaddedDoubleTryGetInstance()
  257. {
  258. DenseMatrix<string, string, double> denseMatObj =
  259. GetDenseMatrix();
  260. ParallelOptions parOptObj = new ParallelOptions();
  261. Matrix<string, string, double> matObj = null;
  262. denseMatObj.WritePaddedDouble(Constants.FastQTempTxtFileName,
  263. parOptObj);
  264. Assert.IsTrue(PaddedDouble.TryGetInstance(Constants.FastQTempTxtFileName,
  265. double.NaN, parOptObj, out matObj));
  266. Assert.AreEqual(denseMatObj.ColCount, matObj.ColCount);
  267. Assert.AreEqual(denseMatObj.RowCount, matObj.RowCount);
  268. Assert.AreEqual(denseMatObj.RowKeys.Count, matObj.RowKeys.Count);
  269. Assert.AreEqual(denseMatObj.ColKeys.Count, matObj.ColKeys.Count);
  270. if (File.Exists(Constants.FastQTempTxtFileName))
  271. File.Delete(Constants.FastQTempTxtFileName);
  272. Console.WriteLine(
  273. "PaddedDouble BVT : Validation of TryGetInstance() method successful");
  274. ApplicationLog.WriteLine(
  275. "PaddedDouble BVT : Validation of TryGetInstance() method successful");
  276. }
  277. /// <summary>
  278. /// Validates TryGetInstanceFromSparse method with PD
  279. /// Input : Valid values for PaddedDouble
  280. /// Validation : Try Get Instance from sparse with PD
  281. /// </summary>
  282. [TestMethod]
  283. [Priority(0)]
  284. [TestCategory("Priority0")]
  285. public void ValidatePaddedDoubleTryGetInstanceFromSparsePD()
  286. {
  287. SparseMatrix<string, string, double> sparseMatObj =
  288. CreateSimpleSparseMatrix();
  289. PaddedDouble padDoubObj = null;
  290. sparseMatObj.WriteSparse(Constants.FastQTempTxtFileName);
  291. Assert.IsTrue(PaddedDouble.TryGetInstanceFromSparse(
  292. Constants.FastQTempTxtFileName, out padDoubObj));
  293. Assert.AreEqual(sparseMatObj.ColCount, padDoubObj.ColCount);
  294. Assert.AreEqual(sparseMatObj.RowCount, padDoubObj.RowCount);
  295. Assert.AreEqual(sparseMatObj.RowKeys.Count, padDoubObj.RowKeys.Count);
  296. Assert.AreEqual(sparseMatObj.ColKeys.Count, padDoubObj.ColKeys.Count);
  297. if (File.Exists(Constants.FastQTempTxtFileName))
  298. File.Delete(Constants.FastQTempTxtFileName);
  299. Console.WriteLine(
  300. "PaddedDouble BVT : Validation of TryGetInstanceFromSparse(padded double) method successful");
  301. ApplicationLog.WriteLine(
  302. "PaddedDouble BVT : Validation of TryGetInstanceFromSparse(padded double) method successful");
  303. }
  304. /// <summary>
  305. /// Validates TryGetInstanceFromSparse method with Matrix
  306. /// Input : Valid values for PaddedDouble
  307. /// Validation : Try Get Instance from sparse with Matrix
  308. /// </summary>
  309. [TestMethod]
  310. [Priority(0)]
  311. [TestCategory("Priority0")]
  312. public void ValidatePaddedDoubleTryGetInstanceFromSparseMatrix()
  313. {
  314. SparseMatrix<string, string, double> sparseMatObj =
  315. CreateSimpleSparseMatrix();
  316. Matrix<string, string, double> matObj = null;
  317. sparseMatObj.WriteSparse(Constants.FastQTempTxtFileName);
  318. Assert.IsTrue(PaddedDouble.TryGetInstanceFromSparse(
  319. Constants.FastQTempTxtFileName, out matObj));
  320. Assert.AreEqual(sparseMatObj.ColCount, matObj.ColCount);
  321. Assert.AreEqual(sparseMatObj.RowCount, matObj.RowCount);
  322. Assert.AreEqual(sparseMatObj.RowKeys.Count, matObj.RowKeys.Count);
  323. Assert.AreEqual(sparseMatObj.ColKeys.Count, matObj.ColKeys.Count);
  324. if (File.Exists(Constants.FastQTempTxtFileName))
  325. File.Delete(Constants.FastQTempTxtFileName);
  326. Console.WriteLine(
  327. "PaddedDouble BVT : Validation of TryGetInstanceFromSparse(matrix) method successful");
  328. ApplicationLog.WriteLine(
  329. "PaddedDouble BVT : Validation of TryGetInstanceFromSparse(matrix) method successful");
  330. }
  331. #endregion
  332. #region Helper Methods
  333. /// <summary>
  334. /// Creates a simple matrix for local validation
  335. /// </summary>
  336. /// <returns>Dense Matrix</returns>
  337. static SparseMatrix<string, string, double> CreateSimpleSparseMatrix()
  338. {
  339. SparseMatrix<string, string, double> sparseMatObj =
  340. SparseMatrix<string, string, double>.CreateEmptyInstance(
  341. new string[] { "R0", "R1", "R2" }, new string[] { "C0", "C1", "C2" }, double.NaN);
  342. sparseMatObj[0, 0] = 1;
  343. sparseMatObj[0, 1] = 2;
  344. sparseMatObj[0, 2] = 3;
  345. sparseMatObj[1, 0] = 4;
  346. sparseMatObj[1, 1] = 5;
  347. sparseMatObj[1, 2] = 6;
  348. sparseMatObj[2, 0] = 7;
  349. sparseMatObj[2, 1] = 8;
  350. sparseMatObj[2, 2] = 9;
  351. return sparseMatObj;
  352. }
  353. /// <summary>
  354. /// Gets the two D array from the xml
  355. /// </summary>
  356. /// <param name="nodeName">Node Name of the xml to be parsed</param>
  357. /// <param name="maxRows">Maximum rows</param>
  358. /// <param name="maxColumns">Maximum columns</param>
  359. /// <returns>2 D Array</returns>
  360. double[,] GetTwoDArray(string nodeName, out int maxRows,
  361. out int maxColumns)
  362. {
  363. string[] rowArray = utilityObj.xmlUtil.GetTextValues(nodeName, Constants.RowsNode);
  364. // Gets the max number columns in the array
  365. maxColumns = 0;
  366. maxRows = rowArray.Length;
  367. for (int i = 0; i < maxRows; i++)
  368. {
  369. string[] colArray = rowArray[i].Split(',');
  370. if (maxColumns < colArray.Length)
  371. maxColumns = colArray.Length;
  372. }
  373. // Creates a 2 D with max row and column length
  374. double[,] twoDArray = new double[maxRows, maxColumns];
  375. for (int i = 0; i < maxRows; i++)
  376. {
  377. string[] colArray = rowArray[i].Split(',');
  378. for (int j = 0; j < colArray.Length; j++)
  379. {
  380. twoDArray[i, j] = double.Parse(colArray[j], (IFormatProvider)null);
  381. }
  382. }
  383. return twoDArray;
  384. }
  385. /// <summary>
  386. /// Gets the key sequence with the max length specified
  387. /// </summary>
  388. /// <param name="maxKey">Max length of the key sequence</param>
  389. /// <param name="isRow">If Row, append R else append C</param>
  390. /// <returns>Key Sequence Array</returns>
  391. static string[] GetKeySequence(int maxKey, bool isRow)
  392. {
  393. string[] keySeq = new string[maxKey];
  394. string tempSeq = string.Empty;
  395. if (isRow)
  396. tempSeq = "R";
  397. else
  398. tempSeq = "C";
  399. for (int i = 0; i < maxKey; i++)
  400. {
  401. keySeq[i] = tempSeq + i.ToString((IFormatProvider)null);
  402. }
  403. return keySeq;
  404. }
  405. /// <summary>
  406. /// Creates a DenseMatrix instance and returns the same.
  407. /// </summary>
  408. /// <returns>DenseMatrix Instance</returns>
  409. DenseMatrix<string, string, double> GetDenseMatrix()
  410. {
  411. int maxRows = 0;
  412. int maxColumns = 0;
  413. double[,] twoDArray = GetTwoDArray(Constants.SimpleMatrixNodeName,
  414. out maxRows, out maxColumns);
  415. string[] rowKeySeq = GetKeySequence(maxRows, true);
  416. string[] colKeySeq = GetKeySequence(maxColumns, false);
  417. DenseMatrix<string, string, double> denseMatrixObj =
  418. new DenseMatrix<string, string, double>(twoDArray, rowKeySeq,
  419. colKeySeq, double.NaN);
  420. return denseMatrixObj;
  421. }
  422. #endregion;
  423. }
  424. }