<commit_before>/******************************************************************
*
* Round for C++
*
* Copyright (C) <NAME> 2014
*
* This is licensed under BSD-style license, see file COPYING.
*
******************************************************************/

#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>

#include <round/common/JSON.h>

using namespace std;
using namespace Round;

////////////////////////////////////////////////////////////
// For Array
////////////////////////////////////////////////////////////

BOOST_AUTO_TEST_CASE(JSONParseNumberArrayTest) {
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse("[0, 1, 2]"));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());
  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  BOOST_CHECK_EQUAL(jsonArray->size(), 3);
  
  for (int n=0; n<3; n++) {
    int value;
    BOOST_CHECK(jsonArray->getInteger(n, &value));
    BOOST_CHECK_EQUAL(value, n);
  }
}

BOOST_AUTO_TEST_CASE(JSONParseStringArrayTest) {
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse("[\"milk\", \"bread\", \"eggs\"]"));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());
  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  BOOST_CHECK_EQUAL(jsonArray->size(), 3);

  string value;

  BOOST_CHECK(jsonArray->getString(0, &value));
  BOOST_CHECK_EQUAL(value.compare("milk"), 0);

  BOOST_CHECK(jsonArray->getString(1, &value));
  BOOST_CHECK_EQUAL(value.compare("bread"), 0);

  BOOST_CHECK(jsonArray->getString(2, &value));
  BOOST_CHECK_EQUAL(value.compare("eggs"), 0);
}

BOOST_AUTO_TEST_CASE(JSONParseAStringWithSpaceArrayTest) {
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse("[\"name\", \"<NAME>\"]"));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());
  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  BOOST_CHECK_EQUAL(jsonArray->size(), 2);

  string value;

  BOOST_CHECK(jsonArray->getString(0, &value));
  BOOST_CHECK_EQUAL(value.compare("name"), 0);

  BOOST_CHECK(jsonArray->getString(1, &value));
  BOOST_CHECK_EQUAL(value.compare("<NAME>"), 0);
}

BOOST_AUTO_TEST_CASE(JSONParseArraySerialize) {
  const char *testString = "[\"milk\",\"bread\",\"eggs\"]";
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testString));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());
  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  string value;
  jsonArray->toJSONString(&value);
  //cout << value << endl;
  BOOST_CHECK_EQUAL(value.compare(testString), 0);
}

////////////////////////////////////////////////////////////
// For Dictionary
////////////////////////////////////////////////////////////

BOOST_AUTO_TEST_CASE(JSONParseDictionaryTest01) {
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse("{\"name\": \"<NAME>\", \"age\": 33}"));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isDictionary());
  JSONDictionary *jsonDict = dynamic_cast<JSONDictionary *>(jsonParser.getObject());
  BOOST_CHECK(jsonDict);
  BOOST_CHECK_EQUAL(jsonDict->size(), 2);
  string value;
  int intValue;
  BOOST_CHECK(jsonDict->get("name", &value));
  BOOST_CHECK_EQUAL(value.compare("<NAME>"), 0);
  BOOST_CHECK(jsonDict->get("age", &value));
  BOOST_CHECK_EQUAL(value.compare("33"), 0);
  BOOST_CHECK(jsonDict->get("age", &intValue));
  BOOST_CHECK_EQUAL(intValue, 33);
}

BOOST_AUTO_TEST_CASE(JSONParseDictionarySerialize) {
  const char *testString = "{\"age\":\"33\",\"name\":\"<NAME>\"}";
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testString));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isDictionary());
  JSONDictionary *jsonDict = dynamic_cast<JSONDictionary *>(jsonParser.getObject());
  BOOST_CHECK(jsonDict);
  string value;
  jsonDict->toJSONString(&value);
  //cout << value << endl;
  BOOST_CHECK_EQUAL(value.compare(testString), 0);
}
////////////////////////////////////////////////////////////
// For Dictionary in Array
////////////////////////////////////////////////////////////

