Beispiel #1
0
    def test_insert_into_global_state_branch_of_leaf_unit(self):
        self._state_handler.initialize(self._root_nodes)
        branch = Node(Unit(identifier=(0,), position=[0.2, 0.3], charge=None,
                           velocity=[0.5, 0], time_stamp=Time(0.0, 0.3)), weight=1)
        branch.add_child(Node(Unit(identifier=(0, 1), position=[0.4, 0.6],
                                   charge={"e": -1}, velocity=[1, 0], time_stamp=Time(0.0, 0.3)), weight=0.5))
        self._state_handler.insert_into_global_state([branch])
        all_root_cnodes = self._state_handler.extract_global_state()

        self.assertEqual(len(all_root_cnodes), 2)
        unit = all_root_cnodes[0].value
        self.assertEqual(unit.position, [0.2, 0.3])
        self.assertIsNone(unit.charge)
        self.assertEqual(unit.identifier, (0,))
        self.assertEqual(unit.velocity, [0.5, 0])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.3))
        self.assertEqual(len(all_root_cnodes[0].children), 2)

        unit = all_root_cnodes[0].children[0].value
        self.assertEqual(unit.position, [0, 0])
        self.assertEqual(unit.charge, {"e": 1})
        self.assertEqual(unit.identifier, (0, 0))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[0].children[0].children), 0)

        unit = all_root_cnodes[0].children[1].value
        self.assertEqual(unit.position, [0.4, 0.6])
        self.assertEqual(unit.charge, {"e": -1})
        self.assertEqual(unit.identifier, (0, 1))
        self.assertEqual(unit.velocity, [1, 0])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.3))
        self.assertEqual(len(all_root_cnodes[0].children[1].children), 0)

        unit = all_root_cnodes[1].value
        self.assertEqual(unit.position, [0.9, 0.775])
        self.assertIsNone(unit.charge)
        self.assertEqual(unit.identifier, (1,))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[1].children), 2)

        unit = all_root_cnodes[1].children[0].value
        self.assertEqual(unit.position, [0.9, 0.8])
        self.assertEqual(unit.charge, {"e": 1})
        self.assertEqual(unit.identifier, (1, 0))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[1].children[0].children), 0)

        unit = all_root_cnodes[1].children[1].value
        self.assertEqual(unit.position, [0.9, 0.75])
        self.assertEqual(unit.charge, {"e": -1})
        self.assertEqual(unit.identifier, (1, 1))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[1].children[1].children), 0)
Beispiel #2
0
 def test_update(self):
     time = Time(3.0, 0.3247823)
     other_time = Time(6.0, 0.1231)
     time.update(other_time)
     self.assertEqual(time.quotient, 6.0)
     self.assertEqual(time.remainder, 0.1231)
     self.assertEqual(other_time.quotient, 6.0)
     self.assertEqual(other_time.remainder, 0.1231)
     self.assertIsNot(time, other_time)
Beispiel #3
0
 def test_extract_from_global_state_copied_time_stamp(self):
     self._state_handler.initialize(self._root_nodes)
     branch = Node(Unit(identifier=(0,), position=[0.2, 0.3], charge=None,
                        velocity=[0.5, 0], time_stamp=Time(0.0, 0.3)), weight=1)
     branch.add_child(Node(Unit(identifier=(0, 1), position=[0.4, 0.6],
                                charge={"e": -1}, velocity=[1, 0], time_stamp=Time(0.0, 0.3)), weight=0.5))
     self._state_handler.insert_into_global_state([branch])
     root_cnode = self._state_handler.extract_from_global_state((0, 1))
     time_stamp = root_cnode.value.time_stamp
     time_stamp.update(Time(1.0, 0.2))
     all_root_cnodes = self._state_handler.extract_global_state()
     self.assertEqual(all_root_cnodes[0].value.time_stamp, Time(0.0, 0.3))
