json_loader_test.cpp 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/**
 * \file sdk/load-and-run/test/test_json_loader.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#include <cfloat>
#include <cstdint>
#include <cstdio>
#include <cmath>
#include "../src/json_loader.h"

using namespace mgb;

void test_number(double real, std::string str) {
    JsonLoader json;
    auto root = json.load(str.data(), str.size());
    mgb_assert(root->is_number());
    mgb_assert(std::fabs(real - root->number()) <= DBL_EPSILON);
}

void test_string(std::string str, std::string json_str) {
    JsonLoader json;
    auto root = json.load(json_str.data(), json_str.size());
    mgb_assert(root->is_str());
    mgb_assert(str == root->str());
}

void test_array(size_t num, std::string str) {
    JsonLoader json;
    auto root = json.load(str.data(), str.size());
    mgb_assert(root->is_array());
    mgb_assert(root->len() == num);
}

void test_object(size_t num, std::string str) {
    JsonLoader json;
    auto root = json.load(str.data(), str.size());
    mgb_assert(root->is_object());
    mgb_assert(root->len() == num);
}

int main() {
    test_number(1.0, "1.0");
    test_number(1e10, "1e10");
    test_number(0.2345678, "0.02345678e1");
    test_number(-10086, "-1.0086E4");
    test_number(1.7976931348623157e+308,
                "1.7976931348623157e+308");  // max double

    test_string("a", "\"a\"");
    test_string("\\table", "\"\\table\"");

    test_array(0, "  [  ] ");
    test_array(4, " [  0.1,  0.2,0.3, 1990 ] ");
    test_array(2, " [  0.1,  \"hello-world\"]");
    test_array(3, " [  0.1,  \"hello-world\", [2.0, 33]]");
    test_array(1, " [ [  [ [2020] ], [2021], [[2022]]  ] ]");

    test_object(0, "   {      }            ");
    test_object(1, "{\"key1\": 2023}");
    test_object(1,
                "{\"key1\": { 		\"key2\": { 			"
                "\"key3\": \"value\" 		} 	} }");
    test_object(1, "{\"key1\":{\"key2\":{}}}");

    printf("test passed\n");
    return 0;
}