示例#1
0
def hostnames(draw,
              allow_leading_digit=True,
              allow_idn=True):  # pragma: no cover
    # type: (DrawCallable, bool, bool) -> Text
    """
    A strategy which generates host names.

    @param allow_leading_digit: Whether to allow a leading digit in host names;
        they were not allowed prior to RFC 1123.

    @param allow_idn: Whether to allow non-ASCII characters as allowed by
        internationalized domain names (IDNs).
    """
    labels = draw(
        lists(hostname_labels(allow_idn=allow_idn), min_size=1,
              max_size=5).filter(
                  lambda ls: sum(len(l) for l in ls) + len(ls) - 1 <= 252))

    name = u".".join(labels)

    # Filter names that are not IDNA-encodable.
    # We try pretty hard not to generate bogus names in the first place... but
    # catching all cases is not trivial.
    try:
        idna_encode(name)
    except IDNAError:
        assume(False)

    return name
示例#2
0
 def test_flip(self, cls, values, start, end, cow):
     st.assume(start < end)
     bm_before = cls(values, copy_on_write=cow)
     bm_copy = cls(bm_before)
     bm_after = bm_before.flip(start, end)
     self.assertEqual(bm_before, bm_copy)
     self.check_flip(bm_before, bm_after, start, end)
示例#3
0
 def test_constructor_copy(self, values, other_value):
     st.assume(other_value not in values)
     bitmap1 = BitMap(values)
     bitmap2 = BitMap(bitmap1)
     self.assertEqual(bitmap1, bitmap2)
     bitmap1.add(other_value)
     self.assertNotEqual(bitmap1, bitmap2)
示例#4
0
 def test_flip(self, cls, values, start, end, cow):
     st.assume(start < end)
     bm_before = cls(values, copy_on_write=cow)
     bm_copy = cls(bm_before)
     bm_after = bm_before.flip(start, end)
     self.assertEqual(bm_before, bm_copy)
     self.check_flip(bm_before, bm_after, start, end)
示例#5
0
 def test_flip_empty(self, values, start, end, cow):
     st.assume(start >= end)
     bm_before = BitMap(values, copy_on_write=cow)
     bm_copy = BitMap(bm_before)
     bm_after = bm_before.flip(start, end)
     self.assertEqual(bm_before, bm_copy)
     self.assertEqual(bm_before, bm_after)
示例#6
0
 def test_flip(self, values, start, end):
     st.assume(start < end)
     bm_before = BitMap(values)
     bm_copy = BitMap(bm_before)
     bm_after = bm_before.flip(start, end)
     self.assertEqual(bm_before, bm_copy)
     self.check_flip(bm_before, bm_after, start, end)
def test_points_from_polygon_and_line(pair):
    """A Line through the centre of a polygon should have two intersections"""
    p, rp = pair
    centre = rp.args[0]
    assume(p != centre)
    line = Line(p, centre)
    assert len(EuclideanWorld([rp, line]).get_points()) == 2 + rp.args[2]
示例#8
0
 def test_serialization(self, values, other_value):
     st.assume(other_value not in values)
     old_bm = BitMap(values)
     buff = old_bm.serialize()
     new_bm = BitMap.deserialize(buff)
     self.assertEqual(old_bm, new_bm)
     old_bm.add(other_value)
     self.assertNotEqual(old_bm, new_bm)
示例#9
0
 def test_slice_select(self, values, start, stop, step, cow):
     st.assume(step != 0)
     bitmap = BitMap(values, copy_on_write=cow)
     values = list(bitmap)  # enforce sorted order
     expected = values[start:stop:step]
     expected.sort()
     observed = list(bitmap[start:stop:step])
     self.assertEqual(expected, observed)
示例#10
0
 def test_jaccard_index(self, cls1, cls2, values1, values2, cow):
     st.assume(len(values1) > 0 or len(values2) > 0)
     self.bitmap1 = cls1(values1, copy_on_write=cow)
     self.bitmap2 = cls2(values2, copy_on_write=cow)
     real_value = float(len(self.bitmap1 & self.bitmap2)) / \
         float(max(1, len(self.bitmap1 | self.bitmap2)))
     estimated_value = self.bitmap1.jaccard_index(self.bitmap2)
     self.assertAlmostEqual(real_value, estimated_value)