Beispiel #4
0
 def test_insert_into_global_state_velocity_none_but_time_stamp_not_none_raises_error(self):
     self._state_handler.initialize(self._root_nodes)
     branch = Node(Unit(identifier=(0,), position=[0.2, 0.3], charge=None,
                        velocity=[0.5, 0], time_stamp=Time(0.0, 0.3)), weight=1)
     branch.add_child(Node(Unit(identifier=(0, 1), position=[0.4, 0.6], charge={"e": -1},
                                velocity=[1, 0], time_stamp=Time(0.0, 0.3)), weight=0.5))
     self._state_handler.insert_into_global_state([branch])
     branch = Node(Unit(identifier=(0,), position=[0.1, 0.2], charge=None,
                        velocity=None, time_stamp=Time(0.0, 0.5)), weight=1)
     branch.add_child(Node(Unit(identifier=(0, 1), position=[0.3, 0.4],
                                charge={"e": -1}, velocity=None, time_stamp=Time(0.0, 0.5)), weight=0.5))
     branch.add_child(Node(Unit(identifier=(0, 0), position=[0.5, 0.6],
                                charge={"e": 1}, velocity=None, time_stamp=Time(0.0, 0.5)), weight=0.5))
     with self.assertRaises(AssertionError):
         self._state_handler.insert_into_global_state([branch])
    def send_out_state(self, cnode_with_initially_active_unit: Node) -> Sequence[Node]:
        """
        Return the out-state.

        This method receives the branch of the initially active unit.

        Parameters
        ----------
        cnode_with_initially_active_unit : base.node.Node
            The branch of the initially active unit.

        Returns
        -------
        Sequence[base.node.Node]
            The out-state.
        """
        self._store_in_state([cnode_with_initially_active_unit])
        self._construct_leaf_cnodes()

        for leaf_cnode in self._leaf_cnodes:
            unit = leaf_cnode.value
            assert unit.velocity is None
            assert unit.time_stamp is None
            unit.velocity = self._initial_velocity.copy()
            unit.time_stamp = Time(0.0, 0.0)
            self._register_velocity_change_leaf_cnode(leaf_cnode, self._initial_velocity)

        self._commit_non_leaf_velocity_changes()
        return self._state
Beispiel #6
0
    def __init__(self, warn_on_equal_event_times: bool = False) -> None:
        """
        The constructor of the HeapScheduler class.

        Parameters
        ----------
        warn_on_equal_event_times : bool, optional
            Whether this scheduler should log a warning when succeeding event times are equal.

        Raises
        ------
        MemoryError
            If the C code fails to allocate memory.
        """
        self._logger = logging.getLogger(__name__)
        self._logger_enabled_for_debug = self._logger.isEnabledFor(
            logging.DEBUG)
        log_init_arguments(self._logger.debug, self.__class__.__name__)
        super().__init__()
        c_heap = lib.construct_heap()
        if c_heap == ffi.NULL:
            raise MemoryError(
                "Could not allocate memory for the class {0}.".format(
                    self.__class__.__name__))
        self._heap = ffi.gc(c_heap,
                            lib.destroy_heap,
                            size=lib.estimated_size(c_heap))
        self._minimal_valid_counter = {}
        self._last_returned_event = (Time(-float("inf"), -float("inf")), None)
        self._allocated_memory_bytes = 0
        self._scheduler_handle = _new_handle(self)
        self._event_handler_handles = {}
        self._warn_on_equal_event_times = warn_on_equal_event_times
Beispiel #7
0
    def __init__(self, sampling_interval: float, output_handler: str, first_event_time_zero: bool = False) -> None:
        """
        The constructor of the FixedIntervalSamplingEventHandler class.

        Parameters
        ----------
        sampling_interval : float
            The time interval of the sampling.
        output_handler : str
            The name of the output handler.
        first_event_time_zero : bool, optional
            If the first returned candidate event time is zero.

        Raises
        ------
        base.exceptions.ConfigurationError:
            If the sampling interval is not greater than zero.
        """
        log_init_arguments(logging.getLogger(__name__).debug, self.__class__.__name__,
                           sampling_interval=sampling_interval, output_handler=output_handler)
        super().__init__(output_handler=output_handler)
        if not sampling_interval > 0.0:
            raise ConfigurationError("The sampling_interval in the event handler {0} has to be > 0.0."
                                     .format(self.__class__.__name__))
        self._sampling_interval = sampling_interval
        self._event_time = Time(0.0, 0.0) if not first_event_time_zero else Time.from_float(-sampling_interval)
    def __init__(self, dumping_interval: float, output_handler: str) -> None:
        """
        The constructor of the FixedIntervalDumpingEventHandler class.

        Parameters
        ----------
        dumping_interval : float
            The time interval of the dumping.
        output_handler :
            The name of the output handler.

        Raises
        ------
        base.exceptions.ConfigurationError:
            If the dumping interval is not greater than zero.
        """
        log_init_arguments(logging.getLogger(__name__).debug,
                           self.__class__.__name__,
                           dumping_interval=dumping_interval,
                           output_handler=output_handler)
        super().__init__(output_handler=output_handler)
        if not dumping_interval > 0.0:
            raise ConfigurationError(
                "The dumping_interval in the event handler {0} has to be > 0.0."
                .format(self.__class__.__name__))
        self._dumping_interval = dumping_interval
        self._event_time = Time(0.0, 0.0)
Beispiel #9
0
    def test_almost_equal_comparison(self):
        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924378.0, 0.3216781233653267)
        self.assertAlmostEqual(time, other_time, places=13)

        time = Time(54678924378.0, 0.3216781233653266)
        other_time = Time(54678924378.0, 0.3216781233653267)
        self.assertAlmostEqual(time, other_time, places=13)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924378.0, 0.3216781233653367)
        self.assertAlmostEqual(time, other_time, places=13)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924378.0, 0.3216781233654267)
        self.assertNotAlmostEqual(time, other_time, places=13)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924377.0, 0.3216781233653267)
        self.assertNotAlmostEqual(time, other_time)
Beispiel #10
0
    def test_unit(self):
        test_position = [1]
        test_velocity = [2]
        test_time_stamp = Time(0.0, 0.4)
        unit = Unit(0,
                    test_position,
                    velocity=test_velocity,
                    time_stamp=test_time_stamp)
        self.assertEqual(unit.identifier, 0)
        self.assertEqual(unit.position, [1])
        self.assertIsNone(unit.charge)
        self.assertEqual(unit.velocity, [2])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.4))

        # The position, velocity and time stamp was not copied
        test_position[0] += 1
        test_velocity[0] /= 2
        test_time_stamp.update(Time(1.0, 0.1))
        self.assertEqual(unit.position, [2])
        self.assertEqual(unit.velocity, [1])
        self.assertEqual(unit.time_stamp, Time(1.0, 0.1))
Beispiel #11
0
    def test_extract_active_global_state_root_node_active(self):
        self._state_handler.initialize(self._root_nodes)
        branch = Node(Unit(identifier=(1,), position=[0.2, 0.3], charge=None,
                           velocity=[0.1, 0.2], time_stamp=Time(0.0, 0.0)), weight=1)
        branch.add_child(Node(Unit(identifier=(1, 0), position=[0.4, 0.6], charge={"e": -1},
                                   velocity=[0.6, -0.1], time_stamp=Time(-1.0, 0.8)), weight=0.5))
        branch.add_child(Node(Unit(identifier=(1, 1), position=[0.4, 0.6], charge={"e": -1},
                                   velocity=[0.6, -0.1], time_stamp=Time(-1.0, 0.8)), weight=0.5))
        self._state_handler.insert_into_global_state([branch])

        branches = self._state_handler.extract_active_global_state()
        self.assertEqual(len(branches), 1)
        info = branches[0].value
        self.assertEqual(info.position, [0.2, 0.3])
        self.assertEqual(info.charge, None)
        self.assertEqual(info.identifier, (1,))
        self.assertEqual(info.velocity, [0.1, 0.2])
        self.assertEqual(info.time_stamp, Time(0.0, 0.0))
        self.assertEqual(len(branches[0].children), 2)
        info = branches[0].children[0].value
        self.assertEqual(info.position, [0.4, 0.6])
        self.assertEqual(info.charge, {"e": 1})
        self.assertEqual(info.identifier, (1, 0))
        self.assertEqual(info.velocity, [0.6, -0.1])
        self.assertEqual(info.time_stamp, Time(-1.0, 0.8))
        self.assertEqual(len(branches[0].children[0].children), 0)
        info = branches[0].children[1].value
        self.assertEqual(info.position, [0.4, 0.6])
        self.assertEqual(info.charge, {"e": -1})
        self.assertEqual(info.identifier, (1, 1))
        self.assertEqual(info.velocity, [0.6, -0.1])
        self.assertEqual(info.time_stamp, Time(-1.0, 0.8))
        self.assertEqual(len(branches[0].children[1].children), 0)
