72 lines
3.6 KiB
C
72 lines
3.6 KiB
C
#include "c_LinearRegression.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
// Your updated line tracing diagnostic macro
|
|
#define EXPECT_EQ(actual, expected, msg) \
|
|
do { \
|
|
if ((actual) != (expected)) { \
|
|
printf(" [X] Assert Failed: %s (Expected %d, got %d) %s:%d\n", msg, (int)(expected), (int)(actual), __FILE__, __LINE__); \
|
|
return C_FALSE; \
|
|
} \
|
|
} while(0)
|
|
|
|
// Helper macro for double comparisons with floating-point tolerance
|
|
#define EXPECT_NEAR(actual, expected, tolerance, msg) \
|
|
do { \
|
|
if (fabs((actual) - (expected)) > (tolerance)) { \
|
|
printf(" [X] Assert Failed: %s (Expected %f, got %f) %s:%d\n", msg, (double)(expected), (double)(actual), __FILE__, __LINE__); \
|
|
return C_FALSE; \
|
|
} \
|
|
} while(0)
|
|
|
|
c_bool_t test_linear_regression_ops(void) {
|
|
c_LinearRegression_t model;
|
|
|
|
// Test Case 1: Param Parameter Enforcement Boundary Checks
|
|
EXPECT_EQ(c_LinearRegression_Init(NULL), C_ERR_PARAM, "NULL model instance initializer guard missed");
|
|
EXPECT_EQ(c_LinearRegression_Init(&model), C_ERR_OK, "Model context initialization failed");
|
|
EXPECT_EQ(model.is_trained, C_FALSE, "Untrained model reported trained status flags on entry");
|
|
|
|
double pred_buffer = 0.0;
|
|
EXPECT_EQ(c_LinearRegression_Predict(&model, 5.0, &pred_buffer), C_ERR_INVALID, "Untrained model permitted prediction inference runs");
|
|
|
|
// Prepare an un-ordered linear dataset mapping the pure mathematical trend line: y = 2.0 * x + 5.0
|
|
double train_x[] = { 1.0, 2.0, 4.0, 5.0, 3.0 };
|
|
double train_y[] = { 7.0, 9.0, 13.0, 15.0, 11.0 };
|
|
c_size_t samples = sizeof(train_x) / sizeof(train_x[0]);
|
|
|
|
// Test Case 2: Core Model Fitting (Ordinary Least Squares verification)
|
|
printf(" [LOG] Training Ordinary Least Squares Linear Regression Model...\n");
|
|
EXPECT_EQ(c_LinearRegression_Fit(&model, train_x, train_y, samples), C_ERR_OK, "Model training fit routine failed");
|
|
EXPECT_EQ(model.is_trained, C_TRUE, "Successful fit sequence missed toggling active trained flag status");
|
|
|
|
// Assert derived trend coefficients map exactly to Slope (Beta) = 2.0, Intercept (Alpha) = 5.0
|
|
EXPECT_NEAR(model.slope, 2.0, 1e-6, "Derived model slope (Beta) coefficient mathematically incorrect");
|
|
EXPECT_NEAR(model.intercept, 5.0, 1e-6, "Derived model y-intercept (Alpha) coefficient mathematically incorrect");
|
|
|
|
// Test Case 3: Inference Prediction Checking
|
|
// Predict value for x = 10.0 -> y = 5.0 + 2.0 * 10.0 = 25.0
|
|
EXPECT_EQ(c_LinearRegression_Predict(&model, 10.0, &pred_buffer), C_ERR_OK, "Prediction inference run crashed");
|
|
EXPECT_NEAR(pred_buffer, 25.0, 1e-6, "Model inference lookup yielded inaccurate coordinate value");
|
|
printf(" [STAT] Model Trained. Equation: y = %.2f + %.2fx | Prediction(x=10): %.2f\n", model.intercept, model.slope, pred_buffer);
|
|
|
|
// Test Case 4: Division-by-Zero Vertical Alignment Mathematical Anomaly Check
|
|
double vertical_x[] = { 3.0, 3.0, 3.0 };
|
|
double vertical_y[] = { 1.0, 5.0, 9.0 };
|
|
EXPECT_EQ(c_LinearRegression_Fit(&model, vertical_x, vertical_y, 3), C_ERR_INVALID, "Vertical line infinite slope anomaly bypassed zero variance filter");
|
|
EXPECT_EQ(model.is_trained, C_FALSE, "Failed fit sequence left model registered in an active trained state");
|
|
|
|
return C_TRUE;
|
|
}
|
|
|
|
int main(void) {
|
|
printf("=== Starting Framework Verification: c_LinearRegression ===\n");
|
|
if (test_linear_regression_ops()) {
|
|
printf(" [PASS] Ordinary Least Squares Linear Regression Matrix Pipelines Verified.\n");
|
|
} else {
|
|
printf(" [FAIL] Mathematical Linear Modeling Processing Anomaly Intercepted.\n");
|
|
}
|
|
return 0;
|
|
}
|