<commit_before>#include "BoundingBox.hpp"
#include "formats/osm/xml/OsmXmlParser.hpp"
#include "config.hpp"

#include <fstream>
#include <boost/test/unit_test.hpp>

using namespace utymap::formats;

namespace {
    struct Formats_Osm_Xml_OsmXmlParserFixture
    {
        Formats_Osm_Xml_OsmXmlParserFixture() :
            istream(TEST_XML_FILE, std::ios::in)
        {
        }

        OsmXmlParser<CountableOsmDataVisitor> parser;
        CountableOsmDataVisitor visitor;
        std::ifstream istream;
    };
}

BOOST_FIXTURE_TEST_SUITE(Formats_Osm_Xml_Parser, Formats_Osm_Xml_OsmXmlParserFixture)

BOOST_AUTO_TEST_CASE(GivenDefaultXml_WhenParserParse_ThenHasExpectedElementCount)
{
    parser.parse(istream, visitor);

    BOOST_CHECK_EQUAL(1, visitor.bounds);
    BOOST_CHECK_EQUAL(7653, visitor.nodes);
    BOOST_CHECK_EQUAL(1116, visitor.ways);
    BOOST_CHECK_EQUAL(92, visitor.relations);
}

BOOST_AUTO_TEST_SUITE_END()<commit_msg>core: add unit test for checking osm xml node parsing<commit_after>#include "BoundingBox.hpp"
#include "entities/Node.hpp"
#include "formats/osm/xml/OsmXmlParser.hpp"

#include "config.hpp"
#include "test_utils/ElementUtils.hpp"
#include "test_utils/DependencyProvider.hpp"

#include <fstream>
#include <numeric>
#include <boost/test/unit_test.hpp>

using namespace utymap::entities;
using namespace utymap::formats;

namespace {

    //typedef std::vector<std::pair<std::string, std::string>> RawTags;

    struct Formats_Osm_Xml_OsmXmlParserFixture
    {
        utymap::tests::DependencyProvider dependencyProvider;

        utymap::entities::Tag createTag(const std::string& key, const std::string& value)
        {
            return utymap::tests::ElementUtils::createTag(*dependencyProvider.getStringTable(), key, value);
        }
    };

    template <typename Iterator>
    bool reduce(Iterator begin, Iterator end)
    {
        return std::accumulate(begin, end, true, [](bool a, bool b) {
            return a && b;
        });
    }

    void assertTags(const std::vector<utymap::entities::Tag> expectedTags,
        const std::vector<utymap::entities::Tag> actualTags)
    {
        BOOST_CHECK_EQUAL(expectedTags.size(), actualTags.size());
        for (int i = 0; i < actualTags.size(); ++i)
        {
            BOOST_CHECK_EQUAL(expectedTags.at(i).key, actualTags.at(i).key);
            BOOST_CHECK_EQUAL(expectedTags.at(i).value, actualTags.at(i).value);
        }
    }

    void assertNode(const Node& node,
        const utymap::GeoCoordinate coordinate,
        const std::vector<utymap::entities::Tag> tags)
    {
        BOOST_CHECK_EQUAL(coordinate.latitude, node.coordinate.latitude);
        BOOST_CHECK_EQUAL(coordinate.longitude, node.coordinate.longitude);
        assertTags(tags, node.tags);
    }
}

BOOST_FIXTURE_TEST_SUITE(Formats_Osm_Xml_Parser, Formats_Osm_Xml_OsmXmlParserFixture)

BOOST_AUTO_TEST_CASE(GivenDefaultXml_WhenParserParse_ThenHasExpectedElementCount)
{
    std::ifstream istream(TEST_XML_FILE, std::ios::in);
    OsmXmlParser<CountableOsmDataVisitor> parser;
    CountableOsmDataVisitor visitor;

    parser.parse(istream, visitor);

    BOOST_CHECK_EQUAL(1, visitor.bounds);
    BOOST_CHECK_EQUAL(7653, visitor.nodes);
    BOOST_CHECK_EQUAL(1116, visitor.ways);
    BOOST_CHECK_EQUAL(92, visitor.relations);
}

BOOST_AUTO_TEST_CASE(GivenTestDummyXml_WhenParserParse_ThenHasProperNodes)
{
    std::vector<bool> checkList { false, false };
    std::ifstream istream(TEST_OSM_DUMMY_FILE, std::ios::in);
    OsmXmlParser<OsmDataVisitor> parser;
    OsmDataVisitor visitor(*dependencyProvider.getStringTable(), [&](Element& element) {
        if (Node* node = dynamic_cast<Node*>(&element)) {
            if (node->id == 42421951) {
                assertNode(*node, utymap::GeoCoordinate(40.7033242, -74.0077787), { createTag("highway","traffic_signals") });
                checkList[0] = true;
            }
            else if (node->id == 42424605) {
                assertNode(*node, utymap::GeoCoordinate(40.8142100, -73.9341897), {});
                checkList[1] = true;
            }
        }
        return true;
    });

    parser.parse(istream, visitor);
    visitor.complete();

    BOOST_CHECK(reduce(checkList.begin(), checkList.end()));
}

BOOST_AUTO_TEST_SUITE_END()
{'fraction_non_alphanumeric': 0.11939252336448598, 'fraction_numerical': 0.017990654205607477, 'mean_word_length': 3.875854214123007, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, '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': '125654', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1394, 'n_words': 210}