Beispiel #12
0
    def test_insert_into_global_state_root_branch(self):
        self._state_handler.initialize(self._root_nodes)
        branch = Node(Unit(identifier=(1,), position=[0.5, 0.6], charge=None,
                           velocity=[1, 1], time_stamp=Time(0.0, 0.2)), weight=1)
        # There are no consistency checks within the state handler
        branch.add_child(Node(Unit(identifier=(1, 0), position=[0.4, 0.6], charge={"e": 1},
                                   velocity=[-0.3, 2.1], time_stamp=Time(0.0, 0.53)), weight=0.5))
        branch.add_child(Node(Unit(identifier=(1, 1), position=[0.1, 0.2], charge={"e": -1},
                                   velocity=[0.7, 0.7], time_stamp=Time(0.0, 0.1)), weight=0.5))
        self._state_handler.insert_into_global_state([branch])

        all_root_cnodes = self._state_handler.extract_global_state()
        self.assertEqual(len(all_root_cnodes), 2)
        unit = all_root_cnodes[0].value
        self.assertEqual(unit.position, [0.05, 0.025])
        self.assertIsNone(unit.charge)
        self.assertEqual(unit.identifier, (0,))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[0].children), 2)

        unit = all_root_cnodes[0].children[0].value
        self.assertEqual(unit.position, [0, 0])
        self.assertEqual(unit.charge, {"e": 1})
        self.assertEqual(unit.identifier, (0, 0))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[0].children[0].children), 0)

        unit = all_root_cnodes[0].children[1].value
        self.assertEqual(unit.position, [0.1, 0.05])
        self.assertEqual(unit.charge, {"e": -1})
        self.assertEqual(unit.identifier, (0, 1))
        self.assertIsNone(unit.velocity)
        self.assertIsNone(unit.time_stamp)
        self.assertEqual(len(all_root_cnodes[0].children[1].children), 0)

        unit = all_root_cnodes[1].value
        self.assertEqual(unit.position, [0.5, 0.6])
        self.assertIsNone(unit.charge)
        self.assertEqual(unit.identifier, (1,))
        self.assertEqual(unit.velocity, [1, 1])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.2))
        self.assertEqual(len(all_root_cnodes[1].children), 2)

        unit = all_root_cnodes[1].children[0].value
        self.assertEqual(unit.position, [0.4, 0.6])
        self.assertEqual(unit.charge, {"e": 1})
        self.assertEqual(unit.identifier, (1, 0))
        self.assertEqual(unit.velocity, [-0.3, 2.1])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.53))
        self.assertEqual(len(all_root_cnodes[1].children[0].children), 0)

        unit = all_root_cnodes[1].children[1].value
        self.assertEqual(unit.position, [0.1, 0.2])
        self.assertEqual(unit.charge, {"e": -1})
        self.assertEqual(unit.identifier, (1, 1))
        self.assertEqual(unit.velocity, [0.7, 0.7])
        self.assertEqual(unit.time_stamp, Time(0.0, 0.1))
        self.assertEqual(len(all_root_cnodes[1].children[1].children), 0)
    def __init__(self, **kwargs: Any) -> None:
        """
        The constructor of the EndOfChainEventHandler class.

        This class is designed for cooperative inheritance, meaning that it passes through all unused kwargs in the
        init to the next class in the MRO via super.

        Parameters
        ----------
        kwargs : Any
            Additional kwargs which are passed to the __init__ method of the next class in the MRO.
        """
        super().__init__(**kwargs)
        self._event_time = Time(0.0, 0.0)
