Commit c793d3f1 authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz
Browse files

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.
parent a8fd188d
......@@ -42,6 +42,7 @@ namespace spot
template <typename Data, bool boxed = !std::is_class<Data>::value>
struct boxed_label
{
typedef Data data_t;
Data label;
template <typename... Args>
......@@ -68,13 +69,26 @@ namespace spot
};
template <>
struct boxed_label<void, true>
struct boxed_label<void, true>: public std::tuple<>
{
typedef std::tuple<> data_t;
std::tuple<>& data()
{
return *this;
}
const std::tuple<>& data() const
{
return *this;
}
};
template <typename Data>
struct boxed_label<Data, false>: public Data
{
typedef Data data_t;
template <typename... Args>
boxed_label(Args&&... args):
Data{std::forward<Args>(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();
......
......@@ -20,6 +20,7 @@
#include <iostream>
#include "graph/ngraph.hh"
#include "tgba/state.hh"
template <typename SL, typename TL>
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<const my_state*>(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<const char*>(this) - static_cast<const char*>(nullptr);
}
virtual my_state*
clone() const
{
return const_cast<my_state*>(this);
}
virtual void destroy() const
{
}
friend std::ostream& operator<<(std::ostream& os, const my_state&)
{
return os;
}
};
bool f9()
{
typedef spot::digraph<my_state, int_pair> graph_t;
graph_t g(3);
spot::named_graph<graph_t, std::string> 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);
}
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment