def pytestcase_seek_time_before_first_event_npy(tmpdir):
    """Tests seeking in a file at a position in time lower than first event timestamp"""
    filename, bboxes = create_temporary_npy_file(
        tmpdir, "seek_time_before first_event",
        "99999 0 BB_CREATE 7 96 102 22 21 0.3\n"
        "99999 2 BB_CREATE 7 127 87 53 53 0.3\n"
        "99999 3 BB_CREATE 7 141 81 119 78 0.7\n"
        "100000 4 BB_CREATE 7 97 102 22 21 0.3\n"
        "100000 5 BB_CREATE 6 8 69 51 53 0.6\n"
        "100000 6 BB_CREATE 7 127 87 53 53 0.4\n"
        "199999 7 BB_CREATE 7 141 81 119 78 0.7\n")
    record = EventNpyReader(filename)
    assert record.current_event_index() == 0
    assert record.event_count() == 7
    assert record.current_time == 0
    record.seek_time(9000)
    assert record.current_event_index() == 0 + 0
    assert record.done is False
    assert record.current_time == 9000
def pytestcase_seek_time_with_negative_time_npy(tmpdir):
    """Tests seeking in a file at a position with negative time"""
    filename, bboxes = create_temporary_npy_file(
        tmpdir, "seek_time_with_negative_time",
        "99999 0 BB_CREATE 7 96 102 22 21 0.3\n"
        "99999 2 BB_CREATE 7 127 87 53 53 0.3\n"
        "99999 3 BB_CREATE 7 141 81 119 78 0.7\n"
        "100000 4 BB_CREATE 7 97 102 22 21 0.3\n"
        "100000 5 BB_CREATE 6 8 69 51 53 0.6\n"
        "100000 6 BB_CREATE 7 127 87 53 53 0.4\n"
        "199999 7 BB_CREATE 7 141 81 119 78 0.7\n")
    record = EventNpyReader(filename)
    assert record.current_event_index() == 0
    assert record.event_count() == 7
    assert record.current_time == 0
    record.seek_time(-15)
    assert record.current_event_index() == 0 + 0
    assert record.done is False
    # current_time should be last event timestamp + 1
    assert record.current_time == 0
def pytestcase_seek_time_after_last_event_npy(tmpdir):
    """Tests seeking in a file at a position in time higher than last event timestamp"""
    filename, bboxes = create_temporary_npy_file(
        tmpdir, "seek_time_after_last_event",
        "99999 0 BB_CREATE 7 96 102 22 21 0.3\n"
        "99999 2 BB_CREATE 7 127 87 53 53 0.3\n"
        "99999 3 BB_CREATE 7 141 81 119 78 0.7\n"
        "100000 4 BB_CREATE 7 97 102 22 21 0.3\n"
        "100000 5 BB_CREATE 6 8 69 51 53 0.6\n"
        "100000 6 BB_CREATE 7 127 87 53 53 0.4\n"
        "199999 7 BB_CREATE 7 141 81 119 78 0.7\n")
    record = EventNpyReader(filename)
    assert record.current_event_index() == 0
    assert record.event_count() == 7
    assert record.current_time == 0
    record.seek_time(20000000)
    assert record.current_event_index() == 0 + record.event_count()
    assert record.done
    # current_time should be last event timestamp + 1
    assert record.current_time == 199999 + 1
def pytestcase_seek_time_with_numerous_events_npy(tmpdir):
    """Tests seeking in a file containing with many events at a position defined by a timestamp. The fact
    that there are numerous events implies that position should be found using dichotomy plus numpy searchsort"""
    filename, bboxes = create_temporary_npy_file(
        tmpdir, "seek_time_with_numerous_events",
        "99999 0 BB_CREATE 7 96 102 22 21 0.3\n"
        "99999 2 BB_CREATE 7 127 87 53 53 0.3\n"
        "99999 3 BB_CREATE 7 141 81 119 78 0.7\n"
        "100000 4 BB_CREATE 7 97 102 22 21 0.3\n"
        "100000 5 BB_CREATE 6 8 69 51 53 0.6\n"
        "100000 6 BB_CREATE 7 127 87 53 53 0.4\n"
        "199999 7 BB_CREATE 7 141 81 119 78 0.7\n")
    record = EventNpyReader(filename)
    assert record.current_event_index() == 0
    assert record.event_count() == 7
    assert record.current_time == 0
    record.seek_time(100000)
    assert record.current_event_index() == 0 + 3
    assert record.done is False
    assert record.current_time == 100000
def pytestcase_seek_time_exactly_on_an_event_npy(tmpdir):
    """Tests seeking in a file at an exact event timestamp position"""
    filename, bboxes = create_temporary_npy_file(
        tmpdir, "seek_time_exactly_on_an_event",
        "99997 0 BB_CREATE 7 96 102 22 21 0.3\n"
        "99998 2 BB_CREATE 7 127 87 53 53 0.3\n"
        "99999 3 BB_CREATE 7 141 81 119 78 0.7\n"
        "100000 4 BB_CREATE 7 97 102 22 21 0.3\n"
        "100001 5 BB_CREATE 6 8 69 51 53 0.6\n"
        "100002 6 BB_CREATE 7 127 87 53 53 0.4\n"
        "199999 7 BB_CREATE 7 141 81 119 78 0.7\n")
    record = EventNpyReader(filename)
    assert record.current_event_index() == 0
    assert record.event_count() == 7
    assert record.current_time == 0
    record.seek_time(100001)
    # event with timestamp 131200 is the 5th event, it should not be loaded
    assert record.current_event_index() == 0 + 4
    assert record.done is False
    # current_time should be the 5th event timestamp
    assert record.current_time == 100001