function-test.c++ 4.42 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:
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:
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.
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.
21 22

#include "function.h"
23
#include <kj/compat/gtest.h>
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

namespace kj {
namespace {

TEST(Function, Lambda) {
  int i = 0;

  Function<int(int, int)> f = [&](int a, int b) { return a + b + i++; };

  EXPECT_EQ(123 + 456, f(123, 456));
  EXPECT_EQ(7 + 8 + 1, f(7, 8));
  EXPECT_EQ(9 + 2 + 2, f(2, 9));

  EXPECT_EQ(i, 3);
}

struct TestType {
  int callCount;

  TestType(int callCount = 0): callCount(callCount) {}

  ~TestType() { callCount = 1234; }
  // Make sure we catch invalid post-destruction uses.

  int foo(int a, int b) {
    return a + b + callCount++;
  }
51 52 53 54

  int foo(int c) {
    return c * 100;
  }
55 56 57 58 59 60 61 62 63
};

TEST(Function, Method) {
  TestType obj;
  Function<int(int, int)> f = KJ_BIND_METHOD(obj, foo);
  Function<uint(uint, uint)> f2 = KJ_BIND_METHOD(obj, foo);

  EXPECT_EQ(123 + 456, f(123, 456));
  EXPECT_EQ(7 + 8 + 1, f(7, 8));
Kenton Varda's avatar
Kenton Varda committed
64
  EXPECT_EQ(9u + 2u + 2u, f2(2, 9));
65 66 67

  EXPECT_EQ(3, obj.callCount);

68 69 70
  Function<int(int)> f3 = KJ_BIND_METHOD(obj, foo);
  EXPECT_EQ(12300, f3(123));

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  // Bind to a temporary.
  f = KJ_BIND_METHOD(TestType(10), foo);

  EXPECT_EQ(123 + 456 + 10, f(123, 456));
  EXPECT_EQ(7 + 8 + 11, f(7, 8));
  EXPECT_EQ(9 + 2 + 12, f(2, 9));

  // Bind to a move.
  f = KJ_BIND_METHOD(kj::mv(obj), foo);
  obj.callCount = 1234;

  EXPECT_EQ(123 + 456 + 3, f(123, 456));
  EXPECT_EQ(7 + 8 + 4, f(7, 8));
  EXPECT_EQ(9 + 2 + 5, f(2, 9));
}

87 88 89 90 91 92 93 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
struct TestConstType {
  mutable int callCount;

  TestConstType(int callCount = 0): callCount(callCount) {}

  ~TestConstType() { callCount = 1234; }
  // Make sure we catch invalid post-destruction uses.

  int foo(int a, int b) const {
    return a + b + callCount++;
  }
};

TEST(ConstFunction, Method) {
  TestConstType obj;
  ConstFunction<int(int, int)> f = KJ_BIND_METHOD(obj, foo);
  ConstFunction<uint(uint, uint)> f2 = KJ_BIND_METHOD(obj, foo);

  EXPECT_EQ(123 + 456, f(123, 456));
  EXPECT_EQ(7 + 8 + 1, f(7, 8));
  EXPECT_EQ(9u + 2u + 2u, f2(2, 9));

  EXPECT_EQ(3, obj.callCount);

  // Bind to a temporary.
  f = KJ_BIND_METHOD(TestConstType(10), foo);

  EXPECT_EQ(123 + 456 + 10, f(123, 456));
  EXPECT_EQ(7 + 8 + 11, f(7, 8));
  EXPECT_EQ(9 + 2 + 12, f(2, 9));

  // Bind to a move.
  f = KJ_BIND_METHOD(kj::mv(obj), foo);
  obj.callCount = 1234;

  EXPECT_EQ(123 + 456 + 3, f(123, 456));
  EXPECT_EQ(7 + 8 + 4, f(7, 8));
  EXPECT_EQ(9 + 2 + 5, f(2, 9));
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
int testFunctionParam(FunctionParam<int(char, bool)> func, char c, bool b) {
  return func(c, b);
}

int testFunctionParamRecursive(FunctionParam<int(char, bool)> func, char c, bool b) {
  return testFunctionParam(func, c, b);
}

KJ_TEST("FunctionParam") {
  {
    int i = 123;
    int result = testFunctionParam([i](char c, bool b) {
      KJ_EXPECT(c == 'x');
      KJ_EXPECT(b);
      KJ_EXPECT(i == 123);
      return 456;
    }, 'x', true);

    KJ_EXPECT(result == 456);
  }

  {
    int i = 123;
    auto func = [i](char c, bool b) {
      KJ_EXPECT(c == 'x');
      KJ_EXPECT(b);
      KJ_EXPECT(i == 123);
      return 456;
    };
    int result = testFunctionParam(func, 'x', true);
    KJ_EXPECT(result == 456);
  }

  {
    int i = 123;
    int result = testFunctionParamRecursive([i](char c, bool b) {
      KJ_EXPECT(c == 'x');
      KJ_EXPECT(b);
      KJ_EXPECT(i == 123);
      return 456;
    }, 'x', true);

    KJ_EXPECT(result == 456);
  }
}

173 174
} // namespace
} // namespace kj