Add deterministic QueryPerformanceCounter/Frequency mocks
Implemented Windows high-resolution timing mocks for deterministic simulation: **mock_QueryPerformanceCounter:** - Returns deterministic counter based on simulated_time - Uses fixed 10 MHz frequency (10,000,000 counts/second) - Calculation: count = (tv_sec * freq) + (tv_nsec * freq / 1e9) - Validates NULL pointer with ERROR_INVALID_PARAMETER - Provides consistent, reproducible timing across test runs **mock_QueryPerformanceFrequency:** - Returns fixed frequency of 10 MHz (10,000,000 Hz) - Common frequency on modern Windows systems - Ensures deterministic behavior (no variation between runs) - Validates NULL pointer with ERROR_INVALID_PARAMETER **Integration:** - Added function declarations to system.h - Macros already existed (sys_QueryPerformanceCounter/Frequency) - Complements existing mock_clock_gettime on Linux - Both platforms now have deterministic high-resolution timing This ensures Windows code using QueryPerformanceCounter for timing, benchmarking, or rate limiting behaves deterministically in tests. The fixed 10 MHz frequency provides nanosecond-level precision (100ns per tick) suitable for most timing needs.
This commit is contained in:
@@ -1183,6 +1183,37 @@ int mock__mkdir(char *path)
|
|||||||
return _mkdir(path);
|
return _mkdir(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOL mock_QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
|
||||||
|
{
|
||||||
|
if (lpPerformanceCount == NULL) {
|
||||||
|
SetLastError(ERROR_INVALID_PARAMETER);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use simulated time to generate deterministic performance counter
|
||||||
|
// Frequency is 10 MHz (10,000,000 counts per second)
|
||||||
|
const LONGLONG frequency = 10000000LL;
|
||||||
|
|
||||||
|
LONGLONG count = (LONGLONG)simulated_time.tv_sec * frequency;
|
||||||
|
count += ((LONGLONG)simulated_time.tv_nsec * frequency) / 1000000000LL;
|
||||||
|
|
||||||
|
lpPerformanceCount->QuadPart = count;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL mock_QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
|
||||||
|
{
|
||||||
|
if (lpFrequency == NULL) {
|
||||||
|
SetLastError(ERROR_INVALID_PARAMETER);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return fixed frequency of 10 MHz for deterministic behavior
|
||||||
|
// This is a common frequency on modern systems
|
||||||
|
lpFrequency->QuadPart = 10000000LL; // 10 million counts per second
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
int mock_clock_gettime(clockid_t clockid, struct timespec *tp)
|
int mock_clock_gettime(clockid_t clockid, struct timespec *tp)
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ BOOL mock_FlushFileBuffers(HANDLE handle);
|
|||||||
BOOL mock_ReadFile(HANDLE handle, char *dst, DWORD len, DWORD *num, OVERLAPPED *ov);
|
BOOL mock_ReadFile(HANDLE handle, char *dst, DWORD len, DWORD *num, OVERLAPPED *ov);
|
||||||
BOOL mock_WriteFile(HANDLE handle, char *src, DWORD len, DWORD *num, OVERLAPPED *ov);
|
BOOL mock_WriteFile(HANDLE handle, char *src, DWORD len, DWORD *num, OVERLAPPED *ov);
|
||||||
BOOL mock_GetFileSizeEx(HANDLE handle, LARGE_INTEGER *buf);
|
BOOL mock_GetFileSizeEx(HANDLE handle, LARGE_INTEGER *buf);
|
||||||
|
BOOL mock_QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);
|
||||||
|
BOOL mock_QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
|
||||||
char* mock__fullpath(char *path, char *dst, int cap);
|
char* mock__fullpath(char *path, char *dst, int cap);
|
||||||
int mock__mkdir(char *path);
|
int mock__mkdir(char *path);
|
||||||
#else
|
#else
|
||||||
|
|||||||
Reference in New Issue
Block a user