Skip to content

Commit 24af1a9

Browse files
todd-coxeter: fix rtype of lookahead/behind methods
1 parent db60766 commit 24af1a9

2 files changed

Lines changed: 43 additions & 22 deletions

File tree

src/todd-coxeter.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ node corresponding to index *i* back to the root of that tree.
384384

385385
thing.def(
386386
"perform_lookahead",
387-
[](ToddCoxeter_& self) { return self.perform_lookahead(); },
387+
[](ToddCoxeter_& self) -> detail::ToddCoxeterImpl& {
388+
return self.perform_lookahead();
389+
},
388390
R"pbdoc(
389391
:sig=(self: ToddCoxeter) -> ToddCoxeter:
390392
@@ -404,7 +406,8 @@ style and extent of this lookahead are controlled by the settings
404406

405407
thing.def(
406408
"perform_lookahead_for",
407-
[](ToddCoxeter_& self, std::chrono::nanoseconds t) {
409+
[](ToddCoxeter_& self,
410+
std::chrono::nanoseconds t) -> detail::ToddCoxeterImpl& {
408411
return self.perform_lookahead_for(t);
409412
},
410413
py::arg("t"),
@@ -426,7 +429,8 @@ happens first.
426429

427430
thing.def(
428431
"perform_lookahead_until",
429-
[](ToddCoxeter_& self, std::function<bool()> const& pred) {
432+
[](ToddCoxeter_& self,
433+
std::function<bool()> const& pred) -> detail::ToddCoxeterImpl& {
430434
return self.perform_lookahead_until(pred);
431435
},
432436
py::arg("pred"),
@@ -447,7 +451,9 @@ This function runs a lookahead until the nullary predicate *pred* returns
447451

448452
thing.def(
449453
"perform_lookbehind",
450-
[](ToddCoxeter_& self) { return self.perform_lookbehind(); },
454+
[](ToddCoxeter_& self) -> detail::ToddCoxeterImpl& {
455+
return self.perform_lookbehind();
456+
},
451457
R"pbdoc(
452458
:sig=(self: ToddCoxeter) -> ToddCoxeter:
453459
@@ -510,7 +516,8 @@ Pro).
510516
thing.def(
511517
"perform_lookbehind_no_checks",
512518
[](ToddCoxeter_& self,
513-
std::function<Word(Word const&)> const& collapser) {
519+
std::function<Word(Word const&)> const& collapser)
520+
-> detail::ToddCoxeterImpl& {
514521
auto wrap = [&collapser](auto d_it, auto first, auto last) {
515522
Word copy(first, last);
516523
// Shame to do so much copying here but couldn't figure out how to
@@ -578,7 +585,8 @@ function :any:`ToddCoxeter.reduce_no_run`.
578585

579586
thing.def(
580587
"perform_lookbehind_for",
581-
[](ToddCoxeter_& self, std::chrono::nanoseconds t) {
588+
[](ToddCoxeter_& self,
589+
std::chrono::nanoseconds t) -> detail::ToddCoxeterImpl& {
582590
return self.perform_lookbehind_for(t);
583591
},
584592
py::arg("t"),
@@ -606,7 +614,8 @@ happens first.
606614
"perform_lookbehind_for_no_checks",
607615
[](ToddCoxeter_& self,
608616
std::chrono::nanoseconds t,
609-
std::function<Word(Word const&)> const& collapser) {
617+
std::function<Word(Word const&)> const& collapser)
618+
-> detail::ToddCoxeterImpl& {
610619
auto wrap = [&collapser](auto d_it, auto first, auto last) {
611620
Word copy(first, last);
612621
// Shame to do so much copying here but couldn't figure out how to
@@ -649,7 +658,8 @@ happens first. See :any:`perform_lookbehind_no_checks` for more details.
649658

650659
thing.def(
651660
"perform_lookbehind_until",
652-
[](ToddCoxeter_& self, std::function<bool()> const& pred) {
661+
[](ToddCoxeter_& self,
662+
std::function<bool()> const& pred) -> detail::ToddCoxeterImpl& {
653663
return self.perform_lookbehind_until(pred);
654664
},
655665
py::arg("pred"),
@@ -676,7 +686,8 @@ This function runs a lookbehind until the nullary predicate *pred* returns
676686
"perform_lookbehind_until_no_checks",
677687
[](ToddCoxeter_& self,
678688
std::function<bool()> const& pred,
679-
std::function<Word(Word const&)> const& collapser) {
689+
std::function<Word(Word const&)> const& collapser)
690+
-> detail::ToddCoxeterImpl& {
680691
auto wrap = [&collapser](auto d_it, auto first, auto last) {
681692
Word copy(first, last);
682693
// Shame to do so much copying here but couldn't figure out how to

tests/test_todd_coxeter.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -420,22 +420,24 @@ def test_todd_coxeter_return_policy():
420420
def test_todd_coxeter_perform_lookahead():
421421
p = examples.full_transformation_monoid_Aiz58(10)
422422
tc = ToddCoxeter(congruence_kind.twosided, p)
423-
tc.perform_lookahead()
423+
assert tc.perform_lookahead() is tc
424424
tc.run_for(timedelta(seconds=0.01))
425425
num_nodes = tc.number_of_nodes_active()
426-
tc.perform_lookahead()
426+
# Check the return type is correct
427+
assert tc.perform_lookahead() is tc
427428
assert tc.number_of_nodes_active() < num_nodes
428429

429430

430431
def test_todd_coxeter_perform_lookahead_for():
431432
p = examples.full_transformation_monoid_Aiz58(10)
432433
tc = ToddCoxeter(congruence_kind.twosided, p)
433-
tc.perform_lookahead()
434+
assert tc.perform_lookahead() is tc
434435
tc.run_for(timedelta(seconds=0.01))
435436
num_nodes = tc.number_of_nodes_active()
436437
start_time = time.time()
437-
tc.perform_lookahead_for(timedelta(seconds=0.1))
438-
assert time.time() - start_time <= 0.2
438+
# Check the return type is correct
439+
assert tc.perform_lookahead_for(timedelta(seconds=0.1)) is tc
440+
assert time.time() - start_time <= 1.0
439441
assert tc.number_of_nodes_active() < num_nodes
440442

441443

@@ -445,7 +447,8 @@ def test_todd_coxeter_perform_lookahead_until():
445447
tc.perform_lookahead()
446448
tc.run_for(timedelta(seconds=0.01))
447449
num_nodes = tc.number_of_nodes_active()
448-
tc.perform_lookahead_until(lambda: tc.number_of_nodes_active() < num_nodes)
450+
# Check the return type is correct
451+
assert tc.perform_lookahead_until(lambda: tc.number_of_nodes_active() < num_nodes) is tc
449452
assert tc.number_of_nodes_active() < num_nodes
450453

451454

@@ -456,8 +459,9 @@ def test_todd_coxeter_perform_lookbehind_for():
456459
tc.run_for(timedelta(seconds=0.01))
457460
num_nodes = tc.number_of_nodes_active()
458461
start_time = time.time()
459-
tc.perform_lookbehind_for(timedelta(seconds=0.1))
460-
assert time.time() - start_time <= 0.2
462+
# Run and check the return type is correct
463+
assert tc.perform_lookbehind_for(timedelta(seconds=0.1)) is tc
464+
assert time.time() - start_time <= 1.0
461465
assert tc.number_of_nodes_active() < num_nodes
462466

463467

@@ -467,7 +471,8 @@ def test_todd_coxeter_perform_lookbehind_until():
467471
tc.perform_lookbehind()
468472
tc.run_for(timedelta(seconds=0.01))
469473
num_nodes = tc.number_of_nodes_active()
470-
tc.perform_lookbehind_until(lambda: tc.number_of_nodes_active() < num_nodes)
474+
# Run and check the return type is correct
475+
assert tc.perform_lookbehind_until(lambda: tc.number_of_nodes_active() < num_nodes) is tc
471476
assert tc.number_of_nodes_active() < num_nodes
472477

473478

@@ -478,8 +483,9 @@ def test_todd_coxeter_perform_lookbehind_for_no_checks():
478483
tc.run_for(timedelta(seconds=0.01))
479484
num_nodes = tc.number_of_nodes_active()
480485
start_time = time.time()
481-
tc.perform_lookbehind_for_no_checks(timedelta(seconds=0.1), lambda w: [])
482-
assert time.time() - start_time <= 0.2
486+
# Run and check the return type is correct
487+
assert tc.perform_lookbehind_for_no_checks(timedelta(seconds=0.1), lambda w: []) is tc
488+
assert time.time() - start_time <= 1.0
483489
assert tc.number_of_nodes_active() < num_nodes
484490

485491

@@ -489,7 +495,11 @@ def test_todd_coxeter_perform_lookbehind_until_no_checks():
489495
tc.perform_lookbehind()
490496
tc.run_for(timedelta(seconds=0.01))
491497
num_nodes = tc.number_of_nodes_active()
492-
tc.perform_lookbehind_until_no_checks(
493-
lambda: tc.number_of_nodes_active() < num_nodes, lambda w: []
498+
# Run and check the return type is correct
499+
assert (
500+
tc.perform_lookbehind_until_no_checks(
501+
lambda: tc.number_of_nodes_active() < num_nodes, lambda w: []
502+
)
503+
is tc
494504
)
495505
assert tc.number_of_nodes_active() < num_nodes

0 commit comments

Comments
 (0)