test-util.h 9.88 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

22
#pragma once
Kenton Varda's avatar
Kenton Varda committed
23

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

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

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

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

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

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

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

75 76
// 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.
77 78 79 80 81 82 83 84
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
85

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

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

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

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

101
#if !CAPNP_LITE
102 103 104 105 106 107 108 109
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);
110
#endif  // !CAPNP_LITE
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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);
}

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

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

143 144 145 146 147 148 149
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());
150 151 152 153 154 155 156 157 158 159 160 161
  }
}

// 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); }

162
#if !CAPNP_LITE
163 164 165 166 167 168 169 170 171 172 173 174 175 176
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]);
  }
}
177
#endif  // !CAPNP_LITE
178 179 180

#undef as

181 182 183
// =======================================================================================
// Interface implementations.

184 185
#if !CAPNP_LITE

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

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

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

private:
  int& callCount;
};

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

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

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

private:
  int& callCount;
};

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

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

private:
  int& callCount;
};

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

private:
  uint count = 0;
};

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

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

private:
  int& callCount;
};

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

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

private:
  int& callCount;
};

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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();
  }

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

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

306 307
#endif  // !CAPNP_LITE

308
}  // namespace _ (private)
309
}  // namespace capnp