File: MultiCallbackTest.cpp

package info (click to toggle)
ola 0.10.9.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,440 kB
  • sloc: cpp: 132,294; python: 14,445; javascript: 7,109; sh: 4,713; ansic: 2,189; java: 518; xml: 253; makefile: 175
file content (115 lines) | stat: -rw-r--r-- 2,985 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * MultiCallbackTest.cpp
 * Unittest for MultiCallback class
 * Copyright (C) 2011 Simon Newton
 */

#include <cppunit/extensions/HelperMacros.h>
#include <string>

#include "ola/Callback.h"
#include "ola/MultiCallback.h"
#include "ola/testing/TestUtils.h"


using std::string;

class MultiCallbackTest: public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(MultiCallbackTest);
  CPPUNIT_TEST(testMultiCallback);
  CPPUNIT_TEST(testZeroLimit);
  CPPUNIT_TEST(testSingleLimit);
  CPPUNIT_TEST_SUITE_END();

 public:
    void testMultiCallback();
    void testZeroLimit();
    void testSingleLimit();

    void CallbackMethod() {
      m_callback_count++;
    }

    void setUp() {
      m_callback_count = 0;
    }

 private:
    int m_callback_count;
};

CPPUNIT_TEST_SUITE_REGISTRATION(MultiCallbackTest);

using ola::BaseCallback0;
using ola::NewSingleCallback;
using ola::NewMultiCallback;


/**
 * Test the MultiCallback class.
 */
void MultiCallbackTest::testMultiCallback() {
  BaseCallback0<void> *callback = NewSingleCallback(
      this,
      &MultiCallbackTest::CallbackMethod);

  OLA_ASSERT_EQ(0, m_callback_count);
  BaseCallback0<void> *multi_callback = NewMultiCallback(3, callback);
  OLA_ASSERT_EQ(0, m_callback_count);

  multi_callback->Run();
  OLA_ASSERT_EQ(0, m_callback_count);

  multi_callback->Run();
  OLA_ASSERT_EQ(0, m_callback_count);

  multi_callback->Run();
  OLA_ASSERT_EQ(1, m_callback_count);
}


/**
 * Test the MultiCallback works with a 0 limit
 */
void MultiCallbackTest::testZeroLimit() {
  BaseCallback0<void> *callback = NewSingleCallback(
      this,
      &MultiCallbackTest::CallbackMethod);

  OLA_ASSERT_EQ(0, m_callback_count);
  BaseCallback0<void> *multi_callback = NewMultiCallback(0, callback);
  OLA_ASSERT_EQ(1, m_callback_count);

  (void) multi_callback;
}


/**
 * Test the MultiCallback works with a single limit
 */
void MultiCallbackTest::testSingleLimit() {
  BaseCallback0<void> *callback = NewSingleCallback(
      this,
      &MultiCallbackTest::CallbackMethod);

  OLA_ASSERT_EQ(0, m_callback_count);
  BaseCallback0<void> *multi_callback = NewMultiCallback(1, callback);
  OLA_ASSERT_EQ(0, m_callback_count);

  multi_callback->Run();
  OLA_ASSERT_EQ(1, m_callback_count);
}