Skip to content

Commit

Permalink
PR #11214 from Eran: stabilized-value in pyrsutils; replace 9 cpp tes…
Browse files Browse the repository at this point in the history
…ts with 1 py
  • Loading branch information
maloel authored Dec 14, 2022
2 parents 274ccca + 9fd5a2e commit f2b2434
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 296 deletions.
24 changes: 22 additions & 2 deletions third-party/rsutils/py/pyrsutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <rsutils/easylogging/easyloggingpp.h>
#include <rsutils/string/split.h>
#include <rsutils/version.h>
#include <rsutils/number/stabilized-value.h>
#include <rsutils/string/from.h>

#define NAME pyrsutils
#define SNAME "pyrsutils"
Expand Down Expand Up @@ -37,8 +39,7 @@ PYBIND11_MODULE(NAME, m) {
py::arg( "build" ) = 0 )
.def_static( "from_number", []( version::number_type n ) { return version( n ); } )
.def( "is_valid", &version::is_valid )
.def( "__nonzero__", &version::is_valid ) // Called to implement truth value testing in Python 2
.def( "__bool__", &version::is_valid ) // Called to implement truth value testing in Python 3
.def( "__bool__", &version::is_valid )
.def( "major", &version::get_major )
.def( "minor", &version::get_minor )
.def( "patch", &version::get_patch )
Expand All @@ -63,4 +64,23 @@ PYBIND11_MODULE(NAME, m) {
.def( py::self >= py::self )
.def( py::self > py::self )
.def( "is_between", &version::is_between );

using stabilized_value = rsutils::number::stabilized_value< double >;
auto not_empty = []( stabilized_value const & self ) {
return ! self.empty();
};
auto to_string = []( stabilized_value const & self ) -> std::string {
if( self.empty() )
return "EMPTY";
return rsutils::string::from( self.get() );
};
py::class_< stabilized_value >( m, "stabilized_value" )
.def( py::init< size_t >() )
.def( "empty", &stabilized_value::empty )
.def( "__bool__", not_empty )
.def( "add", &stabilized_value::add )
.def( "get", &stabilized_value::get, py::arg( "stabilization-percent" ) = 0.75 )
.def( "clear", &stabilized_value::clear )
.def( "to_string", to_string )
.def( "__str__", to_string );
}
2 changes: 2 additions & 0 deletions unit-tests/py/rspy/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ def check_throws( _lambda, expected_type, expected_msg = None, abort_if_failed =
"""
We expect the lambda, when called, to raise an exception!
"""
if not callable( _lambda ):
raise RuntimeError( "expecting a function, not " + _lambda )
try:
_lambda()
except Exception as e:
Expand Down
30 changes: 0 additions & 30 deletions unit-tests/rsutils/number/stabilized/test-60-percent.cpp

This file was deleted.

32 changes: 0 additions & 32 deletions unit-tests/rsutils/number/stabilized/test-change-percent.cpp

This file was deleted.

22 changes: 0 additions & 22 deletions unit-tests/rsutils/number/stabilized/test-empty.cpp

This file was deleted.

25 changes: 0 additions & 25 deletions unit-tests/rsutils/number/stabilized/test-flickering-value.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions unit-tests/rsutils/number/stabilized/test-illegal-input.cpp

This file was deleted.

34 changes: 0 additions & 34 deletions unit-tests/rsutils/number/stabilized/test-not-full.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions unit-tests/rsutils/number/stabilized/test-same-value.cpp

This file was deleted.

Loading

0 comments on commit f2b2434

Please sign in to comment.