19750e409Sopenharmony_ci/*
29750e409Sopenharmony_ci  Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
39750e409Sopenharmony_ci
49750e409Sopenharmony_ci  Permission is hereby granted, free of charge, to any person obtaining a copy
59750e409Sopenharmony_ci  of this software and associated documentation files (the "Software"), to deal
69750e409Sopenharmony_ci  in the Software without restriction, including without limitation the rights
79750e409Sopenharmony_ci  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
89750e409Sopenharmony_ci  copies of the Software, and to permit persons to whom the Software is
99750e409Sopenharmony_ci  furnished to do so, subject to the following conditions:
109750e409Sopenharmony_ci
119750e409Sopenharmony_ci  The above copyright notice and this permission notice shall be included in
129750e409Sopenharmony_ci  all copies or substantial portions of the Software.
139750e409Sopenharmony_ci
149750e409Sopenharmony_ci  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
159750e409Sopenharmony_ci  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
169750e409Sopenharmony_ci  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
179750e409Sopenharmony_ci  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
189750e409Sopenharmony_ci  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
199750e409Sopenharmony_ci  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
209750e409Sopenharmony_ci  THE SOFTWARE.
219750e409Sopenharmony_ci*/
229750e409Sopenharmony_ci
239750e409Sopenharmony_ci#include <stdio.h>
249750e409Sopenharmony_ci#include <stdlib.h>
259750e409Sopenharmony_ci#include <string.h>
269750e409Sopenharmony_ci
279750e409Sopenharmony_ci#include "unity/examples/unity_config.h"
289750e409Sopenharmony_ci#include "unity/src/unity.h"
299750e409Sopenharmony_ci#include "common.h"
309750e409Sopenharmony_ci
319750e409Sopenharmony_cistatic void assert_is_int64(cJSON *int64_number_item)
329750e409Sopenharmony_ci{
339750e409Sopenharmony_ci    assert_has_type(int64_number_item, cJSON_Number);
349750e409Sopenharmony_ci    TEST_ASSERT_BITS_MESSAGE(cJSON_IsInt64, cJSON_IsInt64, int64_number_item->type, "Item should be a int64 integer.");
359750e409Sopenharmony_ci}
369750e409Sopenharmony_ci
379750e409Sopenharmony_cistatic void cjson_get_object_item_should_get_object_items_with_int64(void)
389750e409Sopenharmony_ci{
399750e409Sopenharmony_ci    cJSON *item = NULL;
409750e409Sopenharmony_ci    cJSON *found = NULL;
419750e409Sopenharmony_ci
429750e409Sopenharmony_ci    item = cJSON_Parse("{\"one\":1, \"Two\":2, \"tHree\":3}");
439750e409Sopenharmony_ci
449750e409Sopenharmony_ci    found = cJSON_GetObjectItem(NULL, "test");
459750e409Sopenharmony_ci    TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL pointer.");
469750e409Sopenharmony_ci
479750e409Sopenharmony_ci    found = cJSON_GetObjectItem(item, NULL);
489750e409Sopenharmony_ci    TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
499750e409Sopenharmony_ci
509750e409Sopenharmony_ci    found = cJSON_GetObjectItem(item, "one");
519750e409Sopenharmony_ci    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
529750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
539750e409Sopenharmony_ci    assert_is_int64(found);
549750e409Sopenharmony_ci
559750e409Sopenharmony_ci    found = cJSON_GetObjectItem(item, "tWo");
569750e409Sopenharmony_ci    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
579750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 2);
589750e409Sopenharmony_ci    assert_is_int64(found);
599750e409Sopenharmony_ci
609750e409Sopenharmony_ci    found = cJSON_GetObjectItem(item, "three");
619750e409Sopenharmony_ci    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find item.");
629750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 3);
639750e409Sopenharmony_ci    assert_is_int64(found);
649750e409Sopenharmony_ci
659750e409Sopenharmony_ci    found = cJSON_GetObjectItem(item, "four");
669750e409Sopenharmony_ci    TEST_ASSERT_NULL_MESSAGE(found, "Should not find something that isn't there.");
679750e409Sopenharmony_ci
689750e409Sopenharmony_ci    cJSON_Delete(item);
699750e409Sopenharmony_ci}
709750e409Sopenharmony_ci
719750e409Sopenharmony_cistatic void cjson_get_object_item_case_sensitive_should_get_object_items_with_int64(void)
729750e409Sopenharmony_ci{
739750e409Sopenharmony_ci    cJSON *item = NULL;
749750e409Sopenharmony_ci    cJSON *found = NULL;
759750e409Sopenharmony_ci
769750e409Sopenharmony_ci    item = cJSON_Parse("{\"one\":1, \"Two\":2, \"tHree\":3}");
779750e409Sopenharmony_ci
789750e409Sopenharmony_ci    found = cJSON_GetObjectItemCaseSensitive(NULL, "test");
799750e409Sopenharmony_ci    TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL pointer.");
809750e409Sopenharmony_ci
819750e409Sopenharmony_ci    found = cJSON_GetObjectItemCaseSensitive(item, NULL);
829750e409Sopenharmony_ci    TEST_ASSERT_NULL_MESSAGE(found, "Failed to fail on NULL string.");
839750e409Sopenharmony_ci
849750e409Sopenharmony_ci    found = cJSON_GetObjectItemCaseSensitive(item, "one");
859750e409Sopenharmony_ci    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
869750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 1);
879750e409Sopenharmony_ci    assert_is_int64(found);
889750e409Sopenharmony_ci
899750e409Sopenharmony_ci    found = cJSON_GetObjectItemCaseSensitive(item, "Two");
909750e409Sopenharmony_ci    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find first item.");
919750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 2);
929750e409Sopenharmony_ci    assert_is_int64(found);
939750e409Sopenharmony_ci
949750e409Sopenharmony_ci    found = cJSON_GetObjectItemCaseSensitive(item, "tHree");
959750e409Sopenharmony_ci    TEST_ASSERT_NOT_NULL_MESSAGE(found, "Failed to find item.");
969750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE(found->valuedouble, 3);
979750e409Sopenharmony_ci    assert_is_int64(found);
989750e409Sopenharmony_ci
999750e409Sopenharmony_ci    found = cJSON_GetObjectItemCaseSensitive(item, "One");
1009750e409Sopenharmony_ci    TEST_ASSERT_NULL_MESSAGE(found, "Should not find something that isn't there.");
1019750e409Sopenharmony_ci
1029750e409Sopenharmony_ci    cJSON_Delete(item);
1039750e409Sopenharmony_ci}
1049750e409Sopenharmony_ci
1059750e409Sopenharmony_cistatic void cjson_set_number_value_should_set_numbers_with_int64(void)
1069750e409Sopenharmony_ci{
1079750e409Sopenharmony_ci    cJSON number[1] = {{NULL, NULL, NULL, cJSON_Number | cJSON_IsInt64, NULL, 0, 0, NULL}};
1089750e409Sopenharmony_ci
1099750e409Sopenharmony_ci    cJSON_SetInt64NumberValue(number, 1LL);
1109750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_INT64(1LL, number->valueint);
1119750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE(1.0, number->valuedouble);
1129750e409Sopenharmony_ci
1139750e409Sopenharmony_ci    cJSON_SetInt64NumberValue(number, -1LL);
1149750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_INT64(-1LL, number->valueint);
1159750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE(-1.0, number->valuedouble);
1169750e409Sopenharmony_ci
1179750e409Sopenharmony_ci    cJSON_SetInt64NumberValue(number, 0LL);
1189750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_INT64(0LL, number->valueint);
1199750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE(0.0, number->valuedouble);
1209750e409Sopenharmony_ci
1219750e409Sopenharmony_ci    cJSON_SetInt64NumberValue(number, LLONG_MAX);
1229750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_INT64(LLONG_MAX, number->valueint);
1239750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE((double)LLONG_MAX, number->valuedouble);
1249750e409Sopenharmony_ci
1259750e409Sopenharmony_ci    cJSON_SetInt64NumberValue(number, LLONG_MIN);
1269750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_INT64(LLONG_MIN, number->valueint);
1279750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_DOUBLE((double)LLONG_MIN, number->valuedouble);
1289750e409Sopenharmony_ci}
1299750e409Sopenharmony_ci
1309750e409Sopenharmony_cistatic void typecheck_functions_should_check_type_with_int64(void)
1319750e409Sopenharmony_ci{
1329750e409Sopenharmony_ci    cJSON item[1];
1339750e409Sopenharmony_ci    item->type = cJSON_Number;
1349750e409Sopenharmony_ci    TEST_ASSERT_FALSE(cJSON_IsInt64Number(item));
1359750e409Sopenharmony_ci
1369750e409Sopenharmony_ci    item->type = cJSON_IsInt64;
1379750e409Sopenharmony_ci    TEST_ASSERT_FALSE(cJSON_IsInt64Number(item));
1389750e409Sopenharmony_ci
1399750e409Sopenharmony_ci    item->type = cJSON_Number | cJSON_IsInt64;
1409750e409Sopenharmony_ci    TEST_ASSERT_TRUE(cJSON_IsInt64Number(item));
1419750e409Sopenharmony_ci
1429750e409Sopenharmony_ci    item->type = cJSON_False;
1439750e409Sopenharmony_ci    TEST_ASSERT_FALSE(cJSON_IsInt64Number(item));
1449750e409Sopenharmony_ci}
1459750e409Sopenharmony_ci
1469750e409Sopenharmony_cistatic void cjson_functions_should_not_crash_with_null_pointers_with_int64(void)
1479750e409Sopenharmony_ci{
1489750e409Sopenharmony_ci    cJSON *item = cJSON_CreateString("item");
1499750e409Sopenharmony_ci
1509750e409Sopenharmony_ci    TEST_ASSERT_FALSE(cJSON_IsInt64Number(NULL));
1519750e409Sopenharmony_ci    cJSON_AddInt64NumberToObject(NULL, "item", 0LL);
1529750e409Sopenharmony_ci    cJSON_AddInt64NumberToObject(item, NULL, 0LL);
1539750e409Sopenharmony_ci    cJSON_AddInt64NumberToObject(NULL, NULL, 0LL);
1549750e409Sopenharmony_ci    TEST_ASSERT_NULL(cJSON_GetInt64NumberValue(NULL));
1559750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_INT64(0LL, cJSON_SetInt64NumberValue(NULL, 0LL));
1569750e409Sopenharmony_ci
1579750e409Sopenharmony_ci    cJSON_Delete(item);
1589750e409Sopenharmony_ci}
1599750e409Sopenharmony_ci
1609750e409Sopenharmony_cistatic void cjson_get_number_value_should_get_a_number_with_int64(void)
1619750e409Sopenharmony_ci{
1629750e409Sopenharmony_ci    cJSON *string = cJSON_CreateString("test");
1639750e409Sopenharmony_ci    cJSON *number = cJSON_CreateInt64Number(1LL);
1649750e409Sopenharmony_ci
1659750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_INT64(*cJSON_GetInt64NumberValue(number), number->valueint);
1669750e409Sopenharmony_ci    TEST_ASSERT_NULL(cJSON_GetInt64NumberValue(string));
1679750e409Sopenharmony_ci    TEST_ASSERT_NULL(cJSON_GetInt64NumberValue(NULL));
1689750e409Sopenharmony_ci
1699750e409Sopenharmony_ci    cJSON_Delete(number);
1709750e409Sopenharmony_ci    cJSON_Delete(string);
1719750e409Sopenharmony_ci}
1729750e409Sopenharmony_ci
1739750e409Sopenharmony_ciint CJSON_CDECL main(void)
1749750e409Sopenharmony_ci{
1759750e409Sopenharmony_ci    UNITY_BEGIN();
1769750e409Sopenharmony_ci
1779750e409Sopenharmony_ci    RUN_TEST(cjson_get_object_item_should_get_object_items_with_int64);
1789750e409Sopenharmony_ci    RUN_TEST(cjson_get_object_item_case_sensitive_should_get_object_items_with_int64);
1799750e409Sopenharmony_ci    RUN_TEST(cjson_set_number_value_should_set_numbers_with_int64);
1809750e409Sopenharmony_ci    RUN_TEST(typecheck_functions_should_check_type_with_int64);
1819750e409Sopenharmony_ci    RUN_TEST(cjson_functions_should_not_crash_with_null_pointers_with_int64);
1829750e409Sopenharmony_ci    RUN_TEST(cjson_get_number_value_should_get_a_number_with_int64);
1839750e409Sopenharmony_ci
1849750e409Sopenharmony_ci    return UNITY_END();
1859750e409Sopenharmony_ci}
186