Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.11 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using System.Threading;
  4. using System.Reflection;
  5.  
  6. namespace LazyTest
  7. {
  8. [TestClass]
  9. public class LazyRegressionTests
  10. {
  11. class MyException
  12. : Exception
  13. {
  14. public int Value { get; }
  15.  
  16. public MyException(int value)
  17. {
  18. Value = value;
  19. }
  20. }
  21.  
  22. public class Simple
  23. {
  24. public int Value { get; }
  25.  
  26. public Simple(int value)
  27. {
  28. Value = value;
  29. }
  30.  
  31. public Simple()
  32. {
  33. Value = 42;
  34. }
  35. }
  36.  
  37. public void SimpleInt(Lazy<int> lazy)
  38. {
  39. Assert.IsFalse(lazy.IsValueCreated);
  40. Assert.AreEqual(lazy.Value, 42);
  41. Assert.IsTrue(lazy.IsValueCreated);
  42. }
  43.  
  44. [TestMethod]
  45. public void SimpleInteger()
  46. {
  47. SimpleInt(new Lazy<int>(() => 42));
  48. SimpleInt(new Lazy<int>(() => 42, true));
  49. SimpleInt(new Lazy<int>(() => 42, false));
  50. SimpleInt(new Lazy<int>(() => 42, LazyThreadSafetyMode.ExecutionAndPublication));
  51. SimpleInt(new Lazy<int>(() => 42, LazyThreadSafetyMode.None));
  52. SimpleInt(new Lazy<int>(() => 42, LazyThreadSafetyMode.PublicationOnly));
  53. }
  54.  
  55. public void SimpleObject(Lazy<Simple> lazy)
  56. {
  57. Assert.IsFalse(lazy.IsValueCreated);
  58. Assert.AreEqual(lazy.Value.Value, 42);
  59. Assert.IsTrue(lazy.IsValueCreated);
  60. }
  61.  
  62. [TestMethod]
  63. public void SimpleObject()
  64. {
  65. SimpleObject(new Lazy<Simple>());
  66. SimpleObject(new Lazy<Simple>(true));
  67. SimpleObject(new Lazy<Simple>(false));
  68. SimpleObject(new Lazy<Simple>(LazyThreadSafetyMode.ExecutionAndPublication));
  69. SimpleObject(new Lazy<Simple>(LazyThreadSafetyMode.None));
  70. SimpleObject(new Lazy<Simple>(LazyThreadSafetyMode.PublicationOnly));
  71. SimpleObject(new Lazy<Simple>(() => new Simple(42)));
  72. SimpleObject(new Lazy<Simple>(() => new Simple(42), true));
  73. SimpleObject(new Lazy<Simple>(() => new Simple(42), false));
  74. SimpleObject(new Lazy<Simple>(() => new Simple(42), LazyThreadSafetyMode.ExecutionAndPublication));
  75. SimpleObject(new Lazy<Simple>(() => new Simple(42), LazyThreadSafetyMode.None));
  76. SimpleObject(new Lazy<Simple>(() => new Simple(42), LazyThreadSafetyMode.PublicationOnly));
  77. }
  78.  
  79. public void SimpleIntException(Lazy<int> lazy)
  80. {
  81. MyException e = null;
  82. try
  83. {
  84. var _ = lazy.Value;
  85. }
  86. catch(MyException thrown)
  87. {
  88. e = thrown;
  89. }
  90. Assert.IsNotNull(e);
  91. Assert.AreEqual(e.Value, 99);
  92. }
  93.  
  94. [TestMethod]
  95. [ExpectedException(typeof(MyException), "")]
  96. public void SimpleIntegerException()
  97. {
  98. SimpleInt(new Lazy<int>(() => { throw new MyException(99); }));
  99. SimpleInt(new Lazy<int>(() => { throw new MyException(99); }, true));
  100. SimpleInt(new Lazy<int>(() => { throw new MyException(99); }, false));
  101. SimpleInt(new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.ExecutionAndPublication));
  102. SimpleInt(new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.None));
  103. SimpleInt(new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.PublicationOnly));
  104. }
  105.  
  106. public class SimpleException
  107. {
  108. public SimpleException() : this(99) { }
  109.  
  110. public SimpleException(int value)
  111. {
  112. throw new MyException(value);
  113. }
  114. }
  115.  
  116. public void SimpleObjectException(Lazy<SimpleException> lazy)
  117. {
  118. TargetInvocationException e = null;
  119. try
  120. {
  121. var _ = lazy.Value;
  122. }
  123. catch (TargetInvocationException ex)
  124. {
  125. e = ex;
  126. }
  127. Assert.IsNotNull(e);
  128. }
  129.  
  130. [TestMethod]
  131. [ExpectedException(typeof(MyException), "")]
  132. public void SimpleObjectException()
  133. {
  134. SimpleObjectException(new Lazy<SimpleException>());
  135. SimpleObjectException(new Lazy<SimpleException>(true));
  136. SimpleObjectException(new Lazy<SimpleException>(false));
  137. SimpleObjectException(new Lazy<SimpleException>(LazyThreadSafetyMode.ExecutionAndPublication));
  138. SimpleObjectException(new Lazy<SimpleException>(LazyThreadSafetyMode.None));
  139. SimpleObjectException(new Lazy<SimpleException>(LazyThreadSafetyMode.PublicationOnly));
  140. SimpleObjectException(new Lazy<SimpleException>(() => new SimpleException(99)));
  141. SimpleObjectException(new Lazy<SimpleException>(() => new SimpleException(99), true));
  142. SimpleObjectException(new Lazy<SimpleException>(() => new SimpleException(99), false));
  143. SimpleObjectException(new Lazy<SimpleException>(() => new SimpleException(99), LazyThreadSafetyMode.ExecutionAndPublication));
  144. SimpleObjectException(new Lazy<SimpleException>(() => new SimpleException(99), LazyThreadSafetyMode.None));
  145. SimpleObjectException(new Lazy<SimpleException>(() => new SimpleException(99), LazyThreadSafetyMode.PublicationOnly));
  146. }
  147.  
  148. public void SameException<T>(Lazy<T> x)
  149. {
  150. Exception first = null;
  151. try
  152. {
  153. var _ = x.Value;
  154. }
  155. catch (Exception thrown1)
  156. {
  157. first = thrown1;
  158. }
  159. Assert.IsNotNull(first);
  160.  
  161. try
  162. {
  163. var _ = x.Value;
  164. }
  165. catch (MyException thrown2)
  166. {
  167. Assert.AreSame(first, thrown2);
  168. }
  169. }
  170.  
  171. public void DifferentException<T>(Lazy<T> x)
  172. {
  173. Exception first = null;
  174. try
  175. {
  176. var _ = x.Value;
  177. }
  178. catch (Exception thrown1)
  179. {
  180. first = thrown1;
  181. }
  182. Assert.IsNotNull(first);
  183.  
  184. Exception second = null;
  185. try
  186. {
  187. var _ = x.Value;
  188. }
  189. catch (Exception thrown2)
  190. {
  191. second = thrown2;
  192. }
  193. Assert.IsNotNull(second);
  194.  
  195. Assert.AreNotEqual(first, second);
  196. }
  197.  
  198. [TestMethod]
  199. public void SameException()
  200. {
  201. SameException(new Lazy<int>(() => { throw new MyException(99); }));
  202. SameException(new Lazy<int>(() => { throw new MyException(99); }, true));
  203. SameException(new Lazy<int>(() => { throw new MyException(99); }, false));
  204. SameException(new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.ExecutionAndPublication));
  205. SameException(new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.None));
  206. DifferentException(new Lazy<int>(() => { throw new MyException(99); }, LazyThreadSafetyMode.PublicationOnly));
  207.  
  208. DifferentException(new Lazy<SimpleException>());
  209. DifferentException(new Lazy<SimpleException>(true));
  210. DifferentException(new Lazy<SimpleException>(false));
  211. DifferentException(new Lazy<SimpleException>(LazyThreadSafetyMode.ExecutionAndPublication));
  212. DifferentException(new Lazy<SimpleException>(LazyThreadSafetyMode.None));
  213. DifferentException(new Lazy<SimpleException>(LazyThreadSafetyMode.PublicationOnly));
  214. SameException(new Lazy<SimpleException>(() => new SimpleException(99)));
  215. SameException(new Lazy<SimpleException>(() => new SimpleException(99), true));
  216. SameException(new Lazy<SimpleException>(() => new SimpleException(99), false));
  217. SameException(new Lazy<SimpleException>(() => new SimpleException(99), LazyThreadSafetyMode.ExecutionAndPublication));
  218. SameException(new Lazy<SimpleException>(() => new SimpleException(99), LazyThreadSafetyMode.None));
  219. DifferentException(new Lazy<SimpleException>(() => new SimpleException(99), LazyThreadSafetyMode.PublicationOnly));
  220. }
  221.  
  222. public void MultipleInt(Lazy<int> lazy, ref int counter, int expected)
  223. {
  224. counter = 0;
  225. var result = 0;
  226. for (var i=0; i < 10; ++i)
  227. {
  228. try { result = lazy.Value; } catch (Exception) { }
  229. }
  230. Assert.AreEqual(result, expected);
  231. }
  232.  
  233. [TestMethod]
  234. public void Multiple()
  235. {
  236. var counter = 0;
  237. var f = new Func<int>(() => { if (++counter < 5) throw new MyException(42); else return counter; });
  238.  
  239. MultipleInt(new Lazy<int>(f), ref counter, 0);
  240. MultipleInt(new Lazy<int>(f, true), ref counter, 0);
  241. MultipleInt(new Lazy<int>(f, false), ref counter, 0);
  242. MultipleInt(new Lazy<int>(f, LazyThreadSafetyMode.ExecutionAndPublication), ref counter, 0);
  243. MultipleInt(new Lazy<int>(f, LazyThreadSafetyMode.None), ref counter, 0);
  244. MultipleInt(new Lazy<int>(f, LazyThreadSafetyMode.PublicationOnly), ref counter, 5);
  245. }
  246.  
  247. public void MultipleObject(Lazy<Simple> lazy, ref int counter, int? expected)
  248. {
  249. counter = 0;
  250. var result = default(Simple);
  251. for (var i = 0; i < 10; ++i)
  252. {
  253. try { result = lazy.Value; } catch (Exception) { }
  254. }
  255. if (expected == null)
  256. Assert.IsNull(result);
  257. else
  258. Assert.AreEqual(result.Value, expected.Value);
  259. }
  260.  
  261. [TestMethod]
  262. public void MultipleObject()
  263. {
  264. var counter = 0;
  265. var f = new Func<Simple>(() => { if (++counter < 5) throw new MyException(42); else return new Simple(counter); });
  266.  
  267. MultipleObject(new Lazy<Simple>(f), ref counter, null);
  268. MultipleObject(new Lazy<Simple>(f, true), ref counter, null);
  269. MultipleObject(new Lazy<Simple>(f, false), ref counter, null);
  270. MultipleObject(new Lazy<Simple>(f, LazyThreadSafetyMode.ExecutionAndPublication), ref counter, null);
  271. MultipleObject(new Lazy<Simple>(f, LazyThreadSafetyMode.None), ref counter, null);
  272. MultipleObject(new Lazy<Simple>(f, LazyThreadSafetyMode.PublicationOnly), ref counter, 5);
  273. }
  274.  
  275. class SimpleConstructor
  276. {
  277. public static int counter = 0;
  278. public static int getValue()
  279. {
  280. if (++counter < 5)
  281. throw new MyException(42);
  282. else
  283. return counter;
  284. }
  285.  
  286. public int Value { get; }
  287.  
  288. public SimpleConstructor()
  289. {
  290. Value = getValue();
  291. }
  292. }
  293.  
  294. void MultipleObject(Lazy<SimpleConstructor> lazy, int? expected)
  295. {
  296. SimpleConstructor.counter = 0;
  297. var result = default(SimpleConstructor);
  298. for (var i = 0; i < 10; ++i)
  299. {
  300. try { result = lazy.Value; } catch (Exception) { }
  301. }
  302. if (expected == null)
  303. Assert.IsNull(result);
  304. else
  305. Assert.AreEqual(result.Value, expected.Value);
  306. }
  307.  
  308. [TestMethod]
  309. public void MultipleConstructorObject()
  310. {
  311. MultipleObject(new Lazy<SimpleConstructor>(), 5);
  312. MultipleObject(new Lazy<SimpleConstructor>(true), 5);
  313. MultipleObject(new Lazy<SimpleConstructor>(false), 5);
  314. MultipleObject(new Lazy<SimpleConstructor>(LazyThreadSafetyMode.ExecutionAndPublication), 5);
  315. MultipleObject(new Lazy<SimpleConstructor>(LazyThreadSafetyMode.None), 5);
  316. MultipleObject(new Lazy<SimpleConstructor>(LazyThreadSafetyMode.PublicationOnly), 5);
  317. }
  318.  
  319. public void CheckForInvalidOperationException<T>(ref Lazy<T> x, Lazy<T> lazy)
  320. {
  321. x = lazy;
  322.  
  323. var correct = false;
  324. try
  325. {
  326. var _ = lazy.Value;
  327. }
  328. catch (InvalidOperationException)
  329. {
  330. correct = true;
  331. }
  332. Assert.IsTrue(correct);
  333. }
  334.  
  335. [TestMethod]
  336. public void Recursion()
  337. {
  338. Lazy<int> x = null;
  339. Func<int> f = () => x.Value;
  340.  
  341. CheckForInvalidOperationException(ref x, new Lazy<int>(f));
  342. CheckForInvalidOperationException(ref x, new Lazy<int>(f, true));
  343. CheckForInvalidOperationException(ref x, new Lazy<int>(f, false));
  344. CheckForInvalidOperationException(ref x, new Lazy<int>(f, LazyThreadSafetyMode.ExecutionAndPublication));
  345. CheckForInvalidOperationException(ref x, new Lazy<int>(f, LazyThreadSafetyMode.None));
  346.  
  347. // this just hangs in current implementation
  348. // CheckForInvalidOperationException(ref x, new Lazy<int>(f, LazyThreadSafetyMode.PublicationOnly));
  349. }
  350. }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement