Skip to content

Commit

Permalink
Add cache and configuration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eelregit committed Jan 27, 2022
1 parent efd6fcb commit 0056535
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from numpy.testing import assert_raises

from jax_cosmo import Configuration
from jax_cosmo import Cosmology


Expand All @@ -21,6 +22,13 @@ def test_Cosmology_immutability():
cosmo.h = 0.74 # Hubble doesn't budge on the tension


def test_Conguration_immutability():
config = Configuration()

with assert_raises(FrozenInstanceError):
config.log10_a_max = 0.0


def test_cache():
cosmo = Cosmology(
Omega_c=0.25,
Expand All @@ -33,21 +41,22 @@ def test_cache():
wa=0.0,
)

def assert_pure(c1, c2):
assert c1 is not c2 and c1 == c2 and c1._cache != c2._cache

cosmo = cosmo.cache_set("a", 1)
cosmo = cosmo.cache_set("c", 3)

assert cosmo.is_cached("a")
assert not cosmo.is_cached("b")
assert cosmo.cache_get("c") == 3

cosmo = cosmo.cache_set("b", 2)
cosmo_no_c = cosmo.cache_del("c")
assert cosmo.is_cached("b")
assert not cosmo_no_c.is_cached("c")
assert (
cosmo_no_c is not cosmo
and cosmo_no_c == cosmo
and cosmo_no_c._cache != cosmo._cache
)
cosmo_add_b = cosmo.cache_set("b", 2)
cosmo_del_c = cosmo.cache_del("c")
cosmo_clean = cosmo.cache_clear()

cosmo = cosmo.cache_clear()
assert len(cosmo._cache) == 0
assert not cosmo_del_c.is_cached("c")
assert_pure(cosmo_add_b, cosmo) # test set purity
assert_pure(cosmo_del_c, cosmo) # test del purity
assert_pure(cosmo_clean, cosmo) # test clear purity
assert len(cosmo_clean._cache) == 0

0 comments on commit 0056535

Please sign in to comment.