BOOST_AUTO_TEST_CASE(JSONParseDictionaryInArrayTest01) {
  const char *testString = "[ {\"age\":\"33\",\"name\":\"<NAME>\"},{\"age\":\"31\",\"name\":\"<NAME>\"} ]";
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testString));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());

  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  BOOST_CHECK_EQUAL(jsonArray->size(), 2);

  const JSONDictionary *jsonDict;
  string strValue;
  int intValue;

  jsonDict = dynamic_cast<const JSONDictionary *>(jsonArray->getObject(0));
  BOOST_CHECK(jsonDict);
  BOOST_CHECK(jsonDict->get("name", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("<NAME>"), 0);
  BOOST_CHECK(jsonDict->get("age", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("33"), 0);
  BOOST_CHECK(jsonDict->get("age", &intValue));
  BOOST_CHECK_EQUAL(intValue, 33);

  jsonDict = dynamic_cast<const JSONDictionary *>(jsonArray->getObject(1));
  BOOST_CHECK(jsonDict);
  BOOST_CHECK(jsonDict->get("name", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("<NAME>"), 0);
  BOOST_CHECK(jsonDict->get("age", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("31"), 0);
  BOOST_CHECK(jsonDict->get("age", &intValue));
  BOOST_CHECK_EQUAL(intValue, 31);
}

BOOST_AUTO_TEST_CASE(JSONParseDictionaryInArrayTest02) {
  const char *testString = "[\n{\"age\":\"33\",\"name\":\"<NAME>\"},\n{\"age\":\"31\",\"name\":\"<NAME>\"}\n]";
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testString));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());

  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  BOOST_CHECK_EQUAL(jsonArray->size(), 2);

  const JSONDictionary *jsonDict;
  string strValue;
  int intValue;

  jsonDict = dynamic_cast<const JSONDictionary *>(jsonArray->getObject(0));
  BOOST_CHECK(jsonDict);
  BOOST_CHECK(jsonDict->get("name", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("<NAME>"), 0);
  BOOST_CHECK(jsonDict->get("age", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("33"), 0);
  BOOST_CHECK(jsonDict->get("age", &intValue));
  BOOST_CHECK_EQUAL(intValue, 33);

  jsonDict = dynamic_cast<const JSONDictionary *>(jsonArray->getObject(1));
  BOOST_CHECK(jsonDict);
  BOOST_CHECK(jsonDict->get("name", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("<NAME>"), 0);
  BOOST_CHECK(jsonDict->get("age", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("31"), 0);
  BOOST_CHECK(jsonDict->get("age", &intValue));
  BOOST_CHECK_EQUAL(intValue, 31);
}

BOOST_AUTO_TEST_CASE(JSONParseArrayInDictionaryTest01) {
  const char *testString = "{\"phoneNumber\": [\"212 555-1234\", \"646 555-4567\"]}";
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testString));

  string jsonString;

  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isDictionary());
  JSONDictionary *jsonDict = dynamic_cast<JSONDictionary *>(jsonParser.getObject());
  BOOST_CHECK(jsonDict);

  jsonDict->toJSONString(&jsonString);
  //std::cout << jsonDict << std::endl;
  BOOST_CHECK_EQUAL(jsonDict->size(), 1);

  BOOST_CHECK(jsonDict->hasKey("phoneNumber"));
  JSONObject *phoneNumberObj = NULL;
  BOOST_CHECK(jsonDict->get("phoneNumber", &phoneNumberObj));
  BOOST_CHECK(phoneNumberObj);
  JSONArray *phoneNumberArray = dynamic_cast<JSONArray *>(phoneNumberObj);
  BOOST_CHECK_EQUAL(phoneNumberArray->size(), 2);

  string value;

  BOOST_CHECK(phoneNumberArray->getString(0, &value));
  BOOST_CHECK_EQUAL(value.compare("212 555-1234"), 0);
  BOOST_CHECK(phoneNumberArray->getString(1, &value));
  BOOST_CHECK_EQUAL(value.compare("646 555-4567"), 0);
 }

BOOST_AUTO_TEST_CASE(RoundJSONDictionaryParserTest) {
  string testJSONValue = "{\"key\":\"value\"}";

  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testJSONValue));

  JSONObject *jsonValueObject = jsonParser.getObject();
  BOOST_CHECK(jsonValueObject);
  BOOST_CHECK(jsonValueObject->isDictionary());

  JSONDictionary *jsonValueDict = static_cast<JSONDictionary *>(jsonValueObject);
  std::string jsonString;
  jsonValueDict->toJSONString(&jsonString);
  BOOST_CHECK_EQUAL(jsonString.compare(testJSONValue), 0);
}


<commit_msg>* Added a test for JSON dictionay such as {"a":1}.<commit_after>/******************************************************************
*
* Round for C++
*
* Copyright (C) <NAME> 2014
*
* This is licensed under BSD-style license, see file COPYING.
*
******************************************************************/

#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>

#include <round/common/JSON.h>

using namespace std;
using namespace Round;

////////////////////////////////////////////////////////////
// For Array
////////////////////////////////////////////////////////////

BOOST_AUTO_TEST_CASE(JSONParseNumberArrayTest) {
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse("[0, 1, 2]"));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());
  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  BOOST_CHECK_EQUAL(jsonArray->size(), 3);
  
  for (int n=0; n<3; n++) {
    int value;
    BOOST_CHECK(jsonArray->getInteger(n, &value));
    BOOST_CHECK_EQUAL(value, n);
  }
}

