Files

84 lines
3.3 KiB
C
Raw Permalink Normal View History

2026-08-10 01:21:15 +08:00
#include "c_Vector.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.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)
// External references to previous core modules
extern c_err_t c_Vector_Init(c_Vector_t* vec, c_size_t dimensions);
extern void c_Vector_Destroy(c_Vector_t* vec);
c_bool_t test_vector_distance_out_param(void) {
c_Vector_t v1, v2;
c_size_t dims = 3; // Test standard 3D spatial space
double dist_res = 0.0;
EXPECT_EQ(c_Vector_Init(&v1, dims), C_ERR_OK, "v1 initialization failed");
EXPECT_EQ(c_Vector_Init(&v2, dims), C_ERR_OK, "v2 initialization failed");
// Load components: v1 = [7.0, 4.0, 3.0]
v1.components[0] = 7.0;
v1.components[1] = 4.0;
v1.components[2] = 3.0;
// Load components: v2 = [17.0, 12.0, 5.0]
v2.components[0] = 17.0;
v2.components[1] = 12.0;
v2.components[2] = 5.0;
// Test Case 1: Param Parameter Enforcement Boundary Checks
EXPECT_EQ(c_Vector_DistanceTo(NULL, &v2, &dist_res), C_ERR_PARAM, "NULL pointer parameter guard missed");
EXPECT_EQ(c_Vector_DistanceTo(&v1, &v2, NULL), C_ERR_PARAM, "NULL output variable pointer guard missed");
// Test Case 2: Standard Spatial Vector Distance Evaluation
// Variance = (17-7)^2 + (12-4)^2 + (5-3)^2 = 10^2 + 8^2 + 2^2 = 168.
// Distance = sqrt(168) ≈ 12.961481
EXPECT_EQ(c_Vector_DistanceTo(&v1, &v2, &dist_res), C_ERR_OK, "Valid distance mapping computation failed");
EXPECT_NEAR(dist_res, sqrt(168.0), 1e-6, "Euclidean space calculation returned mathematically inaccurate value");
// Test Case 3: Identity Distance Verification (d(v, v) == 0.0)
EXPECT_EQ(c_Vector_DistanceTo(&v1, &v1, &dist_res), C_ERR_OK, "Identity calculation crashed unexpectedly");
EXPECT_NEAR(dist_res, 0.0, 1e-6, "Identity space computation returned non-zero value");
// Test Case 4: Mismatched Dimension Vector Spaces Guard Error Check
c_Vector_t v_bad;
c_Vector_Init(&v_bad, 6); // 6D vector
EXPECT_EQ(c_Vector_DistanceTo(&v1, &v_bad, &dist_res), C_ERR_FAIL, "Dimension mismatch fallback channel skipped filter");
c_Vector_Destroy(&v1);
c_Vector_Destroy(&v2);
c_Vector_Destroy(&v_bad);
return C_TRUE;
}
int main(void) {
printf("=== Starting Framework Verification: c_Vector_DistanceTo (Out Param) ===\n");
if (test_vector_distance_out_param()) {
printf(" [PASS] Euclidean Out-Param Distance Resolution Engine Verified Successfully.\n");
} else {
printf(" [FAIL] Distance Matrix Pipeline Processing Mismatches Detected.\n");
}
return 0;
}