Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ public partial struct Vector2 : System.IEquatable<System.Numerics.Vector2>, Syst
public float Y;
public Vector2(float value) { throw null; }
public Vector2(float x, float y) { throw null; }
public Vector2(System.ReadOnlySpan<float> values) { throw null; }
public static System.Numerics.Vector2 One { get { throw null; } }
public static System.Numerics.Vector2 UnitX { get { throw null; } }
public static System.Numerics.Vector2 UnitY { get { throw null; } }
Expand All @@ -313,6 +314,8 @@ public partial struct Vector2 : System.IEquatable<System.Numerics.Vector2>, Syst
public static System.Numerics.Vector2 Clamp(System.Numerics.Vector2 value1, System.Numerics.Vector2 min, System.Numerics.Vector2 max) { throw null; }
public readonly void CopyTo(float[] array) { }
public readonly void CopyTo(float[] array, int index) { }
public readonly void CopyTo(System.Span<float> destination) { }
public readonly bool TryCopyTo(System.Span<float> destination) { throw null; }
public static float Distance(System.Numerics.Vector2 value1, System.Numerics.Vector2 value2) { throw null; }
public static float DistanceSquared(System.Numerics.Vector2 value1, System.Numerics.Vector2 value2) { throw null; }
public static System.Numerics.Vector2 Divide(System.Numerics.Vector2 left, System.Numerics.Vector2 right) { throw null; }
Expand Down Expand Up @@ -361,6 +364,7 @@ public partial struct Vector3 : System.IEquatable<System.Numerics.Vector3>, Syst
public Vector3(System.Numerics.Vector2 value, float z) { throw null; }
public Vector3(float value) { throw null; }
public Vector3(float x, float y, float z) { throw null; }
public Vector3(System.ReadOnlySpan<float> values) { throw null; }
public static System.Numerics.Vector3 One { get { throw null; } }
public static System.Numerics.Vector3 UnitX { get { throw null; } }
public static System.Numerics.Vector3 UnitY { get { throw null; } }
Expand All @@ -371,6 +375,8 @@ public partial struct Vector3 : System.IEquatable<System.Numerics.Vector3>, Syst
public static System.Numerics.Vector3 Clamp(System.Numerics.Vector3 value1, System.Numerics.Vector3 min, System.Numerics.Vector3 max) { throw null; }
public readonly void CopyTo(float[] array) { }
public readonly void CopyTo(float[] array, int index) { }
public readonly void CopyTo(System.Span<float> destination) { }
public readonly bool TryCopyTo(System.Span<float> destination) { throw null; }
public static System.Numerics.Vector3 Cross(System.Numerics.Vector3 vector1, System.Numerics.Vector3 vector2) { throw null; }
public static float Distance(System.Numerics.Vector3 value1, System.Numerics.Vector3 value2) { throw null; }
public static float DistanceSquared(System.Numerics.Vector3 value1, System.Numerics.Vector3 value2) { throw null; }
Expand Down Expand Up @@ -420,6 +426,7 @@ public partial struct Vector4 : System.IEquatable<System.Numerics.Vector4>, Syst
public Vector4(System.Numerics.Vector3 value, float w) { throw null; }
public Vector4(float value) { throw null; }
public Vector4(float x, float y, float z, float w) { throw null; }
public Vector4(System.ReadOnlySpan<float> values) { throw null; }
public static System.Numerics.Vector4 One { get { throw null; } }
public static System.Numerics.Vector4 UnitW { get { throw null; } }
public static System.Numerics.Vector4 UnitX { get { throw null; } }
Expand All @@ -431,6 +438,8 @@ public partial struct Vector4 : System.IEquatable<System.Numerics.Vector4>, Syst
public static System.Numerics.Vector4 Clamp(System.Numerics.Vector4 value1, System.Numerics.Vector4 min, System.Numerics.Vector4 max) { throw null; }
public readonly void CopyTo(float[] array) { }
public readonly void CopyTo(float[] array, int index) { }
public readonly void CopyTo(System.Span<float> destination) { }
public readonly bool TryCopyTo(System.Span<float> destination) { throw null; }
public static float Distance(System.Numerics.Vector4 value1, System.Numerics.Vector4 value2) { throw null; }
public static float DistanceSquared(System.Numerics.Vector4 value1, System.Numerics.Vector4 value2) { throw null; }
public static System.Numerics.Vector4 Divide(System.Numerics.Vector4 left, System.Numerics.Vector4 right) { throw null; }
Expand Down
42 changes: 42 additions & 0 deletions src/libraries/System.Numerics.Vectors/tests/Vector2Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ public void Vector2CopyToTest()
Assert.Equal(3.0, b[1]);
}

