test_memory.c

Overview

The test_memory.c file contains unit test code that focuses on verifying the correctness and memory management of JSON parsing and serialization functionality. It ensures that JSON data can be parsed into an internal representation, re-serialized back to a string, and compared for structural equality without memory leaks or errors. The test validates the integrity and lifecycle of json_value objects, which represent parsed JSON structures, and confirms that memory is correctly allocated and freed.

This file primarily interacts with JSON parsing and stringifying functions, memory management utilities, and testing framework macros that handle assertions and test lifecycle management.


Detailed Explanation

Included Headers


Function: test_memory_leaks

TEST(test_memory_leaks, char *json)

Function: main

int main(void)

Important Implementation Details


Interaction with Other Components


Mermaid Diagram: Function Workflow in test_memory.c

flowchart TD
A["main()"] --> B[TEST_INITIALIZE]
B --> C["TEST_SUITE("unit tests")"]
C --> D["test_memory_leaks()"]
D --> E[Initialize json_value v]
E --> F["json_parse(source, &v)"]
F --> G{Is &v not NULL?}
G -- Yes --> H["json_stringify(&v) -> out"]
H --> I{Is out not NULL?}
I -- Yes --> J["json_stringify(&v) -> json"]
J --> K{Is json not NULL?}
K -- Yes --> L["json_free(&v)"]
L --> M["utils_test_json_equal(json, source)"]
M --> N["utils_output(json)"]
N --> O["json_free(&v)"]
O --> P["free(json)"]
P --> Q[END_TEST]
Q --> R[TEST_FINALIZE]
R --> S[Return 0]

This flowchart depicts the sequential execution order of the main function and the test function, highlighting key operations such as parsing, serialization, assertions, memory freeing, and test lifecycle management.