CMathUtils::roundToMultipleOf

refs #556
This commit is contained in:
Roland Winklmeier
2016-01-14 00:30:14 +01:00
parent ab2e5af8d6
commit b3d45f58e6
5 changed files with 96 additions and 0 deletions

View File

@@ -93,5 +93,21 @@ namespace BlackMisc
return r % ((high + 1) - low) + low;
}
int CMathUtils::roundToMultipleOf(int value, int divisor)
{
Q_ASSERT(divisor != 0);
Q_ASSERT(divisor >= - std::numeric_limits<int>::max());
divisor = std::abs(divisor);
Q_ASSERT(std::abs(value) < std::numeric_limits<int>::max() - divisor / 2);
int multiplier = value / divisor;
int remainder = std::abs(value % divisor);
int shortfall = divisor - remainder;
if (shortfall < remainder) { multiplier += value < 0 ? -1 : 1; }
return multiplier * divisor;
}
} // namespace
} // namespace

View File

@@ -101,6 +101,9 @@ namespace BlackMisc
//! Random number between low and high
static int randomInteger(int low, int high);
//! Round numToRound to the nearest multiple of multiple
static int roundToMultipleOf(int value, int divisor);
};
} // namespace

View File

@@ -17,6 +17,7 @@
#include "testvaluecache.h"
#include "testblackmiscmain.h"
#include "testweather.h"
#include "testmath.h"
namespace BlackMiscTest
{
@@ -36,6 +37,7 @@ namespace BlackMiscTest
CTestIdentifier identifierTests;
CTestValueCache valueCacheTests;
CTestWeather weatherTests;
CTestMath mathTests;
status |= QTest::qExec(&pqBaseTests, argc, argv);
status |= QTest::qExec(&avBaseTests, argc, argv);
status |= QTest::qExec(&geoTests, argc, argv);
@@ -45,6 +47,7 @@ namespace BlackMiscTest
status |= QTest::qExec(&inputTests, argc, argv);
status |= QTest::qExec(&valueCacheTests, argc, argv);
status |= QTest::qExec(&weatherTests, argc, argv);
status |= QTest::qExec(&mathTests, argc, argv);
}
return status;
}

View File

@@ -0,0 +1,38 @@
/* Copyright (C) 2016
* swift Project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "testmath.h"
#include "blackmisc/math/mathutils.h"
using namespace BlackMisc::Math;
namespace BlackMiscTest
{
void CTestMath::testRoundToMultipleOf()
{
QVERIFY2(CMathUtils::roundToMultipleOf(0, 3) == 0, "Nearest multiple of 3 from 0 should be 0");
QVERIFY2(CMathUtils::roundToMultipleOf(1, 3) == 0, "Nearest multiple of 3 from 1 should be 0");
QVERIFY2(CMathUtils::roundToMultipleOf(2, 3) == 3, "Nearest multiple of 3 from 2 should be 3");
QVERIFY2(CMathUtils::roundToMultipleOf(3, 3) == 3, "Nearest multiple of 3 from 3 should be 3");
QVERIFY2(CMathUtils::roundToMultipleOf(0, -3) == 0, "Nearest multiple of -3 from 0 should be 0");
QVERIFY2(CMathUtils::roundToMultipleOf(1, -3) == 0, "Nearest multiple of -3 from 1 should be 0");
QVERIFY2(CMathUtils::roundToMultipleOf(2, -3) == 3, "Nearest multiple of -3 from 2 should be 3");
QVERIFY2(CMathUtils::roundToMultipleOf(3, -3) == 3, "Nearest multiple of -3 from 3 should be 3");
QVERIFY2(CMathUtils::roundToMultipleOf(-1, 3) == 0, "Nearest multiple of 3 from -1 should be 0");
QVERIFY2(CMathUtils::roundToMultipleOf(-2, 3) == -3, "Nearest multiple of 3 from -2 should be -3");
QVERIFY2(CMathUtils::roundToMultipleOf(-3, 3) == -3, "Nearest multiple of 3 from -3 should be -3");
QVERIFY2(CMathUtils::roundToMultipleOf(-1, -3) == 0, "Nearest multiple of -3 from -1 should be 0");
QVERIFY2(CMathUtils::roundToMultipleOf(-2, -3) == -3, "Nearest multiple of -3 from -2 should be -3");
QVERIFY2(CMathUtils::roundToMultipleOf(-3, -3) == -3, "Nearest multiple of -3 from -3 should be -3");
}
} // namespace

View File

@@ -0,0 +1,36 @@
/* Copyright (C) 2016
* swift project community / contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISCTEST_TESTMATH_H
#define BLACKMISCTEST_TESTMATH_H
#include <QtTest/QtTest>
namespace BlackMiscTest
{
//! Math classes tests
class CTestMath : public QObject
{
Q_OBJECT
public:
//! Standard test case constructor
explicit CTestMath(QObject *parent = nullptr) : QObject(parent) {}
private slots:
//! Unit test for round to multiple of
void testRoundToMultipleOf();
};
} // namespace
#endif // guard