예제 #1
0
    def test_max_overflow_bin_999(self):
        obj = Round(10, 100, max=150, overflow_bin=999)
        self.assertEqual(100, obj(100))
        self.assertEqual(140, obj(149))  # the last bin
        self.assertEqual(999, obj(150))  # overflow
        self.assertEqual(999, obj(500))  # overflow

        self.assertEqual(999, obj.next(140))  # the next to the last
        # bin is the overflow
        # bin

        self.assertEqual(999, obj.next(999))  # the next to the overflow
예제 #2
0
def test_max_int_on_a_boundary(overflow_bin):
    # max is always exactly a boundary
    max_ = 150
    obj = Round(10, 100, max=max_, overflow_bin=overflow_bin)
    assert 100 == obj(100)

    if overflow_bin is True:
        overflow_bin = obj.boundaries[-1]

    assert max_ == obj.boundaries[-1]
    assert obj(max_) is overflow_bin
    assert obj.next(obj.boundaries[-2]) is overflow_bin
    assert obj.next(overflow_bin) is overflow_bin
예제 #3
0
def test_max_int_on_a_boundary(overflow_bin):
    # max is always exactly a boundary
    max_ = 150
    obj = Round(10, 100, max=max_, overflow_bin=overflow_bin)
    assert 100 == obj(100)

    if overflow_bin is True:
        overflow_bin = obj.boundaries[-1]

    assert max_ == obj.boundaries[-1]
    assert obj(max_) is overflow_bin
    assert obj.next(obj.boundaries[-2]) is overflow_bin
    assert obj.next(overflow_bin) is overflow_bin
예제 #4
0
def test_next():
    obj = Round()
    assert  -0.5 == obj.next( -1.5)
    assert   0.5 == obj.next( -0.5)
    assert   1.5 == obj.next(  0.5)
    assert   2.5 == obj.next(  1.5)

    obj = Round(0.02, 0.005)
    assert  -0.015 == obj.next( -0.035)
    assert   0.005 == obj.next( -0.015)
    assert   0.025 == obj.next(  0.005)
    assert   0.045 == obj.next(  0.025)
    assert   0.065 == obj.next(  0.045)
예제 #5
0
    def test_min_underflow_bin(self):
        obj = Round(10, 100, min=30, underflow_bin=0)
        self.assertEqual(100, obj(100))
        self.assertEqual(30, obj(30))
        self.assertEqual(0, obj(29))

        self.assertEqual(obj(30), obj.next(0))  # the next to the underflow
예제 #6
0
def test_max_int():
    obj = Round(10, 100, max=150)
    assert   100 == obj( 100)
    assert  None == obj( 150)
    assert  None == obj( 500)

    assert  None == obj.next(140) # the next to the last bin is
예제 #7
0
def test_max_int():
    obj = Round(10, 100, max=150)
    assert 100 == obj(100)
    assert None == obj(150)
    assert None == obj(500)

    assert None == obj.next(140)  # the next to the last bin is
예제 #8
0
    def test_max(self):
        obj = Round(10, 100, max=150)
        self.assertEqual(100, obj(100))
        self.assertEqual(None, obj(150))
        self.assertEqual(None, obj(500))

        self.assertEqual(None, obj.next(140))  # the next to the last bin is
