Test-primitives.C 7.2 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2017-2019 OpenCFD Ltd.
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
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM 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 General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Application
    Test-primitives

Description
    Parsing etc for primitives.

\*---------------------------------------------------------------------------*/

#include "scalar.H"
#include "label.H"
#include "StringStream.H"
#include "NASCore.H"
#include "parsing.H"
#include "Tuple2.H"
38 39
#include "Switch.H"
#include "dictionary.H"
40 41 42 43 44 45 46 47 48 49

using namespace Foam;

// Shadow fileFormats::NASCore::readNasScalar
inline scalar readNasScalar(const std::string& str)
{
    return fileFormats::NASCore::readNasScalar(str);
}


50 51 52 53 54 55 56 57 58 59 60 61 62 63
// As a function
inline Switch readSwitch(const std::string& str)
{
    Switch sw(str);

    if (sw.type() == Switch::ON)
    {
        Info<< "Was 'on'" << nl;
    }

    return sw;
}


Mark Olesen's avatar
Mark Olesen committed
64 65 66 67 68 69
void printInfo(const Switch& sw)
{
    Info<<"Switch " << sw.c_str() << " (enum=" << label(sw.type()) << ")\n";
}


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
template<class T>
bool hadParsingError
(
    const std::pair<bool, std::string>& input,
    const std::pair<bool, T>& result,
    std::string errMsg
)
{
    if (result.first)
    {
        if (input.first)
        {
            Info<< "(pass) parsed "
                << input.second << " = " << result.second << nl;
        }
        else
        {
            Info<< "(fail) unexpected success for " << input.second << nl;
        }
    }
    else
    {
        if (input.first)
        {
            Info<< "(fail) unexpected";
        }
        else
        {
            Info<< "(pass) expected";
        }

        Info<< " failure " << input.second << "  >> " << errMsg.c_str() << nl;
    }

    return (input.first != result.first);
}


template<class T>
111 112
unsigned testParsing
(
113 114
    T (*function)(const std::string&),
    std::initializer_list<std::pair<bool, std::string>> tests
115 116 117
)
{
    unsigned nFail = 0;
118
    string errMsg;
119 120

    // Expect some failures
121 122
    const bool prev1 = FatalError.throwExceptions();
    const bool prev2 = FatalIOError.throwExceptions();
123

124
    for (const std::pair<bool, std::string>& test : tests)
125
    {
126
        std::pair<bool, T> result(false, T());
127 128 129

        try
        {
130 131
            result.second = function (test.second);
            result.first = true;
132
        }
133
        catch (const Foam::error& err)
134
        {
135
            errMsg = err.message();
136 137
        }

138
        if (test.first != result.first)
139
        {
140
            ++nFail;
141
        }
142

143
        hadParsingError(test, result, errMsg);
144 145
    }

146 147
    FatalError.throwExceptions(prev1);
    FatalIOError.throwExceptions(prev2);
148 149 150 151 152 153 154 155 156

    return nFail;
}


int main(int argc, char *argv[])
{
    unsigned nFail = 0;

157 158 159 160 161 162 163 164 165 166 167 168 169 170
    {
        Info<< nl << "Test Switch parsing:" << nl;
        nFail += testParsing
        (
            &readSwitch,
            {
                { false, "True" },
                { true,  "false" },
                { true,  "on" },
                { false, "None" },
                { false, "default" },
            }
        );

Mark Olesen's avatar
Mark Olesen committed
171 172
        Info<< nl << "Test Switch defaults:" << nl;

173 174
        dictionary dict;
        dict.add("key1" , "true");
Mark Olesen's avatar
Mark Olesen committed
175
        dict.add("key2" , "off");
176

Mark Olesen's avatar
Mark Olesen committed
177
        for (const word& k : { "key", "key1", "key2" })
178
        {
Mark Olesen's avatar
Mark Olesen committed
179 180
            Switch sw1(k, dict, Switch::YES);
            Switch sw2(k, dict, Switch::NO);
181

Mark Olesen's avatar
Mark Olesen committed
182 183 184 185 186
            bool sw3(Switch(k, dict, Switch::YES));

            printInfo(sw1);
            printInfo(sw2);
            Info<<"bool " << sw3 << nl;
187 188 189
        }
    }

190
    {
191 192
        Info<< nl << "Test readDouble: (small=" << doubleScalarVSMALL
            << " great=" << doubleScalarVSMALL << "):" << nl;
193 194 195 196
        nFail += testParsing
        (
            &readDouble,
            {
197 198 199 200 201 202 203 204 205 206 207 208
                { false, "" },
                { false, "  " },
                { false, " xxx " },
                { false, " 1234E-" },
                { false, " 1234E junk" },
                { true,  " 3.14159 " },
                { true,  " 31.4159E-1 "  },
                { false, " 100E1000 "  },
                { true,  " 1E-40 "  },
                { true,  " 1E-305 "  },
                { true,  " 1E-37 "  },
                { true,  " 1E-300 "  },
209 210 211 212 213
            }
        );
    }

    {
214 215 216
        Info<< nl << "Test readFloat: (small=" << floatScalarVSMALL
            << " great=" << floatScalarVGREAT << "):" << nl;

217 218 219 220
        nFail += testParsing
        (
            &readFloat,
            {
221 222 223 224 225 226 227 228
                { true,  " 3.14159 " },
                { true,  " 31.4159E-1 "  },
                { false, " 31.4159E200 "  },
                { true,  " 31.4159E20 "  },
                { true,  " 1E-40 "  },
                { true,  " 1E-305 "  },
                { true,  " 1E-37 "  },
                { true,  " 1E-300 "  },
229 230 231 232 233 234 235 236 237 238
            }
        );
    }

    {
        Info<< nl << "Test readNasScalar:" << nl;
        nFail += testParsing
        (
            &readNasScalar,
            {
239 240 241 242 243 244 245 246 247
                { true,  " 3.14159 " },
                { true,  " 31.4159E-1 "  },
                { true,  " 314.159-2 "  },
                { true,  " 31.4159E200 "  },
                { true,  " 31.4159E20 "  },
                { true,  " 1E-40 "  },
                { true,  " 1E-305 "  },
                { true,  " 1E-37 "  },
                { true,  " 1E-300 "  },
248 249 250 251 252
            }
        );
    }

    {
253
        Info<< nl << "Test readInt32 (max=" << INT32_MAX << "):" << nl;
254 255 256 257
        nFail += testParsing
        (
            &readInt32,
            {
258 259 260 261 262 263
                { false, " 3.14159 " },
                { false, " 31E1 " },
                { false, " 31.4159E-1 "  },
                { true,  "100"  },
                { true,  "	2147483644"  },
                { false, "   2147483700  "  },
264 265 266 267 268
            }
        );
    }

    {
269
        Info<< nl << "Test readUint32 (max="
270
            << unsigned(UINT32_MAX) << "):" << nl;
271 272 273 274
        nFail += testParsing
        (
            &readUint32,
            {
275 276 277 278
                { true,  "\t2147483644"  },
                { true,  " 2147483700  "  },
                { true,  " 4294967295  "  },
                { false, " 4294968000  "  },
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
            }
        );
    }

    if (nFail)
    {
        Info<< nl << "failed " << nFail << " tests" << nl;
        return 1;
    }

    Info<< nl << "passed all tests" << nl;
    return 0;
}

// ************************************************************************* //