// Klasa pomocnicza do testowania prywatnych metod IntervalExtractor
class IntervalExtractorPrivateTests {
public:
static float TestTrapezoidalMethod(IntervalExtractor& extractor, int startIndex, int length) {
return extractor.trapezoidalMethod(startIndex, length);
}
};
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
Jasne, oto dostosowane testy jednostkowe zgodne z podanym początkiem pliku. Nazwy testów zostały zmienione tak, aby wskazywały na testowaną metodę (TrapezoidalMethod) w ramach klasy IntervalExtractorPrivateTestsCore.
cpp
#include "pch.h" // Standardowy plik nagłówkowy dla Precompiled Headers w Visual Studio
#include "CppUnitTest.h" // Biblioteka testowa Visual Studio
#include "../Segmentation/IntervalExtractor.h" // Ścieżka do pliku nagłówkowego klasy IntervalExtractor
// Klasa pomocnicza do testowania prywatnych metod klasy IntervalExtractor.
// Jest zadeklarowana jako 'friend class' w IntervalExtractor, co umożliwia jej dostęp do prywatnych składowych.
class IntervalExtractorPrivateTests {
public:
/**
* @brief Statyczna metoda udostępniająca dostęp do prywatnej metody trapezoidalMethod.
* @param extractor Instancja klasy IntervalExtractor.
* @param startIndex Indeks początkowy segmentu danych.
* @param length Długość segmentu danych.
* @return Wynik obliczenia energii metodą trapezów.
*/
static float TestTrapezoidalMethod(IntervalExtractor& extractor, int startIndex, int length) {
return extractor.trapezoidalMethod(startIndex, length);
}
};
// Deklaracja przestrzeni nazw dla testów jednostkowych
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitIntervalExtractorPrivateTest // Zgodnie z podanym początkiem pliku
{
TEST_CLASS(IntervalExtractorPrivateTestsCore) // Zgodnie z podanym początkiem pliku
{
public:
// --- Testy dla metody trapezoidalMethod ---
// ✓ Podstawowy przypadek - pojedynczy interwał
TEST_METHOD(TrapezoidalMethod_SingleInterval)
{
std::vector<float> y = {2.0f, 3.0f};
std::vector<float> t = {0.0f, 1.0f};
// Konstruktor klasy IntervalExtractor ustawia domyślnie metodę na TRAPEZOIDAL_METHOD,
// ale jawne podanie zapewnia poprawność testu niezależnie od domyślnych ustawień.
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
// Wywołanie testowanej metody dla fragmentu danych: od indeksu 0, długość 1 (obejmuje y[0], y[1])
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 1);
// Oczekiwana wartość: 0.5 * (y[0] + y[1]) * (t[1] - t[0]) = 0.5 * (2.0 + 3.0) * (1.0 - 0.0) = 0.5 * 5.0 * 1.0 = 2.5
Assert::AreEqual(2.5f, energy, 0.001f, L"TestTrapezoidalMethod: Single interval calculation is incorrect.");
}
// ✓ Wiele interwałów - sprawdzenie sumowania trapezów
TEST_METHOD(TrapezoidalMethod_MultipleIntervals)
{
std::vector<float> y = {2.0f, 3.0f, 4.0f, 5.0f};
std::vector<float> t = {0.0f, 1.0f, 2.0f, 3.0f}; // Równe odstępy czasowe delta_t = 1.0
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
// Wywołanie testowanej metody dla całego dostępnego zakresu danych: od indeksu 0, długość 3 (obejmuje y[0]..y[3])
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Oczekiwana wartość: 0.5 * [(y[0]+y[1])*(t[1]-t[0]) + (y[1]+y[2])*(t[2]-t[1]) + (y[2]+y[3])*(t[3]-t[2)]
// = 0.5 * [(2+3)*1 + (3+4)*1 + (4+5)*1] = 0.5 * [5 + 7 + 9] = 0.5 * 21 = 10.5
Assert::AreEqual(10.5f, energy, 0.001f, L"TestTrapezoidalMethod: Multiple intervals calculation is incorrect.");
}
// ✓ Nierówne interwały czasowe - różne długości przedziałów
TEST_METHOD(TrapezoidalMethod_NonUniformTimeIntervals)
{
std::vector<float> y = {2.0f, 3.0f, 4.0f, 5.0f};
std::vector<float> t = {0.0f, 1.0f, 3.0f, 6.0f}; // Różne przyrosty czasowe: dt1=1.0, dt2=2.0, dt3=3.0
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
// Wywołanie dla całego zakresu: od indeksu 0, długość 3
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Oczekiwana wartość: 0.5 * [(y[0]+y[1])*(t[1]-t[0]) + (y[1]+y[2])*(t[2]-t[1]) + (y[2]+y[3])*(t[3]-t[2)]
// = 0.5 * [(2+3)*1.0 + (3+4)*2.0 + (4+5)*3.0] = 0.5 * [5*1.0 + 7*2.0 + 9*3.0] = 0.5 * [5 + 14 + 27] = 0.5 * 46 = 23.0
Assert::AreEqual(23.0f, energy, 0.001f, L"TestTrapezoidalMethod: Non-uniform time intervals calculation is incorrect.");
}
// ✓ Wartości zerowe - przypadek brzegowy
TEST_METHOD(TrapezoidalMethod_ZeroValues)
{
std::vector<float> y = {0.0f, 0.0f, 0.0f, 0.0f};
std::vector<float> t = {0.0f, 1.0f, 2.0f, 3.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Oczekiwana wartość: Suma iloczynów (0+0)*dt jest zawsze 0.
Assert::AreEqual(0.0f, energy, 0.001f, L"TestTrapezoidalMethod: Zero values calculation is incorrect.");
}
// ✓ Wartości ujemne - sprawdzenie obsługi liczb ujemnych
TEST_METHOD(TrapezoidalMethod_NegativeValues)
{
std::vector<float> y = {-2.0f, -3.0f, -4.0f, -5.0f};
std::vector<float> t = {0.0f, 1.0f, 2.0f, 3.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Oczekiwana wartość: 0.5 * [(-2+-3)*1 + (-3+-4)*1 + (-4+-5)*1] = 0.5 * [-5 -7 -9] = 0.5 * -21 = -10.5
Assert::AreEqual(-10.5f, energy, 0.001f, L"TestTrapezoidalMethod: Negative values calculation is incorrect.");
}
// ✓ Wartości mieszane - dodatnie i ujemne
TEST_METHOD(TrapezoidalMethod_MixedValues)
{
std::vector<float> y = {-2.0f, 3.0f, -4.0f, 5.0f};
std::vector<float> t = {0.0f, 1.0f, 2.0f, 3.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Oczekiwana wartość: 0.5 * [(-2+3)*1 + (3+-4)*1 + (-4+5)*1] = 0.5 * [1*1 + -1*1 + 1*1] = 0.5 * [1 - 1 + 1] = 0.5 * 1 = 0.5
Assert::AreEqual(0.5f, energy, 0.001f, L"TestTrapezoidalMethod: Mixed values calculation is incorrect.");
}
// ✓ Pojedynczy punkt - przypadek brzegowy z length = 0
// Metoda trapezoidalMethod ma pętlę for(int i = startIndex + 1; i <= startIndex + length; i++).
// Jeśli length jest 0, pętla nie wykona się ani razu.
TEST_METHOD(TrapezoidalMethod_ZeroLength)
{
std::vector<float> y = {2.0f, 3.0f, 4.0f, 5.0f};
std::vector<float> t = {0.0f, 1.0f, 2.0f, 3.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
// Wywołanie dla length = 0. Pętla nie powinna dodać niczego do energii.
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 0);
// Oczekiwana wartość: 0.0
Assert::AreEqual(0.0f, energy, 0.001f, L"TestTrapezoidalMethod: Zero length segment calculation should be 0.");
}
// ✓ Funkcja stała - sprawdzenie dla prostokąta
// Dla funkcji stałej metoda trapezów powinna zwracać taki sam wynik jak metoda prostokątów.
TEST_METHOD(TrapezoidalMethod_ConstantFunction)
{
std::vector<float> y = {5.0f, 5.0f, 5.0f, 5.0f};
std::vector<float> t = {0.0f, 1.0f, 2.0f, 3.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Oczekiwana wartość: 0.5 * [(5+5)*1 + (5+5)*1 + (5+5)*1] = 0.5 * [10 + 10 + 10] = 0.5 * 30 = 15.0
// Jest to zgodne z polem prostokąta: wysokość * szerokość = 5.0 * 3.0 = 15.0
Assert::AreEqual(15.0f, energy, 0.001f, L"TestTrapezoidalMethod: Constant function calculation incorrect.");
}
// ✓ Funkcja liniowa - sprawdzenie dokładności dla przypadku, gdzie metoda trapezów jest dokładna
// Metoda trapezów jest analitycznie dokładna dla funkcji liniowych.
TEST_METHOD(TrapezoidalMethod_LinearFunction)
{
// Funkcja liniowa y = 2x + 1. Dla x=0,1,2,3 wartości y wynoszą 1,3,5,7.
std::vector<float> y = {1.0f, 3.0f, 5.0f, 7.0f};
std::vector<float> t = {0.0f, 1.0f, 2.0f, 3.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Analityczna całka funkcji 2x+1 od 0 do 3 to x^2+x | od 0 do 3 = (3^2+3) - (0^2+0) = 9+3=12.
// Obliczenie numeryczne: 0.5 * [(1+3)*1 + (3+5)*1 + (5+7)*1] = 0.5 * [4 + 8 + 12] = 0.5 * 24 = 12.0
Assert::AreEqual(12.0f, energy, 0.001f, L"TestTrapezoidalMethod: Linear function calculation precision failed.");
}
// ✓ Podinterwał - sprawdzenie obliczania energii dla fragmentu danych
TEST_METHOD(TrapezoidalMethod_SubInterval)
{
std::vector<float> y = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
std::vector<float> t = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
// Oblicz energię dla fragmentu danych od indeksu 2, o długości 2.
// Oznacza to, że używamy punktów y[2], y[3], y[4] i odpowiadających im czasów t[2], t[3], t[4].
// Pętla for(int i = startIndex + 1; i <= startIndex + length; i++)
// dla startIndex=2, length=2 -> pętla dla i=3 i i=4
// i=3: (y[2] + y[3]) * (t[3] - t[2]) = (3.0f + 4.0f) * (2.0f - 1.0f) = 7.0f * 1.0f = 7.0f
// i=4: (y[3] + y[4]) * (t[4] - t[3]) = (4.0f + 5.0f) * (3.0f - 2.0f) = 9.0f * 1.0f = 9.0f
// Suma: 7.0f + 9.0f = 16.0f
// Wynik: 0.5f * 16.0f = 8.0f
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 2, 2);
Assert::AreEqual(8.0f, energy, 0.001f, L"TestTrapezoidalMethod: Sub-interval calculation is incorrect.");
}
// Dodatkowy test - sprawdzenie różnych startIndex dla wielu interwałów
TEST_METHOD(TrapezoidalMethod_DifferentStartIndices)
{
std::vector<float> y = {10.0f, 20.0f, 30.0f, 40.0f, 50.0f};
std::vector<float> t = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
// Test fragmentu od index 0, length 2 (obejmuje y[0], y[1], y[2])
// Pętla for (i=1 do 2):
// i=1: (y[0]+y[1])*(t[1]-t[0]) = (10+20)*1 = 30
// i=2: (y[1]+y[2])*(t[2]-t[1]) = (20+30)*1 = 50
// Suma = 30 + 50 = 80. Wynik = 0.5 * 80 = 40.0
float energy1 = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 2);
Assert::AreEqual(40.0f, energy1, 0.001f, L"TestTrapezoidalMethod: Different start indices (0, 2) failed.");
// Test fragmentu od index 1, length 2 (obejmuje y[1], y[2], y[3])
// Pętla for (i=2 do 3):
// i=2: (y[1]+y[2])*(t[2]-t[1]) = (20+30)*1 = 50
// i=3: (y[2]+y[3])*(t[3]-t[2]) = (30+40)*1 = 70
// Suma = 50 + 70 = 120. Wynik = 0.5 * 120 = 60.0
float energy2 = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 1, 2);
Assert::AreEqual(60.0f, energy2, 0.001f, L"TestTrapezoidalMethod: Different start indices (1, 2) failed.");
// Test fragmentu od index 2, length 2 (obejmuje y[2], y[3], y[4])
// Pętla for (i=3 do 4):
// i=3: (y[2]+y[3])*(t[3]-t[2]) = (30+40)*1 = 70
// i=4: (y[3]+y[4])*(t[4]-t[3]) = (40+50)*1 = 90
// Suma = 70 + 90 = 160. Wynik = 0.5 * 160 = 80.0
float energy3 = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 2, 2);
Assert::AreEqual(80.0f, energy3, 0.001f, L"TestTrapezoidalMethod: Different start indices (2, 2) failed.");
// Dodatkowy test z length = 1
// Test fragmentu od index 1, length 1 (obejmuje y[1], y[2])
// Pętla for (i=2 do 2):
// i=2: (y[1]+y[2])*(t[2]-t[1]) = (20+30)*1 = 50
// Suma = 50. Wynik = 0.5 * 50 = 25.0
float energy4 = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 1, 1);
Assert::AreEqual(25.0f, energy4, 0.001f, L"TestTrapezoidalMethod: Different start indices (1, 1) failed.");
}
// Test bardzo małych wartości
TEST_METHOD(TrapezoidalMethod_SmallValues)
{
std::vector<float> y = {0.00001f, 0.00002f, 0.00003f};
std::vector<float> t = {0.0f, 0.00001f, 0.00002f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
// Pętla for (i=1 do 2):
// i=1: (y[0]+y[1])*(t[1]-t[0]) = (0.00001 + 0.00002) * (0.00001 - 0.0) = 0.00003 * 0.00001 = 0.0000000003
// i=2: (y[1]+y[2])*(t[2]-t[1]) = (0.00002 + 0.00003) * (0.00002 - 0.00001) = 0.00005 * 0.00001 = 0.0000000005
// Suma = 0.0000000008. Wynik = 0.5 * Suma = 0.0000000004
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 2);
float expected = 0.5f * ((0.00001f + 0.00002f) * 0.00001f + (0.00002f + 0.00003f) * 0.00001f);
// Używamy niższej precyzji ze względu na operacje na małych liczbach
Assert::AreEqual(expected, energy, 1e-10f, L"TestTrapezoidalMethod: Small values calculation precision failed.");
}
// Test dużych wartości
TEST_METHOD(TrapezoidalMethod_LargeValues)
{
std::vector<float> y = {10000.0f, 20000.0f, 30000.0f};
std::vector<float> t = {0.0f, 1000.0f, 2000.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
// Pętla for (i=1 do 2):
// i=1: (y[0]+y[1])*(t[1]-t[0]) = (10000 + 20000) * (1000 - 0) = 30000 * 1000 = 30,000,000
// i=2: (y[1]+y[2])*(t[2]-t[1]) = (20000 + 30000) * (2000 - 1000) = 50000 * 1000 = 50,000,000
// Suma = 30,000,000 + 50,000,000 = 80,000,000
// Wynik = 0.5 * 80,000,000 = 40,000,000
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 2);
// Tolerancja na poziomie 1.0 jest rozsądna dla tak dużych wartości.
Assert::AreEqual(40000000.0f, energy, 1.0f, L"TestTrapezoidalMethod: Large values calculation failed.");
}
};
}