PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/v5.0/Mercury.ParticleEngine.Core.Test/IntervalFTest.cs

http://mpe.codeplex.com
C# | 460 lines | 371 code | 87 blank | 2 comment | 4 complexity | da2ffa5f95acce7c6fcc4b99f45c857e MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. namespace Mercury.ParticleEngine
  2. {
  3. using System;
  4. using System.Globalization;
  5. using System.Runtime.Serialization;
  6. using FluentAssertions;
  7. using Xunit;
  8. using Xunit.Extensions;
  9. // ReSharper disable InconsistentNaming
  10. public class IntervalFTests
  11. {
  12. public class Constructor
  13. {
  14. [Theory]
  15. [Trait("Type", "IntervalF")]
  16. [InlineData(Single.PositiveInfinity, 1f)]
  17. [InlineData(Single.NegativeInfinity, 1)]
  18. [InlineData(Single.NaN, 1f)]
  19. [InlineData(0f, Single.PositiveInfinity)]
  20. [InlineData(0f, Single.NegativeInfinity)]
  21. [InlineData(0f, Single.NaN)]
  22. public void WhenGivenNaNValue_ThrowsArgumentException(Single x, Single y)
  23. {
  24. Action invocation = () => new IntervalF(x, y);
  25. invocation.ShouldThrow<ArgumentException>();
  26. }
  27. [Fact]
  28. [Trait("Type", "IntervalF")]
  29. public void WhenGivenFiniteValues_CalculatesSize()
  30. {
  31. var interval = new IntervalF(0f, 10f);
  32. interval.Diameter.As<Object>().Should().Be(10f);
  33. }
  34. }
  35. public class DeserializationConstructor
  36. {
  37. [Fact]
  38. [Trait("Type", "IntervalF")]
  39. public void WhenGivenNullSerializationInfo_ThrowsArgumentNullException()
  40. {
  41. Action invocation = () => new IntervalF(null, new StreamingContext());
  42. invocation.ShouldThrow<ArgumentNullException>();
  43. }
  44. [Fact]
  45. [Trait("Type", "IntervalF")]
  46. public void WhenGivenSerializationInfo_ReturnsInitializedInterval()
  47. {
  48. var serializationInfo = new SerializationInfo(typeof(IntervalF), new FormatterConverter());
  49. serializationInfo.AddValue("X", 1f);
  50. serializationInfo.AddValue("Y", 2f);
  51. var result = new IntervalF(serializationInfo, new StreamingContext());
  52. result.X.Should<Single>().Be(1f);
  53. result.Y.Should<Single>().Be(2f);
  54. }
  55. }
  56. public class UnionMethod
  57. {
  58. [Fact]
  59. [Trait("Type", "IntervalF")]
  60. public void WhenGivenIntervalValues_ReturnsUnion()
  61. {
  62. var x = new IntervalF(0f, 5f);
  63. var y = new IntervalF(5f, 10f);
  64. var result = IntervalF.Union(x, y);
  65. result.X.Should<Single>().Be(0f);
  66. result.Y.Should<Single>().Be(10f);
  67. }
  68. }
  69. public class ContainsMethod
  70. {
  71. [Theory]
  72. [Trait("Type", "IntervalF")]
  73. [InlineData(Single.PositiveInfinity)]
  74. [InlineData(Single.NegativeInfinity)]
  75. [InlineData(Single.NaN)]
  76. public void WhenGivenNaNValue_ThrowsArgumentException(Single value)
  77. {
  78. var target = new IntervalF(0f, 1f);
  79. Action invocation = () => target.Contains(value);
  80. invocation.ShouldThrow<ArgumentException>();
  81. }
  82. [Theory]
  83. [Trait("Type", "IntervalF")]
  84. [InlineData(0f)]
  85. [InlineData(0.5f)]
  86. [InlineData(1f)]
  87. public void WhenGivenValueInsideBounds_ReturnsTrue(Single value)
  88. {
  89. var target = new IntervalF(0f, 1f);
  90. target.Contains(value).Should().BeTrue();
  91. }
  92. [Theory]
  93. [Trait("Type", "IntervalF")]
  94. [InlineData(-1f)]
  95. [InlineData(2f)]
  96. public void WhenGivenValueOutsideBounds_ReturnsFalse(Single value)
  97. {
  98. var target = new IntervalF(0f, 1f);
  99. target.Contains(value).Should().BeFalse();
  100. }
  101. }
  102. public class ParseMethod
  103. {
  104. [Fact]
  105. [Trait("Type", "IntervalF")]
  106. public void WhenGivenNullValue_ThrowsArgumentNullException()
  107. {
  108. Action invocation = () => IntervalF.Parse(null, CultureInfo.InvariantCulture);
  109. invocation.ShouldThrow<ArgumentNullException>();
  110. }
  111. [Fact]
  112. [Trait("Type", "IntervalF")]
  113. public void WhenGivenEmptyString_ThrowsArgumentNullException()
  114. {
  115. Action invocation = () => IntervalF.Parse(String.Empty, CultureInfo.InvariantCulture);
  116. invocation.ShouldThrow<ArgumentNullException>();
  117. }
  118. [Theory]
  119. [Trait("Type", "IntervalF")]
  120. [InlineData("abc")]
  121. [InlineData("]0.00,1.00]")]
  122. [InlineData("]0.00,1.00[")]
  123. [InlineData("[abc,def]")]
  124. public void WhenGivenBadFormat_ThrowsFormatException(String input)
  125. {
  126. Action invocation = () => IntervalF.Parse(input);
  127. invocation.ShouldThrow<FormatException>();
  128. }
  129. [Theory]
  130. [Trait("Type", "IntervalF")]
  131. [InlineData("[0.00,1.00]", "de-DE")]
  132. [InlineData("[0,00.1,00]", "en-GB")]
  133. public void WhenGivenBadFormatForCulture_ThrowsFormtException(String input, String cultureCode)
  134. {
  135. var culture = new CultureInfo(cultureCode);
  136. Action invocation = () => IntervalF.Parse(input, culture);
  137. invocation.ShouldThrow<FormatException>();
  138. }
  139. [Theory]
  140. [Trait("Type", "IntervalF")]
  141. [InlineData("[0.00,1.00]", 0f, 1f)]
  142. public void WhenGivenGoodFormat_ReturnsNewInterval(String input, Single expectedX, Single expectedY)
  143. {
  144. var expected = new IntervalF(expectedX, expectedY);
  145. var actual = IntervalF.Parse(input);
  146. actual.Should().Be(expected);
  147. }
  148. [Theory]
  149. [Trait("Type", "IntervalF")]
  150. [InlineData("[0.00,1.00]", "en-GB", 0f, 1f)]
  151. [InlineData("[0,00.1,00]", "de-DE", 0f, 1f)]
  152. public void WhenGivenGoodFormatForCulture_ReturnsNewInterval(String input, String cultureCode, Single expectedX, Single expectedY)
  153. {
  154. var culture = new CultureInfo(cultureCode);
  155. var expected = new IntervalF(expectedX, expectedY);
  156. var actual = IntervalF.Parse(input, culture);
  157. actual.Should().Be(expected);
  158. }
  159. }
  160. public class EqualsMethod
  161. {
  162. [Fact]
  163. [Trait("Type", "IntervalF")]
  164. public void WhenGivenEqualValue_ReturnsTrue()
  165. {
  166. var x = new IntervalF(0f, 1f);
  167. var y = new IntervalF(0f, 1f);
  168. x.Equals(y).Should().BeTrue();
  169. }
  170. [Fact]
  171. [Trait("Type", "IntervalF")]
  172. public void WhenGivenDifferentValue_ReturnsFalse()
  173. {
  174. var x = new IntervalF(0f, 1f);
  175. var y = new IntervalF(0f, 2f);
  176. x.Equals(y).Should().BeFalse();
  177. }
  178. [Fact]
  179. [Trait("Type", "IntervalF")]
  180. public void WhenGivenEqualValueAsObject_ReturnsTrue()
  181. {
  182. var x = new IntervalF(0f, 1f);
  183. var y = new IntervalF(0f, 1f);
  184. x.Equals(y as Object).Should().BeTrue();
  185. }
  186. [Fact]
  187. [Trait("Type", "IntervalF")]
  188. public void WhenGivenDifferentValueAsObject_ReturnsFalse()
  189. {
  190. var x = new IntervalF(0f, 1f);
  191. var y = new IntervalF(0f, 2f);
  192. x.Equals(y as Object).Should().BeFalse();
  193. }
  194. [Fact]
  195. [Trait("Type", "IntervalF")]
  196. public void WhenGivenNull_ReturnsFalse()
  197. {
  198. var interval = new IntervalF(0f, 1f);
  199. interval.Equals(null).Should().BeFalse();
  200. }
  201. }
  202. public class GetHashCodeMethod
  203. {
  204. [Fact]
  205. [Trait("Type", "IntervalF")]
  206. public void WhenObjectsAreSame_YieldsIdenticalHashCode()
  207. {
  208. var interval1 = new IntervalF(0f, 1f);
  209. var interval2 = new IntervalF(0f, 1f);
  210. interval1.GetHashCode().Should().Be(interval2.GetHashCode());
  211. }
  212. [Fact]
  213. [Trait("Type", "IntervalF")]
  214. public void WhenObjectsAreDifferent_YieldsDifferentHashCode()
  215. {
  216. var interval1 = new IntervalF(0f, 1f);
  217. var interval2 = new IntervalF(0f, 2f);
  218. interval1.GetHashCode().Should().NotBe(interval2.GetHashCode());
  219. }
  220. }
  221. public class ToStringMethod
  222. {
  223. [Theory]
  224. [Trait("Type", "IntervalF")]
  225. [InlineData(0f, 1f, "[0,1]")]
  226. public void ReturnsCorrectValue(Single x, Single y, String expected)
  227. {
  228. var actual = new IntervalF(x, y).ToString();
  229. actual.Should().Be(expected);
  230. }
  231. [Theory]
  232. [Trait("Type", "IntervalF")]
  233. [InlineData(0f, 1f, "en-GB", "[0,1]")]
  234. [InlineData(0f, 1f, "de-DE", "[0.1]")]
  235. [InlineData(0.5f, 1.5f, "en-GB", "[0.5,1.5]")]
  236. [InlineData(0.5f, 1.5f, "de-DE", "[0,5.1,5]")]
  237. public void ReturnsCorrectValueForCulture(Single x, Single y, String cultureCode, String expected)
  238. {
  239. var culture = new CultureInfo(cultureCode);
  240. var actual = new IntervalF(x, y).ToString(culture);
  241. actual.Should().Be(expected);
  242. }
  243. [Theory]
  244. [Trait("Type", "IntervalF")]
  245. [InlineData(0f, 1f, "F3", "[0.000,1.000]")]
  246. public void ReturnsCorrectValueWithFormat(Single x, Single y, String numberFormat, String expected)
  247. {
  248. var actual = new IntervalF(x, y).ToString(numberFormat, null);
  249. actual.Should().Be(expected);
  250. }
  251. }
  252. public class IsDegenerateProperty
  253. {
  254. [Fact]
  255. [Trait("Type", "IntervalF")]
  256. public void WhenIntervalIsDegenerate_ReturnsTrue()
  257. {
  258. var interval = new IntervalF(1f, 1f);
  259. interval.IsDegenerate.Should().BeTrue();
  260. }
  261. [Fact]
  262. [Trait("Type", "IntervalF")]
  263. public void WhenIntervalIsProper_ReturnsFalse()
  264. {
  265. var interval = new IntervalF(0f, 1f);
  266. interval.IsDegenerate.Should().BeFalse();
  267. }
  268. }
  269. public class IsProperProperty
  270. {
  271. [Fact]
  272. [Trait("Type", "IntervalF")]
  273. public void WhenIntervalIsProper_ReturnsTrue()
  274. {
  275. var interval = new IntervalF(0f, 1f);
  276. interval.IsProper.Should().BeTrue();
  277. }
  278. [Fact]
  279. [Trait("Type", "IntervalF")]
  280. public void WhenIntervalIsDegenerate_ReturnsFalse()
  281. {
  282. var interval = new IntervalF(1f, 1f);
  283. interval.IsProper.Should().BeFalse();
  284. }
  285. }
  286. public class InteriorProperty
  287. {
  288. [Fact]
  289. [Trait("Type", "IntervalF")]
  290. public void ReturnsIntervalWithLargerLowerBound()
  291. {
  292. var interval = new IntervalF(0f, 1f);
  293. interval.Interior.X.Should().BeGreaterThan(interval.X);
  294. }
  295. [Fact]
  296. [Trait("Type", "IntervalF")]
  297. public void ReturnsIntervalWithSmallerUpperBound()
  298. {
  299. var interval = new IntervalF(0f, 1f);
  300. interval.Interior.Y.Should().BeLessThan(interval.Y);
  301. }
  302. }
  303. public class ClosureProperty
  304. {
  305. [Fact]
  306. [Trait("Type", "IntervalF")]
  307. public void ReturnsIntervalWithSmallerLowerBound()
  308. {
  309. var interval = new IntervalF(0f, 1f);
  310. interval.Closure.X.Should().BeLessThan(interval.X);
  311. }
  312. [Fact]
  313. [Trait("Type", "IntervalF")]
  314. public void ReturnsIntervalWithLargerUpperBound()
  315. {
  316. var interval = new IntervalF(0f, 1f);
  317. interval.Closure.Y.Should().BeGreaterThan(interval.Y);
  318. }
  319. }
  320. public class EqualityOperator
  321. {
  322. [Fact]
  323. [Trait("Type", "IntervalF")]
  324. public void WhenGivenEqualValue_ReturnsTrue()
  325. {
  326. var interval1 = new IntervalF(0f, 1f);
  327. var interval2 = new IntervalF(0f, 1f);
  328. (interval1 == interval2).Should().BeTrue();
  329. }
  330. [Fact]
  331. [Trait("Type", "IntervalF")]
  332. public void WhenGivenDifferentValue_ReturnsFalse()
  333. {
  334. var interval1 = new IntervalF(0f, 1f);
  335. var interval2 = new IntervalF(0f, 2f);
  336. (interval1 == interval2).Should().BeFalse();
  337. }
  338. }
  339. public class InequalityOperator
  340. {
  341. [Fact]
  342. [Trait("Type", "IntervalF")]
  343. public void WhenGivenEqualValue_ReturnsFalse()
  344. {
  345. var interval1 = new IntervalF(0f, 1f);
  346. var interval2 = new IntervalF(0f, 1f);
  347. (interval1 != interval2).Should().BeFalse();
  348. }
  349. [Fact]
  350. [Trait("Type", "IntervalF")]
  351. public void WhenGivenDifferentValue_ReturnsTrue()
  352. {
  353. var interval1 = new IntervalF(0f, 1f);
  354. var interval2 = new IntervalF(0f, 2f);
  355. (interval1 != interval2).Should().BeTrue();
  356. }
  357. }
  358. public class GetObjectDataMethod
  359. {
  360. [Fact]
  361. [Trait("Type", "IntervalF")]
  362. public void WhenGivenSerializationInfo_PopulatesIt()
  363. {
  364. ISerializable instance = new IntervalF(1f, 10f);
  365. var serializationInfo = new SerializationInfo(typeof(IntervalF), new FormatterConverter());
  366. instance.GetObjectData(serializationInfo, new StreamingContext());
  367. serializationInfo.GetSingle("X").Should<Single>().Be(1f);
  368. serializationInfo.GetSingle("Y").Should<Single>().Be(10f);
  369. }
  370. }
  371. }
  372. // ReSharper restore InconsistentNaming
  373. }