[Orc] Add support for remote JITing to the ORC API.
[oota-llvm.git] / unittests / ExecutionEngine / Orc / RPCUtilsTest.cpp
1 //===----------- RPCUtilsTest.cpp - Unit tests the Orc RPC utils ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ExecutionEngine/Orc/RPCChannel.h"
11 #include "llvm/ExecutionEngine/Orc/RPCUtils.h"
12 #include "gtest/gtest.h"
13
14 #include <queue>
15
16 using namespace llvm;
17 using namespace llvm::orc;
18 using namespace llvm::orc::remote;
19
20 class QueueChannel : public RPCChannel {
21 public:
22   QueueChannel(std::queue<char> &Queue) : Queue(Queue) {}
23
24   std::error_code readBytes(char *Dst, unsigned Size) override {
25     while (Size--) {
26       *Dst++ = Queue.front();
27       Queue.pop();
28     }
29     return std::error_code();
30   }
31
32   std::error_code appendBytes(const char *Src, unsigned Size) override {
33     while (Size--)
34       Queue.push(*Src++);
35     return std::error_code();
36   }
37
38   std::error_code send() override { return std::error_code(); }
39
40 private:
41   std::queue<char> &Queue;
42 };
43
44 class DummyRPC : public testing::Test,
45                  public RPC<QueueChannel> {
46 public:
47   typedef Procedure<1, bool> Proc1;
48   typedef Procedure<2, int8_t,
49                        uint8_t,
50                        int16_t,
51                        uint16_t,
52                        int32_t,
53                        uint32_t,
54                        int64_t,
55                        uint64_t,
56                        bool,
57                        std::string,
58                        std::vector<int>> AllTheTypes;
59 };
60
61
62 TEST_F(DummyRPC, TestBasic) {
63   std::queue<char> Queue;
64   QueueChannel C(Queue);
65
66   {
67     // Make a call to Proc1.
68     auto EC = call<Proc1>(C, true);
69     EXPECT_FALSE(EC) << "Simple call over queue failed";
70   }
71
72   {
73     // Expect a call to Proc1.
74     auto EC = expect<Proc1>(C,
75                 [&](bool &B) {
76                   EXPECT_EQ(B, true)
77                     << "Bool serialization broken";
78                   return std::error_code();
79                 });
80     EXPECT_FALSE(EC) << "Simple expect over queue failed";
81   }
82 }
83
84 TEST_F(DummyRPC, TestSerialization) {
85   std::queue<char> Queue;
86   QueueChannel C(Queue);
87
88   {
89     // Make a call to Proc1.
90     std::vector<int> v({42, 7});
91     auto EC = call<AllTheTypes>(C,
92                                 -101,
93                                 250,
94                                 -10000,
95                                 10000,
96                                 -1000000000,
97                                 1000000000,
98                                 -10000000000,
99                                 10000000000,
100                                 true,
101                                 "foo",
102                                 v);
103     EXPECT_FALSE(EC) << "Big (serialization test) call over queue failed";
104   }
105
106   {
107     // Expect a call to Proc1.
108     auto EC = expect<AllTheTypes>(C,
109                 [&](int8_t &s8,
110                     uint8_t &u8,
111                     int16_t &s16,
112                     uint16_t &u16,
113                     int32_t &s32,
114                     uint32_t &u32,
115                     int64_t &s64,
116                     uint64_t &u64,
117                     bool &b,
118                     std::string &s,
119                     std::vector<int> &v) {
120
121                     EXPECT_EQ(s8, -101)
122                       << "int8_t serialization broken";
123                     EXPECT_EQ(u8, 250)
124                       << "uint8_t serialization broken";
125                     EXPECT_EQ(s16, -10000)
126                       << "int16_t serialization broken";
127                     EXPECT_EQ(u16, 10000)
128                       << "uint16_t serialization broken";
129                     EXPECT_EQ(s32, -1000000000)
130                       << "int32_t serialization broken";
131                     EXPECT_EQ(u32, 1000000000ULL)
132                       << "uint32_t serialization broken";
133                     EXPECT_EQ(s64, -10000000000)
134                       << "int64_t serialization broken";
135                     EXPECT_EQ(u64, 10000000000ULL)
136                       << "uint64_t serialization broken";
137                     EXPECT_EQ(b, true)
138                       << "bool serialization broken";
139                     EXPECT_EQ(s, "foo")
140                       << "std::string serialization broken";
141                     EXPECT_EQ(v, std::vector<int>({42, 7}))
142                       << "std::vector serialization broken";
143                     return std::error_code();
144                   });
145     EXPECT_FALSE(EC) << "Big (serialization test) call over queue failed";
146   }
147 }