示例#11
0
 def test_jaccard_index(self, cls1, cls2, values1, values2, cow):
     st.assume(len(values1) > 0 or len(values2) > 0)
     self.bitmap1 = cls1(values1, copy_on_write=cow)
     self.bitmap2 = cls2(values2, copy_on_write=cow)
     real_value = float(len(self.bitmap1 & self.bitmap2)) / \
         float(max(1, len(self.bitmap1 | self.bitmap2)))
     estimated_value = self.bitmap1.jaccard_index(self.bitmap2)
     self.assertAlmostEqual(real_value, estimated_value)
示例#12
0
 def test_constructor_copy(self, values, other_value, cow1, cow2):
     st.assume(other_value not in values)
     bitmap1 = BitMap(values, copy_on_write=cow1)
     bitmap2 = BitMap(
         bitmap1,
         copy_on_write=cow2)  # should be robust even if cow2 != cow1
     self.assertEqual(bitmap1, bitmap2)
     bitmap1.add(other_value)
     self.assertNotEqual(bitmap1, bitmap2)
示例#13
0
def geometry_collection(draw):
    """
    To Do
    -----
    * bbox and foreign members
    """
    geoms = draw(lists(geometries()))  # can be empty
    assume(len(geoms) != 1)  # avoid single geom
    return {'type': 'GeometryCollection', 'geometries': geoms}
def test_all_circles_two_points(p1, p2):
    """
    Check whether `all_circles` yields all expected circles with two points
    """
    assume(p1 != p2)
    two_circles = EuclideanWorld.all_circles([p1, p2])
    assert len(two_circles) == 2
    r = p1.distance(p2)
    assert Circle(p1, r) in two_circles
    assert Circle(p2, r) in two_circles
示例#15
0
 def test_check_userdef_dist(self, positive_float, negative_float, word, rm):
     assume(negative_float < 0)
     Q = ciw.Simulation(
         ciw.create_network_from_yml(
         'ciw/tests/testing_parameters/params.yml'))
     self.assertEqual(
         Q.check_userdef_dist(lambda : positive_float), positive_float)
     self.assertRaises(
         ValueError, Q.check_userdef_dist, lambda : negative_float)
     self.assertRaises(
         ValueError, Q.check_userdef_dist, lambda : word)
示例#16
0
 def test_check_userdef_dist(self, positive_float, negative_float, word,
                             rm):
     assume(negative_float < 0)
     Q = ciw.Simulation(
         ciw.create_network_from_yml(
             'ciw/tests/testing_parameters/params.yml'))
     self.assertEqual(Q.check_userdef_dist(lambda: positive_float),
                      positive_float)
     self.assertRaises(ValueError, Q.check_userdef_dist,
                       lambda: negative_float)
     self.assertRaises(ValueError, Q.check_userdef_dist, lambda: word)
示例#17
0
 def test_check_userdef_dist(self, positive_float, negative_float, word,
                             rm):
     assume(negative_float < 0)
     Q = ciw.Simulation(
         ciw.load_parameters(
             'ciw/tests/datafortesting/logs_test_for_simulation/parameters.yml'
         ))
     self.assertEqual(Q.check_userdef_dist(lambda: positive_float),
                      positive_float)
     self.assertRaises(ValueError, Q.check_userdef_dist,
                       lambda: negative_float)
     self.assertRaises(ValueError, Q.check_userdef_dist, lambda: word)
示例#18
0
    def test_stochastic(self, p):

        assume(0 < p < 1)

        p1, p2 = axelrod.Cooperator(), axelrod.Cooperator()
        match = axelrod.Match((p1, p2), 5)
        self.assertFalse(match._stochastic)

        match = axelrod.Match((p1, p2), 5, noise=p)
        self.assertTrue(match._stochastic)

        p1 = axelrod.Random()
        match = axelrod.Match((p1, p2), 5)
        self.assertTrue(match._stochastic)
示例#19
0
    def test_stochastic(self, p):

        assume(0 < p < 1)

        p1, p2 = axelrod.Cooperator(), axelrod.Cooperator()
        match = axelrod.Match((p1, p2), 5)
        self.assertFalse(match._stochastic)

        match = axelrod.Match((p1, p2), 5, noise=p)
        self.assertTrue(match._stochastic)

        p1 = axelrod.Random()
        match = axelrod.Match((p1, p2), 5)
        self.assertTrue(match._stochastic)