BOOST_AUTO_TEST_CASE(JSONParseStringArrayTest) {
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse("[\"milk\", \"bread\", \"eggs\"]"));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());
  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  BOOST_CHECK_EQUAL(jsonArray->size(), 3);

  string value;

  BOOST_CHECK(jsonArray->getString(0, &value));
  BOOST_CHECK_EQUAL(value.compare("milk"), 0);

  BOOST_CHECK(jsonArray->getString(1, &value));
  BOOST_CHECK_EQUAL(value.compare("bread"), 0);

  BOOST_CHECK(jsonArray->getString(2, &value));
  BOOST_CHECK_EQUAL(value.compare("eggs"), 0);
}

BOOST_AUTO_TEST_CASE(JSONParseAStringWithSpaceArrayTest) {
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse("[\"name\", \"<NAME>\"]"));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());
  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  BOOST_CHECK_EQUAL(jsonArray->size(), 2);

  string value;

  BOOST_CHECK(jsonArray->getString(0, &value));
  BOOST_CHECK_EQUAL(value.compare("name"), 0);

  BOOST_CHECK(jsonArray->getString(1, &value));
  BOOST_CHECK_EQUAL(value.compare("<NAME>"), 0);
}

BOOST_AUTO_TEST_CASE(JSONParseArraySerialize) {
  const char *testString = "[\"milk\",\"bread\",\"eggs\"]";
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testString));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());
  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  string value;
  jsonArray->toJSONString(&value);
  //cout << value << endl;
  BOOST_CHECK_EQUAL(value.compare(testString), 0);
}

////////////////////////////////////////////////////////////
// For Dictionary
////////////////////////////////////////////////////////////

BOOST_AUTO_TEST_CASE(JSONParseNumberDictionaryTest) {
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse("{\"a\": 1, \"b\": 2}"));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isDictionary());
  JSONDictionary *jsonDict = dynamic_cast<JSONDictionary *>(jsonParser.getObject());
  BOOST_CHECK(jsonDict);
  BOOST_CHECK_EQUAL(jsonDict->size(), 2);
  string value;
  int intValue;
  BOOST_CHECK(jsonDict->get("a", &intValue));
  BOOST_CHECK_EQUAL(intValue, 1);
  BOOST_CHECK(jsonDict->get("b", &intValue));
  BOOST_CHECK_EQUAL(intValue, 2);
}

BOOST_AUTO_TEST_CASE(JSONParseStringDictionaryTest) {
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse("{\"name\": \"<NAME>\", \"age\": 33}"));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isDictionary());
  JSONDictionary *jsonDict = dynamic_cast<JSONDictionary *>(jsonParser.getObject());
  BOOST_CHECK(jsonDict);
  BOOST_CHECK_EQUAL(jsonDict->size(), 2);
  string value;
  int intValue;
  BOOST_CHECK(jsonDict->get("name", &value));
  BOOST_CHECK_EQUAL(value.compare("<NAME>"), 0);
  BOOST_CHECK(jsonDict->get("age", &value));
  BOOST_CHECK_EQUAL(value.compare("33"), 0);
  BOOST_CHECK(jsonDict->get("age", &intValue));
  BOOST_CHECK_EQUAL(intValue, 33);
}

BOOST_AUTO_TEST_CASE(JSONParseDictionarySerialize) {
  const char *testString = "{\"age\":\"33\",\"name\":\"<NAME>\"}";
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testString));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isDictionary());
  JSONDictionary *jsonDict = dynamic_cast<JSONDictionary *>(jsonParser.getObject());
  BOOST_CHECK(jsonDict);
  string value;
  jsonDict->toJSONString(&value);
  //cout << value << endl;
  BOOST_CHECK_EQUAL(value.compare(testString), 0);
}
////////////////////////////////////////////////////////////
// For Dictionary in Array
////////////////////////////////////////////////////////////

