| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
from collections import Counter |
| 12 |
|
| 13 |
from hypothesis import strategies as st |
| 14 |
from hypothesis.internal.cache import GenericCache |
| 15 |
from hypothesis.stateful import ( |
| 16 |
Bundle, |
| 17 |
RuleBasedStateMachine, |
| 18 |
initialize, |
| 19 |
invariant, |
| 20 |
rule, |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
class CacheWithScores(GenericCache): |
| 25 |
def __init__(self, max_size): |
| 26 |
super().__init__(max_size) |
| 27 |
self.scores = {} |
| 28 |
|
| 29 |
def new_entry(self, key, value): |
| 30 |
return self.scores[key] |
| 31 |
|
| 32 |
|
| 33 |
class CacheRules(RuleBasedStateMachine): |
| 34 |
keys = Bundle("keys") |
| 35 |
|
| 36 |
@initialize(max_size=st.integers(1, 8)) |
| 37 |
def create_cache(self, max_size): |
| 38 |
self.cache = CacheWithScores(max_size) |
| 39 |
self.__values = {} |
| 40 |
|
| 41 |
self.__total_pins = 0 |
| 42 |
self.__pins = Counter() |
| 43 |
self.__live = set() |
| 44 |
self.__next_value = 0 |
| 45 |
self.__last_key = None |
| 46 |
|
| 47 |
def on_evict(evicted_key, value, score): |
| 48 |
assert self.__pins[evicted_key] == 0 |
| 49 |
assert score == self.cache.scores[evicted_key] |
| 50 |
assert value == self.__values[evicted_key] |
| 51 |
for k in self.cache: |
| 52 |
assert ( |
| 53 |
self.__pins[k] > 0 |
| 54 |
or self.cache.scores[k] >= score |
| 55 |
or k == self.__last_key |
| 56 |
) |
| 57 |
|
| 58 |
self.cache.on_evict = on_evict |
| 59 |
|
| 60 |
@rule(key=st.integers(), score=st.integers(0, 100), target=keys) |
| 61 |
def new_key(self, key, score): |
| 62 |
if key not in self.cache.scores: |
| 63 |
self.cache.scores[key] = score |
| 64 |
return key |
| 65 |
|
| 66 |
@rule(key=keys) |
| 67 |
def set_key(self, key): |
| 68 |
if self.__total_pins < self.cache.max_size or key in self.cache: |
| 69 |
self.__last_key = key |
| 70 |
self.cache[key] = self.__next_value |
| 71 |
self.__values[key] = self.__next_value |
| 72 |
self.__next_value += 1 |
| 73 |
|
| 74 |
@invariant() |
| 75 |
def check_values(self): |
| 76 |
self.cache.check_valid() |
| 77 |
for key in self.cache: |
| 78 |
assert self.__values[key] == self.cache[key] |
| 79 |
|
| 80 |
@rule(key=keys) |
| 81 |
def pin_key(self, key): |
| 82 |
if key in self.cache: |
| 83 |
self.cache.pin(key, self.__values[key]) |
| 84 |
if self.__pins[key] == 0: |
| 85 |
self.__total_pins += 1 |
| 86 |
self.__pins[key] += 1 |
| 87 |
|
| 88 |
@rule(key=keys) |
| 89 |
def unpin_key(self, key): |
| 90 |
if self.__pins[key] > 0: |
| 91 |
self.cache.unpin(key) |
| 92 |
self.__pins[key] -= 1 |
| 93 |
if self.__pins[key] == 0: |
| 94 |
self.__total_pins -= 1 |
| 95 |
assert self.__total_pins >= 0 |
| 96 |
|
| 97 |
|
| 98 |
TestCache = CacheRules.TestCase |