[Fact]
public void Vector2CopyToSpanTest()
{
Vector2 vector = new Vector2(1.0f, 2.0f);
Span<float> destination = new float[2];

Assert.Throws<ArgumentException>(() => vector.CopyTo(new Span<float>(new float[1])));
vector.CopyTo(destination);

Assert.Equal(1.0f, vector.X);
Assert.Equal(2.0f, vector.Y);
Assert.Equal(vector.X, destination[0]);
Assert.Equal(vector.Y, destination[1]);
}

[Fact]
public void Vector2TryCopyToTest()
{
Vector2 vector = new Vector2(1.0f, 2.0f);
Span<float> destination = new float[2];

Assert.False(vector.TryCopyTo(new Span<float>(new float[1])));
Assert.True(vector.TryCopyTo(destination));

Assert.Equal(1.0f, vector.X);
Assert.Equal(2.0f, vector.Y);
Assert.Equal(vector.X, destination[0]);
Assert.Equal(vector.Y, destination[1]);
}

[Fact]
public void Vector2GetHashCodeTest()
{
Expand Down Expand Up @@ -834,6 +864,18 @@ public void Vector2ConstructorTest4()
Assert.Equal(expected, target);
}

// A test for Vector2f (ReadOnlySpan<float>)
[Fact]
public void Vector2ConstructorTest5()
{
float value = 1.0f;
Vector2 target = new Vector2(new[] { value, value });
Vector2 expected = new Vector2(value);

Assert.Equal(expected, target);
Assert.Throws<IndexOutOfRangeException>(() => new Vector2(new float[1]));
}

// A test for Add (Vector2f, Vector2f)
[Fact]
public void Vector2AddTest()
Expand Down
46 changes: 46 additions & 0 deletions src/libraries/System.Numerics.Vectors/tests/Vector3Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,40 @@ public void Vector3CopyToTest()
Assert.Equal(3.3f, b[2]);
}

[Fact]
public void Vector3CopyToSpanTest()
{
Vector3 vector = new Vector3(1.0f, 2.0f, 3.0f);
Span<float> destination = new float[3];

Assert.Throws<ArgumentException>(() => vector.CopyTo(new Span<float>(new float[2])));
vector.CopyTo(destination);

Assert.Equal(1.0f, vector.X);
Assert.Equal(2.0f, vector.Y);
Assert.Equal(3.0f, vector.Z);
Assert.Equal(vector.X, destination[0]);
Assert.Equal(vector.Y, destination[1]);
Assert.Equal(vector.Z, destination[2]);
}

[Fact]
public void Vector3TryCopyToTest()
{
Vector3 vector = new Vector3(1.0f, 2.0f, 3.0f);
Span<float> destination = new float[3];

Assert.False(vector.TryCopyTo(new Span<float>(new float[2])));
Assert.True(vector.TryCopyTo(destination));

Assert.Equal(1.0f, vector.X);
Assert.Equal(2.0f, vector.Y);
Assert.Equal(3.0f, vector.Z);
Assert.Equal(vector.X, destination[0]);
Assert.Equal(vector.Y, destination[1]);
Assert.Equal(vector.Z, destination[2]);
}

