Skip to content

Commit

Permalink
PR #12468 from Eran: update rsutils::version to 4x16
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Dec 4, 2023
2 parents bfe95cd + 195b345 commit c84eead
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 64 deletions.
12 changes: 6 additions & 6 deletions third-party/rsutils/include/rsutils/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace rsutils {
//
struct version
{
typedef unsigned sub_type;
typedef uint16_t sub_type;
typedef uint64_t number_type;

number_type number;
Expand Down Expand Up @@ -47,12 +47,12 @@ struct version

void clear() { number = 0; }

sub_type get_major() const { return ( number >> ( 8 * 7 ) ) & 0xFF; }
sub_type get_minor() const { return ( number >> ( 8 * 5 ) ) & 0xFFFF; }
sub_type get_patch() const { return ( number >> ( 8 * 4 ) ) & 0xFF; }
sub_type get_build() const { return number & 0xFFFFFFFF; }
sub_type get_major() const { return sub_type( number >> ( 8 * 6 ) ); }
sub_type get_minor() const { return sub_type( number >> ( 8 * 4 ) ); }
sub_type get_patch() const { return sub_type( number >> ( 8 * 2 ) ); }
sub_type get_build() const { return sub_type( number ); }

version without_build() const { return version( number & ~0xFFFFFFFFULL ); }
version without_build() const { return version( number & ~0xFFFFULL ); }

bool operator<=( version const & other ) const { return number <= other.number; }
bool operator==( version const & other ) const { return number == other.number; }
Expand Down
15 changes: 6 additions & 9 deletions third-party/rsutils/src/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ namespace rsutils {


version::version( sub_type m, sub_type n, sub_type p, sub_type b )
: version( ( ( m & 0xFFULL ) << ( 8 * 7 ) ) + ( ( n & 0xFFFFULL ) << ( 8 * 5 ) )
+ ( ( p & 0xFFULL ) << ( 8 * 4 ) ) + ( b & 0xFFFFFFFFULL ) )
: version( ( uint64_t( m ) << ( 8 * 6 ) ) + ( uint64_t( n ) << ( 8 * 4 ) ) + ( uint64_t( p ) << ( 8 * 2 ) ) + b )
{
// Invalidate if any overflow
if( m != get_major() )
Expand All @@ -37,7 +36,7 @@ version::version( char const * base )
return; // If 0, unexpected; otherwise invalid
major *= 10;
major += *ptr - '0';
if( major > 0xFF )
if( major > 0xFFFF )
return; // Overflow
++ptr;
}
Expand Down Expand Up @@ -69,14 +68,14 @@ version::version( char const * base )
return; // Invalid
patch *= 10;
patch += *ptr - '0';
if( patch > 0xFF )
if( patch > 0xFFFF )
return; // Overflow
++ptr;
}
if( ptr == base )
return; // No patch

uint64_t build = 0;
unsigned build = 0;
if( *ptr )
{
base = ++ptr;
Expand All @@ -86,15 +85,15 @@ version::version( char const * base )
return; // Invalid
build *= 10;
build += *ptr - '0';
if( build > 0xFFFFFFFFULL )
if( build > 0xFFFF )
return; // Overflow
++ptr;
}
if( ptr == base )
return; // No build, but there was a period at the end...!?
}

number = version( major, minor, patch, (unsigned)build ).number;
number = version( major, minor, patch, build ).number;
}


Expand All @@ -106,8 +105,6 @@ std::string version::to_string() const

