开始设计

This commit is contained in:
2026-08-10 01:21:15 +08:00
commit e45398991f
228 changed files with 20827 additions and 0 deletions
+132
View File
@@ -0,0 +1,132 @@
#include "c_Stopwatch.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 void c_Stopwatch_GetTimePoint(c_TimePoint_t* tp);
/**
* Mocks active OS-level processing delays using portable nanosleep/Sleep interfaces.
*/
static void c_Framework_MockExecutionDelayMs(int ms) {
#if defined(_WIN32) || defined(_WIN64)
Sleep(ms);
#else
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000L;
nanosleep(&ts, NULL);
#endif
}
c_bool_t test_stopwatch_compute_diff_in_ms(void) {
c_TimePoint_t t_start, t_end;
// Test Case 1: Param Parameter Enforcement Boundary Checks
EXPECT_NEAR(c_Stopwatch_ComputeDiffInMS(NULL, &t_end), -1.0, 1e-6, "NULL start point guard missed");
EXPECT_NEAR(c_Stopwatch_ComputeDiffInMS(&t_start, NULL), -1.0, 1e-6, "NULL end point guard missed");
// Test Case 2: Standard Interval Difference Evaluation
printf(" [LOG] Capturing timestamp benchmarks across a 60ms thread stall scenario...\n");
c_Stopwatch_GetTimePoint(&t_start);
c_Framework_MockExecutionDelayMs(60);
c_Stopwatch_GetTimePoint(&t_end);
double delta_ms = c_Stopwatch_ComputeDiffInMS(&t_start, &t_end);
// Allow a flexible tolerance boundary for generic OS context switching variances
EXPECT_EQ(delta_ms >= 55.0, C_TRUE, "Computed millisecond difference fell short of target delay thresholds");
printf(" [STAT] Clock cycle delta computed: %f ms\n", delta_ms);
// Test Case 3: Identity Time Check (Difference between identical points must equal 0.0)
EXPECT_NEAR(c_Stopwatch_ComputeDiffInMS(&t_start, &t_start), 0.0, 1e-6, "Identity point calculation returned non-zero value");
return C_TRUE;
}
c_bool_t test_stopwatch_lifecycle(void) {
c_Stopwatch_t sw;
// Test Case 1: Param Checking Guards & Initial Conditions
EXPECT_EQ(c_Stopwatch_Init(NULL), C_ERR_PARAM, "NULL stopwatch handler guard missed");
EXPECT_EQ(c_Stopwatch_Init(&sw), C_ERR_OK, "Stopwatch init initialization failed");
EXPECT_NEAR(c_Stopwatch_GetElapsedSeconds(&sw), 0.0, 1e-6, "Freshly initialized stopwatch reported non-zero runtime");
// Test Case 2: Standard Interval Measurement Tracking Check
printf(" [LOG] Launching Stopwatch profile tracking (100ms thread stall scenario)...\n");
EXPECT_EQ(c_Stopwatch_Start(&sw), C_ERR_OK, "Stopwatch engine start pass failed");
c_Framework_MockExecutionDelayMs(100);
// Check lap peeking capabilities while running
double lap_peek_ms = c_Stopwatch_GetElapsedMilliseconds(&sw);
EXPECT_EQ(lap_peek_ms >= 90.0, C_TRUE, "Lap peeking reported impossibly low runtime under active intervals");
EXPECT_EQ(c_Stopwatch_Stop(&sw), C_ERR_OK, "Stopwatch engine stop pass failed");
EXPECT_EQ(sw.is_running, C_FALSE, "Stop execution failed to flag active timeline states offline");
double final_seconds = c_Stopwatch_GetElapsedSeconds(&sw);
// Allow a wide tolerance for generic OS process scheduling variances
EXPECT_NEAR(final_seconds >= 0.09, C_TRUE, 0.0, "Measured timing fell short of target delay thresholds");
// Test Case 3: Accumulated Timing Checks (Resume feature validation)
printf(" [LOG] Resuming stopwatch profile tracking (Additional 50ms delay path)...\n");
EXPECT_EQ(c_Stopwatch_Start(&sw), C_ERR_OK, "Stopwatch engine resume phase failed");
c_Framework_MockExecutionDelayMs(50);
EXPECT_EQ(c_Stopwatch_Stop(&sw), C_ERR_OK, "Stopwatch subsequent interval termination failed");
double cumulative_ms = c_Stopwatch_GetElapsedMilliseconds(&sw);
EXPECT_EQ(cumulative_ms >= 140.0, C_TRUE, "Stopwatch failed to accumulate consecutive interval data layers");
// Test Case 4: Soft Clear Verification
EXPECT_EQ(c_Stopwatch_Clear(&sw), C_ERR_OK, "Stopwatch clear execution crashed");
EXPECT_NEAR(c_Stopwatch_GetElapsedSeconds(&sw), 0.0, 1e-6, "Clear protocol left residual timing tracking metrics inside buffer");
return C_TRUE;
}
int main(void) {
printf("=== Starting Framework Verification: c_Stopwatch_ComputeDiffInMS ===\n");
if (test_stopwatch_compute_diff_in_ms()) {
printf(" [PASS] Standalone Monotonic Delta Millisecond Compute Engine Verified Successfully.\n");
} else {
printf(" [FAIL] Delta Resolution Pipeline Processing Mismatches Detected.\n");
}
printf("=== Starting Framework Verification: c_Stopwatch ===\n");
if (test_stopwatch_lifecycle()) {
printf(" [PASS] High-Precision Monotonic Stopwatch Lifecycle Pipelines Verified Successfully.\n");
} else {
printf(" [FAIL] Stopwatch Engine Processing or State Tracking Mismatches Intercepted.\n");
}
return 0;
}