utils.c

Overview

The utils.c file provides a collection of utility functions primarily for time measurement, colored console output initialization, JSON test data handling, and JSON comparison in a testing context. It facilitates precise time retrieval and formatting, reading JSON content from files, and comparing JSON strings while ignoring whitespace differences. The file also manages terminal color codes for console output, adapting to different operating systems (Windows and POSIX). These utilities are designed to support test execution and reporting.

Functions and Details

void utils_initialize(void)

Initializes console color codes for output formatting. On Windows, it enables virtual terminal processing to support ANSI escape codes. On other platforms, it simply assigns ANSI color codes.


long long utils_get_time(void)

Retrieves the current monotonic time in nanoseconds. This provides a high-resolution time useful for performance measurement.


void utils_print_time_diff(long long start_ns, long long end_ns)

Prints the difference between two timestamps in a formatted string representing hours, minutes, seconds, and milliseconds.


char *utils_get_test_json_data(const char *filename)

Reads the contents of a JSON file and returns it as a null-terminated string.


bool utils_test_json_equal(const char *a, const char *b)

Compares two JSON strings for equality ignoring whitespace differences.


void utils_output(const char *s)

Prints a string s enclosed in delimiter lines for clear separation in console output.


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram of Function Relationships

flowchart TD
A[utils_initialize] -->|sets| B[GREEN, RED, RESET]
C[utils_get_time] --> D[Returns nanoseconds timestamp]
E[utils_print_time_diff] --> C
E --> F[Formats time difference]
G[utils_get_test_json_data] --> H[Reads JSON file content]
I[utils_test_json_equal] --> J[Compares JSON ignoring whitespace]
I -->|on mismatch| K[Prints context]
L[utils_output] --> M[Prints string with delimiters]

This diagram illustrates the main functions in utils.c and their primary functionality or flow relationships, providing a clear overview of the utility operations within this file.