Beispiel #14
0
    def __init__(self, warn_on_equal_event_times: bool = False) -> None:
        """
        The constructor of the ListScheduler class.

        Parameters
        ----------
        warn_on_equal_event_times : bool, optional
            Whether this scheduler should log a warning when succeeding event times are equal.
        """
        self._logger = logging.getLogger(__name__)
        self._logger_enabled_for_debug = self._logger.isEnabledFor(logging.DEBUG)
        log_init_arguments(self._logger.debug, self.__class__.__name__)
        super().__init__()
        self._times = []
        self._last_returned_event = (Time(-float("inf"), -float("inf")), None)
        self._warn_on_equal_event_times = warn_on_equal_event_times
Beispiel #15
0
    def get_succeeding_event(self) -> Any:
        """
        Get the valid event handler reference currently stored in the scheduler which was pushed with the smallest
        candidate event time.

        Note that the _event_time_increasing method is called via an assert so that it can be skipped using the -O
        option of the interpreter. This method raises a SchedulerError if the event time is not increasing.

        The 'root' function implemented in C returns a root element, where the time is -inf, the event handler points to
        ffi.NULL, and the counter is undefined if the heap is empty after the lazy deletion was carried out. If this is
        detected, a SchedulerError is raised.

        Returns
        -------
        Any
            The event handler associated to the smallest stored event time.

        Raises
        ------
        base.exceptions.SchedulerError
            If the newest smallest event time is greater than the last returned event time.
        base.exceptions.SchedulerError
            If the scheduler does not contain any event.
        """
        # TODO: Add automatic garbage collection if number of elements in the heap becomes too large?
        top = _lib_root(self._heap, self._scheduler_handle,
                        _lib_event_valid_callback)
        try:
            event_handler = _from_handle(top.event_handler)
        except RuntimeError as error:
            if (top.time_quotient == -float("inf")
                    and top.time_remainder == -float("inf")
                    and top.event_handler == ffi.NULL):
                raise SchedulerError(
                    "The succeeding event was requested from the class {0}. However, the scheduler "
                    "does not contain any events.".format(
                        self.__class__.__name__))
            raise error
        if self._logger_enabled_for_debug:
            self._logger.debug(
                "Smallest event time in the scheduler: {0}".format(
                    str(top.time_quotient + top.time_remainder)))
        assert self._event_time_increasing(
            Time(top.time_quotient, top.time_remainder),
            event_handler.__class__.__name__)
        return event_handler
    def __init__(self, initial_direction_of_motion: int, speed: float,
                 initial_active_identifier: Sequence[int]) -> None:
        """
        The constructor of the InitialChainStartOfRunEventHandler class.

        Parameters
        ----------
        initial_direction_of_motion : int
            The initial direction of motion.
        speed : float
            The initial absolute value of the velocity.
        initial_active_identifier : List[int]
            The global state identifier of the initially active leaf unit.

        Raises
        ------
        base.exceptions.ConfigurationError
            If the initial direction of motion exceeds the dimension.
        base.exceptions.ConfigurationError
            If the speed is not larger than zero.
        """
        log_init_arguments(logging.getLogger(__name__).debug, self.__class__.__name__,
                           initial_direction_of_motion=initial_direction_of_motion, speed=speed,
                           initial_active_identifier=initial_active_identifier)
        super().__init__()
        if initial_direction_of_motion >= setting.dimension:
            raise ConfigurationError("The index of the initial direction of motion {0} "
                                     "has to be smaller than the dimension {1} "
                                     "in the event handler {2}."
                                     .format(initial_direction_of_motion, setting.dimension, self.__class__.__name__))
        if speed <= 0.0:
            raise ConfigurationError("The speed in the event handler {0} should be larger than 0.0."
                                     .format(self.__class__.__name__))
        self._initial_velocity = [0.0] * setting.dimension
        self._initial_velocity[initial_direction_of_motion] = speed
        self._initial_active_identifier = tuple(initial_active_identifier)
        self._event_time = Time(0.0, 0.0)
