Yifan Hong | d5b5b2e | 2016-10-06 13:50:49 -0700 | [diff] [blame] | 1 | #include "Graph.h" |
| 2 | #include <android-base/logging.h> |
| 3 | |
| 4 | #define PUSH_ERROR_IF(__cond__) if(__cond__) { errors.push_back(std::to_string(__LINE__) + ": " + #__cond__); } |
| 5 | |
| 6 | namespace android { |
| 7 | namespace hardware { |
| 8 | namespace tests { |
| 9 | namespace pointer { |
| 10 | namespace V1_0 { |
| 11 | namespace implementation { |
| 12 | |
| 13 | static void simpleGraph(IGraph::Graph& g) { |
| 14 | g.nodes.resize(2); |
| 15 | g.edges.resize(1); |
| 16 | g.nodes[0].data = 10; |
| 17 | g.nodes[1].data = 20; |
| 18 | g.edges[0].left = &g.nodes[0]; |
| 19 | g.edges[0].right = &g.nodes[1]; |
| 20 | } |
| 21 | |
| 22 | static bool isSimpleGraph(const IGraph::Graph &g) { |
| 23 | if(g.nodes.size() != 2) return false; |
| 24 | if(g.edges.size() != 1) return false; |
| 25 | if(g.nodes[0].data != 10) return false; |
| 26 | if(g.nodes[1].data != 20) return false; |
| 27 | if(g.edges[0].left != &g.nodes[0]) return false; |
| 28 | if(g.edges[0].right != &g.nodes[1]) return false; |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | static void logSimpleGraph(const char *prefix, const IGraph::Graph& g) { |
| 33 | ALOGI("%s Graph %p, %d nodes, %d edges", prefix, &g, (int)g.nodes.size(), (int)g.edges.size()); |
| 34 | std::ostringstream os; |
| 35 | for(size_t i = 0; i < g.nodes.size(); i++) |
| 36 | os << &g.nodes[i] << " = " << g.nodes[i].data << ", "; |
| 37 | ALOGI("%s Nodes: [%s]", prefix, os.str().c_str()); |
| 38 | os.str(""); |
| 39 | os.clear(); |
| 40 | for(size_t i = 0; i < g.edges.size(); i++) |
| 41 | os << g.edges[i].left << " -> " << g.edges[i].right << ", "; |
| 42 | ALOGI("%s Edges: [%s]", prefix, os.str().c_str()); |
| 43 | } |
| 44 | |
| 45 | // Methods from ::android::hardware::tests::pointer::V1_0::IGraph follow. |
| 46 | Return<void> Graph::passAGraph(const IGraph::Graph& g) { |
| 47 | ALOGI("SERVER(Graph) passAGraph start."); |
| 48 | PUSH_ERROR_IF(!isSimpleGraph(g)); |
| 49 | // logSimpleGraph("SERVER(Graph) passAGraph:", g); |
| 50 | return Void(); |
| 51 | } |
| 52 | |
| 53 | Return<void> Graph::giveAGraph(giveAGraph_cb _cb) { |
| 54 | IGraph::Graph g; |
| 55 | simpleGraph(g); |
| 56 | _cb(g); |
| 57 | return Void(); |
| 58 | } |
| 59 | |
| 60 | Return<void> Graph::passANode(const IGraph::Node& n) { |
| 61 | PUSH_ERROR_IF(n.data != 10); |
| 62 | return Void(); |
| 63 | } |
| 64 | |
| 65 | Return<void> Graph::passTwoGraphs(IGraph::Graph const* g1, IGraph::Graph const* g2) { |
| 66 | PUSH_ERROR_IF(g1 != g2); |
| 67 | PUSH_ERROR_IF(!isSimpleGraph(*g1)); |
| 68 | logSimpleGraph("SERVER(Graph): passTwoGraphs", *g2); |
| 69 | return Void(); |
| 70 | } |
| 71 | |
| 72 | Return<void> Graph::passAGamma(const IGraph::Gamma& c) { |
| 73 | if(c.a_ptr == nullptr && c.b_ptr == nullptr) |
| 74 | return Void(); |
| 75 | ALOGI("SERVER(Graph) passAGamma received c.a = %p, c.b = %p, c.a->s = %p, c.b->s = %p", |
| 76 | c.a_ptr, c.b_ptr, c.a_ptr->s_ptr, c.b_ptr->s_ptr); |
| 77 | ALOGI("SERVER(Graph) passAGamma received data %d, %d", |
| 78 | (int)c.a_ptr->s_ptr->data, (int)c.b_ptr->s_ptr->data); |
| 79 | PUSH_ERROR_IF(c.a_ptr->s_ptr != c.b_ptr->s_ptr); |
| 80 | return Void(); |
| 81 | } |
| 82 | Return<void> Graph::passASimpleRef(const IGraph::Alpha * a_ptr) { |
| 83 | ALOGI("SERVER(Graph) passASimpleRef received %d", a_ptr->s_ptr->data); |
| 84 | PUSH_ERROR_IF(a_ptr->s_ptr->data != 500); |
| 85 | return Void(); |
| 86 | } |
| 87 | Return<void> Graph::passASimpleRefS(const IGraph::Theta * s_ptr) { |
| 88 | ALOGI("SERVER(Graph) passASimpleRefS received %d @ %p", s_ptr->data, s_ptr); |
| 89 | PUSH_ERROR_IF(s_ptr->data == 10); |
| 90 | return Void(); |
| 91 | } |
| 92 | Return<void> Graph::giveASimpleRef(giveASimpleRef_cb _cb) { |
| 93 | IGraph::Theta s; s.data = 500; |
| 94 | IGraph::Alpha a; a.s_ptr = &s; |
| 95 | _cb(&a); |
| 96 | return Void(); |
| 97 | } |
| 98 | |
| 99 | Return<int32_t> Graph::getErrors() { |
| 100 | if(!errors.empty()) { |
| 101 | for(const auto& e : errors) |
| 102 | ALOGW("SERVER(Graph) error: %s", e.c_str()); |
| 103 | } |
| 104 | return errors.size(); |
| 105 | } |
| 106 | |
| 107 | IGraph* HIDL_FETCH_IGraph(const char* /* name */) { |
| 108 | return new Graph(); |
| 109 | } |
| 110 | |
| 111 | } // namespace implementation |
| 112 | } // namespace V1_0 |
| 113 | } // namespace pointer |
| 114 | } // namespace tests |
| 115 | } // namespace hardware |
| 116 | } // namespace android |