File: EsysExceptionTestCase.cpp

package info (click to toggle)
python-escript 5.6-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,304 kB
  • sloc: python: 592,074; cpp: 136,909; ansic: 18,675; javascript: 9,411; xml: 3,384; sh: 738; makefile: 207
file content (166 lines) | stat: -rw-r--r-- 4,468 bytes parent folder | download | duplicates (3)
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
116
117
118
119
120
121
122
123
124
125
126
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

/*****************************************************************************
*
* Copyright (c) 2003-2020 by The University of Queensland
* http://www.uq.edu.au
*
* Primary Business: Queensland, Australia
* Licensed under the Apache License, version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Development until 2012 by Earth Systems Science Computational Center (ESSCC)
* Development 2012-2013 by School of Earth Sciences
* Development from 2014-2017 by Centre for Geoscience Computing (GeoComp)
* Development from 2019 by School of Earth and Environmental Sciences
**
*****************************************************************************/


#include "EsysExceptionTestCase.h"
#include "escript/EsysException.h"

#include <cppunit/TestCaller.h>
#include <iostream>

using namespace std;
using namespace CppUnit;
using namespace escript;

class DerivedEx : public EsysException
{
    typedef EsysException Parent;
public:
    DerivedEx(const string& str) : Parent(str) {}
};

void EsysExceptionTestCase::testCase1()
{
    string ex1Text("My first funny exception message.");
    EsysException ex1(ex1Text);

    string ex1String = ex1.what();

    //
    // exception text should contain entered exception message
    //
    CPPUNIT_ASSERT(ex1String.find(ex1Text) != string::npos);

    //
    // copy constructed exception should match original
    //
    EsysException copyEx(ex1);
    string copyString = copyEx.what();
    CPPUNIT_ASSERT(ex1String == copyString);

    //
    // check throw/catch mechanism
    //
    string ex2Text("My second funny exception message.");
    try {
        EsysException ex2(ex2Text);
        throw(ex2);
    } catch (EsysException& e) {
        //
        // exception text should contain entered exception message
        //
        string eString = e.what();
        CPPUNIT_ASSERT(eString.find(ex2Text) != string::npos);
    }
}

//
// test derived EsysException
//
void EsysExceptionTestCase::testCase2()
{
    string ex1Text("asdjhieurncidhfjsnfkjefkjndfjkhsdrdfjksdhfweh");
    DerivedEx ex1(ex1Text);

    //
    // exception text should contain entered exception message
    //
    string ex1String = ex1.what();
    CPPUNIT_ASSERT(ex1String.find(ex1Text) != string::npos);

    //
    // copy constructed exception should match original
    //
    DerivedEx copyEx(ex1);
    string copyString = copyEx.what();
    CPPUNIT_ASSERT(ex1String == copyString);

    //
    // check throw/catch mechanism
    //
    string ex2Text("pjkkjhdfbnkjerbkjsduflfkjahalkgjlklhjhj");
    try {

        DerivedEx ex2(ex2Text);
        throw(ex2);
    } catch (DerivedEx& e) {
        //
        // exception text should contain entered exception message
        //
        string eString = e.what();
        CPPUNIT_ASSERT(eString.find(ex2Text) != string::npos);
    }

    //
    // check throw/catch mechanism
    //
    string ex3Text("irfjvniouf;iarhglAKSDIghlAKSDghladg");
    try {

        DerivedEx ex3(ex3Text);
        throw(ex3);
    } catch (EsysException& e) {
        //
        // exception text should contain entered exception message
        //
        std::string eString = e.what();
        CPPUNIT_ASSERT(eString.find(ex3Text) != string::npos);
    }

    //
    // test to see if exception name gets lost on rethrow
    //
    try {
        try {
            DerivedEx ex4("D ex4 text.");
            throw ex4;
        }
        catch (EsysException& e) {
            cout << endl << e.what() << endl;
            throw;
        }
    } catch (EsysException& e) {
        cout << e.what() << endl;
    }

    cout << "Test EsysException may be caught as a std::exception" << endl;
    try {
        DerivedEx ex4("Exception caught as std::exception");
        throw ex4;
    } catch (exception& e) {
        // cout << e.what() << endl;
        CPPUNIT_ASSERT(e.what() == string("Exception caught as std::exception"));
    } catch (...) {
        //
        // if the exception is caught here there is a problem
        CPPUNIT_ASSERT(false);
    }
}

TestSuite* EsysExceptionTestCase::suite()
{
    //
    // create the suite of tests to perform.
    TestSuite *testSuite = new TestSuite("EsysExceptionTestCase");

    testSuite->addTest(new TestCaller<EsysExceptionTestCase>(
                "testCase1",&EsysExceptionTestCase::testCase1));
    testSuite->addTest(new TestCaller<EsysExceptionTestCase>(
                "testCase2",&EsysExceptionTestCase::testCase2));
    return testSuite;
}