Bug 1198451 - Tests for mozilla::Function. r=froydnj
authorBotond Ballo <botond@mozilla.com>
Sun, 06 Sep 2015 21:43:34 -0400
changeset 294684 a95aa524767529a7c92427e07fb3963d928de074
parent 294683 21eb363017b1be31b999bd7ed88a606d193dab40
child 294685 89571ebbdd078442765a2c6390f5f87d008386a7
push id5245
push userraliiev@mozilla.com
push dateThu, 29 Oct 2015 11:30:51 +0000
treeherdermozilla-beta@dac831dc1bd0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1198451
milestone43.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1198451 - Tests for mozilla::Function. r=froydnj
mfbt/tests/TestFunction.cpp
mfbt/tests/moz.build
new file mode 100644
--- /dev/null
+++ b/mfbt/tests/TestFunction.cpp
@@ -0,0 +1,95 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+   /* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/Assertions.h"
+#include "mozilla/Function.h"
+
+using mozilla::Function;
+
+#define CHECK(c) \
+  do { \
+    bool cond = !!(c); \
+    MOZ_RELEASE_ASSERT(cond, "Failed assertion: " #c); \
+  } while (false)
+
+struct ConvertibleToInt
+{
+  operator int() const { return 42; }
+};
+
+int increment(int arg) { return arg + 1; }
+
+struct S {
+  static int increment(int arg) { return arg + 1; }
+};
+
+struct Incrementor {
+  int operator()(int arg) { return arg + 1; }
+};
+
+static void
+TestNonmemberFunction()
+{
+  Function<int(int)> f = &increment;
+  CHECK(f(42) == 43);
+}
+
+static void
+TestStaticMemberFunction()
+{
+  Function<int(int)> f = &S::increment;
+  CHECK(f(42) == 43);
+}
+
+static void
+TestFunctionObject()
+{
+  Function<int(int)> f = Incrementor();
+  CHECK(f(42) == 43);
+}
+
+static void
+TestLambda()
+{
+  // Test non-capturing lambda
+  Function<int(int)> f = [](int arg){ return arg + 1; };
+  CHECK(f(42) == 43);
+
+  // Test capturing lambda
+  int one = 1;
+  Function<int(int)> g = [one](int arg){ return arg + one; };
+  CHECK(g(42) == 43);
+}
+
+static void
+TestDefaultConstructionAndAssignmentLater()
+{
+  Function<int(int)> f;  // allowed
+  // Would get an assertion if we tried calling f now.
+  f = &increment;
+  CHECK(f(42) == 43);
+}
+
+static void
+TestReassignment()
+{
+  Function<int(int)> f = &increment;
+  CHECK(f(42) == 43);
+  f = [](int arg){ return arg + 2; };
+  CHECK(f(42) == 44);
+}
+
+int
+main()
+{
+  TestNonmemberFunction();
+  TestStaticMemberFunction();
+  TestFunctionObject();
+  TestLambda();
+  TestDefaultConstructionAndAssignmentLater();
+  TestReassignment();
+  return 0;
+}
--- a/mfbt/tests/moz.build
+++ b/mfbt/tests/moz.build
@@ -12,16 +12,17 @@ CppUnitTests([
     'TestCasting',
     'TestCeilingFloor',
     'TestCheckedInt',
     'TestCountPopulation',
     'TestCountZeroes',
     'TestEndian',
     'TestEnumSet',
     'TestFloatingPoint',
+    'TestFunction',
     'TestIntegerPrintfMacros',
     'TestIntegerRange',
     'TestJSONWriter',
     'TestMacroArgs',
     'TestMacroForEach',
     'TestMathAlgorithms',
     'TestMaybe',
     'TestPair',