test-util.h 9.82 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Kenton Varda's avatar
Kenton Varda committed
24 25
#ifndef CAPNP_TEST_UTIL_H_
#define CAPNP_TEST_UTIL_H_
Kenton Varda's avatar
Kenton Varda committed
26

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

33
namespace capnp {
Kenton Varda's avatar
Kenton Varda committed
34 35

inline std::ostream& operator<<(std::ostream& os, const Data::Reader& value) {
36
  return os.write(reinterpret_cast<const char*>(value.begin()), value.size());
Kenton Varda's avatar
Kenton Varda committed
37 38
}
inline std::ostream& operator<<(std::ostream& os, const Data::Builder& value) {
39
  return os.write(reinterpret_cast<const char*>(value.begin()), value.size());
Kenton Varda's avatar
Kenton Varda committed
40 41
}
inline std::ostream& operator<<(std::ostream& os, const Text::Reader& value) {
42
  return os.write(value.begin(), value.size());
Kenton Varda's avatar
Kenton Varda committed
43 44
}
inline std::ostream& operator<<(std::ostream& os, const Text::Builder& value) {
45
  return os.write(value.begin(), value.size());
Kenton Varda's avatar
Kenton Varda committed
46 47 48 49 50
}
inline std::ostream& operator<<(std::ostream& os, Void) {
  return os << "void";
}

51
namespace _ {  // private
Kenton Varda's avatar
Kenton Varda committed
52

53 54 55 56
inline Data::Reader data(const char* str) {
  return Data::Reader(reinterpret_cast<const byte*>(str), strlen(str));
}

57
namespace test = capnproto_test::capnp::test;
Kenton Varda's avatar
Kenton Varda committed
58

59 60
// 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.
61 62 63 64 65 66 67 68
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
69

70 71
void initTestMessage(TestAllTypes::Builder builder);
void initTestMessage(TestDefaults::Builder builder);
72
void initTestMessage(TestListDefaults::Builder builder);
Kenton Varda's avatar
Kenton Varda committed
73

74 75
void checkTestMessage(TestAllTypes::Builder builder);
void checkTestMessage(TestDefaults::Builder builder);
76
void checkTestMessage(TestListDefaults::Builder builder);
77

78 79
void checkTestMessage(TestAllTypes::Reader reader);
void checkTestMessage(TestDefaults::Reader reader);
80
void checkTestMessage(TestListDefaults::Reader reader);
81 82 83

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

85 86 87 88 89 90 91 92 93
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);

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
template <typename T, typename U>
void checkList(T reader, std::initializer_list<U> expected) {
  ASSERT_EQ(expected.size(), reader.size());
  for (uint i = 0; i < expected.size(); i++) {
    EXPECT_EQ(expected.begin()[i], reader[i]);
  }
}

template <typename T>
void checkList(T reader, std::initializer_list<float> expected) {
  ASSERT_EQ(expected.size(), reader.size());
  for (uint i = 0; i < expected.size(); i++) {
    EXPECT_FLOAT_EQ(expected.begin()[i], reader[i]);
  }
}

template <typename T>
void checkList(T reader, std::initializer_list<double> expected) {
  ASSERT_EQ(expected.size(), reader.size());
  for (uint i = 0; i < expected.size(); i++) {
    EXPECT_DOUBLE_EQ(expected.begin()[i], reader[i]);
  }
}

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

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

#undef as

144 145 146 147 148 149 150
// =======================================================================================
// Interface implementations.

class TestInterfaceImpl final: public test::TestInterface::Server {
public:
  TestInterfaceImpl(int& callCount);

151
  kj::Promise<void> foo(FooParams::Reader params, FooResults::Builder result) override;
152

153
  kj::Promise<void> bazAdvanced(CallContext<BazParams, BazResults> context) override;
154 155 156 157 158 159 160 161 162

private:
  int& callCount;
};

class TestExtendsImpl final: public test::TestExtends::Server {
public:
  TestExtendsImpl(int& callCount);

163
  kj::Promise<void> foo(FooParams::Reader params, FooResults::Builder result) override;
164

165
  kj::Promise<void> graultAdvanced(CallContext<GraultParams, test::TestAllTypes> context) override;
166 167 168 169 170 171 172 173 174

private:
  int& callCount;
};

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

175
  kj::Promise<void> getCapAdvanced(CallContext<GetCapParams, GetCapResults> context) override;
176 177 178 179 180

private:
  int& callCount;
};

181 182 183
class TestCallOrderImpl final: public test::TestCallOrder::Server {
public:
  kj::Promise<void> getCallSequence(
184 185
      GetCallSequenceParams::Reader params,
      GetCallSequenceResults::Builder result) override;
186 187 188 189 190 191 192 193 194 195

private:
  uint count = 0;
};

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

  kj::Promise<void> fooAdvanced(
196
      CallContext<FooParams, test::TestTailCallee::TailResult> context) override;
197 198 199 200 201 202 203 204 205 206

private:
  int& callCount;
};

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

  kj::Promise<void> fooAdvanced(
207
      CallContext<FooParams, test::TestTailCallee::TailResult> context) override;
208 209 210 211 212

private:
  int& callCount;
};

213 214 215 216 217
class TestMoreStuffImpl final: public test::TestMoreStuff::Server {
public:
  TestMoreStuffImpl(int& callCount);

  kj::Promise<void> getCallSequence(
218 219
      GetCallSequenceParams::Reader params,
      GetCallSequenceResults::Builder result) override;
220

221 222 223
  kj::Promise<void> callFoo(
      CallFooParams::Reader params,
      CallFooResults::Builder result) override;
224 225

  kj::Promise<void> callFooWhenResolved(
226 227 228 229 230 231 232 233 234 235 236 237 238
      CallFooWhenResolvedParams::Reader params,
      CallFooWhenResolvedResults::Builder result) override;

  kj::Promise<void> neverReturnAdvanced(
      CallContext<NeverReturnParams, NeverReturnResults> context) override;

  kj::Promise<void> hold(HoldParams::Reader params, HoldResults::Builder result) override;

  kj::Promise<void> callHeld(CallHeldParams::Reader params,
                             CallHeldResults::Builder result) override;

  kj::Promise<void> getHeld(GetHeldParams::Reader params,
                            GetHeldResults::Builder result) override;
239

Kenton Varda's avatar
Kenton Varda committed
240 241
  kj::Promise<void> echo(EchoParams::Reader params, EchoResults::Builder result) override;

242 243 244 245 246 247
  kj::Promise<void> expectAsyncCancelAdvanced(
      CallContext<ExpectAsyncCancelParams, ExpectAsyncCancelResults> context) override;

  kj::Promise<void> expectSyncCancelAdvanced(
      CallContext<ExpectSyncCancelParams, ExpectSyncCancelResults> context) override;

248 249
private:
  int& callCount;
250 251
  kj::Own<kj::PromiseFulfiller<void>> neverFulfill;
  test::TestInterface::Client clientToHold = nullptr;
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

  kj::Promise<void> loop(uint depth, test::TestInterface::Client cap,
      CallContext<ExpectAsyncCancelParams, ExpectAsyncCancelResults> context);
  kj::Promise<void> loop(uint depth, test::TestInterface::Client cap,
      CallContext<ExpectSyncCancelParams, ExpectSyncCancelResults> context);
};

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

  kj::Promise<void> foo(FooParams::Reader params, FooResults::Builder result) {
    return impl.foo(params, result);
  }

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

280
}  // namespace _ (private)
281
}  // namespace capnp
Kenton Varda's avatar
Kenton Varda committed
282 283

#endif  // TEST_UTIL_H_