test-util.h 9.94 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
Kenton Varda's avatar
Kenton Varda committed
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
Kenton Varda's avatar
Kenton Varda committed
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
Kenton Varda's avatar
Kenton Varda committed
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Kenton Varda's avatar
Kenton Varda committed
21

Kenton Varda's avatar
Kenton Varda committed
22 23
#ifndef CAPNP_TEST_UTIL_H_
#define CAPNP_TEST_UTIL_H_
Kenton Varda's avatar
Kenton Varda committed
24

25
#if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
26 27 28
#pragma GCC system_header
#endif

29
#include <capnp/test.capnp.h>
Kenton Varda's avatar
Kenton Varda committed
30 31
#include <iostream>
#include "blob.h"
32
#include <kj/compat/gtest.h>
Kenton Varda's avatar
Kenton Varda committed
33

34 35 36 37
#if !CAPNP_LITE
#include "dynamic.h"
#endif  // !CAPNP_LITE

38 39
// TODO(cleanup): Auto-generate stringification functions for union discriminants.
namespace capnproto_test {
40
namespace capnp {
41 42 43 44 45 46
namespace test {
inline kj::String KJ_STRINGIFY(TestUnion::Union0::Which which) {
  return kj::str(static_cast<uint16_t>(which));
}
inline kj::String KJ_STRINGIFY(TestUnion::Union1::Which which) {
  return kj::str(static_cast<uint16_t>(which));
Kenton Varda's avatar
Kenton Varda committed
47
}
48 49
inline kj::String KJ_STRINGIFY(TestUnion::Union2::Which which) {
  return kj::str(static_cast<uint16_t>(which));
Kenton Varda's avatar
Kenton Varda committed
50
}
51 52
inline kj::String KJ_STRINGIFY(TestUnion::Union3::Which which) {
  return kj::str(static_cast<uint16_t>(which));
Kenton Varda's avatar
Kenton Varda committed
53
}
54 55
inline kj::String KJ_STRINGIFY(TestUnnamedUnion::Which which) {
  return kj::str(static_cast<uint16_t>(which));
Kenton Varda's avatar
Kenton Varda committed
56
}
57 58
inline kj::String KJ_STRINGIFY(TestGroups::Groups::Which which) {
  return kj::str(static_cast<uint16_t>(which));
Kenton Varda's avatar
Kenton Varda committed
59
}
60 61 62 63 64 65
inline kj::String KJ_STRINGIFY(TestInterleavedGroups::Group1::Which which) {
  return kj::str(static_cast<uint16_t>(which));
}
}  // namespace test
}  // namespace capnp
}  // namespace capnproto_test
Kenton Varda's avatar
Kenton Varda committed
66

