def test_fuzzy_implies(): from syn.base_utils import fuzzy_implies assert feq(fuzzy_implies(0, 0), 1) assert feq(fuzzy_implies(0, 1), 1) assert feq(fuzzy_implies(1, 0), 0) assert feq(fuzzy_implies(1, 1), 1)
def test_fuzzy_equiv(): from syn.base_utils import fuzzy_equiv assert feq(fuzzy_equiv(0, 0), 1) assert feq(fuzzy_equiv(0, 1), 0) assert feq(fuzzy_equiv(1, 0), 0) assert feq(fuzzy_equiv(1, 1), 1)
def test_fuzzy_xor(): from syn.base_utils import fuzzy_xor assert feq(fuzzy_xor(0, 0), 0) assert feq(fuzzy_xor(0, 1), 1) assert feq(fuzzy_xor(1, 0), 1) assert feq(fuzzy_xor(1, 1), 0)
def test_union(): u = Union({1, 2, 3}, {3, 4, 5}) assert u._children == [SetWrapper({1, 2, 3}), SetWrapper({3, 4, 5})] assert set(u.enumerate()) == {1, 2, 3, 4, 5} assert set(u.enumerate(lazy=True)) == {1, 2, 3, 4, 5} assert u.size() == 5 assert u.size_limits() == (3, 6) assert feq(u.expected_size(), 4.5) assert u.hasmember(1) assert u.hasmember(5) assert not u.hasmember(6) for k in range(SAMPLES): item = u.sample() with on_error(elog, u.hasmember, (u, item)): assert u.hasmember(item) item = u.sample(lazy=True) assert u.hasmember(item) assert u.to_set() == {1, 2, 3, 4, 5} u2 = Union(Range(1, 3), Range(5, 7), Range(6, 9), NULL, {10}, {13}) assert u2.to_set() == {1, 2, 3, 5, 6, 7, 8, 9, 10, 13} assert sorted(u2.enumerate()) == [1, 2, 3, 5, 6, 7, 8, 9, 10, 13] assert len(list(u2.enumerate(max_enumerate = 2))) == 2 for k in range(SAMPLES): item = u2.sample() with on_error(elog, u2.hasmember, (u2, item)): assert u2.hasmember(item) assert u2.display() == '([1, 3] | [5, 7] | [6, 9] | {} | {10} | {13})' # Sanity Check class Foo(object): pass class F1(Foo): pass u = Union(range(10), Range(10, 20), ClassWrapper(Foo)) assert u.size() == 23 assert feq(u.expected_size(), 17) us = u.to_set() assert len(us) == 23 assert us == set(range(10)) | set(range(10, 21)) | set([Foo, F1]) # Edge Cases assert list(Union().enumerate()) == [] assert Union().to_set() == set() assert Union(Range(1, 2)).to_set() == {1, 2} assert Union({2, 4}).to_set() == {2, 4}
def test_prod(): from syn.base_utils import prod, feq assert prod([2, 3, 4]) == 24 assert feq(prod([2, 3, 4], log=True), 24) assert prod(range(1, 21)) == math.factorial(20)
def feq_comp(a, b, tol=DEFAULT_TOLERANCE, relative=True): if isinstance(a, STR) or isinstance(b, STR): return a == b if isinstance(a, CONTAINERS) or isinstance(b, CONTAINERS): return type(a) is type(b) and len(a) == len(b) if isinstance(a, complex) or isinstance(b, complex): return cfeq(a, b, tol, relative) return feq(a, b, tol, relative)
def test_enumeration_values(): assert TypeType(int).enumeration_value(0) == 0 v = ValuesType([1, 2, 3]) assert v.enumeration_value(0) == 1 assert v.enumeration_value(1) == 2 assert v.enumeration_value(2) == 3 assert v.enumeration_value(3) == 1 m = MultiType([int, float]) assert m.enumeration_value(0) == 0 assert feq(m.enumeration_value(1), 0.1) assert m.enumeration_value(2) == 2 assert feq(m.enumeration_value(3), 0.3) anys = [AnyType().enumeration_value(k) for k in xrange(SAMPLES)] if len(anys) > 2: assert any(x is not None for x in anys) class Foo(object): pass assert AnyType().enumeration_value(0, types=[Foo]) == 0
def test_product(): p = Product(Range(1, 5), Range(6, 10), Range(11, 15)) ps = p.to_set() assert (1, 6, 11) in ps assert (5, 6, 11) in ps assert (5, 10, 11) in ps assert (5, 10, 15) in ps assert p.display() == '([1, 5] x [6, 10] x [11, 15])' assert feq(p.expected_size(), 125) assert not p.hasmember((0, 6, 11)) for k in range(SAMPLES): item = p.sample() with on_error(elog, p.hasmember, (p, item)): assert p.hasmember(item) p = Product({1}, {2}, {3}) assert p.to_set() == set([(1, 2, 3)]) assert p.sample() == (1, 2, 3)
def test_feq(): from syn.base_utils import feq assert feq(1, 1) assert feq(1, 1.0) assert not feq(1, 2) assert not feq(1, 0.9) assert feq(1.0, 1.00) assert feq(1e-10, 2e-10, tol=1e-9) assert not feq(1e-10, 2e-10, tol=1e-11) assert not feq(1.0001, 1.0002, tol = 1e-5) assert feq(1.00001, 1.00002, tol = 1e-5) assert not feq(1.00001e300, 1.00002e300, tol = 1e-5) assert feq(1.00001e300, 1.00002e300, tol = 1e-5, relative=True) assert not feq(1.0001e300, 1.0002e300, tol = 1e-5, relative=True) assert not feq(1, 2, relative=True) assert feq('abc', 'abc') assert not feq('abc', 'ab')
def test_fuzzy_and(): from syn.base_utils import fuzzy_and assert feq(fuzzy_and(1, 1, 1), 1) assert feq(fuzzy_and(1, 1, 0), 0)
def test_fuzzy_not(): from syn.base_utils import fuzzy_not assert feq(fuzzy_not(1), 0) assert feq(fuzzy_not(0), 1)
def test_fuzzy_nand(): from syn.base_utils import fuzzy_nand assert feq(fuzzy_nand(1, 1, 1), 0) assert feq(fuzzy_nand(1, 1, 0), 1) assert feq(fuzzy_nand(0, 0, 0), 1)
def test_feq(): from syn.base_utils import feq assert feq(1, 1) assert feq(1, 1.0) assert not feq(1, 2) assert not feq(1, 0.9) assert feq(1.0, 1.00) assert feq(1e-10, 2e-10, tol=1e-9) assert not feq(1e-10, 2e-10, tol=1e-11) assert not feq(1.0001, 1.0002, tol=1e-5) assert feq(1.00001, 1.00002, tol=1e-5) assert not feq(1.00001e300, 1.00002e300, tol=1e-5) assert feq(1.00001e300, 1.00002e300, tol=1e-5, relative=True) assert not feq(1.0001e300, 1.0002e300, tol=1e-5, relative=True) assert not feq(1, 2, relative=True) assert feq('abc', 'abc') assert not feq('abc', 'ab')