源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 rivejä
3.2 KiB

1 kuukausi sitten
  1. using System;
  2. using NUnit.Framework;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using System.Collections;
  6. using System.Linq;
  7. namespace Treemap
  8. {
  9. [TestFixture]
  10. public class TreemapTests
  11. {
  12. static readonly Rect s_Rect = new UnityEngine.Rect (0, 0, 100, 100);
  13. [Test]
  14. public void Empty()
  15. {
  16. Assert.Throws<ArgumentException>(() => Utility.GetTreemapRects (new float[0], s_Rect));
  17. }
  18. [Test]
  19. public void One()
  20. {
  21. var result = Utility.GetTreemapRects (new [] { 1.0f }, s_Rect);
  22. Assert.AreEqual (1, result.Length);
  23. Assert.AreEqual (s_Rect, result[0]);
  24. }
  25. [Test]
  26. public void TwoEquallySized()
  27. {
  28. var result = Utility.GetTreemapRects (new [] { 1.0f, 1.0f }, s_Rect);
  29. Assert.AreEqual (2, result.Length);
  30. Assert.AreEqual (new Rect (0, 0, 100, 50), result [0]);
  31. Assert.AreEqual (new Rect (0, 50, 100, 50), result [1]);
  32. }
  33. [Test]
  34. public void Zero()
  35. {
  36. Assert.Throws<ArgumentException>(() => Utility.GetTreemapRects (new [] { 0f }, s_Rect));
  37. }
  38. [Test]
  39. public void NegativeNumber()
  40. {
  41. Assert.Throws<ArgumentException>(() => Utility.GetTreemapRects (new [] { -3f }, s_Rect));
  42. }
  43. [Test, TestCaseSource("InterestingDataSets")]
  44. public void ResultRectsHaveCorrectArea(float[] floats)
  45. {
  46. var results = Utility.GetTreemapRects (floats, s_Rect);
  47. Assert.AreEqual (floats.Length, results.Length);
  48. var sum = floats.Sum ();
  49. var totalSize = s_Rect.width * s_Rect.height;
  50. for(int i =0; i!= floats.Length; i++)
  51. {
  52. var f = floats [i];
  53. var r = results [i];
  54. var size = r.width * r.height;
  55. var ratio = size / totalSize;
  56. UnityEngine.Assertions.Assert.AreApproximatelyEqual (f / sum, ratio);
  57. }
  58. }
  59. [Test, TestCaseSource("InterestingDataSets")]
  60. public void ResultRectsDoNotOverlap(float[] floats)
  61. {
  62. var results = Utility.GetTreemapRects (floats, s_Rect);
  63. Assert.AreEqual (floats.Length, results.Length);
  64. for(int i1 =0; i1!= floats.Length; i1++)
  65. {
  66. var rect1 = results [i1];
  67. for (int i2 = i1 + 1; i2 != floats.Length; i2++) {
  68. var rect2 = results [i2];
  69. float overlapSize = SizeOfOverlappingRectOf(rect1, rect2);
  70. const float tolerance = 0.000001f;
  71. if (overlapSize > tolerance * rect1.width * rect1.height || overlapSize > tolerance * rect2.width * rect2.height)
  72. throw new AssertionException ("too big overlap. overlapSize: " + overlapSize + " rect1:" + rect1 + " rect2: " + rect2);
  73. }
  74. }
  75. }
  76. private static Rect RectIntersect(Rect a, Rect b)
  77. {
  78. float xMin = Mathf.Max(a.x, b.x);
  79. float xMax = Mathf.Min(a.x + a.width, b.x + b.width);
  80. float yMin = Mathf.Max(a.y, b.y);
  81. float yMax = Mathf.Min(a.y + a.height, b.y + b.height);
  82. if (xMax >= xMin && yMax >= yMin)
  83. return new Rect(xMin, yMin, xMax - xMin, yMax - yMin);
  84. return new Rect(0f, 0f, 0f, 0f);
  85. }
  86. static float SizeOfOverlappingRectOf(Rect rect1, Rect rect2)
  87. {
  88. var result = RectIntersect (rect1, rect2);
  89. return result.width * result.height;
  90. }
  91. static IEnumerable InterestingDataSets()
  92. {
  93. yield return new TestCaseData (Enumerable.Range (100, 10).Select (i => (float)i).ToArray ()).SetName ("1000 floats counting up");
  94. yield return new TestCaseData (Enumerable.Repeat (10, 100).Select (i => (float)i).ToArray ()).SetName ("100 identical floats");
  95. }
  96. }
  97. }