Files
MuditaOS/module-utils/test/test_math.cpp
Marcin Smoczyński f1fc9df152 [EGD-4977] Reduce audio lag during voice call
Reduce audio delay by reducing audio buffer size in router operation.
Audio streams are now created directly in the operations, not in the
audio service, which gives more flexibility.

Audio Buffer size is calculated based on endpoints (source, sink) and
operation capabilities. This commit also enables allocations in a
non-cacheable region of OCRAM for endpoints that use DMA for data
transport.

Introduce power-of-two operations that use built-in functions and
possibly dedicated hardware instructions of an MCU. These operations
are required by the audio stream buffer size calculation algorithm.

Signed-off-by: Marcin Smoczyński <smoczynski.marcin@gmail.com>
2021-02-01 22:22:12 +01:00

89 lines
3.0 KiB
C++

// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include <catch2/catch.hpp>
#include "math/Math.hpp"
#include <climits>
using namespace trigonometry;
using namespace binary;
TEST_CASE("Math")
{
SECTION("Trigonometry")
{
SECTION("Degrees to radians")
{
REQUIRE(toRadians(-720) == Approx(-4 * M_PI));
REQUIRE(toRadians(-360) == Approx(-2 * M_PI));
REQUIRE(toRadians(-180) == Approx(-M_PI));
REQUIRE(toRadians(-30) == Approx(-M_PI / 6));
REQUIRE(toRadians(-12) == Approx(-M_PI / 15));
REQUIRE(toRadians(-1) == Approx(-M_PI / 180));
REQUIRE(toRadians(1) == Approx(M_PI / 180));
REQUIRE(toRadians(12) == Approx(M_PI / 15));
REQUIRE(toRadians(30) == Approx(M_PI / 6));
REQUIRE(toRadians(180) == Approx(M_PI));
REQUIRE(toRadians(360) == Approx(2 * M_PI));
REQUIRE(toRadians(720) == Approx(4 * M_PI));
}
SECTION("Adjacent side length calculation")
{
REQUIRE(AdjacentSide::fromAngle(toRadians(30), 6) == 5);
REQUIRE(AdjacentSide::fromCosine(std::cos(toRadians(30)), 6) == 5);
REQUIRE(AdjacentSide::fromAngle(toRadians(60), 6) == 3);
REQUIRE(AdjacentSide::fromCosine(std::cos(toRadians(60)), 6) == 3);
}
SECTION("Opposite side length calculation")
{
REQUIRE(OppositeSide::fromAngle(toRadians(30), 6) == 3);
REQUIRE(OppositeSide::fromSine(std::sin(toRadians(30)), 6) == 3);
REQUIRE(OppositeSide::fromAngle(toRadians(60), 6) == 5);
REQUIRE(OppositeSide::fromSine(std::sin(toRadians(60)), 6) == 5);
}
}
SECTION("Binary operations")
{
SECTION("Is number a power of two")
{
REQUIRE(isPowerOfTwo(0) == false);
REQUIRE(isPowerOfTwo(1) == true);
REQUIRE(isPowerOfTwo(2) == true);
REQUIRE(isPowerOfTwo(3) == false);
REQUIRE(isPowerOfTwo(64) == true);
REQUIRE(isPowerOfTwo(128) == true);
REQUIRE(isPowerOfTwo(64 + 128) == false);
}
SECTION("Find nearest power of two (floor)")
{
REQUIRE(floorPowerOfTwo(0) == 0);
REQUIRE(floorPowerOfTwo(1) == 1);
REQUIRE(floorPowerOfTwo(2) == 2);
REQUIRE(floorPowerOfTwo(3) == 2);
REQUIRE(floorPowerOfTwo(7) == 4);
REQUIRE(floorPowerOfTwo(0xffffffff) == 0x80000000);
}
SECTION("Find nearest power of two (ceil)")
{
REQUIRE(ceilPowerOfTwo(0) == 0);
REQUIRE(ceilPowerOfTwo(1) == 1);
REQUIRE(ceilPowerOfTwo(2) == 2);
REQUIRE(ceilPowerOfTwo(3) == 4);
REQUIRE(ceilPowerOfTwo(7) == 8);
REQUIRE(ceilPowerOfTwo(62) == 64);
REQUIRE(ceilPowerOfTwo(UINT_MAX) == 0);
}
}
}