/
Zamar_Terrier
/
TigorEngine
Обзор
Документация
Войти
/
Zamar_Terrier
/
TigorEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
tests/json_parser.c
262 строки
9 KB
Zamar_Terrier
Запрос на слияние 'feature-12032026' (
#9
) из feature-12032026 в master
08 июн 2026, 11:31
Верифицирован
08 июн 2026, 11:31
2200f38
Код
Авторство
О чём код?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "Tools/json_parser.h" int test_count = 0; int pass_count = 0; void test_assert(int condition, const char* message) { test_count++; if (condition) { pass_count++; printf("[PASS] %s\n", message); } else { printf("[FAIL] %s\n", message); } } void test_parse_null() { json_value_t* value = json_parse("null"); test_assert(value != NULL && value->type == JSON_NULL, "Parse null value"); if (value) json_free(value); } void test_parse_boolean() { json_value_t* true_val = json_parse("true"); test_assert(true_val != NULL && true_val->type == JSON_BOOL && true_val->data.boolean == 1, "Parse true"); if (true_val) json_free(true_val); json_value_t* false_val = json_parse("false"); test_assert(false_val != NULL && false_val->type == JSON_BOOL && false_val->data.boolean == 0, "Parse false"); if (false_val) json_free(false_val); } void test_parse_number() { json_value_t* num_val = json_parse("123.45"); test_assert(num_val != NULL && num_val->type == JSON_NUMBER && num_val->data.number == 123.45, "Parse number"); if (num_val) json_free(num_val); } void test_parse_string() { json_value_t* str_val = json_parse("\"Hello, World!\""); test_assert(str_val != NULL && str_val->type == JSON_STRING && strcmp(str_val->data.string, "Hello, World!") == 0, "Parse string"); if (str_val) json_free(str_val); } void test_parse_array() { json_value_t* arr_val = json_parse("[1, 2, 3]"); test_assert(arr_val != NULL && arr_val->type == JSON_ARRAY, "Parse array"); if (arr_val) { test_assert(json_array_size(arr_val) == 3, "Array size"); test_assert(json_array_get(arr_val, 0)->type == JSON_NUMBER && json_array_get(arr_val, 0)->data.number == 1, "Array element 0"); test_assert(json_array_get(arr_val, 1)->type == JSON_NUMBER && json_array_get(arr_val, 1)->data.number == 2, "Array element 1"); test_assert(json_array_get(arr_val, 2)->type == JSON_NUMBER && json_array_get(arr_val, 2)->data.number == 3, "Array element 2"); json_free(arr_val); } } void test_parse_object() { json_value_t* obj_val = json_parse("{\"name\": \"John\", \"age\": 30}"); test_assert(obj_val != NULL && obj_val->type == JSON_OBJECT, "Parse object"); char buff[256] = { 0 }; if (obj_val) { json_value_t* name_val = json_object_get(obj_val, "name"); test_assert(name_val != NULL && name_val->type == JSON_STRING && strcmp(name_val->data.string, "John") == 0, "Object property 'name'"); sprintf(buff, "Value is %s\n", name_val->data.string); printf(buff); json_value_t* age_val = json_object_get(obj_val, "age"); test_assert(age_val != NULL && age_val->type == JSON_NUMBER && age_val->data.number == 30, "Object property 'age'"); sprintf(buff, "Value is %f\n", age_val->data.number); printf(buff); json_free(obj_val); } } void printf_json(json_value_t* root){ if(root->type == JSON_NULL){ printf("Value : null\n"); }else if(root->type == JSON_BOOL){ printf("Value : %s\n", root->data.boolean ? "true" : "false"); }else if(root->type == JSON_NUMBER){ printf("Value : %f\n", root->data.number); }else if(root->type == JSON_STRING){ printf("Value : %s\n", root->data.string); }else if(root->type == JSON_ARRAY){ for_each_json_array(item, i, root->data.array){ printf_json(item->value); } }else if(root->type == JSON_OBJECT){ for_each_json_object(item, root){ printf("Item name : %s\n", item->key); switch(item->value->type){ case JSON_NULL: printf("Value : null\n"); break; case JSON_BOOL: printf("Value : %s\n", item->value->data.boolean ? "true" : "false"); break; case JSON_NUMBER: printf("Value : %f\n", item->value->data.number); break; case JSON_STRING: printf("Value : %s\n", item->value->data.string); break; case JSON_ARRAY: for_each_json_array(item2, i, item->value->data.array){ printf_json(item2->value); } printf("It's array!\n"); break; case JSON_OBJECT: printf_json(item->value); printf("It's object!\n"); break; } } } } void test_nested_structures() { const char* json = "{" "\"person\": {" " \"name\": \"John\"," " \"address\": {" " \"city\": \"New York\"," " \"zip\": \"10001\"" " }," " \"hobbies\": [\"reading\", \"swimming\", \"coding\"]" "}" "}"; json_value_t* root = json_parse(json); test_assert(root != NULL, "Parse nested structures"); if (root) { printf_json(root); json_value_t* person = json_object_get(root, "person"); test_assert(person != NULL && person->type == JSON_OBJECT, "Get person object"); if (person) { json_value_t* name = json_object_get(person, "name"); test_assert(name != NULL && name->type == JSON_STRING && strcmp(name->data.string, "John") == 0, "Person name"); json_value_t* address = json_object_get(person, "address"); test_assert(address != NULL && address->type == JSON_OBJECT, "Get address object"); if (address) { json_value_t* city = json_object_get(address, "city"); test_assert(city != NULL && city->type == JSON_STRING && strcmp(city->data.string, "New York") == 0, "Address city"); } json_value_t* hobbies = json_object_get(person, "hobbies"); test_assert(hobbies != NULL && hobbies->type == JSON_ARRAY, "Get hobbies array"); if (hobbies) { test_assert(json_array_size(hobbies) == 3, "Hobbies array size"); test_assert(json_array_get(hobbies, 0)->type == JSON_STRING && strcmp(json_array_get(hobbies, 0)->data.string, "reading") == 0, "Hobby 0"); } } json_free(root); } } void test_stringify() { json_value_t* obj = json_object(); json_object_set(obj, "name", json_string("John")); json_object_set(obj, "age", json_number(30)); json_value_t* hobbies = json_array(); json_array_add(hobbies, json_string("reading")); json_array_add(hobbies, json_string("swimming")); json_array_add(hobbies, json_string("coding")); json_object_set(obj, "hobbies", hobbies); char* json_str = json_stringify(obj); test_assert(json_str != NULL, "Stringify object"); if (json_str) { printf("Generated JSON: %s\n", json_str); // Простая проверка наличия ключевых элементов test_assert(strstr(json_str, "\"name\"") != NULL, "Stringify contains name"); test_assert(strstr(json_str, "\"John\"") != NULL, "Stringify contains John"); test_assert(strstr(json_str, "\"age\"") != NULL, "Stringify contains age"); test_assert(strstr(json_str, "30") != NULL, "Stringify contains 30"); test_assert(strstr(json_str, "\"hobbies\"") != NULL, "Stringify contains hobbies"); test_assert(strstr(json_str, "\"reading\"") != NULL, "Stringify contains reading"); free(json_str); } json_free(obj); } void test_modify_object() { json_value_t* obj = json_object(); // Добавляем свойства json_object_set(obj, "initial", json_number(1)); json_object_set(obj, "temp", json_string("temporary")); // Меняем значение json_object_set(obj, "temp", json_string("updated")); // Проверяем json_value_t* temp = json_object_get(obj, "temp"); test_assert(temp != NULL && temp->type == JSON_STRING && strcmp(temp->data.string, "updated") == 0, "Modify object property"); // Добавляем новое свойство json_object_set(obj, "new", json_boolean(1)); json_value_t* new_val = json_object_get(obj, "new"); test_assert(new_val != NULL && new_val->type == JSON_BOOL && new_val->data.boolean == 1, "Add new property"); json_free(obj); } int main() { printf("JSON Parser Tests\n"); printf("=================\n"); test_parse_null(); test_parse_boolean(); test_parse_number(); test_parse_string(); test_parse_array(); test_parse_object(); test_nested_structures(); test_stringify(); test_modify_object(); printf("\nTest Results: %d/%d tests passed\n", pass_count, test_count); return (test_count == pass_count) ? 0 : 1; }