From 55abe0247bb7ece86277230c56488c0457a72317 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Sun, 14 Jun 2020 23:42:25 +0200 Subject: [PATCH] [libcalamares] Fix tests on 32-bit platforms - The size of a 2GiB partition (in bytes) is larger than the largest 32-bit signed integer; we hit signed overflow while calculating 2^11 * 2^10 * 2^10 and the test fails. - Switch the whole table of sizes to qint64 instead. - For testing purposes only, introduce a _qi suffix for qint64. FIXES #1430 --- src/libcalamares/partition/Tests.cpp | 63 +++++++++++++++------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/src/libcalamares/partition/Tests.cpp b/src/libcalamares/partition/Tests.cpp index 7719f2e84..e52b8edf1 100644 --- a/src/libcalamares/partition/Tests.cpp +++ b/src/libcalamares/partition/Tests.cpp @@ -1,5 +1,5 @@ /* === This file is part of Calamares - === - * + * * SPDX-FileCopyrightText: 2019 Adriaan de Groot * * Calamares is free software: you can redistribute it and/or modify @@ -103,36 +103,43 @@ PartitionSizeTests::testUnitComparison() QCOMPARE( original_compare( u1, u2 ), PartitionSize::unitsComparable( u1, u2 ) ); } +/* Operator to make the table in testUnitNormalisation_data easier to write */ +constexpr qint64 operator""_qi( unsigned long long m ) +{ + return qint64( m ); +} + void PartitionSizeTests::testUnitNormalisation_data() { 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( "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( "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::addColumn< qint64 >( "bytes" ); + + QTest::newRow( "none" ) << SizeUnit::None << 16 << -1_qi; + QTest::newRow( "none" ) << SizeUnit::None << 0 << -1_qi; + QTest::newRow( "none" ) << SizeUnit::None << -2 << -1_qi; + + QTest::newRow( "percent" ) << SizeUnit::Percent << 0 << -1_qi; + QTest::newRow( "percent" ) << SizeUnit::Percent << 16 << -1_qi; + QTest::newRow( "percent" ) << SizeUnit::Percent << -2 << -1_qi; + + QTest::newRow( "KiB" ) << SizeUnit::KiB << 0 << -1_qi; + QTest::newRow( "KiB" ) << SizeUnit::KiB << 1 << 1024_qi; + QTest::newRow( "KiB" ) << SizeUnit::KiB << 1000 << 1024000_qi; + QTest::newRow( "KiB" ) << SizeUnit::KiB << 1024 << 1024 * 1024_qi; + QTest::newRow( "KiB" ) << SizeUnit::KiB << -2 << -1_qi; + + QTest::newRow( "MiB" ) << SizeUnit::MiB << 0 << -1_qi; + QTest::newRow( "MiB" ) << SizeUnit::MiB << 1 << 1024 * 1024_qi; + QTest::newRow( "MiB" ) << SizeUnit::MiB << 1000 << 1024 * 1024000_qi; + QTest::newRow( "MiB" ) << SizeUnit::MiB << 1024 << 1024 * 1024 * 1024_qi; + QTest::newRow( "MiB" ) << SizeUnit::MiB << -2 << -1_qi; + + QTest::newRow( "GiB" ) << SizeUnit::GiB << 0 << -1_qi; + QTest::newRow( "GiB" ) << SizeUnit::GiB << 1 << 1024_qi * 1024 * 1024_qi; + // This one overflows 32-bits, which is why we want 64-bits for the whole table + QTest::newRow( "GiB" ) << SizeUnit::GiB << 2 << 2048_qi * 1024 * 1024_qi; } void @@ -140,7 +147,7 @@ PartitionSizeTests::testUnitNormalisation() { QFETCH( SizeUnit, u1 ); QFETCH( int, v ); - QFETCH( long, bytes ); + QFETCH( qint64, bytes ); - QCOMPARE( PartitionSize( v, u1 ).toBytes(), static_cast< qint64 >( bytes ) ); + QCOMPARE( PartitionSize( v, u1 ).toBytes(), bytes ); }