示例#20
0
    def test_cache_update_required(self, p):

        assume(0 < p < 1)

        p1, p2 = axelrod.Cooperator(), axelrod.Cooperator()
        match = axelrod.Match((p1, p2), 5, noise=p)
        self.assertFalse(match._cache_update_required)

        cache = DeterministicCache()
        cache.mutable = False
        match = axelrod.Match((p1, p2), 5, deterministic_cache=cache)
        self.assertFalse(match._cache_update_required)

        match = axelrod.Match((p1, p2), 5)
        self.assertTrue(match._cache_update_required)

        p1 = axelrod.Random()
        match = axelrod.Match((p1, p2), 5)
        self.assertFalse(match._cache_update_required)
示例#21
0
    def test_cache_update_required(self, p):

        assume(0 < p < 1)

        p1, p2 = axelrod.Cooperator(), axelrod.Cooperator()
        match = axelrod.Match((p1, p2), 5, noise=p)
        self.assertFalse(match._cache_update_required)

        cache = DeterministicCache()
        cache.mutable = False
        match = axelrod.Match((p1, p2), 5, deterministic_cache=cache)
        self.assertFalse(match._cache_update_required)

        match = axelrod.Match((p1, p2), 5)
        self.assertTrue(match._cache_update_required)

        p1 = axelrod.Random()
        match = axelrod.Match((p1, p2), 5)
        self.assertFalse(match._cache_update_required)
示例#22
0
def hostname_labels(draw, allow_idn=True):  # pragma: no cover
    # type: (DrawCallable, bool) -> Text
    """
    A strategy which generates host name labels.

    @param allow_idn: Whether to allow non-ASCII characters as allowed by
        internationalized domain names (IDNs).
    """
    if allow_idn:
        label = draw(idna_text(min_size=1, max_size=63))

        try:
            label.encode("ascii")
        except UnicodeEncodeError:
            # If the label doesn't encode to ASCII, then we need to check the
            # length of the label after encoding to punycode and adding the
            # xn-- prefix.
            while len(label.encode("punycode")) > 63 - len("xn--"):
                # Rather than bombing out, just trim from the end until it is
                # short enough, so hypothesis doesn't have to generate new
                # data.
                label = label[:-1]

    else:
        label = draw(
            text(min_size=1,
                 max_size=63,
                 alphabet=unicode(ascii_letters + digits + u"-")))

    # Filter invalid labels.
    # It would be better not to generate bogus labels in the first place... but
    # that's not trivial.
    try:
        check_label(label)
    except UnicodeError:
        assume(False)

    return label
def test_all_circles_two_points(p1, p2, p3):
    """
    Check whether `all_circles` yields all expected circles with three points
    """
    assume(p1 != p2)
    assume(p2 != p3)
    assume(p3 != p1)
    two_circles = list(EuclideanWorld.all_circles([p1, p2, p3]))
    assert len(two_circles) <= 6
    assert len(two_circles) >= 3
    assert Circle(p1, p1.distance(p2)) in two_circles
    assert Circle(p1, p1.distance(p3)) in two_circles
    assert Circle(p2, p2.distance(p1)) in two_circles
    assert Circle(p2, p2.distance(p3)) in two_circles
    assert Circle(p3, p3.distance(p1)) in two_circles
    assert Circle(p3, p3.distance(p2)) in two_circles
示例#24
0
 def test_flip_inplace(self, values, start, end):
     st.assume(start < end)
     bm_before = BitMap(values)
     bm_after = BitMap(bm_before)
     bm_after.flip_inplace(start, end)
     self.check_flip(bm_before, bm_after, start, end)
示例#25
0
 def test_slice_select_none(self, cls, values, start, stop, step, cow):
     st.assume(step != 0)
     self.check_slice(cls, values, start, stop, step, cow)
示例#26
0
 def test_flip_inplace_empty(self, values, start, end):
     st.assume(start >= end)
     bm_before = BitMap(values)
     bm_after = BitMap(bm_before)
     bm_after.flip_inplace(start, end)
     self.assertEqual(bm_before, bm_after)
示例#27
0
 def test_flip_inplace_empty(self, values, start, end, cow):
     st.assume(start >= end)
     bm_before = BitMap(values, copy_on_write=cow)
     bm_after = BitMap(bm_before)
     bm_after.flip_inplace(start, end)
     self.assertEqual(bm_before, bm_after)
示例#28
0
 def test_max(self, cls, values, cow):
     st.assume(len(values) > 0)
     bitmap = cls(values, copy_on_write=cow)
     self.assertEqual(bitmap.max(), max(values))
