[libcalamares] Apply current coding style to libcalamares/partition/

This commit is contained in:
Adriaan de Groot 2019-08-04 22:13:58 +02:00
parent 312865cdd9
commit fa2b94d931
4 changed files with 120 additions and 89 deletions

View file

@ -26,16 +26,13 @@ namespace CalamaresUtils
namespace Partition
{
static const NamedEnumTable<SizeUnit>&
static const NamedEnumTable< SizeUnit >&
unitSuffixes()
{
static const NamedEnumTable<SizeUnit> names{
{ QStringLiteral( "%" ), SizeUnit::Percent },
{ QStringLiteral( "K" ), SizeUnit::KiB },
{ QStringLiteral( "KiB" ), SizeUnit::KiB },
{ QStringLiteral( "M" ), SizeUnit::MiB },
{ QStringLiteral( "MiB" ), SizeUnit::MiB },
{ QStringLiteral( "G" ), SizeUnit::GiB },
static const NamedEnumTable< SizeUnit > names {
{ QStringLiteral( "%" ), SizeUnit::Percent }, { QStringLiteral( "K" ), SizeUnit::KiB },
{ QStringLiteral( "KiB" ), SizeUnit::KiB }, { QStringLiteral( "M" ), SizeUnit::MiB },
{ QStringLiteral( "MiB" ), SizeUnit::MiB }, { QStringLiteral( "G" ), SizeUnit::GiB },
{ QStringLiteral( "GiB" ), SizeUnit::GiB }
};
@ -55,7 +52,9 @@ PartitionSize::PartitionSize( const QString& s )
{
m_value = s.toInt();
if ( m_value > 0 )
{
m_unit = SizeUnit::Byte;
}
}
if ( m_value <= 0 )
@ -69,9 +68,13 @@ qint64
PartitionSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const
{
if ( !isValid() )
{
return -1;
}
if ( totalSectors < 1 || sectorSize < 1 )
{
return -1;
}
switch ( m_unit )
{
@ -79,14 +82,18 @@ PartitionSize::toSectors( qint64 totalSectors, qint64 sectorSize ) const
return -1;
case SizeUnit::Percent:
if ( value() == 100 )
{
return totalSectors; // Common-case, avoid futzing around
}
else
{
return totalSectors * value() / 100;
}
case SizeUnit::Byte:
case SizeUnit::KiB:
case SizeUnit::MiB:
case SizeUnit::GiB:
return CalamaresUtils::bytesToSectors ( toBytes(), sectorSize );
return CalamaresUtils::bytesToSectors( toBytes(), sectorSize );
}
return -1;
@ -96,7 +103,9 @@ qint64
PartitionSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const
{
if ( !isValid() )
{
return -1;
}
switch ( m_unit )
{
@ -104,11 +113,17 @@ PartitionSize::toBytes( qint64 totalSectors, qint64 sectorSize ) const
return -1;
case SizeUnit::Percent:
if ( totalSectors < 1 || sectorSize < 1 )
{
return -1;
}
if ( value() == 100 )
{
return totalSectors * sectorSize; // Common-case, avoid futzing around
}
else
{
return totalSectors * value() / 100;
}
case SizeUnit::Byte:
case SizeUnit::KiB:
case SizeUnit::MiB:
@ -124,7 +139,9 @@ qint64
PartitionSize::toBytes( qint64 totalBytes ) const
{
if ( !isValid() )
{
return -1;
}
switch ( m_unit )
{
@ -132,11 +149,17 @@ PartitionSize::toBytes( qint64 totalBytes ) const
return -1;
case SizeUnit::Percent:
if ( totalBytes < 1 )
{
return -1;
}
if ( value() == 100 )
{
return totalBytes; // Common-case, avoid futzing around
}
else
{
return totalBytes * value() / 100;
}
case SizeUnit::Byte:
case SizeUnit::KiB:
case SizeUnit::MiB:
@ -152,7 +175,9 @@ qint64
PartitionSize::toBytes() const
{
if ( !isValid() )
{
return -1;
}
switch ( m_unit )
{
@ -162,20 +187,22 @@ PartitionSize::toBytes() const
case SizeUnit::Byte:
return value();
case SizeUnit::KiB:
return CalamaresUtils::KiBtoBytes( static_cast<unsigned long long>( value() ) );
return CalamaresUtils::KiBtoBytes( static_cast< unsigned long long >( value() ) );
case SizeUnit::MiB:
return CalamaresUtils::MiBtoBytes( static_cast<unsigned long long>( value() ) );
return CalamaresUtils::MiBtoBytes( static_cast< unsigned long long >( value() ) );
case SizeUnit::GiB:
return CalamaresUtils::GiBtoBytes( static_cast<unsigned long long>( value() ) );
return CalamaresUtils::GiBtoBytes( static_cast< unsigned long long >( value() ) );
}
NOTREACHED return -1;
}
bool
PartitionSize::operator< ( const PartitionSize& other ) const
PartitionSize::operator<( const PartitionSize& other ) const
{
if ( !unitsComparable( m_unit, other.m_unit ) )
{
return false;
}
switch ( m_unit )
{
@ -187,16 +214,18 @@ PartitionSize::operator< ( const PartitionSize& other ) const
case SizeUnit::KiB:
case SizeUnit::MiB:
case SizeUnit::GiB:
return ( toBytes() < other.toBytes () );
return ( toBytes() < other.toBytes() );
}
NOTREACHED return false;
}
bool
PartitionSize::operator> ( const PartitionSize& other ) const
PartitionSize::operator>( const PartitionSize& other ) const
{
if ( !unitsComparable( m_unit, other.m_unit ) )
{
return false;
}
switch ( m_unit )
{
@ -208,16 +237,18 @@ PartitionSize::operator> ( const PartitionSize& other ) const
case SizeUnit::KiB:
case SizeUnit::MiB:
case SizeUnit::GiB:
return ( toBytes() > other.toBytes () );
return ( toBytes() > other.toBytes() );
}
NOTREACHED return false;
}
bool
PartitionSize::operator== ( const PartitionSize& other ) const
PartitionSize::operator==( const PartitionSize& other ) const
{
if ( !unitsComparable( m_unit, other.m_unit ) )
{
return false;
}
switch ( m_unit )
{
@ -229,10 +260,10 @@ PartitionSize::operator== ( const PartitionSize& other ) const
case SizeUnit::KiB:
case SizeUnit::MiB:
case SizeUnit::GiB:
return ( toBytes() == other.toBytes () );
return ( toBytes() == other.toBytes() );
}
NOTREACHED return false;
}
}
} // namespace
} // namespace Partition
} // namespace CalamaresUtils

View file

@ -20,8 +20,8 @@
#ifndef PARTITION_PARTITIONSIZE_H
#define PARTITION_PARTITIONSIZE_H
#include "utils/Units.h"
#include "utils/NamedSuffix.h"
#include "utils/Units.h"
// Qt
#include <QString>
@ -47,21 +47,24 @@ enum class SizeUnit
* the available drive space are on). This class handles parsing
* of such strings from the config file.
*/
class PartitionSize : public NamedSuffix<SizeUnit, SizeUnit::None>
class PartitionSize : public NamedSuffix< SizeUnit, SizeUnit::None >
{
public:
PartitionSize() : NamedSuffix() { }
PartitionSize( int v, SizeUnit u ) : NamedSuffix( v, u ) { }
PartitionSize()
: NamedSuffix()
{
}
PartitionSize( int v, SizeUnit u )
: NamedSuffix( v, u )
{
}
PartitionSize( const QString& );
bool isValid() const
{
return ( unit() != SizeUnit::None ) && ( value() > 0 );
}
bool isValid() const { return ( unit() != SizeUnit::None ) && ( value() > 0 ); }
bool operator< ( const PartitionSize& other ) const;
bool operator> ( const PartitionSize& other ) const;
bool operator== ( const PartitionSize& other ) const;
bool operator<( const PartitionSize& other ) const;
bool operator>( const PartitionSize& other ) const;
bool operator==( const PartitionSize& other ) const;
/** @brief Convert the size to the number of sectors @p totalSectors .
*
@ -109,14 +112,13 @@ public:
*/
static constexpr bool unitsComparable( const SizeUnit u1, const SizeUnit u2 )
{
return !( ( u1 == SizeUnit::None || u2 == SizeUnit::None ) ||
( u1 == SizeUnit::Percent && u2 != SizeUnit::Percent ) ||
( u1 != SizeUnit::Percent && u2 == SizeUnit::Percent ) );
return !( ( u1 == SizeUnit::None || u2 == SizeUnit::None )
|| ( u1 == SizeUnit::Percent && u2 != SizeUnit::Percent )
|| ( u1 != SizeUnit::Percent && u2 == SizeUnit::Percent ) );
}
};
}
} // namespace
} // namespace Partition
} // namespace CalamaresUtils
#endif // PARTITION_PARTITIONSIZE_H
#endif // PARTITION_PARTITIONSIZE_H

