mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 14:55:36 +08:00
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
38
tests/blackmisc/testmath.cpp
Normal file
38
tests/blackmisc/testmath.cpp
Normal 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
|
||||
36
tests/blackmisc/testmath.h
Normal file
36
tests/blackmisc/testmath.h
Normal 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
|
||||
Reference in New Issue
Block a user