From 92aa235a269a2fce3de2307a2c8fbeb4b4a535aa Mon Sep 17 00:00:00 2001 From: florijan <florijan@memgraph.io> Date: Thu, 2 Mar 2017 08:54:33 +0100 Subject: [PATCH] traversal/test.hpp contents moved to tests/unit/traversal.cpp Summary: See above. Reviewers: buda Reviewed By: buda Subscribers: pullbot, florijan, buda Differential Revision: https://phabricator.memgraph.io/D82 --- src/traversal/test.hpp | 195 --------------------------------------- tests/unit/traversal.cpp | 188 ++++++++++++++++++++++++++++++++++++- 2 files changed, 187 insertions(+), 196 deletions(-) delete mode 100644 src/traversal/test.hpp diff --git a/src/traversal/test.hpp b/src/traversal/test.hpp deleted file mode 100644 index af71248d0..000000000 --- a/src/traversal/test.hpp +++ /dev/null @@ -1,195 +0,0 @@ -// -// Copyright 2017 Memgraph -// Created by Florijan Stamenkovic on 20.02.17. -// - -#pragma once - -#include <iostream> -#include <list> -#include <memory> - -#include "enums.hpp" -#include "path.hpp" -#include "templates.hpp" - -/** - * A specialization of the "traversal" namespace that uses - * test Vertex and Edge classes (as opposed to the real database - * ones). - */ -namespace traversal_test { - -// add the standard enums to this namespace -using Direction = traversal_template::Direction; -using Expansion = traversal_template::Expansion; -using Uniqueness = traversal_template::Uniqueness; - -// forward declaring Edge because Vertex holds references to it -class Edge; - -/** - * A Vertex class for traversal testing. Has only the - * traversal capabilities and an exposed id_ member. - */ -class Vertex { - friend class Edge; - -public: - // unique identifier (uniqueness guaranteed by this class) - const int id_; - - // vertex set by the user, for testing purposes - const int label_; - - Vertex(int label=0) : id_(counter_++), label_(label) {} - - const auto &in() const { return *in_; } - const auto &out() const { return *out_; } - - friend std::ostream &operator<<(std::ostream &stream, const Vertex &v) { - return stream << "(" << v.id_ << ")"; - } - - bool operator==(const Vertex &v) const { return this->id_ == v.id_; } - bool operator!=(const Vertex &v) const { return !(*this == v); } - -private: - // shared pointers to outgoing and incoming edges are used so that when - // a Vertex is copied it still points to the same connectivity storage - std::shared_ptr<std::vector<Edge>> in_ = std::make_shared<std::vector<Edge>>(); - std::shared_ptr<std::vector<Edge>> out_ = std::make_shared<std::vector<Edge>>(); - - // Vertex counter, used for generating IDs - static int counter_; - -}; - -int Vertex::counter_ = 0; - -/** - * An Edge class for traversal testing. Has only traversal capabilities - * and an exposed id_ member. - */ -class Edge { -public: - // unique identifier (uniqueness guaranteed by this class) - const int id_; - - // vertex set by the user, for testing purposes - const int type_; - - Edge(const Vertex &from, const Vertex &to, int type=0) : - id_(counter_++), from_(from), to_(to), type_(type) { - from.out_->emplace_back(*this); - to.in_->emplace_back(*this); - } - - const Vertex &from() const { return from_; } - const Vertex &to() const { return to_; } - - bool operator==(const Edge &e) const { return id_ == e.id_; } - bool operator!=(const Edge &e) const { return !(*this == e); } - - friend std::ostream &operator<<(std::ostream &stream, const Edge &e) { - return stream << "[" << e.id_ << "]"; - } - -private: - const Vertex &from_; - const Vertex &to_; - - // Edge counter, used for generating IDs - static int counter_; -}; - -int Edge::counter_ = 0; - -// expose Path and Paths as class template instantiations -using Path = traversal_template::Path<Vertex, Edge>; -using Paths = traversal_template::Paths<Vertex, Edge>; - -/** - * Specialization of the traversal_template::Begin function. - */ -template<typename TCollection> -auto Begin(const TCollection &vertices, std::function<bool(const Vertex &)> vertex_filter = {}) { - return traversal_template::Begin<TCollection, Vertex, Edge>(vertices, vertex_filter); -} - -/** - * Specialization of the traversal_template::Cartesian function that accepts - * a single argument. - */ -template<typename TVisitable> -auto Cartesian(TVisitable &&visitable) { - return traversal_template::Cartesian<TVisitable, Vertex, Edge>( - std::forward<TVisitable>(visitable)); -} - - -/** - * Specialization of the traversal_template::Cartesian function that accepts - * multiple arguments. - */ - // TODO (code-review) can this be specialized more elegantly? -template<typename TVisitableFirst, typename... TVisitableOthers> -auto Cartesian(TVisitableFirst &&first, TVisitableOthers &&... others) { - return traversal_template::CartesianBinaryType<TVisitableFirst, decltype(Cartesian( - std::forward<TVisitableOthers>(others)...)), - Vertex, Edge>( - std::forward<TVisitableFirst>(first), - Cartesian(std::forward<TVisitableOthers>(others)...) - ); -} -} - -/** - * Hash calculations for text vertex, edge, Path and Paths. - * Only for testing purposes. - */ -namespace std{ - -template <> -class hash<traversal_test::Vertex> { -public: - size_t operator()(const traversal_test::Vertex &vertex) const { - return (size_t) vertex.id_; - } -}; - -template <> -class hash<traversal_test::Edge> { -public: - size_t operator()(const traversal_test::Edge &edge) const { - return (size_t) edge.id_; - } -}; - -template <> -class hash<traversal_test::Path> { -public: - std::size_t operator()(const traversal_test::Path &path) const { - std::size_t r_val = 0; - - for (const auto &vertex : path.Vertices()) - r_val ^= std::hash<traversal_test::Vertex>{}(vertex); - - return r_val; - } -}; - -template <> -class hash<traversal_test::Paths> { -public: - std::size_t operator()(const traversal_test::Paths &paths) const { - std::size_t r_val = 0; - - for (const auto &path : paths) - r_val ^= std::hash<traversal_test::Path>{}(path); - - return r_val; - } -}; -} - diff --git a/tests/unit/traversal.cpp b/tests/unit/traversal.cpp index d97d07e44..e480764f6 100644 --- a/tests/unit/traversal.cpp +++ b/tests/unit/traversal.cpp @@ -6,10 +6,196 @@ #include <vector> #include <list> #include <unordered_set> +#include <iostream> +#include <list> +#include <memory> #include "gtest/gtest.h" -#include "traversal/test.hpp" +#include "traversal/enums.hpp" +#include "traversal/path.hpp" +#include "traversal/templates.hpp" + +/** + * A specialization of the "traversal" namespace that uses + * test Vertex and Edge classes (as opposed to the real database + * ones). + */ +namespace traversal_test { + +// add the standard enums to this namespace +using Direction = traversal_template::Direction; +using Expansion = traversal_template::Expansion; +using Uniqueness = traversal_template::Uniqueness; + +// forward declaring Edge because Vertex holds references to it +class Edge; + +/** + * A Vertex class for traversal testing. Has only the + * traversal capabilities and an exposed id_ member. + */ +class Vertex { + friend class Edge; + +public: + // unique identifier (uniqueness guaranteed by this class) + const int id_; + + // vertex set by the user, for testing purposes + const int label_; + + Vertex(int label=0) : id_(counter_++), label_(label) {} + + const auto &in() const { return *in_; } + const auto &out() const { return *out_; } + + friend std::ostream &operator<<(std::ostream &stream, const Vertex &v) { + return stream << "(" << v.id_ << ")"; + } + + bool operator==(const Vertex &v) const { return this->id_ == v.id_; } + bool operator!=(const Vertex &v) const { return !(*this == v); } + +private: + // shared pointers to outgoing and incoming edges are used so that when + // a Vertex is copied it still points to the same connectivity storage + std::shared_ptr<std::vector<Edge>> in_ = std::make_shared<std::vector<Edge>>(); + std::shared_ptr<std::vector<Edge>> out_ = std::make_shared<std::vector<Edge>>(); + + // Vertex counter, used for generating IDs + static int counter_; + +}; + +int Vertex::counter_ = 0; + +/** + * An Edge class for traversal testing. Has only traversal capabilities + * and an exposed id_ member. + */ +class Edge { +public: + // unique identifier (uniqueness guaranteed by this class) + const int id_; + + // vertex set by the user, for testing purposes + const int type_; + + Edge(const Vertex &from, const Vertex &to, int type=0) : + id_(counter_++), from_(from), to_(to), type_(type) { + from.out_->emplace_back(*this); + to.in_->emplace_back(*this); + } + + const Vertex &from() const { return from_; } + const Vertex &to() const { return to_; } + + bool operator==(const Edge &e) const { return id_ == e.id_; } + bool operator!=(const Edge &e) const { return !(*this == e); } + + friend std::ostream &operator<<(std::ostream &stream, const Edge &e) { + return stream << "[" << e.id_ << "]"; + } + +private: + const Vertex &from_; + const Vertex &to_; + + // Edge counter, used for generating IDs + static int counter_; +}; + +int Edge::counter_ = 0; + +// expose Path and Paths as class template instantiations +using Path = traversal_template::Path<Vertex, Edge>; +using Paths = traversal_template::Paths<Vertex, Edge>; + +/** + * Specialization of the traversal_template::Begin function. + */ +template<typename TCollection> +auto Begin(const TCollection &vertices, std::function<bool(const Vertex &)> vertex_filter = {}) { + return traversal_template::Begin<TCollection, Vertex, Edge>(vertices, vertex_filter); +} + +/** + * Specialization of the traversal_template::Cartesian function that accepts + * a single argument. + */ +template<typename TVisitable> +auto Cartesian(TVisitable &&visitable) { + return traversal_template::Cartesian<TVisitable, Vertex, Edge>( + std::forward<TVisitable>(visitable)); +} + + +/** + * Specialization of the traversal_template::Cartesian function that accepts + * multiple arguments. + */ +// TODO (code-review) can this be specialized more elegantly? +template<typename TVisitableFirst, typename... TVisitableOthers> +auto Cartesian(TVisitableFirst &&first, TVisitableOthers &&... others) { + return traversal_template::CartesianBinaryType<TVisitableFirst, decltype(Cartesian( + std::forward<TVisitableOthers>(others)...)), + Vertex, Edge>( + std::forward<TVisitableFirst>(first), + Cartesian(std::forward<TVisitableOthers>(others)...) + ); +} +} + +/** + * Hash calculations for text vertex, edge, Path and Paths. + * Only for testing purposes. + */ +namespace std{ + +template <> +class hash<traversal_test::Vertex> { +public: + size_t operator()(const traversal_test::Vertex &vertex) const { + return (size_t) vertex.id_; + } +}; + +template <> +class hash<traversal_test::Edge> { +public: + size_t operator()(const traversal_test::Edge &edge) const { + return (size_t) edge.id_; + } +}; + +template <> +class hash<traversal_test::Path> { +public: + std::size_t operator()(const traversal_test::Path &path) const { + std::size_t r_val = 0; + + for (const auto &vertex : path.Vertices()) + r_val ^= std::hash<traversal_test::Vertex>{}(vertex); + + return r_val; + } +}; + +template <> +class hash<traversal_test::Paths> { +public: + std::size_t operator()(const traversal_test::Paths &paths) const { + std::size_t r_val = 0; + + for (const auto &path : paths) + r_val ^= std::hash<traversal_test::Path>{}(path); + + return r_val; + } +}; +} + /** * Hash for a list of edges, used in tests.