def test_intersect_sum():
    truth = _load_truth()
    ilistmap = MmapIntervalListMapping(DATA_PATH, PAYLOAD_LEN)
    for _ in range(N_REPEAT):
        i = random.choice(list(truth.keys()))
        for j in range(DISTINCT_PAYLOADS):
            assert (sum(x[1] - x[0] for x in _filter(truth[i], 0xFFFF, j)) ==
                    ilistmap.intersect_sum(i, [(0, MAX_T)], 0xFFFF, j, False))
def test_sum():
    truth = _load_truth()
    ilistmap = MmapIntervalListMapping(DATA_PATH, PAYLOAD_LEN)
    true_sum = sum(
        sum(b - a for a, b, _ in intervals) for intervals in truth.values())
    calc_sum = ilistmap.sum(0, 0)
    assert _is_close(true_sum, calc_sum), \
        'diff: {} -- {}'.format(true_sum, calc_sum)
def test_contains():
    truth = _load_truth()
    ilistmap = MmapIntervalListMapping(DATA_PATH, PAYLOAD_LEN)

    i = random.choice(list(truth.keys()))

    def truth_contains(v, mask, payload):
        for (a, b, _) in _filter(truth[i], mask, payload):
            if v >= a and v < b:
                return True
        return False

    for v in range(MAX_T):
        j = random.randint(0, DISTINCT_PAYLOADS - 1)
        assert truth_contains(v, 0xFFFF, j) == \
            ilistmap.is_contained(i, v, 0xFFFF, j, False, MAX_SPAN), \
            'Truth: {}'.format(truth[i])
def test_integrity():
    truth = _load_truth()
    ilistmap = MmapIntervalListMapping(DATA_PATH, PAYLOAD_LEN)
    assert len(truth) == ilistmap.len()
    assert set(truth.keys()) == set(ilistmap.get_ids())
    for i in truth:
        assert ilistmap.has_id(i)

        for j in range(DISTINCT_PAYLOADS):
            true_count = len(_filter(truth[i], 0xFFFF, j))
            assert true_count == ilistmap.get_interval_count(i, 0xFFFF, j)
            assert true_count == len(ilistmap.get_intervals(
                i, 0xFFFF, j, True))

        assert len(truth[i]) == ilistmap.get_interval_count(i, 0, 0)
        assert (len(truth[i]) == len(
            ilistmap.get_intervals_with_payload(i, True)))
def load_hosts():
    people = []
    with open(HOST_FILE) as fp:
        for l in fp:
            l = l.strip()
            if not l:
                continue
            channel, name = l.split(',', 1)

            for dir in PERSON_ILIST_DIRS:
                ilist_path = os.path.join(dir, '{}.ilist.bin'.format(name))
                if os.path.isfile(ilist_path):
                    break
            else:
                print('No interval list:', name)
                continue

            ilist = MmapIntervalListMapping(ilist_path, 1)
            people.append(Person(name, channel, ilist))
    return people
def load_num_faces():
    return MmapIntervalListMapping(NUM_FACES_FILE, 1)
def test_empty_ilistmap():
    ilistmap = MmapIntervalListMapping(DATA_PATH, 0)
    assert ilistmap.sum(0, 0) == 0