[Fact]
public void Vector3GetHashCodeTest()
{
Expand Down Expand Up @@ -887,6 +921,18 @@ public void Vector3ConstructorTest4()
Assert.True(float.IsPositiveInfinity(target.Z), "Vector3f.constructor (Vector3f) did not return the expected value.");
}

// A test for Vector3f (ReadOnlySpan<float>)
[Fact]
public void Vector3ConstructorTest6()
{
float value = 1.0f;
Vector3 target = new Vector3(new[] { value, value, value });
Vector3 expected = new Vector3(value);

Assert.Equal(expected, target);
Assert.Throws<IndexOutOfRangeException>(() => new Vector3(new float[2]));
}

// A test for Add (Vector3f, Vector3f)
[Fact]
public void Vector3AddTest()
Expand Down
50 changes: 50 additions & 0 deletions src/libraries/System.Numerics.Vectors/tests/Vector4Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,44 @@ public void Vector4CopyToTest()
Assert.Equal(3.3f, b[3]);
}

[Fact]
public void Vector4CopyToSpanTest()
{
Vector4 vector = new Vector4(1.0f, 2.0f, 3.0f, 4.0f);
Span<float> destination = new float[4];

Assert.Throws<ArgumentException>(() => vector.CopyTo(new Span<float>(new float[3])));
vector.CopyTo(destination);

Assert.Equal(1.0f, vector.X);
Assert.Equal(2.0f, vector.Y);
Assert.Equal(3.0f, vector.Z);
Assert.Equal(4.0f, vector.W);
Assert.Equal(vector.X, destination[0]);
Assert.Equal(vector.Y, destination[1]);
Assert.Equal(vector.Z, destination[2]);
Assert.Equal(vector.W, destination[3]);
}

[Fact]
public void Vector4TryCopyToTest()
{
Vector4 vector = new Vector4(1.0f, 2.0f, 3.0f, 4.0f);
Span<float> destination = new float[4];

Assert.False(vector.TryCopyTo(new Span<float>(new float[3])));
Assert.True(vector.TryCopyTo(destination));

Assert.Equal(1.0f, vector.X);
Assert.Equal(2.0f, vector.Y);
Assert.Equal(3.0f, vector.Z);
Assert.Equal(4.0f, vector.W);
Assert.Equal(vector.X, destination[0]);
Assert.Equal(vector.Y, destination[1]);
Assert.Equal(vector.Z, destination[2]);
Assert.Equal(vector.W, destination[3]);
}

[Fact]
public void Vector4GetHashCodeTest()
{
Expand Down Expand Up @@ -1099,6 +1137,18 @@ public void Vector4ConstructorTest5()
Assert.True(float.Equals(float.Epsilon, target.W), "Vector4f.constructor (float, float, float, float) did not return the expected value.");
}

// A test for Vector4f (ReadOnlySpan<float>)
[Fact]
public void Vector4ConstructorTest7()
{
float value = 1.0f;
Vector4 target = new Vector4(new[] { value, value, value, value });
Vector4 expected = new Vector4(value);

Assert.Equal(expected, target);
Assert.Throws<IndexOutOfRangeException>(() => new Vector4(new float[3]));
}

// A test for Add (Vector4f, Vector4f)
[Fact]
public void Vector4AddTest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Internal.Runtime.CompilerServices;

namespace System.Numerics
{
Expand Down Expand Up @@ -39,6 +41,18 @@ public Vector2(float x, float y)
Y = y;
}

/// <summary>Constructs a vector from the given <see cref="ReadOnlySpan{Single}" />. The span must contain at least 2 elements.</summary>
/// <param name="values">The span of elements to assign to the vector.</param>
public Vector2(ReadOnlySpan<float> values)
{
if (values.Length < 2)
{
Vector.ThrowInsufficientNumberOfElementsException(2);
}

this = Unsafe.ReadUnaligned<Vector2>(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(values)));
}

