From c793d3f13a613a83ea1748c6783f5b6856ae7077 Mon Sep 17 00:00:00 2001 From: Alexandre Duret-Lutz Date: Mon, 17 Feb 2014 15:36:22 +0100 Subject: [PATCH] graph: Add a test close to the tgba_explicit_string setup. * src/graph/graph.hh (boxed_label): If State_Data==void, inherit from std::tuple<> and implement a data() method. (digraph::state_data): Return by reference. * src/graphtest/ngraph.cc, src/graphtest/ngraph.test: Test the case where State_Data implements the spot::state interface. --- src/graph/graph.hh | 23 +++++++++--- src/graphtest/ngraph.cc | 75 +++++++++++++++++++++++++++++++++++++-- src/graphtest/ngraph.test | 11 +++++- 3 files changed, 102 insertions(+), 7 deletions(-) diff --git a/src/graph/graph.hh b/src/graph/graph.hh index 17c1356cc..7e46abf77 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -42,6 +42,7 @@ namespace spot template ::value> struct boxed_label { + typedef Data data_t; Data label; template @@ -68,13 +69,26 @@ namespace spot }; template <> - struct boxed_label + struct boxed_label: public std::tuple<> { + typedef std::tuple<> data_t; + std::tuple<>& data() + { + return *this; + } + + const std::tuple<>& data() const + { + return *this; + } + }; template struct boxed_label: public Data { + typedef Data data_t; + template boxed_label(Args&&... args): Data{std::forward(args)...} @@ -302,15 +316,16 @@ namespace spot return s; } - // May not be called on states with no data. - State_Data + // Do not use State_Data& as return type, because State_Data might + // be void. + typename state_storage_t::data_t& state_data(state s) { return states_[s].data(); } // May not be called on states with no data. - State_Data + const typename state_storage_t::data_t& state_data(state s) const { return states_[s].data(); diff --git a/src/graphtest/ngraph.cc b/src/graphtest/ngraph.cc index 70e324887..62542577b 100644 --- a/src/graphtest/ngraph.cc +++ b/src/graphtest/ngraph.cc @@ -20,6 +20,7 @@ #include #include "graph/ngraph.hh" +#include "tgba/state.hh" template void @@ -339,6 +340,74 @@ bool f8() return f == 69; } +struct my_state: public spot::state +{ +public: + virtual ~my_state() noexcept + { + } + + virtual int compare(const spot::state* other) const + { + auto o = down_cast(other); + assert(o); + + // Do not simply return "other - this", it might not fit in an int. + if (o < this) + return -1; + if (o > this) + return 1; + return 0; + } + + virtual size_t hash() const + { + return + reinterpret_cast(this) - static_cast(nullptr); + } + + virtual my_state* + clone() const + { + return const_cast(this); + } + + virtual void destroy() const + { + } + + friend std::ostream& operator<<(std::ostream& os, const my_state&) + { + return os; + } +}; + +bool f9() +{ + typedef spot::digraph graph_t; + graph_t g(3); + spot::named_graph gg(g); + auto s1 = gg.new_state("s1"); + gg.new_state("s2"); + gg.new_state("s3"); + gg.new_transition("s1", "s2", 1, 3); + gg.new_transition("s1", "s3", 2, 5); + gg.new_transition("s2", "s3", 3, 7); + gg.new_transition("s3", "s2", 4, 9); + + dot(std::cout, gg); + + int f = 0; + for (auto& t: g.out(s1)) + { + f += t.one + t.two; + } + + + return (f == 11) && + g.state_data(s1).compare(&g.state_data(gg.get_state("s1"))) == 0 && + g.state_data(s1).compare(&g.state_data(gg.get_state("s2"))) != 0; +} int main() { @@ -350,6 +419,7 @@ int main() bool a6 = f6(); bool a7 = f7(); bool a8 = f8(); + bool a9 = f9(); std::cout << a1 << ' ' << a2 << ' ' << a3 << ' ' @@ -357,6 +427,7 @@ int main() << a5 << ' ' << a6 << ' ' << a7 << ' ' - << a8 << '\n'; - return !(a1 && a2 && a3 && a4 && a5 && a6 && a7 && a8); + << a8 << ' ' + << a9 << '\n'; + return !(a1 && a2 && a3 && a4 && a5 && a6 && a7 && a8 && a9); } diff --git a/src/graphtest/ngraph.test b/src/graphtest/ngraph.test index 1ce2c6206..900534820 100755 --- a/src/graphtest/ngraph.test +++ b/src/graphtest/ngraph.test @@ -80,7 +80,16 @@ digraph { 2 [label="s3\n(4,8)"] 2 -> 1 [label="(4,9)"] } -1 1 1 1 1 1 1 1 +digraph { + 0 [label="s1\n"] + 0 -> 1 [label="(1,3)"] + 0 -> 2 [label="(2,5)"] + 1 [label="s2\n"] + 1 -> 2 [label="(3,7)"] + 2 [label="s3\n"] + 2 -> 1 [label="(4,9)"] +} +1 1 1 1 1 1 1 1 1 EOF diff stdout expected