67
namespace capnp {
68
namespace _ {  // private
Kenton Varda's avatar
Kenton Varda committed
69

70 71 72 73
inline Data::Reader data(const char* str) {
  return Data::Reader(reinterpret_cast<const byte*>(str), strlen(str));
}

74
namespace test = capnproto_test::capnp::test;
Kenton Varda's avatar
Kenton Varda committed
75

76 77
// We don't use "using namespace" to pull these in because then things would still compile
// correctly if they were generated in the global namespace.
78 79 80 81 82 83 84 85
using ::capnproto_test::capnp::test::TestAllTypes;
using ::capnproto_test::capnp::test::TestDefaults;
using ::capnproto_test::capnp::test::TestEnum;
using ::capnproto_test::capnp::test::TestUnion;
using ::capnproto_test::capnp::test::TestUnionDefaults;
using ::capnproto_test::capnp::test::TestNestedTypes;
using ::capnproto_test::capnp::test::TestUsing;
using ::capnproto_test::capnp::test::TestListDefaults;
Kenton Varda's avatar
Kenton Varda committed
86

87 88
void initTestMessage(TestAllTypes::Builder builder);
void initTestMessage(TestDefaults::Builder builder);
89
void initTestMessage(TestListDefaults::Builder builder);
Kenton Varda's avatar
Kenton Varda committed
90

91 92
void checkTestMessage(TestAllTypes::Builder builder);
void checkTestMessage(TestDefaults::Builder builder);
93
void checkTestMessage(TestListDefaults::Builder builder);
94

95 96
void checkTestMessage(TestAllTypes::Reader reader);
void checkTestMessage(TestDefaults::Reader reader);
97
void checkTestMessage(TestListDefaults::Reader reader);
98 99 100

void checkTestMessageAllZero(TestAllTypes::Builder builder);
void checkTestMessageAllZero(TestAllTypes::Reader reader);
101

102
#if !CAPNP_LITE
103 104 105 106 107 108 109 110
void initDynamicTestMessage(DynamicStruct::Builder builder);
void initDynamicTestLists(DynamicStruct::Builder builder);
void checkDynamicTestMessage(DynamicStruct::Builder builder);
void checkDynamicTestLists(DynamicStruct::Builder builder);
void checkDynamicTestMessage(DynamicStruct::Reader reader);
void checkDynamicTestLists(DynamicStruct::Reader reader);
void checkDynamicTestMessageAllZero(DynamicStruct::Builder builder);
void checkDynamicTestMessageAllZero(DynamicStruct::Reader reader);
111
#endif  // !CAPNP_LITE
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
template <typename T>
inline void checkElement(T a, T b) {
  EXPECT_EQ(a, b);
}

template <>
inline void checkElement<float>(float a, float b) {
  EXPECT_FLOAT_EQ(a, b);
}

template <>
inline void checkElement<double>(double a, double b) {
  EXPECT_DOUBLE_EQ(a, b);
}

128
template <typename T, typename L = typename T::Reads>
129
void checkList(T reader, std::initializer_list<decltype(reader[0])> expected) {
130 131
  ASSERT_EQ(expected.size(), reader.size());
  for (uint i = 0; i < expected.size(); i++) {
132
    checkElement<decltype(reader[0])>(expected.begin()[i], reader[i]);
133 134 135
  }
}

136 137
template <typename T, typename L = typename T::Builds, bool = false>
void checkList(T reader, std::initializer_list<decltype(typename L::Reader()[0])> expected) {
138 139
  ASSERT_EQ(expected.size(), reader.size());
  for (uint i = 0; i < expected.size(); i++) {
140
    checkElement<decltype(typename L::Reader()[0])>(expected.begin()[i], reader[i]);
141 142 143
  }
}

144 145 146 147 148 149 150
inline void checkList(List<test::TestOldVersion>::Reader reader,
                      std::initializer_list<int64_t> expectedData,
                      std::initializer_list<Text::Reader> expectedPointers) {
  ASSERT_EQ(expectedData.size(), reader.size());
  for (uint i = 0; i < expectedData.size(); i++) {
    EXPECT_EQ(expectedData.begin()[i], reader[i].getOld1());
    EXPECT_EQ(expectedPointers.begin()[i], reader[i].getOld2());
151 152 153 154 155 156 157 158 159 160 161 162
  }
}

// Hack because as<>() is a template-parameter-dependent lookup everywhere below...
#define as template as

template <typename T> void expectPrimitiveEq(T a, T b) { EXPECT_EQ(a, b); }
inline void expectPrimitiveEq(float a, float b) { EXPECT_FLOAT_EQ(a, b); }
inline void expectPrimitiveEq(double a, double b) { EXPECT_DOUBLE_EQ(a, b); }
inline void expectPrimitiveEq(Text::Reader a, Text::Builder b) { EXPECT_EQ(a, b); }
inline void expectPrimitiveEq(Data::Reader a, Data::Builder b) { EXPECT_EQ(a, b); }

163
#if !CAPNP_LITE
164 165 166 167 168 169 170 171 172 173 174 175 176 177
template <typename Element, typename T>
void checkList(T reader, std::initializer_list<ReaderFor<Element>> expected) {
  auto list = reader.as<DynamicList>();
  ASSERT_EQ(expected.size(), list.size());
  for (uint i = 0; i < expected.size(); i++) {
    expectPrimitiveEq(expected.begin()[i], list[i].as<Element>());
  }

  auto typed = reader.as<List<Element>>();
  ASSERT_EQ(expected.size(), typed.size());
  for (uint i = 0; i < expected.size(); i++) {
    expectPrimitiveEq(expected.begin()[i], typed[i]);
  }
}
178
#endif  // !CAPNP_LITE
179 180 181

#undef as

182 183 184
// =======================================================================================
// Interface implementations.

185 186
#if !CAPNP_LITE

187 188 189 190
class TestInterfaceImpl final: public test::TestInterface::Server {
public:
  TestInterfaceImpl(int& callCount);

191
  kj::Promise<void> foo(FooContext context) override;
192

193
  kj::Promise<void> baz(BazContext context) override;
194 195 196 197 198

private:
  int& callCount;
};

199
class TestExtendsImpl final: public test::TestExtends2::Server {
200 201 202
public:
  TestExtendsImpl(int& callCount);

203
  kj::Promise<void> foo(FooContext context) override;
204

205
  kj::Promise<void> grault(GraultContext context) override;
206 207 208 209 210 211 212 213 214

private:
  int& callCount;
};

class TestPipelineImpl final: public test::TestPipeline::Server {
public:
  TestPipelineImpl(int& callCount);

215
  kj::Promise<void> getCap(GetCapContext context) override;
216
  kj::Promise<void> getAnyCap(GetAnyCapContext context) override;
217 218 219 220 221

private:
  int& callCount;
};

222 223
class TestCallOrderImpl final: public test::TestCallOrder::Server {
public:
224
  kj::Promise<void> getCallSequence(GetCallSequenceContext context) override;
225 226 227 228 229 230 231 232 233

private:
  uint count = 0;
};

class TestTailCallerImpl final: public test::TestTailCaller::Server {
public:
  TestTailCallerImpl(int& callCount);

234
  kj::Promise<void> foo(FooContext context) override;
235 236 237 238 239 240 241 242 243

private:
  int& callCount;
};

class TestTailCalleeImpl final: public test::TestTailCallee::Server {
public:
  TestTailCalleeImpl(int& callCount);

244
  kj::Promise<void> foo(FooContext context) override;
245 246 247 248 249

private:
  int& callCount;
};

250 251
class TestMoreStuffImpl final: public test::TestMoreStuff::Server {
public:
252
  TestMoreStuffImpl(int& callCount, int& handleCount);
253

254
  kj::Promise<void> getCallSequence(GetCallSequenceContext context) override;
255

256
  kj::Promise<void> callFoo(CallFooContext context) override;
257

258
  kj::Promise<void> callFooWhenResolved(CallFooWhenResolvedContext context) override;
259

260
  kj::Promise<void> neverReturn(NeverReturnContext context) override;
261

262
  kj::Promise<void> hold(HoldContext context) override;
263

264
  kj::Promise<void> callHeld(CallHeldContext context) override;
265

266
  kj::Promise<void> getHeld(GetHeldContext context) override;
267

268
  kj::Promise<void> echo(EchoContext context) override;
Kenton Varda's avatar
Kenton Varda committed
269

270
  kj::Promise<void> expectCancel(ExpectCancelContext context) override;
271

272 273
  kj::Promise<void> getHandle(GetHandleContext context) override;

274 275
  kj::Promise<void> getNull(GetNullContext context) override;

276 277
  kj::Promise<void> getEnormousString(GetEnormousStringContext context) override;

278 279
private:
  int& callCount;
280
  int& handleCount;
281
  test::TestInterface::Client clientToHold = nullptr;
282

283
  kj::Promise<void> loop(uint depth, test::TestInterface::Client cap, ExpectCancelContext context);
284 285 286 287 288 289 290 291 292 293 294 295 296
};

class TestCapDestructor final: public test::TestInterface::Server {
  // Implementation of TestInterface that notifies when it is destroyed.

public:
  TestCapDestructor(kj::Own<kj::PromiseFulfiller<void>>&& fulfiller)
      : fulfiller(kj::mv(fulfiller)), impl(dummy) {}

  ~TestCapDestructor() {
    fulfiller->fulfill();
  }

297 298
  kj::Promise<void> foo(FooContext context) {
    return impl.foo(context);
299 300 301 302 303 304
  }

private:
  kj::Own<kj::PromiseFulfiller<void>> fulfiller;
  int dummy = 0;
  TestInterfaceImpl impl;
305 306
};

307 308
#endif  // !CAPNP_LITE

309
}  // namespace _ (private)
310
}  // namespace capnp
Kenton Varda's avatar
Kenton Varda committed
311 312

#endif  // TEST_UTIL_H_