using System;
using System.Drawing;
namespace ScottPlot
{
public static class Validate
{
private static string ValidLabel(string label) =>
string.IsNullOrWhiteSpace(label) ? "[unknown variable]" : label;
///
/// Throw an exception if the value is NaN or infinity
///
public static void AssertIsReal(string label, double value)
{
label = ValidLabel(label);
if (double.IsNaN(value))
throw new InvalidOperationException($"{label} is NaN");
if (double.IsInfinity(value))
throw new InvalidOperationException($"{label} is infinity");
}
///
/// Throw an exception if the array is null or contains NaN or infinity
///
public static void AssertAllReal(string label, double[] values)
{
label = ValidLabel(label);
if (values is null)
throw new InvalidOperationException($"{label} must not be null");
for (int i = 0; i < values.Length; i++)
if (double.IsNaN(values[i]) || double.IsInfinity(values[i]))
throw new InvalidOperationException($"{label} index {i} is invalid ({values[i]})");
}
///
/// Throw an exception if the array is null or contains NaN or infinity
///
public static void AssertAllReal(string label, T[] values)
{
if (typeof(T) == typeof(double))
AssertAllReal(label, (double[])(object)values);
else if (typeof(T) == typeof(float))
AssertAllReal(label, (float[])(object)values);
else
throw new InvalidOperationException("values must be float[] or double[]");
}
///
/// Throw an exception if one elemnt is equal to or less than the previous element
///
public static void AssertAscending(string label, double[] values)
{
label = ValidLabel(label);
if (values is null)
throw new InvalidOperationException($"{label} must not be null");
for (int i = 0; i < values.Length - 1; i++)
if (values[i] >= values[i + 1])
throw new InvalidOperationException($"{label} must be ascending values (index {i} >= {i + 1}");
}
///
/// Throw an exception if one elemnt is equal to or less than the previous element
///
public static void AssertAscending(string label, T[] values)
{
label = ValidLabel(label);
if (values is null)
throw new InvalidOperationException($"{label} must not be null");
for (int i = 0; i < values.Length - 1; i++)
if (Convert.ToDouble(values[i]) >= Convert.ToDouble(values[i + 1]))
throw new InvalidOperationException($"{label} must be ascending values (index {i} >= {i + 1}");
}
///
/// Throw an exception if the array does not contain at least one element
///
public static void AssertHasElements(string label, double[] values)
{
label = ValidLabel(label);
if (values is null)
throw new InvalidOperationException($"{label} must not be null");
if (values.Length == 0)
throw new InvalidOperationException($"{label} must contain at least one element");
}
///
/// Throw an exception if the array does not contain at least one element
///
public static void AssertHasElements(string label, T[] values)
{
label = ValidLabel(label);
if (values is null)
throw new InvalidOperationException($"{label} must not be null");
if (values.Length == 0)
throw new InvalidOperationException($"{label} must contain at least one element");
}
///
/// Throw an exception if the array does not contain at least one element
///
public static void AssertHasElements(string label, Color[] values)
{
label = ValidLabel(label);
if (values is null)
throw new InvalidOperationException($"{label} must not be null");
if (values.Length == 0)
throw new InvalidOperationException($"{label} must contain at least one element");
}
///
/// Throw an exception if the array does not contain at least one element
///
public static void AssertHasElements(string label, string[] values)
{
label = ValidLabel(label);
if (values is null)
throw new InvalidOperationException($"{label} must not be null");
if (values.Length == 0)
throw new InvalidOperationException($"{label} must contain at least one element");
}
///
/// Throw an exception if non-null arrays have different lengths
///
public static void AssertEqualLength(string label,
double[] a, double[] b = null, double[] c = null,
double[] d = null, double[] e = null, double[] f = null)
{
label = ValidLabel(label);
if (!IsEqualLength(a, b, c, d, e, f))
throw new InvalidOperationException($"{label} must all have same length");
}
///
/// Throw an exception if non-null arrays have different lengths
///
public static void AssertEqualLength(string label, T1[] a, T2[] b)
{
label = ValidLabel(label);
if (a.Length != b.Length)
throw new InvalidOperationException($"{label} must all have same length");
}
///
/// Returns true if all non-null arguments have equal length
///
public static bool IsEqualLength(double[] a, double[] b = null, double[] c = null,
double[] d = null, double[] e = null, double[] f = null)
{
if (a is null)
throw new InvalidOperationException($"first array must not be null");
if (b is object && b.Length != a.Length) return false;
if (c is object && c.Length != a.Length) return false;
if (d is object && d.Length != a.Length) return false;
if (e is object && e.Length != a.Length) return false;
if (f is object && f.Length != a.Length) return false;
return true;
}
///
/// Throws an exception if the string is null, empty, or only contains whitespace
///
public static void AssertHasText(string label, string value)
{
label = ValidLabel(label);
if (string.IsNullOrWhiteSpace(value))
throw new InvalidOperationException($"{label} must contain text");
}
}
}