Beispiel #17
0
    def __init__(self, chain_time: float) -> None:
        """
        The constructor of the SingleIndependentActivePeriodicDirectionEndOfChainEventHandler class.

        Parameters
        ----------
        chain_time : float
            The time interval after which a new end-of-chain event occurs.

        Raises
        ------
        base.exceptions.ConfigurationError:
            If the chain time is not greater than zero.
        """
        log_init_arguments(logging.getLogger(__name__).debug,
                           self.__class__.__name__,
                           chain_time=chain_time)
        super().__init__()
        if not chain_time > 0.0:
            raise ConfigurationError(
                "The chain_time in the event handler {0} must be > 0.0.".
                format(self.__class__.__name__))
        self._chain_time = chain_time
        self._last_committed_event_time = Time(0.0, 0.0)
Beispiel #18
0
    def test_add(self):
        time = Time(0.0, 0.0)
        result = time + 1.0
        self.assertEqual(time.quotient, 0.0)
        self.assertEqual(time.remainder, 0.0)
        self.assertEqual(result.quotient, 1.0)
        self.assertEqual(result.remainder, 0.0)

        time = Time(57854.0, 0.4536734584357)
        result = time + 1.5426347561237
        self.assertEqual(time.quotient, 57854.0)
        self.assertEqual(time.remainder, 0.4536734584357)
        self.assertEqual(result.quotient, 57855.0)
        self.assertAlmostEqual(result.remainder, 0.9963082145594, places=13)

        time = Time(54678924378.0, 0.3216781233653267)
        result = time + 2.241783567823847
        self.assertEqual(time.quotient, 54678924378.0)
        self.assertEqual(time.remainder, 0.3216781233653267)
        self.assertEqual(result.quotient, 54678924380.0)
        self.assertAlmostEqual(result.remainder, 0.5634616911891737, places=13)

        time = Time(54678924378.0, 0.3216781233653267)
        result = time + 1.0e-13
        self.assertEqual(time.quotient, 54678924378.0)
        self.assertEqual(time.remainder, 0.3216781233653267)
        self.assertEqual(result.quotient, 54678924378.0)
        self.assertAlmostEqual(result.remainder, 0.3216781233654267, places=13)

        time = Time(57854.0, 0.4536734584357)
        result = time + float("inf")
        self.assertEqual(time.quotient, 57854.0)
        self.assertEqual(time.remainder, 0.4536734584357)
        self.assertEqual(result.quotient, float("inf"))
        self.assertEqual(result.remainder, float("inf"))

        time = Time(57854.0, 0.4536734584357)
        result = time + (-float("inf"))
        self.assertEqual(time.quotient, 57854.0)
        self.assertEqual(time.remainder, 0.4536734584357)
        self.assertEqual(result.quotient, -float("inf"))
        self.assertEqual(result.remainder, -float("inf"))