예제 #9
0
def test_max_float_on_a_boundary(width, max_, overflow_bin):

    # max_ on a boundary (within rounding of float)
    obj = Round(width, 1.0, max=max_, overflow_bin=overflow_bin)

    # example boundaries (depending on the architecture)
    # when width = 0.1:
    #     boundaries = [
    #         1.0, 1.1, 1.2000000000000002, 1.3000000000000003,
    #         1.4000000000000004, 1.5000000000000004, 1.6000000000000005,
    #         1.7000000000000006, 1.8000000000000007, 1.9000000000000008,
    #         2.000000000000001, 2.100000000000001, 2.200000000000001,
    #         2.300000000000001, 2.4000000000000012]
    #   slightly greater than 1.6 and 2.4
    #
    # when width = 0.2:
    #     boundaries = [
    #         1.0, 1.2, 1.4, 1.5999999999999999, 1.7999999999999998,
    #         1.9999999999999998, 2.1999999999999997, 2.4]
    #   slightly less than 1.6, exactly 2.4!

    #
    if overflow_bin is True:
        overflow_bin = obj.boundaries[-1]

    assert 1.0 == obj(1.0)
    # this is always true because 1.0 is the given boundary.

    # If the max is exactly a boundary, the max is the upper edge of
    # the last bin and bin(max) is overflow because the upper edge is
    # open. Otherwise, bin(max) is in the last bin.
    if max_ == obj.boundaries[-1]:
        assert obj(max_) == overflow_bin
    else:
        assert obj.boundaries[-2] == obj(max_)

    # The max is either the upper or lower edge of the last bin, but
    # not necessarily exact.
    if not max_ == pytest.approx(obj.boundaries[-1]):
        assert max_ == pytest.approx(obj.boundaries[-2])

    # the next to the last bin is the overflow bin
    assert obj.next(obj.boundaries[-2]) is overflow_bin

    # the next to the overflow bin is the overflow bin
    assert obj.next(overflow_bin) is overflow_bin
예제 #10
0
def test_max_float_on_a_boundary(width, max_, overflow_bin):

    # max_ on a boundary (within rounding of float)
    obj = Round(width, 1.0, max=max_, overflow_bin=overflow_bin)

    # example boundaries (depending on the architecture)
    # when width = 0.1:
    #     boundaries = [
    #         1.0, 1.1, 1.2000000000000002, 1.3000000000000003,
    #         1.4000000000000004, 1.5000000000000004, 1.6000000000000005,
    #         1.7000000000000006, 1.8000000000000007, 1.9000000000000008,
    #         2.000000000000001, 2.100000000000001, 2.200000000000001,
    #         2.300000000000001, 2.4000000000000012]
    #   slightly greater than 1.6 and 2.4
    #
    # when width = 0.2:
    #     boundaries = [
    #         1.0, 1.2, 1.4, 1.5999999999999999, 1.7999999999999998,
    #         1.9999999999999998, 2.1999999999999997, 2.4]
    #   slightly less than 1.6, exactly 2.4!

    #
    if overflow_bin is True:
        overflow_bin = obj.boundaries[-1]

    assert 1.0 == obj(1.0)
    # this is always true because 1.0 is the given boundary.

    # If the max is exactly a boundary, the max is the upper edge of
    # the last bin and bin(max) is overflow because the upper edge is
    # open. Otherwise, bin(max) is in the last bin.
    if max_ == obj.boundaries[-1]:
        assert obj(max_) == overflow_bin
    else:
        assert obj.boundaries[-2] == obj(max_)

    # The max is either the upper or lower edge of the last bin, but
    # not necessarily exact.
    if not max_ == pytest.approx(obj.boundaries[-1]):
        assert max_ == pytest.approx(obj.boundaries[-2])

    # the next to the last bin is the overflow bin
    assert obj.next(obj.boundaries[-2]) is overflow_bin

    # the next to the overflow bin is the overflow bin
    assert obj.next(overflow_bin) is overflow_bin
예제 #11
0
def test_min_on_a_boundary(width, underflow_bin):
    # this test is related to
    # the issue 43
    # https://github.com/alphatwirl/alphatwirl/issues/43

    min_ = 1.0 # on a boundary (within rounding of float)
    obj = Round(width, 2.0, min=min_, underflow_bin=underflow_bin)

    # example boundaries (depending on the architecture)
    # when width = 0.1:
    #     boundaries = [
    #         0.9999999999999992, 1.0999999999999992,
    #         1.1999999999999993, 1.2999999999999994, 1.3999999999999995,
    #         1.4999999999999996, 1.5999999999999996, 1.6999999999999997,
    #         1.7999999999999998, 1.9, 2.0]
    #   min=1.0 is the 1st element. but the precise value is slightly
    #   less than 1.0
    #
    # when width = 0.2:
    #     boundaries = [
    #         0.8000000000000003, 1.0000000000000002,
    #         1.2000000000000002, 1.4000000000000001, 1.6, 1.8, 2.0]
    #   min=1.0 is the 2nd element, slightly greater than 1.0.

    assert 2.0 == obj(2.0)
    # this is always true because 2.0 is the given boundary.

    # min=1.0 is a boundary, but not necessarily exact.
    if min_ == pytest.approx(obj(min_)):
        assert obj(1.05) == obj(min_)
        assert obj(0.95) is underflow_bin
    else:
        assert obj(0.95) == obj(min_)
        assert obj(0.95) is not underflow_bin
    # the results depend on the architecture.
    # it is wise to not set min a boundary.

    assert obj(0.7) is underflow_bin

    if underflow_bin is None:
        assert obj.next(underflow_bin) is None
    else:
        assert obj(min_) == obj.next(underflow_bin)
