| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2011 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
| 12 * | 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #include "config.h" | 26 #include "config.h" |
| 27 | 27 |
| 28 #include "wtf/Functional.h" |
| 29 #include "wtf/OwnPtr.h" |
| 28 #include "wtf/RefCounted.h" | 30 #include "wtf/RefCounted.h" |
| 29 #include "wtf/Functional.h" | |
| 30 #include <gtest/gtest.h> | 31 #include <gtest/gtest.h> |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 static int returnFortyTwo() | 35 static int returnFortyTwo() |
| 35 { | 36 { |
| 36 return 42; | 37 return 42; |
| 37 } | 38 } |
| 38 | 39 |
| 39 TEST(FunctionalTest, Basic) | 40 TEST(FunctionalTest, Basic) |
| 40 { | 41 { |
| 41 Function<int()> emptyFunction; | 42 Function<int()> emptyFunction; |
| 42 EXPECT_TRUE(emptyFunction.isNull()); | 43 EXPECT_TRUE(emptyFunction.isNull()); |
| 43 | 44 |
| 44 Function<int()> returnFortyTwoFunction = bind(returnFortyTwo); | 45 OwnPtr<Function<int()>> returnFortyTwoFunction = bind(returnFortyTwo); |
| 45 EXPECT_FALSE(returnFortyTwoFunction.isNull()); | 46 EXPECT_FALSE(returnFortyTwoFunction->isNull()); |
| 46 EXPECT_EQ(42, returnFortyTwoFunction()); | 47 EXPECT_EQ(42, (*returnFortyTwoFunction)()); |
| 47 } | 48 } |
| 48 | 49 |
| 49 static int multiplyByTwo(int n) | 50 static int multiplyByTwo(int n) |
| 50 { | 51 { |
| 51 return n * 2; | 52 return n * 2; |
| 52 } | 53 } |
| 53 | 54 |
| 54 static double multiplyByOneAndAHalf(double d) | 55 static double multiplyByOneAndAHalf(double d) |
| 55 { | 56 { |
| 56 return d * 1.5; | 57 return d * 1.5; |
| 57 } | 58 } |
| 58 | 59 |
| 59 TEST(FunctionalTest, UnaryBind) | 60 TEST(FunctionalTest, UnaryBind) |
| 60 { | 61 { |
| 61 Function<int()> multiplyFourByTwoFunction = bind(multiplyByTwo, 4); | 62 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiplyByTwo, 4); |
| 62 EXPECT_EQ(8, multiplyFourByTwoFunction()); | 63 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); |
| 63 | 64 |
| 64 Function<double()> multiplyByOneAndAHalfFunction = bind(multiplyByOneAndAHal
f, 3); | 65 OwnPtr<Function<double()>> multiplyByOneAndAHalfFunction = bind(multiplyByOn
eAndAHalf, 3); |
| 65 EXPECT_EQ(4.5, multiplyByOneAndAHalfFunction()); | 66 EXPECT_EQ(4.5, (*multiplyByOneAndAHalfFunction)()); |
| 66 } | 67 } |
| 67 | 68 |
| 68 TEST(FunctionalTest, UnaryPartBind) | 69 TEST(FunctionalTest, UnaryPartBind) |
| 69 { | 70 { |
| 70 Function<int(int)> multiplyByTwoFunction = bind<int>(multiplyByTwo); | 71 OwnPtr<Function<int(int)>> multiplyByTwoFunction = bind<int>(multiplyByTwo); |
| 71 EXPECT_EQ(8, multiplyByTwoFunction(4)); | 72 EXPECT_EQ(8, (*multiplyByTwoFunction)(4)); |
| 72 | 73 |
| 73 Function<double(double)> multiplyByOneAndAHalfFunction = bind<double>(multip
lyByOneAndAHalf); | 74 OwnPtr<Function<double(double)>> multiplyByOneAndAHalfFunction = bind<double
>(multiplyByOneAndAHalf); |
| 74 EXPECT_EQ(4.5, multiplyByOneAndAHalfFunction(3)); | 75 EXPECT_EQ(4.5, (*multiplyByOneAndAHalfFunction)(3)); |
| 75 } | 76 } |
| 76 | 77 |
| 77 static int multiply(int x, int y) | 78 static int multiply(int x, int y) |
| 78 { | 79 { |
| 79 return x * y; | 80 return x * y; |
| 80 } | 81 } |
| 81 | 82 |
| 82 static int subtract(int x, int y) | 83 static int subtract(int x, int y) |
| 83 { | 84 { |
| 84 return x - y; | 85 return x - y; |
| 85 } | 86 } |
| 86 | 87 |
| 87 TEST(FunctionalTest, BinaryBind) | 88 TEST(FunctionalTest, BinaryBind) |
| 88 { | 89 { |
| 89 Function<int()> multiplyFourByTwoFunction = bind(multiply, 4, 2); | 90 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiply, 4, 2); |
| 90 EXPECT_EQ(8, multiplyFourByTwoFunction()); | 91 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); |
| 91 | 92 |
| 92 Function<int()> subtractTwoFromFourFunction = bind(subtract, 4, 2); | 93 OwnPtr<Function<int()>> subtractTwoFromFourFunction = bind(subtract, 4, 2); |
| 93 EXPECT_EQ(2, subtractTwoFromFourFunction()); | 94 EXPECT_EQ(2, (*subtractTwoFromFourFunction)()); |
| 94 } | 95 } |
| 95 | 96 |
| 96 TEST(FunctionalTest, BinaryPartBind) | 97 TEST(FunctionalTest, BinaryPartBind) |
| 97 { | 98 { |
| 98 Function<int(int)> multiplyFourFunction = bind<int>(multiply, 4); | 99 OwnPtr<Function<int(int)>> multiplyFourFunction = bind<int>(multiply, 4); |
| 99 EXPECT_EQ(8, multiplyFourFunction(2)); | 100 EXPECT_EQ(8, (*multiplyFourFunction)(2)); |
| 100 Function<int(int, int)> multiplyFunction = bind<int, int>(multiply); | 101 OwnPtr<Function<int(int, int)>> multiplyFunction = bind<int, int>(multiply); |
| 101 EXPECT_EQ(8, multiplyFunction(4, 2)); | 102 EXPECT_EQ(8, (*multiplyFunction)(4, 2)); |
| 102 | 103 |
| 103 Function<int(int)> subtractFromFourFunction = bind<int>(subtract, 4); | 104 OwnPtr<Function<int(int)>> subtractFromFourFunction = bind<int>(subtract, 4)
; |
| 104 EXPECT_EQ(2, subtractFromFourFunction(2)); | 105 EXPECT_EQ(2, (*subtractFromFourFunction)(2)); |
| 105 Function<int(int, int)> subtractFunction = bind<int, int>(subtract); | 106 OwnPtr<Function<int(int, int)>> subtractFunction = bind<int, int>(subtract); |
| 106 EXPECT_EQ(2, subtractFunction(4, 2)); | 107 EXPECT_EQ(2, (*subtractFunction)(4, 2)); |
| 107 } | 108 } |
| 108 | 109 |
| 109 static void sixArgFunc(int a, double b, char c, int* d, double* e, char* f) | 110 static void sixArgFunc(int a, double b, char c, int* d, double* e, char* f) |
| 110 { | 111 { |
| 111 *d = a; | 112 *d = a; |
| 112 *e = b; | 113 *e = b; |
| 113 *f = c; | 114 *f = c; |
| 114 } | 115 } |
| 115 | 116 |
| 116 static void assertArgs(int actualInt, double actualDouble, char actualChar, int
expectedInt, double expectedDouble, char expectedChar) | 117 static void assertArgs(int actualInt, double actualDouble, char actualChar, int
expectedInt, double expectedDouble, char expectedChar) |
| 117 { | 118 { |
| 118 EXPECT_EQ(expectedInt, actualInt); | 119 EXPECT_EQ(expectedInt, actualInt); |
| 119 EXPECT_EQ(expectedDouble, actualDouble); | 120 EXPECT_EQ(expectedDouble, actualDouble); |
| 120 EXPECT_EQ(expectedChar, actualChar); | 121 EXPECT_EQ(expectedChar, actualChar); |
| 121 } | 122 } |
| 122 | 123 |
| 123 TEST(FunctionalTest, MultiPartBind) | 124 TEST(FunctionalTest, MultiPartBind) |
| 124 { | 125 { |
| 125 int a = 0; | 126 int a = 0; |
| 126 double b = 0.5; | 127 double b = 0.5; |
| 127 char c = 'a'; | 128 char c = 'a'; |
| 128 | 129 |
| 129 Function<void(int, double, char, int*, double*, char*)> unbound = | 130 OwnPtr<Function<void(int, double, char, int*, double*, char*)>> unbound = |
| 130 bind<int, double, char, int*, double*, char*>(sixArgFunc); | 131 bind<int, double, char, int*, double*, char*>(sixArgFunc); |
| 131 unbound(1, 1.5, 'b', &a, &b, &c); | 132 (*unbound)(1, 1.5, 'b', &a, &b, &c); |
| 132 assertArgs(a, b, c, 1, 1.5, 'b'); | 133 assertArgs(a, b, c, 1, 1.5, 'b'); |
| 133 | 134 |
| 134 Function<void(double, char, int*, double*, char*)> oneBound = | 135 OwnPtr<Function<void(double, char, int*, double*, char*)>> oneBound = |
| 135 bind<double, char, int*, double*, char*>(sixArgFunc, 2); | 136 bind<double, char, int*, double*, char*>(sixArgFunc, 2); |
| 136 oneBound(2.5, 'c', &a, &b, &c); | 137 (*oneBound)(2.5, 'c', &a, &b, &c); |
| 137 assertArgs(a, b, c, 2, 2.5, 'c'); | 138 assertArgs(a, b, c, 2, 2.5, 'c'); |
| 138 | 139 |
| 139 Function<void(char, int*, double*, char*)> twoBound = | 140 OwnPtr<Function<void(char, int*, double*, char*)>> twoBound = |
| 140 bind<char, int*, double*, char*>(sixArgFunc, 3, 3.5); | 141 bind<char, int*, double*, char*>(sixArgFunc, 3, 3.5); |
| 141 twoBound('d', &a, &b, &c); | 142 (*twoBound)('d', &a, &b, &c); |
| 142 assertArgs(a, b, c, 3, 3.5, 'd'); | 143 assertArgs(a, b, c, 3, 3.5, 'd'); |
| 143 | 144 |
| 144 Function<void(int*, double*, char*)> threeBound = | 145 OwnPtr<Function<void(int*, double*, char*)>> threeBound = |
| 145 bind<int*, double*, char*>(sixArgFunc, 4, 4.5, 'e'); | 146 bind<int*, double*, char*>(sixArgFunc, 4, 4.5, 'e'); |
| 146 threeBound(&a, &b, &c); | 147 (*threeBound)(&a, &b, &c); |
| 147 assertArgs(a, b, c, 4, 4.5, 'e'); | 148 assertArgs(a, b, c, 4, 4.5, 'e'); |
| 148 | 149 |
| 149 Function<void(double*, char*)> fourBound = | 150 OwnPtr<Function<void(double*, char*)>> fourBound = |
| 150 bind<double*, char*>(sixArgFunc, 5, 5.5, 'f', &a); | 151 bind<double*, char*>(sixArgFunc, 5, 5.5, 'f', &a); |
| 151 fourBound(&b, &c); | 152 (*fourBound)(&b, &c); |
| 152 assertArgs(a, b, c, 5, 5.5, 'f'); | 153 assertArgs(a, b, c, 5, 5.5, 'f'); |
| 153 | 154 |
| 154 Function<void(char*)> fiveBound = | 155 OwnPtr<Function<void(char*)>> fiveBound = |
| 155 bind<char*>(sixArgFunc, 6, 6.5, 'g', &a, &b); | 156 bind<char*>(sixArgFunc, 6, 6.5, 'g', &a, &b); |
| 156 fiveBound(&c); | 157 (*fiveBound)(&c); |
| 157 assertArgs(a, b, c, 6, 6.5, 'g'); | 158 assertArgs(a, b, c, 6, 6.5, 'g'); |
| 158 | 159 |
| 159 Function<void()> sixBound = | 160 OwnPtr<Function<void()>> sixBound = |
| 160 bind(sixArgFunc, 7, 7.5, 'h', &a, &b, &c); | 161 bind(sixArgFunc, 7, 7.5, 'h', &a, &b, &c); |
| 161 sixBound(); | 162 (*sixBound)(); |
| 162 assertArgs(a, b, c, 7, 7.5, 'h'); | 163 assertArgs(a, b, c, 7, 7.5, 'h'); |
| 163 } | 164 } |
| 164 | 165 |
| 165 class A { | 166 class A { |
| 166 public: | 167 public: |
| 167 explicit A(int i) | 168 explicit A(int i) |
| 168 : m_i(i) | 169 : m_i(i) |
| 169 { | 170 { |
| 170 } | 171 } |
| 171 | 172 |
| 172 int f() { return m_i; } | 173 int f() { return m_i; } |
| 173 int addF(int j) { return m_i + j; } | 174 int addF(int j) { return m_i + j; } |
| 174 | 175 |
| 175 private: | 176 private: |
| 176 int m_i; | 177 int m_i; |
| 177 }; | 178 }; |
| 178 | 179 |
| 179 TEST(FunctionalTest, MemberFunctionBind) | 180 TEST(FunctionalTest, MemberFunctionBind) |
| 180 { | 181 { |
| 181 A a(10); | 182 A a(10); |
| 182 Function<int()> function1 = bind(&A::f, &a); | 183 OwnPtr<Function<int()>> function1 = bind(&A::f, &a); |
| 183 EXPECT_EQ(10, function1()); | 184 EXPECT_EQ(10, (*function1)()); |
| 184 | 185 |
| 185 Function<int()> function2 = bind(&A::addF, &a, 15); | 186 OwnPtr<Function<int()>> function2 = bind(&A::addF, &a, 15); |
| 186 EXPECT_EQ(25, function2()); | 187 EXPECT_EQ(25, (*function2)()); |
| 187 } | 188 } |
| 188 | 189 |
| 189 TEST(FunctionalTest, MemberFunctionPartBind) | 190 TEST(FunctionalTest, MemberFunctionPartBind) |
| 190 { | 191 { |
| 191 A a(10); | 192 A a(10); |
| 192 Function<int(class A*)> function1 = bind<class A*>(&A::f); | 193 OwnPtr<Function<int(class A*)>> function1 = bind<class A*>(&A::f); |
| 193 EXPECT_EQ(10, function1(&a)); | 194 EXPECT_EQ(10, (*function1)(&a)); |
| 194 | 195 |
| 195 Function<int(class A*, int)> unboundFunction2 = | 196 OwnPtr<Function<int(class A*, int)>> unboundFunction2 = |
| 196 bind<class A*, int>(&A::addF); | 197 bind<class A*, int>(&A::addF); |
| 197 EXPECT_EQ(25, unboundFunction2(&a, 15)); | 198 EXPECT_EQ(25, (*unboundFunction2)(&a, 15)); |
| 198 Function<int(int)> objectBoundFunction2 = | 199 OwnPtr<Function<int(int)>> objectBoundFunction2 = |
| 199 bind<int>(&A::addF, &a); | 200 bind<int>(&A::addF, &a); |
| 200 EXPECT_EQ(25, objectBoundFunction2(15)); | 201 EXPECT_EQ(25, (*objectBoundFunction2)(15)); |
| 201 } | 202 } |
| 202 | 203 |
| 203 class Number : public RefCounted<Number> { | 204 class Number : public RefCounted<Number> { |
| 204 public: | 205 public: |
| 205 static PassRefPtr<Number> create(int value) | 206 static PassRefPtr<Number> create(int value) |
| 206 { | 207 { |
| 207 return adoptRef(new Number(value)); | 208 return adoptRef(new Number(value)); |
| 208 } | 209 } |
| 209 | 210 |
| 210 ~Number() | 211 ~Number() |
| (...skipping 13 matching lines...) Expand all Loading... |
| 224 }; | 225 }; |
| 225 | 226 |
| 226 static int multiplyNumberByTwo(Number* number) | 227 static int multiplyNumberByTwo(Number* number) |
| 227 { | 228 { |
| 228 return number->value() * 2; | 229 return number->value() * 2; |
| 229 } | 230 } |
| 230 | 231 |
| 231 TEST(FunctionalTest, RefCountedStorage) | 232 TEST(FunctionalTest, RefCountedStorage) |
| 232 { | 233 { |
| 233 RefPtr<Number> five = Number::create(5); | 234 RefPtr<Number> five = Number::create(5); |
| 234 Function<int()> multiplyFiveByTwoFunction = bind(multiplyNumberByTwo, five); | 235 OwnPtr<Function<int()>> multiplyFiveByTwoFunction = bind(multiplyNumberByTwo
, five); |
| 235 EXPECT_EQ(10, multiplyFiveByTwoFunction()); | 236 EXPECT_EQ(10, (*multiplyFiveByTwoFunction)()); |
| 236 | 237 |
| 237 Function<int()> multiplyFourByTwoFunction = bind(multiplyNumberByTwo, Number
::create(4)); | 238 OwnPtr<Function<int()>> multiplyFourByTwoFunction = bind(multiplyNumberByTwo
, Number::create(4)); |
| 238 EXPECT_EQ(8, multiplyFourByTwoFunction()); | 239 EXPECT_EQ(8, (*multiplyFourByTwoFunction)()); |
| 239 | 240 |
| 240 RefPtr<Number> six = Number::create(6); | 241 RefPtr<Number> six = Number::create(6); |
| 241 Function<int()> multiplySixByTwoFunction = bind(multiplyNumberByTwo, six.rel
ease()); | 242 OwnPtr<Function<int()>> multiplySixByTwoFunction = bind(multiplyNumberByTwo,
six.release()); |
| 242 EXPECT_FALSE(six); | 243 EXPECT_FALSE(six); |
| 243 EXPECT_EQ(12, multiplySixByTwoFunction()); | 244 EXPECT_EQ(12, (*multiplySixByTwoFunction)()); |
| 244 } | 245 } |
| 245 | 246 |
| 246 } // namespace | 247 } // namespace |
| OLD | NEW |