PageRenderTime 24ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.4.0-VSSHFB/Test/TestProduct.cs

#
C# | 502 lines | 382 code | 119 blank | 1 comment | 12 complexity | e2a076b95b45c6ee8b2404e8207eaedd MD5 | raw file
  1. using Kw.Combinatorics;
  2. using System;
  3. using System.Collections.Generic;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. namespace Kw.CombinatoricsTest
  6. {
  7. [TestClass]
  8. public class ProductTest01
  9. {
  10. #region Test constructors
  11. [TestMethod]
  12. public void Test_Interfaces ()
  13. {
  14. Product prod = new Product (new int[] { 2, 3, 4 });
  15. Assert.IsNotNull (prod as ICloneable);
  16. Assert.IsNotNull (prod as IComparable);
  17. Assert.IsNotNull (prod as System.Collections.IEnumerable);
  18. Assert.IsNotNull (prod as IComparable<Product>);
  19. Assert.IsNotNull (prod as IEquatable<Product>);
  20. Assert.IsNotNull (prod as IEnumerable<int>);
  21. }
  22. [TestMethod]
  23. public void Test_Ctor0 ()
  24. {
  25. Product p0 = new Product ();
  26. Assert.AreEqual (0, p0.Height);
  27. Assert.AreEqual (0, p0.Width);
  28. }
  29. [TestMethod]
  30. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  31. public void Test_Ctor1_ArgumentOutOfRangeException ()
  32. {
  33. Product p0 = new Product (new int[] { 2, -4, 3 });
  34. }
  35. [TestMethod]
  36. public void Test_Ctor1 ()
  37. {
  38. int[,] expected2by3 = new int[,] { {0, 0}, { 0, 1 }, { 0, 2 }, { 1, 0 }, {1, 1}, {1, 2} };
  39. Product p0 = new Product (new int[] { 5, 0, 3 });
  40. Assert.AreEqual (0, p0.Height);
  41. Assert.AreEqual (3, p0.Width);
  42. Assert.AreEqual (5, p0.Size (0));
  43. Assert.AreEqual (0, p0.Size (1));
  44. Assert.AreEqual (3, p0.Size (2));
  45. int actualIterations = 0;
  46. foreach (Product px in p0.Rows)
  47. {
  48. ++actualIterations;
  49. }
  50. Assert.AreEqual (0, actualIterations);
  51. Product p3 = new Product (new int[] { 2, 4, 3 });
  52. Assert.AreEqual (2 * 4 * 3, p3.Height);
  53. Product p4 = new Product (new int[0]);
  54. Assert.AreEqual (0, p4.Height);
  55. Assert.AreEqual (0, p4.Width);
  56. }
  57. #endregion
  58. #region Test properties
  59. [TestMethod]
  60. public void Test_RankEmpty ()
  61. {
  62. Product p0 = new Product (new int[] { 5, 0, 3 });
  63. Assert.AreEqual (0, p0.Rank);
  64. p0.Rank = p0.Rank + 1;
  65. Assert.AreEqual (0, p0.Rank);
  66. }
  67. [TestMethod]
  68. public void TestRank ()
  69. {
  70. Product p2 = new Product (new int[] { 3, 4 });
  71. long expectedRank = 5;
  72. p2.Rank = expectedRank;
  73. long actualRank = p2.Rank;
  74. Assert.AreEqual (expectedRank, actualRank);
  75. expectedRank = p2.Height - 1;
  76. p2.Rank = -1;
  77. actualRank = p2.Rank;
  78. Assert.AreEqual (expectedRank, actualRank);
  79. }
  80. [TestMethod]
  81. public void Test_RowsEmpty ()
  82. {
  83. Product p2 = new Product (new int[] { 2, 0, 3 });
  84. int actualCount = 0;
  85. foreach (Product px in p2.Rows)
  86. {
  87. ++actualCount;
  88. }
  89. Assert.AreEqual (0, actualCount);
  90. }
  91. [TestMethod]
  92. public void Test_Rows ()
  93. {
  94. Product p2 = new Product (new int[] { 2, 3 });
  95. int[][] expected = new[]
  96. {
  97. new int[] { 0, 0 }, new int[] { 0, 1 }, new int[] { 0, 2 },
  98. new int[] { 1, 0 }, new int[] { 1, 1 }, new int[] { 1, 2 },
  99. new int[] { 9, 9 }
  100. };
  101. int actualCount = 0;
  102. foreach (Product px in p2.Rows)
  103. {
  104. Assert.AreEqual (actualCount / 3, px[0]);
  105. Assert.AreEqual (actualCount % 3, px[1]);
  106. ++actualCount;
  107. }
  108. Assert.AreEqual (6, actualCount);
  109. }
  110. [TestMethod]
  111. public void Test_Index ()
  112. {
  113. Product p3 = new Product (new int[] { 4, 3, 2 });
  114. p3.Rank = 23;
  115. Assert.AreEqual (3, p3[0]);
  116. Assert.AreEqual (2, p3[1]);
  117. Assert.AreEqual (1, p3[2]);
  118. }
  119. #endregion
  120. #region Test methods
  121. [TestMethod]
  122. public void Test_Clone ()
  123. {
  124. Product p0 = new Product ();
  125. Product p1 = (Product) p0.Clone ();
  126. Assert.AreEqual (p0.Rank, p1.Rank);
  127. }
  128. [TestMethod]
  129. public void Test_CompareToOBJECT ()
  130. {
  131. var objectSortedList = new System.Collections.SortedList ();
  132. Product prod1 = new Product (new int[] { 3, 4, 5 });
  133. Product prod2 = new Product (prod1);
  134. Product prod3 = new Product (prod1);
  135. prod1.Rank = 10;
  136. prod2.Rank = 30;
  137. prod3.Rank = 20;
  138. objectSortedList.Add (prod1, 10);
  139. objectSortedList.Add (prod2, 30);
  140. objectSortedList.Add (prod3, 20);
  141. int expectedValue = 10;
  142. foreach (System.Collections.DictionaryEntry item in objectSortedList)
  143. {
  144. int actualIndex = (int) item.Value;
  145. Assert.AreEqual (expectedValue, actualIndex);
  146. expectedValue += 10;
  147. }
  148. Assert.AreEqual (40, expectedValue);
  149. }
  150. [TestMethod]
  151. public void Test_CompareTo ()
  152. {
  153. Product p0 = null;
  154. Product p16 = new Product (new int[] { 2, 3, 4 }); p16.Rank = 16;
  155. Product p17 = new Product (new int[] { 2, 3, 4 }); p17.Rank = 17;
  156. int actual1 = p16.CompareTo (p0);
  157. Assert.IsTrue (actual1 > 0);
  158. int actual2 = p16.CompareTo (p17);
  159. Assert.IsTrue (actual2 < 0);
  160. }
  161. [TestMethod]
  162. public void Test_Equals_OBJECT ()
  163. {
  164. Product p0 = null;
  165. Product p16a = new Product (new int[] { 2, 3, 4 }); p16a.Rank = 16;
  166. Product p16b = new Product (new int[] { 2, 3, 4 }); p16b.Rank = 16;
  167. Product p17 = new Product (new int[] { 2, 3, 4 }); p17.Rank = 17;
  168. Product q4 = new Product (new int[] { 1, 2, 3, 4 });
  169. object j0 = (object) p0;
  170. object j16b = (object) p16b;
  171. object j17 = (object) p17;
  172. object j4 = (object) q4;
  173. Assert.IsFalse (p16a.Equals (j0));
  174. Assert.IsTrue (p16a.Equals (j16b));
  175. Assert.IsFalse (p16a.Equals (j17));
  176. Assert.IsFalse (p16a.Equals (j4));
  177. }
  178. [TestMethod]
  179. public void Test_Equals ()
  180. {
  181. Product p0 = null;
  182. Product p16a = new Product (new int[] { 2, 3, 4 }); p16a.Rank = 16;
  183. Product p16b = new Product (new int[] { 2, 3, 4 }); p16b.Rank = 16;
  184. Product p17 = new Product (new int[] { 2, 3, 4 }); p17.Rank = 17;
  185. Product q = new Product (new int[] { 1, 2, 3, 4 });
  186. Assert.IsFalse (p16a.Equals (p0));
  187. Assert.IsTrue (p16a.Equals (p16b));
  188. Assert.IsFalse (p16a.Equals (p17));
  189. Assert.IsFalse (p16a.Equals (q));
  190. }
  191. [TestMethod]
  192. public void Test_EqualsOtherType ()
  193. {
  194. Product p234 = new Product (new int[] { 2, 3, 4 });
  195. string s = "Roxy";
  196. // Comparing to different type returns false.
  197. Assert.IsFalse (p234.Equals (s));
  198. }
  199. [TestMethod]
  200. public void Test_GetEnumeratorOBJECT ()
  201. {
  202. Product prod = new Product (new int[] { 2, 3, 4});
  203. prod.Rank = 6;
  204. System.Collections.IEnumerator nu = ((System.Collections.IEnumerable) prod).GetEnumerator ();
  205. int expected = 0;
  206. while (nu.MoveNext ())
  207. {
  208. int actual = (int) nu.Current;
  209. Assert.AreEqual (expected, actual);
  210. ++expected;
  211. }
  212. Assert.AreEqual (prod.Width, expected);
  213. }
  214. [TestMethod]
  215. public void Test_GetEnumerator ()
  216. {
  217. Product prod = new Product (new int[] { 3, 4, 5 });
  218. prod.Rank = 33;
  219. int[] expectedValues = new int[] { 1, 2, 3 };
  220. int index = 0;
  221. foreach (int actualValue in prod)
  222. {
  223. Assert.AreEqual (expectedValues[index], actualValue);
  224. index++;
  225. }
  226. Assert.AreEqual (expectedValues.Length, index);
  227. }
  228. [TestMethod]
  229. public void Test_GetHashCode ()
  230. {
  231. Product p0 = new Product ();
  232. int hash = p0.GetHashCode ();
  233. }
  234. [TestMethod]
  235. [ExpectedException (typeof (ArgumentException))]
  236. public void Test_PermuteArgumentException ()
  237. {
  238. Product p0 = new Product (new int[] { 2, 3 });
  239. object[][] source = new object[][] { new string[] { "A", "B" } };
  240. List<object> j = Product.Permute (p0, source);
  241. }
  242. [TestMethod]
  243. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  244. public void Test_PermuteArgumentOutOfRangeException ()
  245. {
  246. Product p0 = new Product (new int[] { 3 });
  247. p0.Rank = 2;
  248. object[][] source = new object[][] { new string[] { "A", "B" } };
  249. List<object> j = Product.Permute (p0, source);
  250. }
  251. [TestMethod]
  252. public void Test_Permute ()
  253. {
  254. string[] set1 = new string[] { "A", "B", "C" };
  255. string[] set2 = new string[] { "X", "Y" };
  256. object[][] ja = new object[][] { set1, set2 };
  257. string[] expected = new string[] { "AX", "AY", "BX", "BY", "CX", "CY" };
  258. int actualCount = 0;
  259. foreach (Product px in new Product (new int[] { set1.Length, set2.Length }).Rows)
  260. {
  261. string actual = "";
  262. foreach (object j in Product.Permute (px, ja))
  263. actual += j;
  264. Assert.AreEqual (expected[actualCount], actual);
  265. ++actualCount;
  266. }
  267. Assert.AreEqual (expected.Length, actualCount);
  268. }
  269. [TestMethod]
  270. public void Test_ToString0 ()
  271. {
  272. Product prod = new Product (new int[] { });
  273. string actual = prod.ToString ();
  274. Assert.AreEqual ("{ }", actual);
  275. }
  276. [TestMethod]
  277. public void Test_ToString1 ()
  278. {
  279. Product prod = new Product (new int[] { 2 });
  280. prod.Rank = 1;
  281. string actual = prod.ToString ();
  282. Assert.AreEqual ("{ 1 }", actual);
  283. }
  284. [TestMethod]
  285. public void Test_ToString3 ()
  286. {
  287. Product prod = new Product (new int[] { 4, 3, 2 });
  288. prod.Rank = 23;
  289. string actual = prod.ToString ();
  290. Assert.AreEqual ("{ 3, 2, 1 }", actual);
  291. }
  292. #endregion
  293. #region Test static methods
  294. [TestMethod]
  295. public void Test_Comparisons ()
  296. {
  297. int[] dims1 = new int[] { 2, 3 };
  298. Product p0 = null;
  299. Product q0 = null;
  300. Product p1 = new Product (dims1); p1.Rank = 1;
  301. Product p11 = new Product (dims1); p11.Rank = 1;
  302. Product p2 = new Product (dims1); p2.Rank = 2;
  303. Product q4 = new Product (new int[] { 2, 3, 4, 5 }); q4.Rank = 42;
  304. Assert.IsTrue (p0 == q0);
  305. Assert.IsFalse (p0 == p1);
  306. Assert.IsFalse (p1 == p0);
  307. Assert.IsTrue (p1 == p11);
  308. Assert.IsFalse (p1 == p2);
  309. Assert.IsFalse (p0 != q0);
  310. Assert.IsTrue (p0 != p1);
  311. Assert.IsTrue (p1 != p0);
  312. Assert.IsFalse (p1 != p11);
  313. Assert.IsTrue (p1 != p2);
  314. Assert.IsFalse (p0 < q0);
  315. Assert.IsTrue (p0 < p1);
  316. Assert.IsFalse (p1 < p0);
  317. Assert.IsFalse (p1 < p11);
  318. Assert.IsTrue (p1 < p2);
  319. Assert.IsFalse (p2 < p1);
  320. Assert.IsTrue (p0 >= q0);
  321. Assert.IsFalse (p0 >= p1);
  322. Assert.IsTrue (p1 >= p0);
  323. Assert.IsTrue (p1 >= p11);
  324. Assert.IsFalse (p1 >= p2);
  325. Assert.IsTrue (p2 >= p1);
  326. Assert.IsFalse (p0 > q0);
  327. Assert.IsFalse (p0 > p1);
  328. Assert.IsTrue (p1 > p0);
  329. Assert.IsFalse (p1 > p11);
  330. Assert.IsFalse (p1 > p2);
  331. Assert.IsTrue (p2 > p1);
  332. Assert.IsTrue (p0 <= q0);
  333. Assert.IsTrue (p0 <= p1);
  334. Assert.IsFalse (p1 <= p0);
  335. Assert.IsTrue (p1 <= p11);
  336. Assert.IsTrue (p1 <= p2);
  337. Assert.IsFalse (p2 <= p1);
  338. Assert.IsTrue (p2 < q4);
  339. Assert.IsTrue (p2 != q4);
  340. }
  341. [TestMethod]
  342. public void Test_IncrementClass ()
  343. {
  344. Product p1 = new Product (new int[] { 3, 5 });
  345. p1.Rank = 4;
  346. ++p1;
  347. Assert.AreEqual (5, p1.Rank);
  348. p1++;
  349. Assert.AreEqual (6, p1.Rank);
  350. }
  351. [TestMethod]
  352. public void Test_DecrementClass ()
  353. {
  354. Product p1 = new Product (new int[] { 3, 5 });
  355. p1.Rank = 4;
  356. --p1;
  357. Assert.AreEqual (3, p1.Rank);
  358. p1--;
  359. Assert.AreEqual (2, p1.Rank);
  360. }
  361. [TestMethod]
  362. public void Test_IncrementRank ()
  363. {
  364. Product p1 = new Product (new int[] { 3, 5 });
  365. p1.Rank = 7;
  366. ++p1.Rank;
  367. Assert.AreEqual (8, p1.Rank);
  368. p1.Rank++;
  369. Assert.AreEqual (9, p1.Rank);
  370. }
  371. [TestMethod]
  372. public void Test_DecrementRank ()
  373. {
  374. Product p1 = new Product (new int[] { 3, 5 });
  375. p1.Rank = 0;
  376. --p1.Rank;
  377. Assert.AreEqual (14, p1.Rank);
  378. p1.Rank--;
  379. Assert.AreEqual (13, p1.Rank);
  380. }
  381. #endregion
  382. }
  383. }