PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Tests/Bio.TestAutomation/BioUtil/ArgumentParser/CmdLineArgumentParserBvtTestCases.cs

#
C# | 250 lines | 161 code | 27 blank | 62 comment | 3 complexity | 0e2bd7e81cf08f32341b2705c0519657 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. /****************************************************************************
  10. * CmdLineArgumentParserBvtTestCases.cs
  11. *
  12. * This file contains the CmdLineArgumentParser BVT test cases.
  13. *
  14. ******************************************************************************/
  15. using System;
  16. using System.IO;
  17. using System.Text;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using Microsoft.VisualStudio.TestTools.UnitTesting;
  21. using Bio.Util.ArgumentParser;
  22. using System.Threading;
  23. using System.Collections;
  24. namespace Bio.TestAutomation.Util.ArgumentParser
  25. {
  26. /// <summary>
  27. /// BVT Test Cases for CmdLineArgumentParser class
  28. /// </summary>
  29. [TestClass]
  30. public class CmdLineArgumentParserBvtTestCases
  31. {
  32. #region BVT Test Cases
  33. /// <summary>
  34. /// Validates CommandLineArguments
  35. /// </summary>
  36. [TestMethod]
  37. [Priority(0)]
  38. [TestCategory("Priority0")]
  39. public void ValidateCommandLineArguments()
  40. {
  41. AggregateNumbers aggregateNos = new AggregateNumbers();
  42. CommandLineArguments parser = new CommandLineArguments();
  43. //add parameters
  44. parser.Parameter(ArgumentType.Required, "InputFile", ArgumentValueType.String, "i", "File containing numbers to add");
  45. parser.Parameter(ArgumentType.Required, "ResultFile", ArgumentValueType.String, "r", "File to store output");
  46. string inputfileName = Path.GetTempFileName().Replace(Path.GetTempPath(), "");
  47. string outputfileName = Path.GetTempFileName().Replace(Path.GetTempPath(), "");
  48. string[] args = { "/InputFile:" + inputfileName , "/ResultFile:" + outputfileName };
  49. parser.Parse(args, aggregateNos);
  50. Assert.IsNotNull(parser);
  51. Assert.IsTrue(aggregateNos.InputFile.Contains(inputfileName));
  52. Assert.IsTrue(aggregateNos.ResultFile.Contains(outputfileName));
  53. }
  54. /// <summary>
  55. /// Validates GetEnumerator
  56. /// </summary>
  57. [TestMethod]
  58. [Priority(0)]
  59. [TestCategory("Priority0")]
  60. public void ValidateGetEnumerator()
  61. {
  62. AggregateNumbers aggregateNos = new AggregateNumbers();
  63. CommandLineArguments parser = new CommandLineArguments();
  64. //add parameters
  65. parser.Parameter(ArgumentType.Required, "InputFile", ArgumentValueType.String, "i", "File containing numbers to add");
  66. parser.Parameter(ArgumentType.Required, "ResultFile", ArgumentValueType.String, "r", "File to store output");
  67. string inputfileName = Path.GetTempFileName().Replace(Path.GetTempPath(), "");
  68. string outputfileName = Path.GetTempFileName().Replace(Path.GetTempPath(), "");
  69. string[] args = { "/InputFile:" + inputfileName, "/ResultFile:" + outputfileName };
  70. parser.Parse(args, aggregateNos);
  71. IEnumerator parsedVals = parser.GetEnumerator();
  72. Assert.IsNotNull(parsedVals);
  73. string current = string.Empty;
  74. int count = 0;
  75. parser.Reset();
  76. while (parser.MoveNext())
  77. {
  78. current = parser.Current.ToString();
  79. Console.WriteLine(current);
  80. count++;
  81. }
  82. Assert.AreEqual(2, count);
  83. }
  84. /// <summary>
  85. /// Validates types in Parameter
  86. /// </summary>
  87. [TestMethod]
  88. [Priority(0)]
  89. [TestCategory("Priority0")]
  90. public void ValidateTypesInParameter()
  91. {
  92. AggregateNumbers aggregateNos = new AggregateNumbers();
  93. CommandLineArguments parser = new CommandLineArguments();
  94. //add parameters
  95. parser.Parameter(ArgumentType.Required, "bValue", ArgumentValueType.Bool, "bv", "bool");
  96. parser.Parameter(ArgumentType.Required, "iValue", ArgumentValueType.Int, "iv", "int");
  97. parser.Parameter(ArgumentType.Required, "iArrValues", ArgumentValueType.MultipleInts, "imv", "intArrValues");
  98. parser.Parameter(ArgumentType.Required, "bArrValues", ArgumentValueType.Bool, "bmv", "boolArrValues");
  99. parser.Parameter(ArgumentType.Required, "fValue", ArgumentValueType.Int, "fv", "float");
  100. parser.Parameter(ArgumentType.Required, "dValue", ArgumentValueType.Int, "dv", "double");
  101. parser.Parameter(ArgumentType.Required, "ubValues", ArgumentValueType.MultipleUniqueBool, "ubv", "Unique bool");
  102. parser.Parameter(ArgumentType.Optional, "usValues", ArgumentValueType.MultipleUniqueStrings, "usv", "Unique strings");
  103. string[] args = { "/bValue:true", "/iValue:5", "/iArrValues:1", "2", "3", "4", "/bArrValues:true" ,"false",
  104. "/fValue:3.45", "/dValue:78.9876","/ubValues:true", "false", "/usValues:Str1", "Str2","Str3"};
  105. parser.Parse(args, aggregateNos);
  106. Assert.IsNotNull(parser);
  107. }
  108. #endregion BVT Test Cases
  109. }
  110. #region Helper Classes and Interfaces
  111. /// <summary>
  112. /// Test interface to use in BVT test cases
  113. /// </summary>
  114. public interface IBinaryOperator
  115. {
  116. double Aggregate(double x, double y);
  117. }
  118. /// <summary>
  119. /// Test class to use in BVT test cases
  120. /// </summary>
  121. public class Sum : IBinaryOperator
  122. {
  123. public double Aggregate(double x, double y)
  124. {
  125. return x + y;
  126. }
  127. }
  128. /// <summary>
  129. /// Test class to use in BVT test cases
  130. /// </summary>
  131. public class Product : IBinaryOperator
  132. {
  133. public double Aggregate(double x, double y)
  134. {
  135. return x * y;
  136. }
  137. }
  138. /// <summary>
  139. /// Test class to use in BVT test cases
  140. /// </summary>
  141. public class Min : IBinaryOperator
  142. {
  143. public double Aggregate(double x, double y)
  144. {
  145. return Math.Min(x, y);
  146. }
  147. }
  148. /// <summary>
  149. /// Test class to use in BVT test cases
  150. /// </summary>
  151. public class Max : IBinaryOperator
  152. {
  153. public double Aggregate(double x, double y)
  154. {
  155. return Math.Max(x, y);
  156. }
  157. }
  158. /// <summary>
  159. /// Example distributable application that take a list of numbers from an input file and performs some aggregate operation. The result is stored in a Result file.
  160. /// </summary>
  161. public class AggregateNumbers
  162. {
  163. /// <summary>
  164. /// The input file must contain a list of doubles, comma delimited.
  165. /// </summary>
  166. public string InputFile;
  167. /// <summary>
  168. /// The operation you want done.
  169. /// </summary>
  170. //[Parse(ParseAction.Required)]
  171. private Sum Operator = new Sum();
  172. /// <summary>
  173. /// Where the results will live.
  174. /// </summary>
  175. public string ResultFile;
  176. /// <summary>
  177. /// How long to sleep between each step.
  178. /// </summary>
  179. private int SleepMilliseconds = 1000;
  180. //fields to check different types of values;
  181. public bool bValue;
  182. public float fValue;
  183. public int iValue;
  184. public double dValue;
  185. public int[] iArrValues;
  186. public bool[] bArrValues;
  187. //for unique values in array test
  188. public bool[] ubValues;
  189. public string[] usValues;
  190. public void RunTasks()
  191. {
  192. string[] nos;
  193. using (StreamReader reader = new StreamReader(InputFile))
  194. {
  195. nos = reader.ReadLine().Split(',');
  196. }
  197. double myTotal = double.NaN;
  198. bool isFirst = true;
  199. foreach (var num in nos)
  200. {
  201. double number = double.Parse(num, (IFormatProvider)null);
  202. Thread.Sleep(SleepMilliseconds); //make the work take longer
  203. if (isFirst)
  204. {
  205. myTotal = number;
  206. isFirst = false;
  207. }
  208. else
  209. {
  210. myTotal = Operator.Aggregate(myTotal, number);
  211. }
  212. }
  213. if (!isFirst) // make sure we actually did something.
  214. {
  215. using (TextWriter writer = File.CreateText(ResultFile))
  216. {
  217. writer.WriteLine(myTotal);
  218. }
  219. }
  220. }
  221. }
  222. #endregion Helper Classes and Interfaces
  223. }