Beispiel #19
0
    def test_sub(self):
        time = Time(1.0, 0.0)
        other_time = Time(0.0, 0.0)
        result = time - other_time
        self.assertEqual(time.quotient, 1.0)
        self.assertEqual(time.remainder, 0.0)
        self.assertEqual(other_time.quotient, 0.0)
        self.assertEqual(other_time.remainder, 0.0)
        self.assertEqual(result, 1.0)

        time = Time(57854.0, 0.4536734584357)
        other_time = Time(57854.0, 0.2341521367)
        result = time - other_time
        self.assertEqual(time.quotient, 57854.0)
        self.assertEqual(time.remainder, 0.4536734584357)
        self.assertEqual(other_time.quotient, 57854.0)
        self.assertEqual(other_time.remainder, 0.2341521367)
        self.assertAlmostEqual(result, 0.2195213217357, places=13)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924376.0, 0.4536734584357)
        result = time - other_time
        self.assertEqual(time.quotient, 54678924378.0)
        self.assertEqual(time.remainder, 0.3216781233653267)
        self.assertEqual(other_time.quotient, 54678924376.0)
        self.assertEqual(other_time.remainder, 0.4536734584357)
        self.assertAlmostEqual(result, 1.8680046649296267, places=12)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924378.0, 0.3216781233652267)
        result = time - other_time
        self.assertEqual(time.quotient, 54678924378.0)
        self.assertEqual(time.remainder, 0.3216781233653267)
        self.assertEqual(other_time.quotient, 54678924378.0)
        self.assertEqual(other_time.remainder, 0.3216781233652267)
        self.assertAlmostEqual(result, 1.0e-13, places=13)

        time = Time(57854.0, 0.4536734584357)
        other_time = Time(float("inf"), float("inf"))
        result = time - other_time
        self.assertEqual(time.quotient, 57854.0)
        self.assertEqual(time.remainder, 0.4536734584357)
        self.assertEqual(other_time.quotient, float("inf"))
        self.assertEqual(other_time.remainder, float("inf"))
        self.assertEqual(result, -float("inf"))

        time = Time(57854.0, 0.4536734584357)
        other_time = Time(-float("inf"), -float("inf"))
        result = time - other_time
        self.assertEqual(time.quotient, 57854.0)
        self.assertEqual(time.remainder, 0.4536734584357)
        self.assertEqual(other_time.quotient, -float("inf"))
        self.assertEqual(other_time.remainder, -float("inf"))
        self.assertEqual(result, float("inf"))

        time = Time(float("inf"), float("inf"))
        other_time = Time(57854.0, 0.4536734584357)
        result = time - other_time
        self.assertEqual(time.quotient, float("inf"))
        self.assertEqual(time.remainder, float("inf"))
        self.assertEqual(other_time.quotient, 57854.0)
        self.assertEqual(other_time.remainder, 0.4536734584357)
        self.assertEqual(result, float("inf"))

        time = Time(-float("inf"), -float("inf"))
        other_time = Time(57854.0, 0.4536734584357)
        result = time - other_time
        self.assertEqual(time.quotient, -float("inf"))
        self.assertEqual(time.remainder, -float("inf"))
        self.assertEqual(other_time.quotient, 57854.0)
        self.assertEqual(other_time.remainder, 0.4536734584357)
        self.assertEqual(result, -float("inf"))
Beispiel #20
0
    def test_greater_equal_comparison(self):
        time = Time(1.0, 0.5)
        other_time = Time(1.0, 0.5)
        self.assertTrue(time >= other_time)
        self.assertIsNot(time, other_time)

        time = Time(1.0, 0.6)
        other_time = Time(1.0, 0.5)
        self.assertTrue(time >= other_time)
        self.assertIsNot(time, other_time)

        time = Time(1.0, 0.4)
        other_time = Time(1.0, 0.5)
        self.assertFalse(time >= other_time)
        self.assertIsNot(time, other_time)

        time = Time(2.0, 0.5)
        other_time = Time(1.0, 0.5)
        self.assertTrue(time >= other_time)
        self.assertIsNot(time, other_time)

        time = Time(0.0, 0.5)
        other_time = Time(1.0, 0.5)
        self.assertFalse(time >= other_time)
        self.assertIsNot(time, other_time)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924378.0, 0.3216781233653267)
        self.assertTrue(time >= other_time)
        self.assertIsNot(time, other_time)

        time = Time(54678924378.0, 0.3216781233653266)
        other_time = Time(54678924378.0, 0.3216781233653267)
        self.assertFalse(time >= other_time)
        self.assertIsNot(time, other_time)

        time = Time(54678924377.0, 0.3216781233653267)
        other_time = Time(54678924378.0, 0.3216781233653267)
        self.assertFalse(time >= other_time)
        self.assertIsNot(time, other_time)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924378.0, 0.3216781233653266)
        self.assertTrue(time >= other_time)
        self.assertIsNot(time, other_time)

        time = Time(54678924378.0, 0.3216781233653267)
        other_time = Time(54678924377.0, 0.3216781233653267)
        self.assertTrue(time >= other_time)
        self.assertIsNot(time, other_time)