|
| 1 | +#include "../test.h" |
| 2 | + |
| 3 | +/** |
| 4 | + * NB: if ULib is configured and compiled (./configure --disable-shared && make) use this way: |
| 5 | + * |
| 6 | + * #define HAVE_CONFIG_H |
| 7 | + * #include <ulib/json/value.h> |
| 8 | + * #undef HAVE_CONFIG_H |
| 9 | + * |
| 10 | + * otherwise: |
| 11 | + * |
| 12 | + * #include <ULib/src/ulib/all_cpp.cpp> |
| 13 | + */ |
| 14 | + |
| 15 | +#define HAVE_CONFIG_H |
| 16 | +#include <ulib/json/value.h> |
| 17 | +#undef HAVE_CONFIG_H |
| 18 | + |
| 19 | +static ULib ulib("167193,0,0,0,-30,-31,-30,-31,0"); |
| 20 | + |
| 21 | +static void GenStat(Stat& stat, const union UValue::jval val) |
| 22 | +{ |
| 23 | + U_TRACE(5, "::GenStat(%p,0x%x)", &stat, val.ival) |
| 24 | + |
| 25 | + switch (UValue::getTag(val.ival)) |
| 26 | + { |
| 27 | + case UValue::REAL_VALUE: |
| 28 | + case UValue::INT_VALUE: |
| 29 | + case UValue::UINT_VALUE: stat.numberCount++; break; |
| 30 | + case UValue::TRUE_VALUE: stat.trueCount++; break; |
| 31 | + case UValue::FALSE_VALUE: stat.falseCount++; break; |
| 32 | + case UValue::NULL_VALUE: stat.nullCount++; break; |
| 33 | + |
| 34 | + case UValue::STRING_VALUE: |
| 35 | + case UValue::UTF_VALUE: |
| 36 | + { |
| 37 | + stat.stringCount++; |
| 38 | + |
| 39 | + stat.stringLength += UValue::getStringSize(val); |
| 40 | + } |
| 41 | + break; |
| 42 | + |
| 43 | + case UValue::ARRAY_VALUE: |
| 44 | + { |
| 45 | + stat.arrayCount++; |
| 46 | + |
| 47 | + for (auto const& i : val) |
| 48 | + { |
| 49 | + stat.elementCount++; |
| 50 | + |
| 51 | + GenStat(stat, i.getValue()); |
| 52 | + } |
| 53 | + } |
| 54 | + break; |
| 55 | + |
| 56 | + case UValue::OBJECT_VALUE: |
| 57 | + { |
| 58 | + stat.objectCount++; |
| 59 | + |
| 60 | + for (auto const& i : val) |
| 61 | + { |
| 62 | + stat.memberCount++; |
| 63 | + stat.stringCount++; // Key |
| 64 | + stat.stringLength += UValue::getStringSize(i.getKey()); |
| 65 | + |
| 66 | + GenStat(stat, i.getValue()); |
| 67 | + } |
| 68 | + } |
| 69 | + break; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class ULibParseResult : public ParseResultBase { |
| 74 | +public: |
| 75 | + UString s; |
| 76 | + UValue json; |
| 77 | +}; |
| 78 | + |
| 79 | +class ULibStringResult : public StringResultBase { |
| 80 | +public: |
| 81 | + virtual const char* c_str() const { return s.data(); } |
| 82 | + |
| 83 | + UString s; |
| 84 | +}; |
| 85 | + |
| 86 | +class ULibTest : public TestBase { |
| 87 | +public: |
| 88 | +#if TEST_INFO |
| 89 | + virtual const char* GetName() const { return "ULib (C++)"; } |
| 90 | + virtual const char* GetFilename() const { return __FILE__; } |
| 91 | +#endif |
| 92 | + |
| 93 | +#if TEST_PARSE |
| 94 | + virtual ParseResultBase* Parse(const char* json, size_t length) const |
| 95 | + { |
| 96 | + ULibParseResult* pr = new ULibParseResult; |
| 97 | + |
| 98 | + return (pr->json.parse((pr->s = UString(json, length))) ? pr : (delete pr, (ULibParseResult*)0)); |
| 99 | + } |
| 100 | +#endif |
| 101 | + |
| 102 | +#if TEST_STRINGIFY |
| 103 | + virtual StringResultBase* Stringify(const ParseResultBase* parseResult) const |
| 104 | + { |
| 105 | + ULibStringResult* sr = new ULibStringResult; |
| 106 | + |
| 107 | + sr->s = ((const ULibParseResult*)parseResult)->json.output(); |
| 108 | + |
| 109 | + return sr; |
| 110 | + } |
| 111 | +#endif |
| 112 | + |
| 113 | +#if TEST_PRETTIFY |
| 114 | + virtual StringResultBase* Prettify(const ParseResultBase* parseResult) const |
| 115 | + { |
| 116 | + ULibStringResult* sr = new ULibStringResult; |
| 117 | + |
| 118 | + sr->s = ((const ULibParseResult*)parseResult)->json.prettify(); |
| 119 | + |
| 120 | + return sr; |
| 121 | + } |
| 122 | +#endif |
| 123 | + |
| 124 | +#if TEST_STATISTICS |
| 125 | + virtual bool Statistics(const ParseResultBase* parseResult, Stat* stat) const |
| 126 | + { |
| 127 | + (void) memset(stat, 0, sizeof(Stat)); |
| 128 | + |
| 129 | + const ULibParseResult* pr = static_cast<const ULibParseResult*>(parseResult); |
| 130 | + |
| 131 | + GenStat(*stat, pr->json.getValue()); |
| 132 | + |
| 133 | + return true; |
| 134 | + } |
| 135 | +#endif |
| 136 | + |
| 137 | +#if TEST_CONFORMANCE |
| 138 | + virtual bool ParseDouble(const char* json, double* d) const |
| 139 | + { |
| 140 | + UValue v; |
| 141 | + |
| 142 | + if (v.parse(UString(json))) |
| 143 | + { |
| 144 | + *d = v.at(0)->getDouble(); |
| 145 | + |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + virtual bool ParseString(const char* json, std::string& s) const |
| 153 | + { |
| 154 | + UValue v; |
| 155 | + |
| 156 | + if (v.parse(UString(json))) |
| 157 | + { |
| 158 | + UString result = v.at(0)->getString(); |
| 159 | + |
| 160 | + (void) s.assign(U_STRING_TO_PARAM(result)); |
| 161 | + |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + return false; |
| 166 | + } |
| 167 | +#endif |
| 168 | +}; |
| 169 | + |
| 170 | +REGISTER_TEST(ULibTest); |
0 commit comments