Napisz testy jednostkowe w Visual studio native tests dla metody TrapezoidalMethod zaimplementowanej w poniższej klasie.
#pragma once
#include "Interval.h"
#include
#include
class IntervalExtractor
{
friend class IntervalExtractorPrivateTests;
public:
/**
* @brief Type alias for a pointer to an energy calculation method.
*
* This type alias represents a pointer to a member function of the IntervalExtractor class
* that takes two integer parameters (int, int) and returns a float. It is used to specify
* the method for calculating energy based on different integration methods.
/
using EnergyMethodFuncPtr = float (IntervalExtractor::)(int, int) const;
code
/**
* @brief Enum for specifying the energy calculation method.
*
* This enum defines the available methods for energy calculation during the extraction process.
*/
enum class EnergyMethod : uint8_t {
LEFT_RECTANGLE_METHOD, ///< Left rectangle method for energy calculation.
RIGHT_RECTANGLE_METHOD, ///< Right rectangle method for energy calculation.
TRAPEZOIDAL_METHOD ///< Trapezoidal method for energy calculation.
};
/**
* @brief Default constructor for the `IntervalExtractor` class.
*
* This constructor initializes the `IntervalExtractor` object with empty data vectors. The vectors
* will reference a static empty vector by default, ensuring that no memory allocation occurs unless
* the user provides specific data.
*/
IntervalExtractor() = default;
/**
* @brief Constructs an `IntervalExtractor` with given data vector and optional time vector.
*
* If the time vector 't' is not provided, it defaults to a vector with implicit time indices (0, 1, 2, ...).
* If the sizes of `y` and `t` differ, both vectors are reset to an empty vector, and a warning is logged.
* This ensures that `y` and `t` always have matching sizes and consistent state.
*
* @param y Reference to a vector of function values (y-values).
* @param energyMethod Energy calculation method (defaults to `TRAPEZOIDAL_METHOD`).
* @param t Reference to a vector of time points corresponding to `y` (optional).
* @param maxAcceptableTimeGap Maximum time gap between consecutive data points;
* greater time gap is considered as data gathering failure and ends the interval
* (defaults to 1.0).
*/
IntervalExtractor(std::vector<float>& y,
EnergyMethod energyMethod = EnergyMethod::TRAPEZOIDAL_METHOD,
std::vector<float>& t = empty,
float maxAcceptableTimeGap = 1.0);
/// @brief Deleted copy constructor to prevent copying.
IntervalExtractor(const IntervalExtractor&) = delete;
/// @brief Deleted copy assignment operator to prevent copying.
IntervalExtractor& operator=(const IntervalExtractor&) = delete;
/// @brief Default move constructor for efficient resource transfer.
IntervalExtractor(IntervalExtractor&&) noexcept = default;
/// @brief Default move assignment operator for efficient resource transfer.
IntervalExtractor& operator=(IntervalExtractor&&) noexcept = default;
/// @name Getters
///@{
const std::vector<float>& getY() const {return y;}
const std::vector<float>& getT() const {return t;}
const std::vector<Interval>& getIntervals() const {return intervals;}
float getMaxAcceptableTimeGap() const {return maxAcceptableTimeGap;}
///@}
void extractIntervals(float absolute, float relative);
private:
/// @brief Static empty vector used as the default reference for y and t.
static std::vector empty;
Poniżej prezentuję testy jednostkowe dla metody trapezoidalMethod używając Visual Studio Native Tests. Ponieważ metoda ta jest prywatna, będziemy używać klasy IntervalExtractorPrivateTests (zadeklarowanej jako przyjaciela klasy IntervalExtractor), aby uzyskać dostęp do testowanej metody.
/// @brief Reference to the primary data vector (e.g., measurements).
/// Defaults to reference to `empty` if not provided.
std::vector<float>& y = empty;
/// @brief Reference to the secondary data vector (e.g., time values).
/// Defaults to reference to `empty` if not provided.
std::vector<float>& t = empty;
std::vector<float> generatedT; ///< @brief Vector for generated time points when `t` is not provided.
/// @brief Vector of extracted intervals from data.
std::vector<Interval> intervals = {};
/// @brief Pointer to the energy calculation method based on user selection (during construction).
EnergyMethodFuncPtr calculateEnergy;
/// @brief Maximum acceptable time gap between consecutive data points.
/// Greater time gap is considered as data gathering failure and ends the interval.
float maxAcceptableTimeGap = 1.0;
/**
* @brief Finds the length and type of a monotonic run starting at a given index.
*
* This method detects a monotonic segment (increasing, decreasing, or steady) from the
* `y` vector, starting at `fromIndex`. It returns the length of the segment along with
* its monotonic type.
*
* @param fromIndex Index at which to start searching for a monotonic run (inclusive).
* @return A tuple consisting of:
* - the length of the monotonic run (at least 1),
* - the monotonicity type: `Interval::IntervalType::UP`, `Interval::IntervalType::DOWN`, or `Interval::IntervalType::STEADY`,
* - a boolean flag indicating if the interval is a gap based on time difference.
*/
std::tuple<int, Interval::IntervalType, bool> findMonotonicRun(int fromIndex) const;
/**
* @brief Creates and adds an interval to the `intervals` vector.
*
* This method calculates the interval based on the specified start index, segment length,
* interval type, and whether a gap exists. The interval is then added to the `intervals` vector.
*
* @param startIndex The starting index of the interval.
* @param length The length of the interval.
* @param intervalType The type of the interval (increasing, decreasing, or steady).
* @param isGap Flag indicating if the interval is a gap based on time difference.
*/
void createInterval(int startIndex, int length, Interval::IntervalType intervalType, bool isGap);
/**
* @brief Extracts intervals from the data with a threshold for steady intervals.
*
* This method iterates through the data to extract intervals based on absolute and relative thresholds,
* detecting changes that exceed the specified thresholds or are considered steady. It updates the `intervals`
* vector with the extracted intervals.
*
* @param absolute The absolute threshold for steady intervals.
* @param relative The relative threshold for steady intervals.
*/
void extractIntervalsWithThreshold(float absolute, float relative);
/**
* @brief Calculates the energy using the left rectangle method.
*
* This method computes the energy for a given segment using the left rectangle method,
* which approximates the integral by using the left endpoints of each subinterval.
*
* @param startIndex The starting index of the segment.
* @param length The length of the segment.
* @return The calculated energy for the segment.
*/
float leftRectangleMethod(int startIndex, int length) const;
/**
* @brief Calculates the energy using the right rectangle method.
*
* This method computes the energy for a given segment using the right rectangle method,
* which approximates the integral by using the right endpoints of each subinterval.
*
* @param startIndex The starting index of the segment.
* @param length The length of the segment.
* @return The calculated energy for the segment.
*/
float rightRectangleMethod(int startIndex, int length) const;
/**
* @brief Calculates the energy using the trapezoidal method.
*
* This method computes the energy for a given segment using the trapezoidal method,
* which approximates the integral by averaging the left and right endpoints of each subinterval.
*
* @param startIndex The starting index of the segment.
* @param length The length of the segment.
* @return The calculated energy for the segment.
*/
float trapezoidalMethod(int startIndex, int length) const;
#include "CppUnitTest.h"
#include "IntervalExtractor.h"
#include <vector>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
// Klasa pomocnicza do testowania prywatnych metod IntervalExtractor
class IntervalExtractorPrivateTests {
public:
static float TestTrapezoidalMethod(IntervalExtractor& extractor, int startIndex, int length) {
return extractor.trapezoidalMethod(startIndex, length);
}
};
namespace TrapezoidalMethodTests
{
TEST_CLASS(TrapezoidalMethodTests)
{
public:
// Test dla równomiernych odstępów czasowych
TEST_METHOD(TestUniformTimeIntervals)
{
// Arrange
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);
// Act
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Assert
// Oczekiwana wartość: 0.5 * [(2+3)*1 + (3+4)*1 + (4+5)*1] = 0.5 * 21 = 10.5
Assert::AreEqual(10.5f, energy, 0.001f);
}
// Test dla nierównomiernych odstępów czasowych
TEST_METHOD(TestNonUniformTimeIntervals)
{
// Arrange
std::vector<float> y = {2.0f, 3.0f, 4.0f, 5.0f};
std::vector<float> t = {0.0f, 1.0f, 3.0f, 6.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
// Act
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Assert
// Oczekiwana wartość: 0.5 * [(2+3)*1 + (3+4)*2 + (4+5)*3] = 0.5 * 46 = 23.0
Assert::AreEqual(23.0f, energy, 0.001f);
}
// Test dla pojedynczego przedziału
TEST_METHOD(TestSingleInterval)
{
// Arrange
std::vector<float> y = {2.0f, 3.0f};
std::vector<float> t = {0.0f, 1.0f};
IntervalExtractor extractor(y, IntervalExtractor::EnergyMethod::TRAPEZOIDAL_METHOD, t);
// Act
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 1);
// Assert
// Oczekiwana wartość: 0.5 * [(2+3)*1] = 2.5
Assert::AreEqual(2.5f, energy, 0.001f);
}
// Test dla ujemnych wartości
TEST_METHOD(TestNegativeValues)
{
// Arrange
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);
// Act
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Assert
// Oczekiwana wartość: 0.5 * [(-2+-3)*1 + (-3+-4)*1 + (-4+-5)*1] = 0.5 * -21 = -10.5
Assert::AreEqual(-10.5f, energy, 0.001f);
}
// Test dla zerowej długości
TEST_METHOD(TestZeroLength)
{
// Arrange
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);
// Act
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 0);
// Assert
// Oczekiwana wartość: 0 (brak przedziałów)
Assert::AreEqual(0.0f, energy, 0.001f);
}
// Test dla bardzo małych wartości
TEST_METHOD(TestSmallValues)
{
// Arrange
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);
// Act
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 2);
// Assert
float expected = 0.5f * ((0.00001f + 0.00002f) * 0.00001f + (0.00002f + 0.00003f) * 0.00001f);
Assert::AreEqual(expected, energy, 0.0000000001f);
}
// Test dla mieszanych wartości dodatnich i ujemnych
TEST_METHOD(TestMixedValues)
{
// Arrange
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);
// Act
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 3);
// Assert
// Oczekiwana wartość: 0.5 * [(-2+3)*1 + (3+-4)*1 + (-4+5)*1] = 0.5 * 1 = 0.5
Assert::AreEqual(0.5f, energy, 0.001f);
}
// Test dla dużych wartości
TEST_METHOD(TestLargeValues)
{
// Arrange
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);
// Act
float energy = IntervalExtractorPrivateTests::TestTrapezoidalMethod(extractor, 0, 2);
// Assert
// 0.5 * [(10000+20000)*1000 + (20000+30000)*1000] = 40000000
Assert::AreEqual(40000000.0f, energy, 0.1f);
}
};
}
Czy chciałbyś, abym wyjaśnił lub omówił któryś z tych testów?