View file

@ -31,13 +31,9 @@ Q_DECLARE_METATYPE( SizeUnit )
QTEST_GUILESS_MAIN( PartitionSizeTests )
PartitionSizeTests::PartitionSizeTests()
{
}
PartitionSizeTests::PartitionSizeTests() {}
PartitionSizeTests::~PartitionSizeTests()
{
}
PartitionSizeTests::~PartitionSizeTests() {}
void
PartitionSizeTests::initTestCase()
@ -47,36 +43,38 @@ PartitionSizeTests::initTestCase()
void
PartitionSizeTests::testUnitComparison_data()
{
QTest::addColumn<SizeUnit>("u1");
QTest::addColumn<SizeUnit>("u2");
QTest::addColumn<bool>("comparable");
QTest::addColumn< SizeUnit >( "u1" );
QTest::addColumn< SizeUnit >( "u2" );
QTest::addColumn< bool >( "comparable" );
QTest::newRow("nones") << SizeUnit::None << SizeUnit::None << false;
QTest::newRow("none+%") << SizeUnit::None << SizeUnit::Percent<< false;
QTest::newRow("%+none") << SizeUnit::Percent << SizeUnit::None << false;
QTest::newRow("KiB+none") << SizeUnit::KiB << SizeUnit::None << false;
QTest::newRow("none+MiB") << SizeUnit::None << SizeUnit::MiB << false;
QTest::newRow( "nones" ) << SizeUnit::None << SizeUnit::None << false;
QTest::newRow( "none+%" ) << SizeUnit::None << SizeUnit::Percent << false;
QTest::newRow( "%+none" ) << SizeUnit::Percent << SizeUnit::None << false;
QTest::newRow( "KiB+none" ) << SizeUnit::KiB << SizeUnit::None << false;
QTest::newRow( "none+MiB" ) << SizeUnit::None << SizeUnit::MiB << false;
QTest::newRow("KiB+KiB") << SizeUnit::KiB << SizeUnit::KiB << true;
QTest::newRow("KiB+MiB") << SizeUnit::KiB << SizeUnit::MiB << true;
QTest::newRow("KiB+GiB") << SizeUnit::KiB << SizeUnit::GiB << true;
QTest::newRow("MiB+MiB") << SizeUnit::MiB << SizeUnit::MiB << true;
QTest::newRow("MiB+GiB") << SizeUnit::MiB << SizeUnit::GiB << true;
QTest::newRow("GiB+GiB") << SizeUnit::GiB << SizeUnit::GiB << true;
QTest::newRow( "KiB+KiB" ) << SizeUnit::KiB << SizeUnit::KiB << true;
QTest::newRow( "KiB+MiB" ) << SizeUnit::KiB << SizeUnit::MiB << true;
QTest::newRow( "KiB+GiB" ) << SizeUnit::KiB << SizeUnit::GiB << true;
QTest::newRow( "MiB+MiB" ) << SizeUnit::MiB << SizeUnit::MiB << true;
QTest::newRow( "MiB+GiB" ) << SizeUnit::MiB << SizeUnit::GiB << true;
QTest::newRow( "GiB+GiB" ) << SizeUnit::GiB << SizeUnit::GiB << true;
QTest::newRow("%+None") << SizeUnit::Percent << SizeUnit::None << false;
QTest::newRow("%+%") << SizeUnit::Percent << SizeUnit::Percent << true;
QTest::newRow("%+KiB") << SizeUnit::Percent << SizeUnit::KiB << false;
QTest::newRow( "%+None" ) << SizeUnit::Percent << SizeUnit::None << false;
QTest::newRow( "%+%" ) << SizeUnit::Percent << SizeUnit::Percent << true;
QTest::newRow( "%+KiB" ) << SizeUnit::Percent << SizeUnit::KiB << false;
}
static bool
original_compare( SizeUnit m_unit, SizeUnit other_m_unit )
{
if ( ( m_unit == SizeUnit::None || other_m_unit == SizeUnit::None ) ||
( m_unit == SizeUnit::Percent && other_m_unit != SizeUnit::Percent ) ||
( m_unit != SizeUnit::Percent && other_m_unit == SizeUnit::Percent ) )
if ( ( m_unit == SizeUnit::None || other_m_unit == SizeUnit::None )
|| ( m_unit == SizeUnit::Percent && other_m_unit != SizeUnit::Percent )
|| ( m_unit != SizeUnit::Percent && other_m_unit == SizeUnit::Percent ) )
{
return false;
}
return true;
}
@ -104,33 +102,33 @@ PartitionSizeTests::testUnitComparison()
void
PartitionSizeTests::testUnitNormalisation_data()
{
QTest::addColumn<SizeUnit>("u1");
QTest::addColumn<int>("v");
QTest::addColumn<long>("bytes");
QTest::addColumn< SizeUnit >( "u1" );
QTest::addColumn< int >( "v" );
QTest::addColumn< long >( "bytes" );
QTest::newRow("none") << SizeUnit::None << 16 << -1L;
QTest::newRow("none") << SizeUnit::None << 0 << -1L;
QTest::newRow("none") << SizeUnit::None << -2 << -1L;
QTest::newRow( "none" ) << SizeUnit::None << 16 << -1L;
QTest::newRow( "none" ) << SizeUnit::None << 0 << -1L;
QTest::newRow( "none" ) << SizeUnit::None << -2 << -1L;
QTest::newRow("percent") << SizeUnit::Percent << 0 << -1L;
QTest::newRow("percent") << SizeUnit::Percent << 16 << -1L;
QTest::newRow("percent") << SizeUnit::Percent << -2 << -1L;
QTest::newRow( "percent" ) << SizeUnit::Percent << 0 << -1L;
QTest::newRow( "percent" ) << SizeUnit::Percent << 16 << -1L;
QTest::newRow( "percent" ) << SizeUnit::Percent << -2 << -1L;
QTest::newRow("KiB") << SizeUnit::KiB << 0 << -1L;
QTest::newRow("KiB") << SizeUnit::KiB << 1 << 1024L;
QTest::newRow("KiB") << SizeUnit::KiB << 1000 << 1024000L;
QTest::newRow("KiB") << SizeUnit::KiB << 1024 << 1024 * 1024L;
QTest::newRow("KiB") << SizeUnit::KiB << -2 << -1L;
QTest::newRow( "KiB" ) << SizeUnit::KiB << 0 << -1L;
QTest::newRow( "KiB" ) << SizeUnit::KiB << 1 << 1024L;
QTest::newRow( "KiB" ) << SizeUnit::KiB << 1000 << 1024000L;
QTest::newRow( "KiB" ) << SizeUnit::KiB << 1024 << 1024 * 1024L;
QTest::newRow( "KiB" ) << SizeUnit::KiB << -2 << -1L;
QTest::newRow("MiB") << SizeUnit::MiB << 0 << -1L;
QTest::newRow("MiB") << SizeUnit::MiB << 1 << 1024 * 1024L;
QTest::newRow("MiB") << SizeUnit::MiB << 1000 << 1024 * 1024000L;
QTest::newRow("MiB") << SizeUnit::MiB << 1024 << 1024 * 1024 * 1024L;
QTest::newRow("MiB") << SizeUnit::MiB << -2 << -1L;
QTest::newRow( "MiB" ) << SizeUnit::MiB << 0 << -1L;
QTest::newRow( "MiB" ) << SizeUnit::MiB << 1 << 1024 * 1024L;
QTest::newRow( "MiB" ) << SizeUnit::MiB << 1000 << 1024 * 1024000L;
QTest::newRow( "MiB" ) << SizeUnit::MiB << 1024 << 1024 * 1024 * 1024L;
QTest::newRow( "MiB" ) << SizeUnit::MiB << -2 << -1L;
QTest::newRow("GiB") << SizeUnit::GiB << 0 << -1L;
QTest::newRow("GiB") << SizeUnit::GiB << 1 << 1024 * 1024 * 1024L;
QTest::newRow("GiB") << SizeUnit::GiB << 2 << 2048 * 1024 * 1024L;
QTest::newRow( "GiB" ) << SizeUnit::GiB << 0 << -1L;
QTest::newRow( "GiB" ) << SizeUnit::GiB << 1 << 1024 * 1024 * 1024L;
QTest::newRow( "GiB" ) << SizeUnit::GiB << 2 << 2048 * 1024 * 1024L;
}
void
@ -140,5 +138,5 @@ PartitionSizeTests::testUnitNormalisation()
QFETCH( int, v );
QFETCH( long, bytes );
QCOMPARE( PartitionSize( v, u1 ).toBytes(), static_cast<qint64>( bytes ) );
QCOMPARE( PartitionSize( v, u1 ).toBytes(), static_cast< qint64 >( bytes ) );
}

View file

@ -30,10 +30,10 @@ public:
private Q_SLOTS:
void initTestCase();
void testUnitComparison_data();
void testUnitComparison();
void testUnitNormalisation_data();
void testUnitNormalisation();
};