std::ostream & operator<<( std::ostream & os, version const & v )
{
// NOTE: FW versions were padded to 2 characters with leading 0s:
// os << std::setfill('0') << std::setw(2) << v.major() << '.' ...
os << v.get_major() << '.' << v.get_minor() << '.' << v.get_patch();
if( v.get_build() )
os << '.' << v.get_build();
Expand Down
90 changes: 41 additions & 49 deletions unit-tests/rsutils/test-version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,68 @@

#############################################################################################
#
test.start( "String constructor" )
try:
test.check( not version() )

test.check( not version( "" ))
test.check( not version( "1" ))
test.check( not version( "1." ))
test.check( not version( "1.2" ))
test.check( not version( "1.2." ))
test.check( version( "1.2.3" ))
test.check( not version( "1.2.3." ))
test.check( version( "1.2.3.4" ))
test.check( not version( "1.2.3.4." ))
test.check( not version( "1 . 2.3.4" ));
test.check( not version( ".1.2.3.4" ))
test.check( not version( "0.0.0.0" ))
test.check( version( "1.0.0.0" ))
test.check( version( "0.1.0.0" ))
test.check( version( "0.0.1.0" ))
test.check( version( "0.0.0.1" ))
test.check( not version( ".2.3.4" ))
test.check( not version( "1..2.4" ))
test.check( not version( "1.2..4" ))

test.check( version( "255.2.3.4" ))
test.check( not version( "256.2.3.4" ))
test.check( version( "1.65535.3.4" ))
test.check( not version( "1.65536.3.4" ))
test.check( version( "1.2.255.4" ))
test.check( not version( "1.2.256.4" ))
test.check( version( "1.2.3.4294967295" ))
test.check( not version( "1.2.3.4294967296" ))

test.check( not version( "xxxxxxxxxxx" ))
except:
test.unexpected_exception()
test.finish()
with test.closure( "String constructor" ):
test.check_false( version() )

test.check_false( version( "" ))
test.check_false( version( "1" ))
test.check_false( version( "1." ))
test.check_false( version( "1.2" ))
test.check_false( version( "1.2." ))
test.check ( version( "1.2.3" ))
test.check_false( version( "1.2.3." ))
test.check ( version( "1.2.3.4" ))
test.check_false( version( "1.2.3.4." ))
test.check_false( version( "1 . 2.3.4" ));
test.check_false( version( ".1.2.3.4" ))
test.check_false( version( "0.0.0.0" ))
test.check ( version( "1.0.0.0" ))
test.check ( version( "0.1.0.0" ))
test.check ( version( "0.0.1.0" ))
test.check ( version( "0.0.0.1" ))
test.check_false( version( ".2.3.4" ))
test.check_false( version( "1..2.4" ))
test.check_false( version( "1.2..4" ))

test.check ( version( "65535.2.3.4" ))
test.check_false( version( "65536.2.3.4" ))
test.check ( version( "1.65535.3.4" ))
test.check_false( version( "1.65536.3.4" ))
test.check ( version( "1.2.65535.4" ))
test.check_false( version( "1.2.65536.4" ))
test.check ( version( "1.2.3.65535" ))
test.check_false( version( "1.2.3.65536" ))

test.check_false( version( "xxxxxxxxxxx" ))
#
#############################################################################################
#
test.start( "Number constructor" )
try:
with test.closure( "Number constructor" ):
test.check( not version.from_number(0) )
test.check( version( 1, 2, 3 ))
test.check( version( 1, 2, 3, 4 ))
test.check_throws( lambda: version( -1, 2, 3, 4 ), TypeError )
test.check_throws( lambda: version( 1, -2, 3, 4 ), TypeError )
test.check_throws( lambda: version( 1, 2, -3, 4 ), TypeError )
test.check_throws( lambda: version( 1, 2, 3, -4 ), TypeError )
test.check( version( 0xFF, 0xFFFF, 0xFF, 0xFFFFFFFF ))
test.check( not version( 0x100, 0xFFFF, 0xFF, 0xFFFFFFFF ))
test.check( not version( 0xFF, 0x10000, 0xFF, 0xFFFFFFFF ))
test.check( not version( 0xFF, 0xFFFF, 0x100, 0xFFFFFFFF ))
#test.check( not version( 0xFF, 0xFFFF, 0xFF, 0x100000000 )) Python throws - type error
test.check( version( 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF ))
test.check_throws( lambda: version( 0x10000, 0xFFFF, 0xFFFF, 0xFFFF ), TypeError )
test.check_throws( lambda: version( 0xFFFF, 0x10000, 0xFFFF, 0xFFFF ), TypeError )
test.check_throws( lambda: version( 0xFFFF, 0xFFFF, 0x10000, 0xFFFF ), TypeError )
test.check_throws( lambda: version( 0xFFFF, 0xFFFF, 0xFFFF, 0x10000 ), TypeError )

v1234 = version.from_number( version( 1, 2, 3, 1234 ).number )
test.check_equal( v1234.major(), 1 );
test.check_equal( v1234.minor(), 2 );
test.check_equal( v1234.patch(), 3 );
test.check_equal( v1234.build(), 1234 );
except:
test.unexpected_exception()
test.finish()
#
#############################################################################################
#
test.start( "Comparisons" )
try:
v0 = version()
vN = version.from_number( 72059805946086610 )
vN = version.from_number( 281483566843090 )
v1233 = version( "1.2.3.1233" )
v1234 = version( "1.2.3.1234" )
v1235 = version( "1.2.3.1235" )
Expand All @@ -88,7 +80,7 @@
test.check_equal( v0, version( "0.0.0.0" ) )
test.check_equal( v0, version( "123" ) )
test.check ( vN != v0 );

test.check_equal( vN, v1234 );
test.check ( v1234 == v1234 );
test.check_false( v1234 != v1234 );
Expand Down

0 comments on commit c84eead

Please sign in to comment.