Validate.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Drawing;
  3. namespace ScottPlot
  4. {
  5. public static class Validate
  6. {
  7. private static string ValidLabel(string label) =>
  8. string.IsNullOrWhiteSpace(label) ? "[unknown variable]" : label;
  9. /// <summary>
  10. /// Throw an exception if the value is NaN or infinity
  11. /// </summary>
  12. public static void AssertIsReal(string label, double value)
  13. {
  14. label = ValidLabel(label);
  15. if (double.IsNaN(value))
  16. throw new InvalidOperationException($"{label} is NaN");
  17. if (double.IsInfinity(value))
  18. throw new InvalidOperationException($"{label} is infinity");
  19. }
  20. /// <summary>
  21. /// Throw an exception if the array is null or contains NaN or infinity
  22. /// </summary>
  23. public static void AssertAllReal(string label, double[] values)
  24. {
  25. label = ValidLabel(label);
  26. if (values is null)
  27. throw new InvalidOperationException($"{label} must not be null");
  28. for (int i = 0; i < values.Length; i++)
  29. if (double.IsNaN(values[i]) || double.IsInfinity(values[i]))
  30. throw new InvalidOperationException($"{label} index {i} is invalid ({values[i]})");
  31. }
  32. /// <summary>
  33. /// Throw an exception if the array is null or contains NaN or infinity
  34. /// </summary>
  35. public static void AssertAllReal<T>(string label, T[] values)
  36. {
  37. if (typeof(T) == typeof(double))
  38. AssertAllReal(label, (double[])(object)values);
  39. else if (typeof(T) == typeof(float))
  40. AssertAllReal(label, (float[])(object)values);
  41. else
  42. throw new InvalidOperationException("values must be float[] or double[]");
  43. }
  44. /// <summary>
  45. /// Throw an exception if one elemnt is equal to or less than the previous element
  46. /// </summary>
  47. public static void AssertAscending(string label, double[] values)
  48. {
  49. label = ValidLabel(label);
  50. if (values is null)
  51. throw new InvalidOperationException($"{label} must not be null");
  52. for (int i = 0; i < values.Length - 1; i++)
  53. if (values[i] >= values[i + 1])
  54. throw new InvalidOperationException($"{label} must be ascending values (index {i} >= {i + 1}");
  55. }
  56. /// <summary>
  57. /// Throw an exception if one elemnt is equal to or less than the previous element
  58. /// </summary>
  59. public static void AssertAscending<T>(string label, T[] values)
  60. {
  61. label = ValidLabel(label);
  62. if (values is null)
  63. throw new InvalidOperationException($"{label} must not be null");
  64. for (int i = 0; i < values.Length - 1; i++)
  65. if (Convert.ToDouble(values[i]) >= Convert.ToDouble(values[i + 1]))
  66. throw new InvalidOperationException($"{label} must be ascending values (index {i} >= {i + 1}");
  67. }
  68. /// <summary>
  69. /// Throw an exception if the array does not contain at least one element
  70. /// </summary>
  71. public static void AssertHasElements(string label, double[] values)
  72. {
  73. label = ValidLabel(label);
  74. if (values is null)
  75. throw new InvalidOperationException($"{label} must not be null");
  76. if (values.Length == 0)
  77. throw new InvalidOperationException($"{label} must contain at least one element");
  78. }
  79. /// <summary>
  80. /// Throw an exception if the array does not contain at least one element
  81. /// </summary>
  82. public static void AssertHasElements<T>(string label, T[] values)
  83. {
  84. label = ValidLabel(label);
  85. if (values is null)
  86. throw new InvalidOperationException($"{label} must not be null");
  87. if (values.Length == 0)
  88. throw new InvalidOperationException($"{label} must contain at least one element");
  89. }
  90. /// <summary>
  91. /// Throw an exception if the array does not contain at least one element
  92. /// </summary>
  93. public static void AssertHasElements(string label, Color[] values)
  94. {
  95. label = ValidLabel(label);
  96. if (values is null)
  97. throw new InvalidOperationException($"{label} must not be null");
  98. if (values.Length == 0)
  99. throw new InvalidOperationException($"{label} must contain at least one element");
  100. }
  101. /// <summary>
  102. /// Throw an exception if the array does not contain at least one element
  103. /// </summary>
  104. public static void AssertHasElements(string label, string[] values)
  105. {
  106. label = ValidLabel(label);
  107. if (values is null)
  108. throw new InvalidOperationException($"{label} must not be null");
  109. if (values.Length == 0)
  110. throw new InvalidOperationException($"{label} must contain at least one element");
  111. }
  112. /// <summary>
  113. /// Throw an exception if non-null arrays have different lengths
  114. /// </summary>
  115. public static void AssertEqualLength(string label,
  116. double[] a, double[] b = null, double[] c = null,
  117. double[] d = null, double[] e = null, double[] f = null)
  118. {
  119. label = ValidLabel(label);
  120. if (!IsEqualLength(a, b, c, d, e, f))
  121. throw new InvalidOperationException($"{label} must all have same length");
  122. }
  123. /// <summary>
  124. /// Throw an exception if non-null arrays have different lengths
  125. /// </summary>
  126. public static void AssertEqualLength<T1, T2>(string label, T1[] a, T2[] b)
  127. {
  128. label = ValidLabel(label);
  129. if (a.Length != b.Length)
  130. throw new InvalidOperationException($"{label} must all have same length");
  131. }
  132. /// <summary>
  133. /// Returns true if all non-null arguments have equal length
  134. /// </summary>
  135. public static bool IsEqualLength(double[] a, double[] b = null, double[] c = null,
  136. double[] d = null, double[] e = null, double[] f = null)
  137. {
  138. if (a is null)
  139. throw new InvalidOperationException($"first array must not be null");
  140. if (b is object && b.Length != a.Length) return false;
  141. if (c is object && c.Length != a.Length) return false;
  142. if (d is object && d.Length != a.Length) return false;
  143. if (e is object && e.Length != a.Length) return false;
  144. if (f is object && f.Length != a.Length) return false;
  145. return true;
  146. }
  147. /// <summary>
  148. /// Throws an exception if the string is null, empty, or only contains whitespace
  149. /// </summary>
  150. public static void AssertHasText(string label, string value)
  151. {
  152. label = ValidLabel(label);
  153. if (string.IsNullOrWhiteSpace(value))
  154. throw new InvalidOperationException($"{label} must contain text");
  155. }
  156. }
  157. }