QuartilesTest.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using AdditionalMathf;
  5. using NUnit.Framework;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using UnityEngine.TestTools;
  9. public class QuartilesTest
  10. {
  11. private const float DELTA = 0.05f;
  12. [Test]
  13. public void Length1()
  14. {
  15. Assert.Throws<ArgumentException>(() =>
  16. {
  17. var quartiles = new Quartiles(new List<float>());
  18. });
  19. }
  20. [Test]
  21. public void Length2()
  22. {
  23. Assert.Throws<ArgumentException>(() =>
  24. {
  25. var quartiles = new Quartiles(new List<float>());
  26. });
  27. }
  28. [Test]
  29. public void Length3()
  30. {
  31. Assert.Throws<ArgumentException>(() =>
  32. {
  33. var quartiles = new Quartiles(new List<float>());
  34. });
  35. }
  36. [Test]
  37. public void Length4()
  38. {
  39. var q = new Quartiles(new[] {1.5f, 3.5f, 7f, 12.3f});
  40. Assert.AreEqual(1.5f, q.Q1, DELTA);
  41. Assert.AreEqual(3.5f, q.Q2, DELTA);
  42. Assert.AreEqual(7f, q.Q3, DELTA);
  43. }
  44. [Test]
  45. public void Empty()
  46. {
  47. Assert.Throws<ArgumentException>(() =>
  48. {
  49. var quartiles = new Quartiles(new List<float>());
  50. });
  51. }
  52. [Test]
  53. public void IQR()
  54. {
  55. var q = new Quartiles(new[]
  56. {
  57. 1.5f, 3.5f,
  58. 7f, 12.3f,
  59. 15f, 17.25f,
  60. 18f, 22.27f
  61. }); //8
  62. Assert.AreEqual(q.Iqr, q.Q3 - q.Q1, DELTA);
  63. }
  64. [Test]
  65. public void Even()
  66. {
  67. var q1 = new Quartiles(new[]
  68. {
  69. 1.5f, 3.5f,
  70. 7f, 12.3f,
  71. 15f, 17.25f,
  72. 18f, 22.27f
  73. }); //8
  74. Assert.AreEqual(3.5f, q1.Q1, DELTA);
  75. Assert.AreEqual(12.3f, q1.Q2, DELTA);
  76. Assert.AreEqual(17.25f, q1.Q3, DELTA);
  77. var q2 = new Quartiles(new[]
  78. {
  79. 1.5f, 3.5f, 7f,
  80. 12.3f, 15f, 17.25f,
  81. 18f, 22.27f, 25f,
  82. 28.5f, 33f, 39.5f
  83. }); //12
  84. Assert.AreEqual(7f, q2.Q1, DELTA);
  85. Assert.AreEqual(17.25f, q2.Q2, DELTA);
  86. Assert.AreEqual(25f, q2.Q3, DELTA);
  87. }
  88. [Test]
  89. public void Uneven()
  90. {
  91. var q1 = new Quartiles(new[] {
  92. 1.5f, 3.5f,
  93. 7f, 12.3f,
  94. 15f,
  95. 17.25f, 18f,
  96. 22.27f, 25f}); //9 [2.25, 4.5, 6.75]
  97. Assert.AreEqual(Mathf.Lerp(3.5f, 7f, .25f), q1.Q1, DELTA);
  98. Assert.AreEqual(Mathf.Lerp(12.3f, 15f, .5f), q1.Q2, DELTA);
  99. Assert.AreEqual(Mathf.Lerp(17.25f, 18f, .75f), q1.Q3, DELTA);
  100. var q2 = new Quartiles(new[]
  101. {1.5f, 3.5f, 7f, 12.3f, 15f, 17.25f, 18f, 22.27f, 25f, 28.5f, 33f, 39.5f, 45f}); //13 [3.25, 6.5, 9.75]
  102. Assert.AreEqual(Mathf.Lerp(7f, 12.3f, .25f), q2.Q1, DELTA);
  103. Assert.AreEqual(Mathf.Lerp(17.25f, 18f, .5f), q2.Q2, DELTA);
  104. Assert.AreEqual(Mathf.Lerp(25f, 28.5f, .75f), q2.Q3, DELTA);
  105. }
  106. }