~clint-fewbar/ubuntu/precise/squid3/ignore-sighup-early

« back to all changes in this revision

Viewing changes to lib/cppunit-1.10.0/examples/cppunittest/TestAssertTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Luigi Gangitano
  • Date: 2006-11-11 10:32:06 UTC
  • Revision ID: james.westby@ubuntu.com-20061111103206-f3p0r9g0vq44rp3r
Tags: upstream-3.0.PRE5
ImportĀ upstreamĀ versionĀ 3.0.PRE5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "CoreSuite.h"
 
2
#include "TestAssertTest.h"
 
3
#include <algorithm>
 
4
 
 
5
/*
 
6
 Note:
 
7
 - tests need to be added to test asserEquals() template function and
 
8
 use of assertion traits. Some check may need to be added to check
 
9
 the message content in Exception.
 
10
 - code need to be refactored with the use of a test caller that expect
 
11
 an exception.
 
12
 */
 
13
 
 
14
 
 
15
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TestAssertTest,
 
16
                                       coreSuiteName() );
 
17
 
 
18
 
 
19
TestAssertTest::TestAssertTest()
 
20
{
 
21
}
 
22
 
 
23
 
 
24
TestAssertTest::~TestAssertTest()
 
25
{
 
26
}
 
27
 
 
28
 
 
29
void 
 
30
TestAssertTest::setUp()
 
31
{
 
32
}
 
33
 
 
34
 
 
35
void 
 
36
TestAssertTest::tearDown()
 
37
{
 
38
}
 
39
 
 
40
 
 
41
void 
 
42
TestAssertTest::testAssertThrow()
 
43
{
 
44
   CPPUNIT_ASSERT_THROW( throw std::exception(), std::exception );
 
45
 
 
46
   try
 
47
   {
 
48
      CPPUNIT_ASSERT_THROW( 1234, std::exception );
 
49
   }
 
50
   catch ( CPPUNIT_NS::Exception & )
 
51
   {
 
52
      return;
 
53
   }
 
54
 
 
55
   throw std::exception();
 
56
}
 
57
 
 
58
 
 
59
void 
 
60
TestAssertTest::testAssertNoThrow()
 
61
{
 
62
   CPPUNIT_ASSERT_NO_THROW( 1234 );
 
63
 
 
64
   try
 
65
   {
 
66
      CPPUNIT_ASSERT_NO_THROW( throw std::exception() );
 
67
   }
 
68
   catch ( CPPUNIT_NS::Exception & )
 
69
   {
 
70
      return;
 
71
   }
 
72
   throw std::exception();
 
73
}
 
74
 
 
75
 
 
76
void 
 
77
TestAssertTest::testAssertAssertionFail()
 
78
{
 
79
   CPPUNIT_ASSERT_ASSERTION_FAIL( throw CPPUNIT_NS::Exception() );
 
80
 
 
81
   try
 
82
   {
 
83
      CPPUNIT_ASSERT_ASSERTION_FAIL( 1234 );
 
84
   }
 
85
   catch ( CPPUNIT_NS::Exception & )
 
86
   {
 
87
      return;
 
88
   }
 
89
 
 
90
   throw std::exception();
 
91
}
 
92
 
 
93
 
 
94
void 
 
95
TestAssertTest::testAssertAssertionPass()
 
96
{
 
97
   CPPUNIT_ASSERT_ASSERTION_PASS( 1234 );
 
98
 
 
99
   try
 
100
   {
 
101
      CPPUNIT_ASSERT_ASSERTION_PASS( throw CPPUNIT_NS::Exception() );
 
102
   }
 
103
   catch ( CPPUNIT_NS::Exception & )
 
104
   {
 
105
      return;
 
106
   }
 
107
 
 
108
   throw std::exception();
 
109
}
 
110
 
 
111
 
 
112
void 
 
113
TestAssertTest::testAssert()
 
114
{
 
115
  CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT( true ) );
 
116
  
 
117
  CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT( false ) );
 
118
}
 
119
 
 
120
 
 
121
static int foo() { return 1; }
 
122
 
 
123
 
 
124
void 
 
125
TestAssertTest::testAssertEqual()
 
126
{
 
127
  CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 1, 1 ) );
 
128
  CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 1, foo() ) );
 
129
  CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_EQUAL( 12345678, 12345678 ) );
 
130
 
 
131
  CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_EQUAL( 1, 2 ) );
 
132
}
 
133
 
 
134
 
 
135
void 
 
136
TestAssertTest::testAssertMessageTrue()
 
137
{
 
138
  CPPUNIT_ASSERT_ASSERTION_PASS( 
 
139
     CPPUNIT_ASSERT_MESSAGE( "This test should not failed", true ) );
 
140
}
 
141
 
 
142
 
 
143
void 
 
144
TestAssertTest::testAssertMessageFalse()
 
145
{
 
146
  bool exceptionCaught = false;
 
147
  std::string message( "This test message should not be seen" );
 
148
  try
 
149
  {
 
150
    CPPUNIT_ASSERT_MESSAGE( message, 2==3 );
 
151
  }
 
152
  catch( CPPUNIT_NS::Exception &e )
 
153
  {
 
154
    exceptionCaught = true; // ok, we were expecting an exception.
 
155
    checkMessageContains( &e, message );
 
156
  }
 
157
 
 
158
  CPPUNIT_ASSERT( exceptionCaught );
 
159
}
 
160
 
 
161
 
 
162
void 
 
163
TestAssertTest::testAssertDoubleEquals()
 
164
{
 
165
  CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.101 ) );
 
166
  CPPUNIT_ASSERT_ASSERTION_PASS( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.101 ) );
 
167
 
 
168
  CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.1, 1.2, 0.09 ) );
 
169
  CPPUNIT_ASSERT_ASSERTION_FAIL( CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.2, 1.1, 0.09 ) );
 
170
}
 
171
 
 
172
 
 
173
void 
 
174
TestAssertTest::testFail()
 
175
{
 
176
  bool exceptionCaught = false;
 
177
  std::string failure( "FailureMessage" );
 
178
  try
 
179
  {
 
180
    CPPUNIT_FAIL( failure );
 
181
  }
 
182
  catch( CPPUNIT_NS::Exception &e )
 
183
  {
 
184
    exceptionCaught = true;
 
185
    checkMessageContains( &e, failure );
 
186
  }
 
187
  CPPUNIT_ASSERT( exceptionCaught );
 
188
}
 
189
 
 
190
 
 
191
void 
 
192
TestAssertTest::checkMessageContains( CPPUNIT_NS::Exception *e,
 
193
                                      std::string expected )
 
194
{
 
195
  std::string actual = e->what();
 
196
  CPPUNIT_ASSERT_MESSAGE( "Expected message not found: " + expected +
 
197
                          ", was: " + actual,
 
198
      std::search( actual.begin(), actual.end(), 
 
199
                   expected.begin(), expected.end() ) != actual.end() );
 
200
}