BOOST_AUTO_TEST_CASE(JSONParseDictionaryInArrayTest01) {
  const char *testString = "[ {\"age\":\"33\",\"name\":\"<NAME>\"},{\"age\":\"31\",\"name\":\"<NAME>\"} ]";
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testString));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());

  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  BOOST_CHECK_EQUAL(jsonArray->size(), 2);

  const JSONDictionary *jsonDict;
  string strValue;
  int intValue;

  jsonDict = dynamic_cast<const JSONDictionary *>(jsonArray->getObject(0));
  BOOST_CHECK(jsonDict);
  BOOST_CHECK(jsonDict->get("name", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("<NAME>"), 0);
  BOOST_CHECK(jsonDict->get("age", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("33"), 0);
  BOOST_CHECK(jsonDict->get("age", &intValue));
  BOOST_CHECK_EQUAL(intValue, 33);

  jsonDict = dynamic_cast<const JSONDictionary *>(jsonArray->getObject(1));
  BOOST_CHECK(jsonDict);
  BOOST_CHECK(jsonDict->get("name", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("<NAME>"), 0);
  BOOST_CHECK(jsonDict->get("age", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("31"), 0);
  BOOST_CHECK(jsonDict->get("age", &intValue));
  BOOST_CHECK_EQUAL(intValue, 31);
}

BOOST_AUTO_TEST_CASE(JSONParseDictionaryInArrayTest02) {
  const char *testString = "[\n{\"age\":\"33\",\"name\":\"<NAME>\"},\n{\"age\":\"31\",\"name\":\"<NAME>\"}\n]";
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testString));
  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isArray());

  JSONArray *jsonArray = dynamic_cast<JSONArray *>(jsonParser.getObject());
  BOOST_CHECK(jsonArray);
  BOOST_CHECK_EQUAL(jsonArray->size(), 2);

  const JSONDictionary *jsonDict;
  string strValue;
  int intValue;

  jsonDict = dynamic_cast<const JSONDictionary *>(jsonArray->getObject(0));
  BOOST_CHECK(jsonDict);
  BOOST_CHECK(jsonDict->get("name", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("<NAME>"), 0);
  BOOST_CHECK(jsonDict->get("age", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("33"), 0);
  BOOST_CHECK(jsonDict->get("age", &intValue));
  BOOST_CHECK_EQUAL(intValue, 33);

  jsonDict = dynamic_cast<const JSONDictionary *>(jsonArray->getObject(1));
  BOOST_CHECK(jsonDict);
  BOOST_CHECK(jsonDict->get("name", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("<NAME>"), 0);
  BOOST_CHECK(jsonDict->get("age", &strValue));
  BOOST_CHECK_EQUAL(strValue.compare("31"), 0);
  BOOST_CHECK(jsonDict->get("age", &intValue));
  BOOST_CHECK_EQUAL(intValue, 31);
}

BOOST_AUTO_TEST_CASE(JSONParseArrayInDictionaryTest01) {
  const char *testString = "{\"phoneNumber\": [\"212 555-1234\", \"646 555-4567\"]}";
  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testString));

  string jsonString;

  BOOST_CHECK(jsonParser.getObject());
  BOOST_CHECK(jsonParser.getObject()->isDictionary());
  JSONDictionary *jsonDict = dynamic_cast<JSONDictionary *>(jsonParser.getObject());
  BOOST_CHECK(jsonDict);

  jsonDict->toJSONString(&jsonString);
  //std::cout << jsonDict << std::endl;
  BOOST_CHECK_EQUAL(jsonDict->size(), 1);

  BOOST_CHECK(jsonDict->hasKey("phoneNumber"));
  JSONObject *phoneNumberObj = NULL;
  BOOST_CHECK(jsonDict->get("phoneNumber", &phoneNumberObj));
  BOOST_CHECK(phoneNumberObj);
  JSONArray *phoneNumberArray = dynamic_cast<JSONArray *>(phoneNumberObj);
  BOOST_CHECK_EQUAL(phoneNumberArray->size(), 2);

  string value;

  BOOST_CHECK(phoneNumberArray->getString(0, &value));
  BOOST_CHECK_EQUAL(value.compare("212 555-1234"), 0);
  BOOST_CHECK(phoneNumberArray->getString(1, &value));
  BOOST_CHECK_EQUAL(value.compare("646 555-4567"), 0);
 }

BOOST_AUTO_TEST_CASE(RoundJSONDictionaryParserTest) {
  string testJSONValue = "{\"key\":\"value\"}";

  JSONParser jsonParser;
  BOOST_CHECK(jsonParser.parse(testJSONValue));

  JSONObject *jsonValueObject = jsonParser.getObject();
  BOOST_CHECK(jsonValueObject);
  BOOST_CHECK(jsonValueObject->isDictionary());

  JSONDictionary *jsonValueDict = static_cast<JSONDictionary *>(jsonValueObject);
  std::string jsonString;
  jsonValueDict->toJSONString(&jsonString);
  BOOST_CHECK_EQUAL(jsonString.compare(testJSONValue), 0);
}



{'fraction_non_alphanumeric': 0.2173644629026827, 'fraction_numerical': 0.014591985632506453, 'mean_word_length': 5.189301840916985, 'pattern_counts': {'":': 31, '<': 94, '<?xml version=': 0, '>': 174, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3082361', 'n_tokens_mistral': 6016, 'n_tokens_neox': 5572, 'n_words': 803}