/// <summary>Returns a vector whose 2 elements are equal to zero.</summary>
/// <value>A vector whose two elements are equal to zero (that is, it returns the vector <c>(0,0)</c>.</value>
public static Vector2 Zero
Expand Down Expand Up @@ -530,6 +544,34 @@ public readonly void CopyTo(float[] array, int index)
array[index + 1] = Y;
}

/// <summary>Copies the vector to the given <see cref="Span{T}" />.The length of the destination span must be at least 2.</summary>
/// <param name="destination">The destination span which the values are copied into.</param>
/// <exception cref="System.ArgumentException">If number of elements in source vector is greater than those available in destination span.</exception>
public readonly void CopyTo(Span<float> destination)
{
if (destination.Length < 2)
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}

Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(destination)), this);
}

/// <summary>Attempts to copy the vector to the given <see cref="Span{Single}" />. The length of the destination span must be at least 2.</summary>
/// <param name="destination">The destination span which the values are copied into.</param>
/// <returns><see langword="true" /> if the source vector was successfully copied to <paramref name="destination" />. <see langword="false" /> if <paramref name="destination" /> is not large enough to hold the source vector.</returns>
public readonly bool TryCopyTo(Span<float> destination)
{
if (destination.Length < 2)
{
return false;
}

Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(destination)), this);

return true;
}

/// <summary>Returns a value that indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param>
/// <returns><see langword="true" /> if the current instance and <paramref name="obj" /> are equal; otherwise, <see langword="false" />. If <paramref name="obj" /> is <see langword="null" />, the method returns <see langword="false" />.</returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Internal.Runtime.CompilerServices;

namespace System.Numerics
{
Expand Down Expand Up @@ -52,6 +54,18 @@ public Vector3(float x, float y, float z)
Z = z;
}

/// <summary>Constructs a vector from the given <see cref="ReadOnlySpan{Single}" />. The span must contain at least 3 elements.</summary>
/// <param name="values">The span of elements to assign to the vector.</param>
public Vector3(ReadOnlySpan<float> values)
{
if (values.Length < 3)
{
Vector.ThrowInsufficientNumberOfElementsException(3);
}

this = Unsafe.ReadUnaligned<Vector3>(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(values)));
}

/// <summary>Gets a vector whose 3 elements are equal to zero.</summary>
/// <value>A vector whose three elements are equal to zero (that is, it returns the vector <c>(0,0,0)</c>.</value>
public static Vector3 Zero
Expand Down Expand Up @@ -553,6 +567,34 @@ public readonly void CopyTo(float[] array, int index)
array[index + 2] = Z;
}

/// <summary>Copies the vector to the given <see cref="Span{T}" />. The length of the destination span must be at least 3.</summary>
/// <param name="destination">The destination span which the values are copied into.</param>
/// <exception cref="System.ArgumentException">If number of elements in source vector is greater than those available in destination span.</exception>
public readonly void CopyTo(Span<float> destination)
{
if (destination.Length < 3)
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}

Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(destination)), this);
}

/// <summary>Attempts to copy the vector to the given <see cref="Span{Single}" />. The length of the destination span must be at least 3.</summary>
/// <param name="destination">The destination span which the values are copied into.</param>
/// <returns><see langword="true" /> if the source vector was successfully copied to <paramref name="destination" />. <see langword="false" /> if <paramref name="destination" /> is not large enough to hold the source vector.</returns>
public readonly bool TryCopyTo(Span<float> destination)
{
if (destination.Length < 3)
{
return false;
}

Unsafe.WriteUnaligned(ref Unsafe.As<float, byte>(ref MemoryMarshal.GetReference(destination)), this);

return true;
}

/// <summary>Returns a value that indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param>
/// <returns><see langword="true" /> if the current instance and <paramref name="obj" /> are equal; otherwise, <see langword="false" />. If <paramref name="obj" /> is <see langword="null" />, the method returns <see langword="false" />.</returns>
Expand Down
Loading