Skip to content

Commit

Permalink
tests: add
Browse files Browse the repository at this point in the history
Signed-off-by: nstarman <[email protected]>
  • Loading branch information
nstarman committed Feb 21, 2024
1 parent 8c2e200 commit b381df5
Show file tree
Hide file tree
Showing 3 changed files with 352 additions and 6 deletions.
27 changes: 26 additions & 1 deletion tests/test_d1.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Test :mod:`vector._d1`."""

import astropy.units as u
import jax.numpy as jnp
import pytest
from jax_quantity import Quantity
from quax import quaxify

from vector import (
AbstractVector,
Expand All @@ -20,10 +22,13 @@
RadialVector,
SphericalDifferential,
SphericalVector,
represent_as,
)

from .test_base import AbstractVectorDifferentialTest, AbstractVectorTest

array_equal = quaxify(jnp.array_equal)


class Abstract1DVectorTest(AbstractVectorTest):
"""Test :class:`vector.Abstract1DVector`."""
Expand All @@ -44,15 +49,20 @@ def vector(self) -> AbstractVector:

def test_cartesian1d_to_cartesian1d(self, vector):
"""Test ``vector.represent_as(Cartesian1DVector)``."""
# Jit can copy
newvec = vector.represent_as(Cartesian1DVector)
assert newvec == vector

# The normal `represent_as` method should return the same object
newvec = represent_as(vector, Cartesian1DVector)
assert newvec is vector

def test_cartesian1d_to_radial(self, vector):
"""Test ``vector.represent_as(RadialVector)``."""
radial = vector.represent_as(RadialVector)

assert isinstance(radial, RadialVector)
assert radial.r == Quantity([1, 2, 3, 4], u.kpc)
assert array_equal(radial.r, Quantity([1, 2, 3, 4], u.kpc))

def test_cartesian1d_to_cartesian2d(self, vector):
"""Test ``vector.represent_as(Cartesian2DVector)``."""
Expand Down Expand Up @@ -148,7 +158,12 @@ def test_radial_to_cartesian1d(self, vector):

def test_radial_to_radial(self, vector):
"""Test ``vector.represent_as(RadialVector)``."""
# Jit can copy
newvec = vector.represent_as(RadialVector)
assert newvec == vector

# The normal `represent_as` method should return the same object
newvec = represent_as(vector, RadialVector)
assert newvec is vector

def test_radial_to_cartesian2d(self, vector):
Expand Down Expand Up @@ -238,7 +253,12 @@ def vector(self) -> Cartesian1DVector:
@pytest.mark.filterwarnings("ignore:Explicitly requested dtype")
def test_cartesian1d_to_cartesian1d(self, difntl, vector):
"""Test ``difntl.represent_as(CartesianDifferential1D)``."""
# Jit can copy
newvec = difntl.represent_as(CartesianDifferential1D, vector)
assert newvec == difntl

# The normal `represent_as` method should return the same object
newvec = represent_as(difntl, CartesianDifferential1D, vector)
assert newvec is difntl

@pytest.mark.filterwarnings("ignore:Explicitly requested dtype")
Expand Down Expand Up @@ -349,7 +369,12 @@ def test_radial_to_cartesian1d(self, difntl, vector):
@pytest.mark.filterwarnings("ignore:Explicitly requested dtype")
def test_radial_to_radial(self, difntl, vector):
"""Test ``difntl.represent_as(RadialDifferential)``."""
# Jit can copy
newvec = difntl.represent_as(RadialDifferential, vector)
assert newvec == difntl

# The normal `represent_as` method should return the same object
newvec = represent_as(difntl, RadialDifferential, vector)
assert newvec is difntl

@pytest.mark.xfail(reason="Not implemented")
Expand Down
32 changes: 28 additions & 4 deletions tests/test_d2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
PolarVector,
RadialVector,
SphericalVector,
represent_as,
)
from vector._d1.builtin import CartesianDifferential1D, RadialDifferential
from vector._d2.builtin import CartesianDifferential2D, PolarDifferential
Expand Down Expand Up @@ -60,6 +61,16 @@ def test_cartesian2d_to_radial(self, vector):
assert isinstance(radial, RadialVector)
assert radial.r == Quantity([1, 2, 3, 4], u.kpc)

def test_cartesian2d_to_cartesian2d(self, vector):
"""Test ``vector.represent_as(Cartesian2DVector)``."""
# Jit can copy
newvec = vector.represent_as(Cartesian2DVector)
assert newvec == vector

# The normal `represent_as` method should return the same object
newvec = represent_as(vector, Cartesian2DVector)
assert newvec is vector

def test_cartesian2d_to_polar(self, vector):
"""Test ``vector.represent_as(PolarVector)``."""
polar = vector.represent_as(PolarVector, phi=Quantity([0, 1, 2, 3], u.rad))
Expand Down Expand Up @@ -151,7 +162,12 @@ def test_polar_to_cartesian2d(self, vector):

def test_polar_to_polar(self, vector):
"""Test ``vector.represent_as(PolarVector)``."""
# Jit can copy
newvec = vector.represent_as(PolarVector)
assert newvec == vector

# The normal `represent_as` method should return the same object
newvec = represent_as(vector, PolarVector)
assert newvec is vector

# def test_polar_to_lnpolar(self, vector):
Expand Down Expand Up @@ -242,9 +258,13 @@ def test_cartesian2d_to_radial(self, difntl, vector):
@pytest.mark.filterwarnings("ignore:Explicitly requested dtype")
def test_cartesian2d_to_cartesian2d(self, difntl, vector):
"""Test ``difntl.represent_as(CartesianDifferential2D, vector)``."""
newdifntl = difntl.represent_as(CartesianDifferential2D, vector)
# Jit can copy
newvec = difntl.represent_as(CartesianDifferential2D, vector)
assert newvec == difntl

assert newdifntl is difntl
# The normal `represent_as` method should return the same object
newvec = represent_as(difntl, CartesianDifferential2D, vector)
assert newvec is difntl

@pytest.mark.filterwarnings("ignore:Explicitly requested dtype")
def test_cartesian2d_to_polar(self, difntl, vector):
Expand Down Expand Up @@ -346,9 +366,13 @@ def test_polar_to_cartesian2d(self, difntl, vector):
@pytest.mark.filterwarnings("ignore:Explicitly requested dtype")
def test_polar_to_polar(self, difntl, vector):
"""Test ``difntl.represent_as(PolarDifferential, vector)``."""
newdifntl = difntl.represent_as(PolarDifferential, vector)
# Jit can copy
newvec = difntl.represent_as(PolarDifferential, vector)
assert newvec == difntl

assert newdifntl is difntl
# The normal `represent_as` method should return the same object
newvec = represent_as(difntl, PolarDifferential, vector)
assert newvec is difntl

@pytest.mark.xfail(reason="Not implemented")
@pytest.mark.filterwarnings("ignore:Explicitly requested dtype")
Expand Down
Loading

0 comments on commit b381df5

Please sign in to comment.