예제 #12
0
def test_min_on_a_boundary(width, underflow_bin):
    # this test is related to
    # the issue 43
    # https://github.com/alphatwirl/alphatwirl/issues/43

    min_ = 1.0  # on a boundary (within rounding of float)
    obj = Round(width, 2.0, min=min_, underflow_bin=underflow_bin)

    # example boundaries (depending on the architecture)
    # when width = 0.1:
    #     boundaries = [
    #         0.9999999999999992, 1.0999999999999992,
    #         1.1999999999999993, 1.2999999999999994, 1.3999999999999995,
    #         1.4999999999999996, 1.5999999999999996, 1.6999999999999997,
    #         1.7999999999999998, 1.9, 2.0]
    #   min=1.0 is the 1st element. but the precise value is slightly
    #   less than 1.0
    #
    # when width = 0.2:
    #     boundaries = [
    #         0.8000000000000003, 1.0000000000000002,
    #         1.2000000000000002, 1.4000000000000001, 1.6, 1.8, 2.0]
    #   min=1.0 is the 2nd element, slightly greater than 1.0.

    assert 2.0 == obj(2.0)
    # this is always true because 2.0 is the given boundary.

    # min=1.0 is a boundary, but not necessarily exact.
    if min_ == pytest.approx(obj(min_)):
        assert obj(1.05) == obj(min_)
        assert obj(0.95) is underflow_bin
    else:
        assert obj(0.95) == obj(min_)
        assert obj(0.95) is not underflow_bin
    # the results depend on the architecture.
    # it is wise to not set min a boundary.

    assert obj(0.7) is underflow_bin

    if underflow_bin is None:
        assert obj.next(underflow_bin) is None
    else:
        assert obj(min_) == obj.next(underflow_bin)
예제 #13
0
def test_next():
    obj = Round()
    assert -0.5 == obj.next(-1.5)
    assert 0.5 == obj.next(-0.5)
    assert 1.5 == obj.next(0.5)
    assert 2.5 == obj.next(1.5)

    obj = Round(0.02, 0.005)
    assert -0.015 == obj.next(-0.035)
    assert 0.005 == obj.next(-0.015)
    assert 0.025 == obj.next(0.005)
    assert 0.045 == obj.next(0.025)
    assert 0.065 == obj.next(0.045)
예제 #14
0
    def test_next(self):
        obj = Round()
        self.assertEqual(-0.5, obj.next(-1.5))
        self.assertEqual(0.5, obj.next(-0.5))
        self.assertEqual(1.5, obj.next(0.5))
        self.assertEqual(2.5, obj.next(1.5))

        obj = Round(0.02, 0.005)
        self.assertEqual(-0.015, obj.next(-0.035))
        self.assertEqual(0.005, obj.next(-0.015))
        self.assertEqual(0.025, obj.next(0.005))
        self.assertEqual(0.045, obj.next(0.025))
        self.assertEqual(0.065, obj.next(0.045))
예제 #15
0
 def test_inf(self):
     obj = Round(10, 100)
     self.assertIsNone(obj(float('inf')))
     self.assertIsNone(obj(float('-inf')))
     self.assertIsNone(obj.next(float('inf')))
     self.assertIsNone(obj.next(float('-inf')))
예제 #16
0
def test_inf():
    obj = Round(10, 100)
    assert obj(float('inf')) is None
    assert obj(float('-inf')) is None
    assert obj.next(float('inf')) is None
    assert obj.next(float('-inf')) is None
예제 #17
0
def test_inf():
    obj = Round(10, 100)
    assert obj(float('inf')) is None
    assert obj(float('-inf')) is None
    assert obj.next(float('inf')) is None
    assert obj.next(float('-inf')) is None