Skip to content

Commit e4e8ee0

Browse files
committed
Bind extra comparison policies and add missing unit tests
1 parent 82d3b4b commit e4e8ee0

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

test/python_bindings.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,29 @@ namespace py = pybind11;
88

99
using namespace util_caching;
1010

11+
struct SomePolicyWithoutParams {
12+
SomePolicyWithoutParams() = default;
13+
bool operator()(const Time& /*lhs*/, const Time& /*rhs*/) const {
14+
return true;
15+
}
16+
};
17+
1118
PYBIND11_MODULE(util_caching_py, mainModule) {
19+
using ApproximateNumberT = policies::ApproximateNumber<double>;
20+
using ApproximateTimeT = policies::ApproximateTime<Time, std::chrono::milliseconds>;
21+
using ApproximateTimeSecondsT = policies::ApproximateTime<Time, std::chrono::seconds>;
22+
23+
py::class_<SomePolicyWithoutParams, std::shared_ptr<SomePolicyWithoutParams>>(mainModule, "SomePolicyWithoutParams")
24+
.def(py::init<>())
25+
.def("__call__", &SomePolicyWithoutParams::operator());
26+
1227
py::module numberBased = mainModule.def_submodule("number_based");
1328
python_api::number_based::bindApproximatePolicy<double>(numberBased);
14-
python_api::number_based::bindCache<double, double, policies::ApproximateNumber<double>>(numberBased);
29+
python_api::number_based::bindCache<double, double, ApproximateNumberT>(numberBased);
1530

1631
py::module timeBased = mainModule.def_submodule("time_based");
17-
python_api::time_based::bindApproximatePolicy<Time, std::chrono::milliseconds>(timeBased);
18-
python_api::time_based::bindCache<Time, double, policies::ApproximateTime<Time, std::chrono::milliseconds>>(
32+
python_api::time_based::bindApproximatePolicy<Time, std::chrono::milliseconds>(timeBased, "ApproximateTime");
33+
python_api::time_based::bindApproximatePolicy<Time, std::chrono::seconds>(timeBased, "ApproximateTimeSeconds");
34+
python_api::time_based::bindCache<Time, double, ApproximateTimeT, ApproximateTimeSecondsT, SomePolicyWithoutParams>(
1935
timeBased);
2036
}

test/util_caching.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33
import time
44

5-
from util_caching_py import number_based, time_based
5+
from util_caching_py import number_based, time_based, SomePolicyWithoutParams
66

77

88
class CacheTest(unittest.TestCase):
@@ -18,7 +18,8 @@ def setUp(self):
1818
self.cache_by_time = time_based.Cache()
1919
self.approximate_number_policy = number_based.ApproximateNumber(0.5)
2020
self.approximate_time_policy = time_based.ApproximateTime(100)
21-
self.approximate_time_policy_2 = time_based.ApproximateTime(1000)
21+
self.approximate_time_policy_2 = time_based.ApproximateTimeSeconds(1)
22+
self.dummy_policy = SomePolicyWithoutParams()
2223

2324
def test_with_number_key(self):
2425
self.assertIsNone(self.cache_by_number.cached(self.key1))
@@ -70,11 +71,19 @@ def test_with_time_key(self):
7071
self.assertIsNone(
7172
self.cache_by_time.cached(self.time3, self.approximate_time_policy)
7273
)
74+
# exactly 1s after rounding to integer
75+
self.assertTrue(
76+
self.cache_by_time.cached(self.time3, self.approximate_time_policy_2)
77+
)
7378
# expect 2s after rounding to integer which is over threshold
7479
self.assertIsNone(
7580
self.cache_by_time.cached(self.time4, self.approximate_time_policy_2)
7681
)
7782

83+
def test_with_other_comparison_policy(self):
84+
self.cache_by_time.cache(self.time1, 1.0)
85+
self.assertTrue(self.cache_by_time.cached(self.time2, self.dummy_policy))
86+
7887

7988
if __name__ == "__main__":
8089
header = "Running " + os.path.basename(__file__)

0 commit comments

Comments
 (0)