File: test_545_acis_cache.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (104 lines) | stat: -rw-r--r-- 2,463 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Copyright (c) 2024, Manfred Moitzi
# License: MIT License
import pytest

import ezdxf
from ezdxf.acis import api as acis


def test_create_a_new_cache():
    cache = acis.AcisCache()

    assert len(cache) == 0


def test_get_bodies_from_sat_data(cube_sat: str):
    cache = acis.AcisCache()
    bodies = cache.get_bodies(cube_sat)

    assert len(bodies) == 1
    assert isinstance(bodies[0], acis.Body)
    assert len(cache) == 1

def test_empty_data_does_not_create_cache_entries():
    cache = acis.AcisCache()
    bodies = cache.get_bodies([])

    assert len(bodies) == 0
    assert len(cache) == 0


def test_get_bodies_from_sab_data(cube_sab: bytes):
    cache = acis.AcisCache()
    bodies = cache.get_bodies(cube_sab)

    assert len(bodies) == 1
    assert isinstance(bodies[0], acis.Body)
    assert len(cache) == 1


def test_add_only_unique_entries(cube_sat: str):
    cache = acis.AcisCache()
    cache.get_bodies(cube_sat)
    cache.get_bodies(cube_sat)

    assert len(cache) == 1, "should not create duplicate entries"


hash_data = acis.AcisCache.hash_data


class TestHashData:
    def test_empty_string(self):
        if ezdxf.PYPY:
            assert hash_data("") != 0
        else:
            assert hash_data("") == 0

    def test_string(self):
        h = hash_data("abc")
        assert isinstance(h, int)
        assert h != 0
        assert h == hash_data("abc")

    def test_strings(self):
        data = ["abc", "def"]
        h = hash_data(data)
        assert isinstance(h, int)
        assert h != 0
        assert h == hash_data(("abc", "def"))

    def test_empty_string_list(self):
        data = []
        h = hash_data(data)
        assert isinstance(h, int)
        assert h != 0
        assert h == hash_data(data)

        # surprise?
        assert hash(tuple()) == hash(tuple())
        assert id(tuple()) == id(tuple())

    def test_empty_bytes(self):
        if ezdxf.PYPY:
            assert hash_data(b"") != 0
        else:
            assert hash_data(b"") == 0

    def test_bytes(self):
        data = b"\x07\x08\x09"
        h = hash_data(data)
        assert isinstance(h, int)
        assert h != 0
        assert h == hash_data(b"\x07\x08\x09")

    def test_bytearray(self):
        data = bytearray([7, 8, 9])
        h = hash_data(data)
        assert isinstance(h, int)
        assert h != 0
        assert h == hash_data(b"\x07\x08\x09")


if __name__ == "__main__":
    pytest.main([__file__])