def _test_simple_truncation(self, tester=None, **options): """ Test whether the module can be truncated. """ from sage.misc.lazy_format import LazyFormat from yacop.categories.functors import truncation tester = self._tester(**options) pt = self.bbox().nearest_point(t=0, s=0, e=0) T = truncation(self, **pt.as_dict()) tester.assertTrue( T.bbox() == pt, LazyFormat("bbox %s of %s seems wrong, expected %s" % (T.bbox(), T, pt)), ) if False: # FIXME: this mostly fails el = T.an_element() tester.assertTrue( el.degree() == pt, LazyFormat("element %s of %s has wrong degree %s" % (el, T, el.degree())), )
def _test_category_contains(self, tester=None, **options): """ Test the implicit __contains__ method of this category:: sage: from yacop.modules.projective_spaces import RealProjectiveSpace sage: M=RealProjectiveSpace() sage: M.category() Category of yacop left module algebras over mod 2 Steenrod algebra, milnor basis sage: M in M.category() True sage: M._test_category_contains() """ from sage.misc.lazy_format import LazyFormat from sage.rings.finite_rings.finite_field_constructor import FiniteField tester = self._tester(**options) tester.assertTrue( self in self.category(), LazyFormat("%s not contained in its category %s" % (self, self.category())), ) M = ModulesWithBasis(FiniteField(self.base_ring().characteristic())) tester.assertTrue(self in M, LazyFormat("%s not contained in %s" % (self, M)))
def _test_suspension(self, tester=None, **options): from sage.misc.sage_unittest import TestSuite from sage.misc.lazy_format import LazyFormat is_sub_testsuite = tester is not None tester = self._tester(tester=tester, **options) myatt = "_suspension_tested" if not hasattr(self, myatt): s = self.an_element() X = suspension(self, t=5, s=3) tester.assertTrue( X is suspension(self, t=5, s=3), LazyFormat( "suspension(X,..) is not suspension(X,..) for X=%s") % (X, ), ) setattr(X, myatt, "yo") try: tester.info("\n Running the test suite of a suspension") TestSuite(X).run( verbose=tester._verbose, prefix=tester._prefix + " ", raise_on_failure=is_sub_testsuite, ) tester.info(tester._prefix + " ", newline=False) x = s.suspend(t=5, s=3) x3 = s.suspend(t=5, s=3) x2 = self.suspend_element(s, t=5, s=3) tester.assertEqual( x, x3, LazyFormat( "x.suspend(...) != x.suspend(...):\n A=%s\n B=%s" ) % ( x, x3, ), ) tester.assertEqual( x, x2, LazyFormat( "x.suspend(...) != parent.suspend_element(x,...):\n A=%s\n B=%s" ) % ( x, x2, ), ) tester.assertTrue( x.parent() == X, LazyFormat("suspended element %s not in suspension %s") % ( x, X, ), ) finally: delattr(X, myatt)
def _test_element_grading(self, tester=None, **options): tester = self._tester(tester=tester, **options) par = self.parent() for (deg, elem) in self.homogeneous_decomposition().items(): tester.assertTrue( not elem == par.zero(), LazyFormat("zeroes in homogeneous_decomposition of %s") % (self,), ) try: t, e, s = elem.t, elem.e, elem.s except: tester.assertTrue( False, LazyFormat("element %s of %s does not have t,e,s attributes") % (self, par), ) reg = region(t=t, e=e, s=s) gb = par.graded_basis(reg) tester.assertTrue( len(gb) > 0, LazyFormat("graded basis of %s in %s empty, should contain %s") % (par, reg, elem), ) gbm = set.union(*[set(x.monomials()) for x in gb]) for m in elem.monomials(): tester.assertTrue( m in gbm, LazyFormat( "element %s of degree (%d,%d,%d) not in graded_basis of its degree" ) % (m, t, e, s), ) for (toff, eoff, soff) in [ (1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1), ]: rg = region(t=t + toff, e=e + eoff, s=s + soff) gb = par.graded_basis(rg) if len(gb) > 0: gbm = set.union(*[set(x.monomials()) for x in gb]) for m in elem.monomials(): tester.assertTrue( not m in gbm, LazyFormat( "element %s of degree (%d,%d,%d) is also in graded_basis(%s)" ) % (m, t, e, s, rg), )
def _test_steenrod_action(self, tester=None, **options): from sage.misc.sage_unittest import TestSuite from sage.misc.lazy_format import LazyFormat tester = self._tester(**options) Y = self.__yacop_category__() A = Y.base_ring() elist = [] try: # under exotic circumstances, this code can fail: #24988 elist = list(A.some_elements()) for e in A.homogeneous_component(2 * (A.prime()**3 - 1)).basis(): elist.append(A(e)) except: pass is_left = LeftModules(A) in Y.all_super_categories() is_right = RightModules(A) in Y.all_super_categories() x = self.an_element() for e in elist: if is_left: y = e * x # does left action work? tester.assertEqual( y, steenrod_antipode(e) % x, LazyFormat( "conjugate action check failed: e.antipode()%x != e*x for x=%s, e=%s" ) % (x, e), ) if is_right: y = x * e # does right action work? tester.assertEqual( y, x % steenrod_antipode(e), LazyFormat( "conjugate action check failed: x%e.antipode() != x*e for x=%s, e=%s" ) % (x, e), ) if is_left and is_right: tester.assertEqual( (e * x) * e, e * (x * e), LazyFormat( "bimodule check failed: (ex)e != e(xe) for x=%s, e=%s") % (x, e), ) if False: tester.assertTrue( bbox.__class__ == region, LazyFormat("bounding box of %s is not a region") % (self, ), )
def _test_element_dumping(self, tester=None, **options): tester = self._tester(tester=tester, **options) par = self.parent() tester.assertTrue( par.load_element(par.dump_element(self)) == self, LazyFormat("loading dump of %s does not give back original") % (self,), )
def _test_some_elements(self, **options): """ Run generic tests on the method :meth:`.some_elements`. See also: :class:`TestSuite`. EXAMPLES:: sage: C = Sets().example() sage: C._test_some_elements() Let us now write a broken :meth:`.some_elements` method:: sage: from sage.categories.examples.sets_cat import * sage: class CCls(PrimeNumbers): ... def some_elements(self): ... return [self(17), 32] sage: CC = CCls() sage: CC._test_some_elements() Traceback (most recent call last): ... AssertionError: the object 32 in self.some_elements() is not in self """ tester = self._tester(**options) elements = self.some_elements() # Todo: enable this once #tester.assert_(elements != iter(elements), # "self.some_elements() should return an iterable, not an iterator") for x in elements: tester.assertTrue( x in self, LazyFormat( "the object %s in self.some_elements() is not in self") % (x, ))
def _test_dump_element(self, tester=None, **options): tester = self._tester(tester=tester, **options) from sage.misc.sage_unittest import TestSuite from sage.misc.lazy_format import LazyFormat for (cnt, el) in zip(list(range(0, 10)), self.some_elements()): str = self.dump_element(el) oth = self.load_element(str) tester.assertEqual( el, oth, LazyFormat("load_element(dump_element(el)) != el for el = %s") % el, )
def _test_homset_sanity(self, tester=None, **options): """ Test whether the identity can be constructed and has its kernel, coker, etc. in the right category. This can fail if Sage puts the wrong "kernel" method into the mro of the Homset category. """ from sage.misc.lazy_format import LazyFormat tester = self._tester(**options) id = self.module_morphism(codomain=self, function=lambda x: x) wanted = self.__yacop_category__().Subquotients() tester.assertTrue( id.kernel() in wanted, LazyFormat("identity of %s has a bad kernel" % (self, )), ) tester.assertTrue( id.cokernel() in wanted, LazyFormat("identity of %s has a bad cokernel" % (self, )), ) tester.assertTrue( id.image() in wanted, LazyFormat("identity of %s has a bad image" % (self, )), )
def _test_generator_degrees(self, tester=None, **options): from sage.misc.sage_unittest import TestSuite from itertools import islice is_sub_testsuite = tester is not None tester = self._tester(tester=tester, **options) if self._is_sorted_by_t_degrees(): gdegs = [g[0] for g in islice(self._degrees, 30)] gd = gdegs for u in gd: if u < 0: gd = [-u for u in gdegs] break for (u, v) in zip(gd, gd[1:]): tester.assertTrue( u <= v, LazyFormat( "generators not sorted by internal degree, degrees= %s..." ) % (gdegs,), ) octs = self.octants() for (t, e, s) in islice(self._degrees, 30): if 0 != (e & 1): # allow mismatch for exterior generators continue ok = False for (ts, es, ss) in octs: if (sgn(t) in (ts, 0)) and (sgn(e) in (es, 0)) and (sgn(s) in (ss, 0)): ok = True break tester.assertTrue( ok, LazyFormat("generator degree (%d,%d,%d) not in one of the octants %s") % (t, e, s, octs), )
def print_compare(x, y): if x == y: return LazyFormat("%s == %s") % (x, y) else: return LazyFormat("%s != %s") % (x, y)
def _test_elements_eq(self, **options): """ Runs generic tests on the equality of elements. In particular, this tests that ``==`` is reflexive, symmetric, and transitive on some_elements of ``self`` together with ``0`` and ``None``. This also tests the consistency with inequality tests with ``!=``. See also: :class:`TestSuite`. EXAMPLES:: sage: C = Sets().example() sage: C._test_elements_eq() Let us test the consistency of a broken equality or inequality:: sage: P = Sets().example("wrapper") sage: P._test_elements_eq() sage: ne = P.element_class.__ne__ sage: eq = P.element_class.__eq__ We first try a broken inequality:: sage: P.element_class.__ne__ = lambda x, y: False sage: P._test_elements_eq() Traceback (most recent call last): ... AssertionError: __eq__ and __ne__ inconsistency: 47 == 53 returns False but 47 != 53 returns False sage: P.element_class.__ne__ = lambda x, y: not(x == y) We then try a non-reflexive equality:: sage: P.element_class.__eq__ = (lambda x, y: ... False if eq(x, P(47)) and eq(y, P(47)) else eq(x, y)) sage: P._test_elements_eq() Traceback (most recent call last): ... AssertionError: non reflexive equality: 47 != 47 What about a non symmetric equality:: sage: def non_sym_eq(x, y): ... if not y in P: return False ... elif eq(x, P(47)) and eq(y, P(53)): return True ... else: return eq(x, y) sage: P.element_class.__eq__ = non_sym_eq sage: P._test_elements_eq() Traceback (most recent call last): ... AssertionError: non symmetric equality: 53 != 47 but 47 == 53 And finally a non transitive equality:: sage: def non_sym_eq(x, y): ... if not y in P: return False ... elif eq(x, P(47)) and eq(y, P(53)): return True ... elif eq(x, P(53)) and eq(y, P(47)): return True ... elif eq(x, P(47)) and eq(y, P(59)): return True ... elif eq(x, P(59)) and eq(y, P(47)): return True ... else: return eq(x, y) sage: P.element_class.__eq__ = non_sym_eq sage: P._test_elements_eq() Traceback (most recent call last): ... AssertionError: non transitive equality: 53 == 47 and 47 == 59 but 53 != 59 We restore ``P.element_class`` in a proper state for further tests:: sage: P.element_class.__ne__ = ne sage: P.element_class.__eq__ = eq """ tester = self._tester(**options) elements = list(self.some_elements()) + [None, 0] # Note: we can't expect that all those elements are hashable equal_eli_elj = {} def print_compare(x, y): if x == y: return LazyFormat("%s == %s") % (x, y) else: return LazyFormat("%s != %s") % (x, y) for i, eli in enumerate(elements): for j, elj in enumerate(elements): equal_eli_elj[i, j] = (eli == elj) tester.assertNotEqual( equal_eli_elj[i, j], eli != elj, LazyFormat( "__eq__ and __ne__ inconsistency:\n" " %s == %s returns %s but %s != %s returns %s") % (eli, elj, (eli == elj), eli, elj, (eli != elj))) if i == j: tester.assertTrue( equal_eli_elj[i, i], LazyFormat("non reflexive equality: %s != %s") % (eli, eli)) if i > j: # (j, i) is already computed tester.assertEqual( equal_eli_elj[i, j], equal_eli_elj[j, i], LazyFormat("non symmetric equality: %s but %s") % (print_compare(eli, elj), print_compare(elj, eli))) # check for transitivity nbel = len(elements) for i in range(nbel): for j in range(nbel): if not equal_eli_elj[i, j]: continue for k in range(nbel): if not equal_eli_elj[j, k]: continue tester.assertTrue( equal_eli_elj[i, k], LazyFormat("non transitive equality:\n" "%s and %s but %s") % (print_compare(elements[i], elements[j]), print_compare(elements[j], elements[k]), print_compare(elements[i], elements[k])))
def _xx_test_truncation(self, tester=None, **options): from yacop.categories.functors import truncation from sage.misc.sage_unittest import TestSuite from sage.misc.lazy_format import LazyFormat is_sub_testsuite = tester is not None tester = self._tester(tester=tester, **options) myatt = "_truncation_tested" if not hasattr(self, myatt): # find a non-zero action a*m = n in self def testops(A): for _ in A.some_elements(): yield _ maxdeg = A.prime()**3 for deg in range(1, maxdeg): for key in A.homogeneous_component(maxdeg - deg + 1).basis(): yield A(key) n = self.zero() for (deg, m) in self.an_element().homogeneous_decomposition().items(): if not m.is_zero(): for a in testops(self._yacop_base_ring): n = a * m # print("trying %s*%s->%s"%(a,m,n)) if not n.is_zero(): break if not n.is_zero(): break if n.is_zero() or m.t == n.t: print( ("WARN: could not find a non-trivial action in %s" % self)) else: tests = [] # tests are tuples: # 1 region to truncate to # flags whether # 2 m survives to truncation # 3 n survives to truncation # and # 4 result of computing a*m in the truncation tests.append(((m.t, n.t), True, True, n)) tests.append(((m.t, m.t), True, False, 0)) tests.append(((n.t, n.t), False, True, 0)) for ((tmin, tmax), sm, sn, am) in tests: T = truncation(self, tmin=tmin, tmax=tmax) if sm: tester.assertTrue( m in T, LazyFormat("contains (+) broken for %s (elem %s)") % (T, n), ) tester.assertTrue( self(T(m)) == m, LazyFormat( "casting from/to truncation broken for %s (elem %s)" ) % (T, n), ) tester.assertTrue( T(m).parent() is T, LazyFormat("wrong parent for %s (elem %s)") % (T, n), ) else: tester.assertTrue( not m in T, LazyFormat("contains (-) broken for %s (elem %s)") % (T, n), ) ok = False try: x = T(m) except: ok = True tester.assertTrue( ok, LazyFormat("no exception when casting %s into %s" % (m, T)), ) if sn: tester.assertEqual( n, self(T(n)), LazyFormat("casting %s into %s destroys element" % (n, T)), ) else: tester.assertTrue( T(n).is_zero(), LazyFormat("%s does not map to zero in %s" % (n, T)), ) if sm: tester.assertEqual( a * T(m), T(am), LazyFormat("bad action %s * %s != %s" % (a, m, n)), )
def _test_yacop_grading(self, tester=None, **options): from sage.misc.sage_unittest import TestSuite from sage.misc.lazy_format import LazyFormat from sage.categories.enumerated_sets import EnumeratedSets from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets is_sub_testsuite = tester is not None tester = self._tester(tester=tester, **options) if hasattr(self.grading(), "_domain"): tester.assertTrue( self is self.grading()._domain, LazyFormat("grading of %s has wrong _domain attribute %s") % (self, self.grading()._domain), ) bbox = self.bbox() tester.assertTrue( bbox.__class__ == region, LazyFormat("bounding box of %s is not a region") % (self, ), ) # make sure we can get a basis of the entire module basis = self.graded_basis() tester.assertTrue( basis in EnumeratedSets(), LazyFormat("graded basis of %s is not an EnumeratedSet") % (self, ), ) # nontrivial_degrees ntdegs = self.nontrivial_degrees() tester.assertTrue( basis in EnumeratedSets(), LazyFormat("nontrivial_degrees of %s is not an EnumeratedSet") % (self, ), ) # check whether a presumably finite piece is also an EnumeratedSet rg = region(tmax=10, tmin=-10, smax=10, smin=-10, emax=10, emin=-10) basis = self.graded_basis(rg) tester.assertTrue( basis in EnumeratedSets(), LazyFormat( "graded basis of %s in the region %s is not an EnumeratedSet" ) % (self, rg), ) for (deg, elem) in self._some_homogeneous_elements(): deg2 = self.degree(elem) if hasattr(elem, "degree"): deg3 = elem.degree() tester.assertEqual( deg2, deg3, LazyFormat( "parent.degree(el) != (el).degree() for parent %s and element %s" ) % (self, elem), ) tester.assertEqual( deg2, deg, LazyFormat( "homogeneuous_decomposition claims %s has degree %s, but degree says %s" ) % (elem, deg, deg2), ) # test graded_basis_coefficients b = self.grading().basis(deg) tester.assertTrue( b.cardinality() < Infinity, LazyFormat("%s not finite in degree %s") % (self, deg), ) from sage.categories.commutative_additive_groups import ( CommutativeAdditiveGroups, ) if self in CommutativeAdditiveGroups(): el = self.an_element() sp = el.homogeneous_decomposition() for (deg, elem) in list(sp.items()): b = self.grading().basis(deg) c = self.graded_basis_coefficients(elem, deg) sm = sum(cf * be for (cf, be) in zip(c, b)) if elem != sm: print(("self=", self)) print(("elem=", elem)) print(("deg=", deg)) print(("graded basis=", list(b))) print(("lhs=", elem.monomial_coefficients())) print(("rhs=", sm.monomial_coefficients())) tester.assertEqual( elem, sm, LazyFormat( "graded_basis_coefficients failed on element %s of %s: sums to %s" ) % (elem, self, sm), ) el2 = sum(smd for (key, smd) in list(sp.items())) tester.assertEqual( el.parent(), el2.parent(), LazyFormat( "%s and the sum of its homogeneous pieces have different parents:\n %s\n!= %s" ) % ( el, el.parent(), el2.parent(), ), ) if el.parent() is el2.parent(): tester.assertEqual( el, el2, LazyFormat( "%s is not the sum of its homogeneous pieces (which is %s)" ) % ( el, el2, ), )