void VM::assert(IOperand const * operand) { if (this->_stack.empty()) { std::cout << BOLDRED << "Exception throwed at line " << __LINE__ << " of " << __FILE__ << RESET << std::endl; throw assertException(); } if (this->_stack.back()->toString().compare(operand->toString()) != 0 || this->_stack.back()->getPrecision() != operand->getPrecision()) { std::cout << BOLDRED << "Exception throwed at line " << __LINE__ << " of " << __FILE__ << RESET << std::endl; throw assertException(); } }
void VM::print(IOperand const * operand) { operand = 0; if (this->_stack.empty() || this->_stack.back()->getType() != Int8) { std::cout << BOLDRED << "Exception throwed at line " << __LINE__ << " of " << __FILE__ << RESET << std::endl; throw assertException(); } std::cout << static_cast<char>(std::stoi(this->_stack.back()->toString())) << std::endl; }
static void runConfigManagerTest(TestRunner& tr) { tr.group("ConfigManager"); tr.test("init"); { DynamicObject expect; expect->setType(Map); ConfigManager cm; Config cfg; cfg[ConfigManager::ID] = "config"; cfg[ConfigManager::MERGE]->setType(Map); assert(cm.addConfig(cfg)); assertDynoCmp(cm.getConfig("config", true), cfg); assertDynoCmp(cm.getConfig("config", false), expect); assertDynoCmp(cm.getConfig("config"), expect); } tr.passIfNoException(); tr.test("init & clear"); { DynamicObject expect; expect->setType(Map); ConfigManager cm; Config cfg; cfg[ConfigManager::ID] = "config"; cfg[ConfigManager::MERGE]->setType(Map); assert(cm.addConfig(cfg)); cm.clear(); Config cfg2 = cm.getConfig("config"); assert(cfg2.isNull()); } tr.passIfException(); tr.test("1 config"); { DynamicObject expect; expect->setType(Map); expect["a"] = 0; ConfigManager cm; Config cfg; cfg[ConfigManager::ID] = "config"; cfg[ConfigManager::MERGE]["a"] = 0; assert(cm.addConfig(cfg)); assertNoExceptionSet(); assertDynoCmp(cm.getConfig("config"), expect); } tr.passIfNoException(); tr.test("config change"); { ConfigManager cm; Config cfg; cfg[ConfigManager::ID] = "config"; cfg[ConfigManager::MERGE]["a"] = 0; assert(cm.addConfig(cfg)); DynamicObject a; a["a"] = 0; assertDynoCmp(cm.getConfig("config"), a); Config change = cm.getConfig("config", true); change[ConfigManager::MERGE]["a"] = 1; assert(cm.setConfig(change)); DynamicObject expect; expect["a"] = 1; assert(cm.getConfig("config") != a); assertDynoCmp(cm.getConfig("config"), expect); } tr.passIfNoException(); tr.test("invalid set config"); { ConfigManager cm; Config cfg; cfg[ConfigManager::ID] = "config"; cfg[ConfigManager::MERGE]["a"] = 0; assert(!cm.setConfig(cfg)); } tr.passIfException(); tr.test("double add config"); { ConfigManager cm; Config cfg; cfg[ConfigManager::ID] = "config"; cfg[ConfigManager::MERGE]["a"] = 0; assert(cm.addConfig(cfg)); cfg[ConfigManager::MERGE]["a"] = 1; assert(cm.addConfig(cfg)); DynamicObject expect; expect["a"] = 1; assertDynoCmp(cm.getConfig("config"), expect); } tr.passIfNoException(); tr.test("add"); { DynamicObject expect; expect["a"] = 0; expect["b"] = 1; expect["c"] = 2; ConfigManager cm; Config a; a[ConfigManager::ID] = "config"; a[ConfigManager::MERGE]["a"] = 0; Config b; b[ConfigManager::ID] = "config"; b[ConfigManager::MERGE]["b"] = 1; Config c; c[ConfigManager::ID] = "config"; c[ConfigManager::MERGE]["c"] = 2; assert(cm.addConfig(a)); assertNoExceptionSet(); assert(cm.addConfig(b)); assertNoExceptionSet(); assert(cm.addConfig(c)); assertNoExceptionSet(); assertDynoCmp(cm.getConfig("config"), expect); } tr.passIfNoException(); tr.test("bad remove"); { ConfigManager cm; assert(!cm.removeConfig("error")); assertExceptionSet(); Exception::clear(); } tr.passIfNoException(); tr.test("remove"); { DynamicObject expect; expect["a"] = 0; expect["b"] = 1; expect["c"] = 2; ConfigManager cm; Config a; a[ConfigManager::ID] = "config a"; a[ConfigManager::GROUP] = "group"; a[ConfigManager::MERGE]["a"] = 0; Config b; b[ConfigManager::ID] = "config b"; b[ConfigManager::GROUP] = "group"; b[ConfigManager::MERGE]["b"] = 1; Config c; c[ConfigManager::ID] = "config c"; c[ConfigManager::GROUP] = "group"; c[ConfigManager::MERGE]["c"] = 2; assert(cm.addConfig(a)); assertNoExceptionSet(); assert(cm.addConfig(b)); assertNoExceptionSet(); assert(cm.addConfig(c)); assertNoExceptionSet(); assertDynoCmp(cm.getConfig("group"), expect); DynamicObject expect2; expect2["a"] = 0; expect2["c"] = 2; assert(cm.removeConfig("config b")); assertDynoCmp(cm.getConfig("group"), expect2); } tr.passIfNoException(); tr.test("default value"); { ConfigManager cm; Config a; a[ConfigManager::ID] = "config a"; a[ConfigManager::MERGE] = 1; assert(cm.addConfig(a)); assertNoExceptionSet(); Config b; b[ConfigManager::ID] = "config b"; b[ConfigManager::PARENT] = "config a"; b[ConfigManager::MERGE] = ConfigManager::DEFAULT_VALUE; assert(cm.addConfig(b)); assertNoExceptionSet(); DynamicObject expect; expect = 1; assertDynoCmp(cm.getConfig("config b"), expect); } tr.passIfNoException(); tr.test("default values"); { ConfigManager cm; Config cfga; cfga[ConfigManager::ID] = "config a"; Config& a = cfga[ConfigManager::MERGE]; a[0] = 10; a[1] = 11; a[2]["0"] = 120; a[2]["1"] = 121; assert(cm.addConfig(cfga)); assertNoExceptionSet(); Config cfgb; cfgb[ConfigManager::ID] = "config b"; cfgb[ConfigManager::PARENT] = "config a"; Config& b = cfgb[ConfigManager::MERGE]; b[0] = ConfigManager::DEFAULT_VALUE; b[1] = 21; b[2]["0"] = ConfigManager::DEFAULT_VALUE; b[2]["1"] = 221; assert(cm.addConfig(cfgb)); assertNoExceptionSet(); DynamicObject expect; expect[0] = 10; expect[1] = 21; expect[2]["0"] = 120; expect[2]["1"] = 221; assertDynoCmp(cm.getConfig("config b"), expect); } tr.passIfNoException(); tr.test("keyword substitution {RESOURCE_DIR}"); { DynamicObject expect; expect["dir"] = "/the/real/dir"; expect["dir-plus"] = "/the/real/dir/plus/more"; //expect["name"] = "Digital Bazaar, Inc."; ConfigManager cm; Config a; a[ConfigManager::ID] = "config"; a[ConfigManager::MERGE]["dir"] = "{RESOURCE_DIR}"; a[ConfigManager::MERGE]["dir-plus"] = "{RESOURCE_DIR}/plus/more"; // FIXME: only supports "{RESOURCE_DIR}" now //a[ConfigManager::MERGE]["other"] = "{DB}"; cm.setKeyword("RESOURCE_DIR", "/the/real/dir"); //cm.setKeyword("DB", "Digital Bazaar, Inc."); assert(cm.addConfig(a)); assertNoExceptionSet(); assertDynoCmp(cm.getConfig("config"), expect); } tr.passIfNoException(); tr.test("keyword substitution {CURRENT_DIR}"); { DynamicObject expect; string cwd; string cwdPlusMore; string absoluteDir; File configFile = File::createTempFile("test-config-file"); FileOutputStream fos(configFile); // create and populate the config file string configFileText = "{\n" "\"_id_\": \"config\",\n" "\"_merge_\": {\n" " \"dir\": \"{CURRENT_DIR}\",\n" " \"dir-plus\": \"{CURRENT_DIR}/plus/more\" }\n" "}\n"; fos.write(configFileText.c_str(), configFileText.length()); fos.close(); // modify the current working directory to the expected value absoluteDir = File::dirname(configFile->getAbsolutePath()); cwd = absoluteDir.c_str(); cwdPlusMore = cwd.c_str(); cwdPlusMore.append("/plus/more"); // set the expected values expect["dir"] = cwd.c_str(); expect["dir-plus"] = cwdPlusMore.c_str(); // create the configuration ConfigManager cm; assert(cm.addConfigFile(configFile->getAbsolutePath(), true, absoluteDir.c_str(), true, false)); assertNoExceptionSet(); assertDynoCmp(cm.getConfig("config"), expect); } tr.passIfNoException(); #if 0 tr.test("user preferences"); { ConfigManager cm; // node // built in or loaded defaults DynamicObject nodec; nodec["node"]["host"] = "localhost"; nodec["node"]["port"] = 19100; nodec["node"]["modulePath"]->append("/usr/lib/bitmunk/modules"); nodec["node"]["userModulePath"] = "~/.bitmunk/modules"; assert(cm.addConfig(nodec)); assertNoExceptionSet(); // user // loaded defaults DynamicObject userc; userc["node"]["port"] = 19100; userc["node"]["comment"] = "My precious..."; assert(cm.addConfig(userc, ConfigManager::Custom)); assertNoExceptionSet(); // user makes changes during runtime DynamicObject c = cm.getConfig(); c["node"]["port"] = 19200; c["node"]["userModulePath"] = "~/.bitmunk/modules:~/.bitmunk/modules-dev"; c["node"][ConfigManager::TMP]["not in changes"] = true; // get the changes from defaults to current config // serialize this to disk as needed DynamicObject changes; cm.getChanges(changes); // check it's correct DynamicObject expect; expect["node"]["port"] = 19200; expect["node"]["comment"] = "My precious..."; expect["node"]["userModulePath"] = "~/.bitmunk/modules:~/.bitmunk/modules-dev"; // NOTE: will not have TMP var assertDynoCmp(changes, expect); } tr.passIfNoException(); #endif tr.test("versioning"); { ConfigManager cm; cm.getVersions()->clear(); Config c; c[ConfigManager::ID] = "config"; assert(cm.addConfig(c)); assertNoExceptionSet(); // config has no version - no check done - pass cm.addVersion("1"); assert(cm.addConfig(c)); assertNoExceptionSet(); // config has known version - pass c[ConfigManager::VERSION] = "1"; assert(cm.addConfig(c)); assertNoExceptionSet(); assert(cm.removeConfig("config")); // config has unknown version - fail c[ConfigManager::VERSION] = "2"; assert(!cm.addConfig(c)); assertExceptionSet(); Exception::clear(); } tr.passIfNoException(); tr.test("empty array & map"); { ConfigManager cm; DynamicObject a; a[ConfigManager::ID] = "config"; a[ConfigManager::MERGE][0]->setType(Array); a[ConfigManager::MERGE][1]->setType(Map); assert(cm.addConfig(a)); assertNoExceptionSet(); DynamicObject expect; expect[0]->setType(Array); expect[1]->setType(Map); assertDynoCmp(cm.getConfig("config"), expect); } tr.passIfNoException(); tr.test("empty group ids"); { ConfigManager cm; DynamicObject expect; expect->setType(Array); assertDynoCmp(cm.getIdsInGroup("Not-A-Group"), expect); } tr.passIfNoException(); tr.test("group ids"); { ConfigManager cm; DynamicObject c; c[ConfigManager::ID] = "c0"; c[ConfigManager::GROUP] = "c"; assert(cm.addConfig(c)); assertNoExceptionSet(); c[ConfigManager::ID] = "c1"; c[ConfigManager::GROUP] = "c"; assert(cm.addConfig(c)); assertNoExceptionSet(); DynamicObject expect; expect->setType(Array); expect[0] = "c0"; expect[1] = "c1"; assertDynoCmp(cm.getIdsInGroup("c"), expect); } tr.passIfNoException(); tr.test("replace keywords"); { ConfigManager cm; DynamicObject c; c[ConfigManager::ID] = "c"; c[ConfigManager::MERGE]["test"] = "{A}"; DynamicObject vars; vars["A"] = "a"; assertNoException(ConfigManager::replaceKeywords(c, vars)); DynamicObject expect; expect[ConfigManager::ID] = "c"; expect[ConfigManager::MERGE]["test"] = "a"; assertDynoCmp(c, expect); } tr.passIfNoException(); tr.test("replace keywords (invalid keyword)"); { ConfigManager cm; DynamicObject c; c[ConfigManager::ID] = "c"; c[ConfigManager::MERGE]["test"] = "{UNKNOWN}"; DynamicObject vars; vars["A"] = "a"; assertException(ConfigManager::replaceKeywords(c, vars)); } tr.passIfException(); tr.ungroup(); }
static void runNodeMonitorTest(TestRunner& tr) { tr.group("NodeMonitor"); tr.test("empty"); { NodeMonitor nm; DynamicObject expect; expect->setType(Map); DynamicObject all = nm.getAll(); assertDynoCmp(expect, all); } tr.passIfNoException(); tr.test("add1 init"); { bool r; NodeMonitor nm; DynamicObject si; si["init"] = "foo"; r = nm.addState("s", si); assert(r); DynamicObject expect; expect["s"] = "foo"; DynamicObject all = nm.getAll(); assertDynoCmp(expect, all); } tr.passIfNoException(); tr.test("addN init"); { bool r; NodeMonitor nm; DynamicObject si; si["s"]["init"] = "foo"; si["u"]["init"] = (uint64_t)0; si["i"]["init"] = (int64_t)0; si["d"]["init"] = 0.; r = nm.addStates(si); assert(r); DynamicObject expect; expect["s"] = "foo"; expect["u"] = (uint64_t)0; expect["i"] = (int64_t)0; expect["d"] = 0.; DynamicObject all = nm.getAll(); assertDynoCmp(expect, all); } tr.passIfNoException(); tr.test("remove"); { bool r; NodeMonitor nm; DynamicObject si; si["n"]["init"] = (uint64_t)0; si["rem"]["init"] = (uint64_t)0; r = nm.addStates(si); assert(r); DynamicObject s; s["rem"].setNull(); r = nm.removeStates(s); assert(r); DynamicObject expect; expect["n"] = (uint64_t)0; DynamicObject all = nm.getAll(); assertDynoCmp(expect, all); } tr.passIfNoException(); tr.test("set"); { bool r; NodeMonitor nm; DynamicObject si; si["n"]["init"] = (uint64_t)0; r = nm.addStates(si); assert(r); DynamicObject s; s["n"] = (uint64_t)123; r = nm.setStates(s); assert(r); DynamicObject expect; expect["n"] = (uint64_t)123; DynamicObject all = nm.getAll(); assertDynoCmp(expect, all); } tr.passIfNoException(); tr.test("adj"); { bool r; NodeMonitor nm; DynamicObject si; si["n"]["init"] = (uint64_t)0; r = nm.addStates(si); assert(r); DynamicObject s; s["n"] = (uint64_t)1; r = nm.adjustStates(s); assert(r); r = nm.adjustStates(s); assert(r); r = nm.adjustStates(s); assert(r); DynamicObject expect; expect["n"] = (uint64_t)3; DynamicObject all = nm.getAll(); assertDynoCmp(expect, all); } tr.passIfNoException(); tr.test("reset"); { bool r; NodeMonitor nm; DynamicObject si; si["n"]["init"] = (uint64_t)100; r = nm.addStates(si); assert(r); DynamicObject s; s["n"] = (uint64_t)123; r = nm.setStates(s); assert(r); r = nm.resetStates(s); assert(r); DynamicObject expect; expect["n"] = (uint64_t)100; DynamicObject all = nm.getAll(); assertDynoCmp(expect, all); } tr.passIfNoException(); tr.test("adj neg"); { bool r; NodeMonitor nm; DynamicObject si; si["n"]["init"] = (uint64_t)100; r = nm.addStates(si); assert(r); DynamicObject s; s["n"] = (int64_t)-1; r = nm.adjustStates(s); assert(r); DynamicObject expect; expect["n"] = (uint64_t)99; DynamicObject all = nm.getAll(); assertDynoCmp(expect, all); } tr.passIfNoException(); tr.test("getN"); { bool r; NodeMonitor nm; DynamicObject si; si["a"]["init"] = "a"; si["b"]["init"] = "b"; si["c"]["init"] = "c"; r = nm.addStates(si); assert(r); DynamicObject s; s["b"].setNull(); s["c"].setNull(); r = nm.getStates(s); assert(r); DynamicObject expect; expect["b"] = "b"; expect["c"] = "c"; assertDynoCmp(expect, s); } tr.passIfNoException(); tr.test("add ex"); { NodeMonitor nm; DynamicObject si; si["bad"] = 0; assertException(nm.addStates(si)); Exception::clear(); } tr.passIfNoException(); tr.test("rem ex"); { NodeMonitor nm; DynamicObject s; s["a"] = 0; assertException(nm.removeStates(s)); Exception::clear(); } tr.passIfNoException(); // Not for use with async StateMonitor implementation. /* tr.test("set ex"); { bool r; NodeMonitor nm; DynamicObject s; s["a"] = 0; assertException(nm.setStates(s)); Exception::clear(); } tr.passIfNoException(); */ tr.test("get ex"); { NodeMonitor nm; DynamicObject s; s["a"] = 0; assertException(nm.getStates(s)); Exception::clear(); } tr.passIfNoException(); tr.test("adj ex"); { NodeMonitor nm; DynamicObject s; s["a"] = 1; assertException(nm.adjustStates(s)); Exception::clear(); } tr.passIfNoException(); tr.test("adj type ex"); { NodeMonitor nm; DynamicObject si; si["a"]["init"] = (uint64_t)0; assertNoException( nm.addStates(si)); // Not for use with async StateMonitor implementation. /* DynamicObject s; s["a"] = 0.; assertException(nm.adjustStates(s)); Exception::clear(); */ } tr.passIfNoException(); tr.test("txn adj ex"); { // check bad transaction doesn't change values NodeMonitor nm; DynamicObject si; si["a"]["init"] = (uint64_t)10; si["b"]["init"] = (uint64_t)10; assertNoException( nm.addStates(si)); // Not for use with async StateMonitor implementation. /* DynamicObject s; s["a"] = (int64_t)-1; s["b"] = -1.; assertException(nm.adjustStates(s)); Exception::clear(); */ DynamicObject expect; expect["a"] = (uint64_t)10; expect["b"] = (uint64_t)10; DynamicObject all = nm.getAll(); assertDynoCmp(expect, all); } tr.passIfNoException(); tr.test("no ex"); { NodeMonitor nm; DynamicObject si; si["a"]["init"] = (uint64_t)10; assertNoException( nm.addStates(si)); DynamicObject s; s["bad"] = (int64_t)-1; assertException(nm.adjustStates(s)); Exception::clear(); DynamicObject expect; expect["a"] = (uint64_t)10; DynamicObject all = nm.getAll(); assertDynoCmp(expect, all); } tr.passIfNoException(); tr.ungroup(); }
void TestSuite::TestInspectors(void){ UnitTest::SetPrefix("TestInspectors.cpp - Inspectors"); JSONNode test = JSONNode(JSON_NULL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("")); assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); assertEquals(test.as_bool(), false); #endif test = 15.5f; assertEquals(test.type(), JSON_NUMBER); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("15.5")); #endif assertEquals(test.as_int(), 15); assertEquals(test.as_float(), 15.5f); #ifdef JSON_CASTABLE assertEquals(test.as_bool(), true); #endif test = 0.0f; assertEquals(test.type(), JSON_NUMBER); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("0")); #endif assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); #ifdef JSON_CASTABLE assertEquals(test.as_bool(), false); #endif test = true; assertEquals(test.type(), JSON_BOOL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("true")); assertEquals(test.as_int(), 1); assertEquals(test.as_float(), 1.0f); #endif assertEquals(test.as_bool(), true); test = false; assertEquals(test.type(), JSON_BOOL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("false")); assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); #endif assertEquals(test.as_bool(), false); #ifdef JSON_CASTABLE test.cast(JSON_NODE); #else test = JSONNode(JSON_NODE); #endif assertEquals(test.type(), JSON_NODE); assertEquals(test.size(), 0); test.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world"))); test.push_back(JSONNode(JSON_TEXT("hello"), JSON_TEXT("mars"))); test.push_back(JSONNode(JSON_TEXT("salut"), JSON_TEXT("france"))); assertEquals(test.size(), 3); TestSuite::testParsingItself(test); #ifdef JSON_CASTABLE JSONNode casted = test.as_array(); #ifdef JSON_UNIT_TEST assertNotEquals(casted.internal, test.internal); #endif assertEquals(casted.type(), JSON_ARRAY); assertEquals(test.type(), JSON_NODE); assertEquals(test.size(), 3); assertEquals(casted.size(), 3); TestSuite::testParsingItself(casted); #endif UnitTest::SetPrefix("TestInspectors.cpp - Location"); try { #ifdef JSON_CASTABLE assertEquals(casted.at(0), JSON_TEXT("world")); assertEquals(casted.at(1), JSON_TEXT("mars")); assertEquals(casted.at(2), JSON_TEXT("france")); assertEquals(casted.at(0).name(), JSON_TEXT("")); assertEquals(casted.at(1).name(), JSON_TEXT("")); assertEquals(casted.at(2).name(), JSON_TEXT("")); #endif assertEquals(test.at(0), JSON_TEXT("world")); assertEquals(test.at(1), JSON_TEXT("mars")); assertEquals(test.at(2), JSON_TEXT("france")); assertEquals(test.at(0).name(), JSON_TEXT("hi")); assertEquals(test.at(1).name(), JSON_TEXT("hello")); assertEquals(test.at(2).name(), JSON_TEXT("salut")); } catch (std::out_of_range){ FAIL("exception caught"); } try { assertEquals(test.at(JSON_TEXT("hi")), JSON_TEXT("world")); assertEquals(test.at(JSON_TEXT("hello")), JSON_TEXT("mars")); assertEquals(test.at(JSON_TEXT("salut")), JSON_TEXT("france")); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertEquals(test.at_nocase(JSON_TEXT("SALUT")), JSON_TEXT("france")); assertEquals(test.at_nocase(JSON_TEXT("HELLO")), JSON_TEXT("mars")); assertEquals(test.at_nocase(JSON_TEXT("HI")), JSON_TEXT("world")); #endif } catch (std::out_of_range){ FAIL("exception caught"); } assertException(test.at(JSON_TEXT("meh")), std::out_of_range); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertException(test.at_nocase(JSON_TEXT("meh")), std::out_of_range); #endif assertEquals(test[JSON_TEXT("hi")], json_string(JSON_TEXT("world"))); assertEquals(test[JSON_TEXT("hello")], json_string(JSON_TEXT("mars"))); assertEquals(test[JSON_TEXT("salut")], json_string(JSON_TEXT("france"))); assertEquals(test[0], JSON_TEXT("world")); assertEquals(test[1], JSON_TEXT("mars")); assertEquals(test[2], JSON_TEXT("france")); #ifdef JSON_ITERATORS #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestInspectors.cpp - Iterators"); for(JSONNode::iterator it = casted.begin(), end = casted.end(); it != end; ++it){ assertEquals((*it).name(), JSON_TEXT("")); } #endif #endif #ifdef JSON_BINARY UnitTest::SetPrefix("TestInspectors.cpp - Binary"); test.set_binary((const unsigned char *)"Hello World", 11); assertEquals(test.type(), JSON_STRING); assertEquals(test.as_string(), JSON_TEXT("SGVsbG8gV29ybGQ=")); assertEquals(test.as_binary(), "Hello World"); assertEquals(test.as_binary().size(), 11); test = JSON_TEXT("Hello World"); assertEquals(test.type(), JSON_STRING); assertEquals(test.as_string(), JSON_TEXT("Hello World")); #ifdef JSON_SAFE assertEquals(test.as_binary(), ""); #endif #endif #ifdef JSON_READ_PRIORITY //This is a regression test for a bug in at() json_string buffer(JSON_TEXT("{ \"myValue1\" : \"foo\", \"myValue2\" : \"bar\"}")); JSONNode current = libjson::parse(buffer); try { JSONNode & value1 = current[JSON_TEXT("myValue1")]; assertEquals(value1.as_string(), JSON_TEXT("foo")); JSONNode & value2 = current[JSON_TEXT("myValue2")]; assertEquals(value2.as_string(), JSON_TEXT("bar")); } catch (...){ assertTrue(false); } #endif }
void TestSuite::TestInspectors(void){ UnitTest::SetPrefix("TestInspectors.cpp - Inspectors"); #ifdef JSON_LIBRARY JSONNODE * test = json_new(JSON_NULL); assertEquals(json_type(test), JSON_NULL); json_char * res = json_as_string(test); assertCStringSame(res, JSON_TEXT("")); json_free(res); assertEquals_Primitive(json_as_int(test), 0); assertEquals_Primitive(json_as_float(test), 0.0f); assertEquals(json_as_bool(test), false); json_set_f(test, 15.5f); assertEquals(json_type(test), JSON_NUMBER); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("15.5")); json_free(res); #endif assertEquals_Primitive(json_as_int(test), 15); assertEquals_Primitive(json_as_float(test), 15.5f); #ifdef JSON_CASTABLE assertEquals(json_as_bool(test), true); #endif json_set_f(test, 0.0f); assertEquals(json_type(test), JSON_NUMBER); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("0")); json_free(res); #endif assertEquals_Primitive(json_as_int(test), 0); assertEquals_Primitive(json_as_float(test), 0.0f); #ifdef JSON_CASTABLE assertEquals(json_as_bool(test), false); #endif json_set_b(test, true); assertEquals(json_type(test), JSON_BOOL); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("true")); json_free(res); assertEquals_Primitive(json_as_int(test), 1); assertEquals_Primitive(json_as_float(test), 1.0f); #endif assertEquals(json_as_bool(test), true); json_set_b(test, false); assertEquals(json_type(test), JSON_BOOL); #ifdef JSON_CASTABLE res = json_as_string(test); assertCStringSame(res, JSON_TEXT("false")); json_free(res); assertEquals_Primitive(json_as_int(test), 0); assertEquals_Primitive(json_as_float(test), 0.0f); #endif assertEquals(json_as_bool(test), false); #ifdef JSON_CASTABLE json_cast(test, JSON_NODE); assertEquals(json_type(test), JSON_NODE); assertEquals(json_size(test), 0); json_push_back(test, json_new_a(JSON_TEXT("hi"), JSON_TEXT("world"))); json_push_back(test, json_new_a(JSON_TEXT("hello"), JSON_TEXT("mars"))); json_push_back(test, json_new_a(JSON_TEXT("salut"), JSON_TEXT("france"))); assertEquals(json_size(test), 3); TestSuite::testParsingItself(test); JSONNODE * casted = json_as_array(test); #ifdef JSON_UNIT_TEST assertNotEquals(((JSONNode*)casted) -> internal, ((JSONNode*)test) -> internal); #endif assertEquals(json_type(casted), JSON_ARRAY); assertEquals(json_type(test), JSON_NODE); assertEquals(json_size(test), 3); assertEquals(json_size(casted), 3); TestSuite::testParsingItself(casted); #endif UnitTest::SetPrefix("TestInspectors.cpp - Location"); #ifdef JSON_CASTABLE #define CheckAt(parent, locale, text)\ if(JSONNODE * temp = json_at(parent, locale)){\ json_char * _res = json_as_string(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckAt: ") + #parent + "[" + #locale + "]");\ } #define CheckNameAt(parent, locale, text)\ if(JSONNODE * temp = json_at(parent, locale)){\ json_char * _res = json_name(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckNameAt: ") + #parent + "[" + #locale + "]");\ } CheckAt(casted, 0, JSON_TEXT("world")); CheckAt(casted, 1, JSON_TEXT("mars")); CheckAt(casted, 2, JSON_TEXT("france")); CheckNameAt(casted, 0, JSON_TEXT("")); CheckNameAt(casted, 1, JSON_TEXT("")); CheckNameAt(casted, 2, JSON_TEXT("")); CheckAt(test, 0, JSON_TEXT("world")); CheckAt(test, 1, JSON_TEXT("mars")); CheckAt(test, 2, JSON_TEXT("france")); CheckNameAt(test, 0, JSON_TEXT("hi")); CheckNameAt(test, 1, JSON_TEXT("hello")); CheckNameAt(test, 2, JSON_TEXT("salut")); #define CheckGet(parent, locale, text)\ if(JSONNODE * temp = json_get(parent, locale)){\ json_char * _res = json_as_string(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckGet: ") + #parent + "[" + #locale + "]");\ } #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS #define CheckGetNoCase(parent, locale, text)\ if(JSONNODE * temp = json_get_nocase(parent, locale)){\ json_char * _res = json_as_string(temp);\ assertCStringSame(_res, text);\ json_free(_res);\ } else {\ FAIL(std::string("CheckGetNoCase: ") + #parent + "[" + #locale + "]");\ } #else #define CheckGetNoCase(parent, locale, text) #endif CheckGet(test, JSON_TEXT("hi"), JSON_TEXT("world")); CheckGetNoCase(test, JSON_TEXT("HI"), JSON_TEXT("world")); CheckGet(test, JSON_TEXT("hello"), JSON_TEXT("mars")); CheckGetNoCase(test, JSON_TEXT("HELLO"), JSON_TEXT("mars")); CheckGet(test, JSON_TEXT("salut"), JSON_TEXT("france")); CheckGetNoCase(test, JSON_TEXT("SALUT"), JSON_TEXT("france")); assertNull(json_get(test, JSON_TEXT("meh"))); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertNull(json_get_nocase(test, JSON_TEXT("meh"))); #endif #endif #ifdef JSON_ITERATORS #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestInspectors.cpp - Iterators"); for(JSONNODE_ITERATOR it = json_begin(casted), end = json_end(casted); it != end; ++it){ json_char * _res = json_name(*it); assertCStringSame(_res, JSON_TEXT("")); json_free(_res); } #endif #endif #ifdef JSON_BINARY UnitTest::SetPrefix("TestInspectors.cpp - Binary"); json_set_binary(test, (const unsigned char *)"Hello World", 11); assertEquals(json_type(test), JSON_STRING); json_char * _res = json_as_string(test); assertCStringSame(_res, JSON_TEXT("SGVsbG8gV29ybGQ=")); json_free(_res); unsigned long i; if(char * bin = (char*)json_as_binary(test, &i)){ assertEquals(i, 11); char * terminated = (char*)std::memcpy(std::malloc(i + 1), bin, i); terminated[i] = '\0'; assertCStringEquals(terminated, "Hello World"); json_free(bin); std::free(terminated); } else { FAIL("as_binary failed"); } json_set_a(test, JSON_TEXT("Hello World")); assertEquals(json_type(test), JSON_STRING); _res = json_as_string(test); assertCStringSame(_res, JSON_TEXT("Hello World")); json_free(_res); #ifdef JSON_SAFE assertEquals(json_as_binary(test, &i), 0); assertEquals(i, 0); #endif #endif json_delete(test); #ifdef JSON_CASTABLE json_delete(casted); #endif #else JSONNode test = JSONNode(JSON_NULL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("")); assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); assertEquals(test.as_bool(), false); #endif test = 15.5f; assertEquals(test.type(), JSON_NUMBER); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("15.5")); #endif assertEquals(test.as_int(), 15); assertEquals(test.as_float(), 15.5f); #ifdef JSON_CASTABLE assertEquals(test.as_bool(), true); #endif test = 0.0f; assertEquals(test.type(), JSON_NUMBER); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("0")); #endif assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); #ifdef JSON_CASTABLE assertEquals(test.as_bool(), false); #endif test = true; assertEquals(test.type(), JSON_BOOL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("true")); assertEquals(test.as_int(), 1); assertEquals(test.as_float(), 1.0f); #endif assertEquals(test.as_bool(), true); test = false; assertEquals(test.type(), JSON_BOOL); #ifdef JSON_CASTABLE assertEquals(test.as_string(), JSON_TEXT("false")); assertEquals(test.as_int(), 0); assertEquals(test.as_float(), 0.0f); #endif assertEquals(test.as_bool(), false); #ifdef JSON_CASTABLE test.cast(JSON_NODE); #else test = JSONNode(JSON_NODE); #endif assertEquals(test.type(), JSON_NODE); assertEquals(test.size(), 0); test.push_back(JSONNode(JSON_TEXT("hi"), JSON_TEXT("world"))); test.push_back(JSONNode(JSON_TEXT("hello"), JSON_TEXT("mars"))); test.push_back(JSONNode(JSON_TEXT("salut"), JSON_TEXT("france"))); assertEquals(test.size(), 3); TestSuite::testParsingItself(test); #ifdef JSON_CASTABLE JSONNode casted = test.as_array(); #ifdef JSON_UNIT_TEST assertNotEquals(casted.internal, test.internal); #endif assertEquals(casted.type(), JSON_ARRAY); assertEquals(test.type(), JSON_NODE); assertEquals(test.size(), 3); assertEquals(casted.size(), 3); TestSuite::testParsingItself(casted); #endif UnitTest::SetPrefix("TestInspectors.cpp - Location"); try { #ifdef JSON_CASTABLE assertEquals(casted.at(0), JSON_TEXT("world")); assertEquals(casted.at(1), JSON_TEXT("mars")); assertEquals(casted.at(2), JSON_TEXT("france")); assertEquals(casted.at(0).name(), JSON_TEXT("")); assertEquals(casted.at(1).name(), JSON_TEXT("")); assertEquals(casted.at(2).name(), JSON_TEXT("")); #endif assertEquals(test.at(0), JSON_TEXT("world")); assertEquals(test.at(1), JSON_TEXT("mars")); assertEquals(test.at(2), JSON_TEXT("france")); assertEquals(test.at(0).name(), JSON_TEXT("hi")); assertEquals(test.at(1).name(), JSON_TEXT("hello")); assertEquals(test.at(2).name(), JSON_TEXT("salut")); } catch (std::out_of_range){ FAIL("exception caught"); } try { assertEquals(test.at(JSON_TEXT("hi")), JSON_TEXT("world")); assertEquals(test.at(JSON_TEXT("hello")), JSON_TEXT("mars")); assertEquals(test.at(JSON_TEXT("salut")), JSON_TEXT("france")); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertEquals(test.at_nocase(JSON_TEXT("SALUT")), JSON_TEXT("france")); assertEquals(test.at_nocase(JSON_TEXT("HELLO")), JSON_TEXT("mars")); assertEquals(test.at_nocase(JSON_TEXT("HI")), JSON_TEXT("world")); #endif } catch (std::out_of_range){ FAIL("exception caught"); } assertException(test.at(JSON_TEXT("meh")), std::out_of_range); #ifdef JSON_CASE_INSENSITIVE_FUNCTIONS assertException(test.at_nocase(JSON_TEXT("meh")), std::out_of_range); #endif assertEquals(test[JSON_TEXT("hi")], json_string(JSON_TEXT("world"))); assertEquals(test[JSON_TEXT("hello")], json_string(JSON_TEXT("mars"))); assertEquals(test[JSON_TEXT("salut")], json_string(JSON_TEXT("france"))); assertEquals(test[0], JSON_TEXT("world")); assertEquals(test[1], JSON_TEXT("mars")); assertEquals(test[2], JSON_TEXT("france")); #ifdef JSON_ITERATORS #ifdef JSON_CASTABLE UnitTest::SetPrefix("TestInspectors.cpp - Iterators"); for(JSONNode::iterator it = casted.begin(), end = casted.end(); it != end; ++it){ assertEquals((*it).name(), JSON_TEXT("")); } #endif #endif #ifdef JSON_BINARY UnitTest::SetPrefix("TestInspectors.cpp - Binary"); test.set_binary((const unsigned char *)"Hello World", 11); assertEquals(test.type(), JSON_STRING); assertEquals(test.as_string(), JSON_TEXT("SGVsbG8gV29ybGQ=")); assertEquals(test.as_binary(), "Hello World"); assertEquals(test.as_binary().size(), 11); test = JSON_TEXT("Hello World"); assertEquals(test.type(), JSON_STRING); assertEquals(test.as_string(), JSON_TEXT("Hello World")); #ifdef JSON_SAFE assertEquals(test.as_binary(), ""); #endif #endif #ifdef JSON_READ_PRIORITY //This is a regression test for a bug in at() json_string buffer(JSON_TEXT("{ \"myValue1\" : \"foo\", \"myValue2\" : \"bar\"}")); JSONNode current = libjson::parse(buffer); try { JSONNode & value1 = current[JSON_TEXT("myValue1")]; assertEquals(value1.as_string(), JSON_TEXT("foo")); JSONNode & value2 = current[JSON_TEXT("myValue2")]; assertEquals(value2.as_string(), JSON_TEXT("bar")); } catch (...){ assertTrue(false); } #endif #endif }