示例#29
0
 def test_check_userdef_dist(self, positive_float, negative_float, word, rm):
     assume(negative_float < 0)
     Q = ciw.Simulation(ciw.load_parameters('ciw/tests/datafortesting/logs_test_for_simulation/parameters.yml'))
     self.assertEqual(Q.check_userdef_dist(lambda : positive_float), positive_float)
     self.assertRaises(ValueError, Q.check_userdef_dist, lambda : negative_float)
     self.assertRaises(ValueError, Q.check_userdef_dist, lambda : word)
示例#30
0
 def testArgs(self, data, endpoint, resource_id):
     assume(data != dict())
     cache.save(data, endpoint, resource_id)
     self.assertEqual(data, cache.load(endpoint, resource_id))
示例#31
0
 def test_hash_uneq(self, values1, values2):
     """This test as a non null (but extremly low) probability to fail."""
     bitmap1 = FrozenBitMap(values1)
     bitmap2 = FrozenBitMap(values2)
     st.assume(bitmap1 != bitmap2)
     self.assertNotEqual(hash(bitmap1), hash(bitmap2))
示例#32
0
 def test_hash_uneq(self, values1, values2):
     """This test as a non null (but extremly low) probability to fail."""
     bitmap1 = FrozenBitMap(values1)
     bitmap2 = FrozenBitMap(values2)
     st.assume(bitmap1 != bitmap2)
     self.assertNotEqual(hash(bitmap1), hash(bitmap2))
示例#33
0
 def test_max(self, cls, values, cow):
     st.assume(len(values) > 0)
     bitmap = cls(values, copy_on_write=cow)
     self.assertEqual(bitmap.max(), max(values))
示例#34
0
 def testArg_resource_id_Text(self, data, endpoint, resource_id):
     assume(data != dict())
     with self.assertRaises(ValueError):
         cache.save(data, endpoint, resource_id)
示例#35
0
 def test_slice_select_none(self, cls, values, start, stop, step, cow):
     st.assume(step != 0)
     self.check_slice(cls, values, start, stop, step, cow)
示例#36
0
 def test_slice_select_empty(self, cls, values, start, stop, step, cow):
     st.assume(step != 0)
     st.assume(len(range(start, stop, step)) == 0)
     self.check_slice(cls, values, start, stop, step, cow)
示例#37
0
 def test_bitmap_unequality(self, cls1, cls2, values1, values2, cow):
     st.assume(set(values1) != set(values2))
     bitmap1 = cls1(values1, copy_on_write=cow)
     bitmap2 = cls2(values2, copy_on_write=cow)
     self.assertNotEqual(bitmap1, bitmap2)
示例#38
0
 def test_slice_select_empty(self, cls, values, start, stop, step, cow):
     st.assume(step != 0)
     st.assume(len(range(start, stop, step)) == 0)
     self.check_slice(cls, values, start, stop, step, cow)
示例#39
0
 def test_flip_inplace(self, values, start, end, cow):
     st.assume(start < end)
     bm_before = BitMap(values, copy_on_write=cow)
     bm_after = BitMap(bm_before)
     bm_after.flip_inplace(start, end)
     self.check_flip(bm_before, bm_after, start, end)
示例#40
0
 def test_bitmap_unequality(self, cls1, cls2, values1, values2, cow):
     st.assume(set(values1) != set(values2))
     bitmap1 = cls1(values1, copy_on_write=cow)
     bitmap2 = cls2(values2, copy_on_write=cow)
     self.assertNotEqual(bitmap1, bitmap2)
示例#41
0
 def test_bitmap_unequality(self, values1, values2):
     st.assume(set(values1) != set(values2))
     bitmap1 = BitMap(values1)
     bitmap2 = BitMap(values2)
     self.assertNotEqual(bitmap1, bitmap2)
示例#42
0
 def testArg_subresource_Text(self, data, endpoint, resource_id,
                              subresource):
     assume(data != dict())
     cache.save(data, endpoint, resource_id, subresource)
     self.assertEqual(data, cache.load(endpoint, resource_id, subresource))
示例#43
0
 def testEnv_CacheFileNotFound(self, data, endpoint, resource_id):
     assume(data != dict())
     os.remove(cache.API_CACHE)
     self.assertIsNone(cache.save(data, endpoint, resource_id))