LUMIERA.clone/src/lib/stat/format.hpp
Ichthyostega 8c344b6a51 Library: bring in statistics helper from Yoshimi-test
[http://yoshimi.sourceforge.net/ Yoshimi] is a software sound synthesizer,
derived from `ZynAddSubFx` and developed by an OpenSource community.
The Repository [https://github.com/Ichthyostega/yoshimi-test/ Yoshimi-test]
is used by the Yoshimi developers to maintain a suite of automated
acceptance tests for the Yoshimi application.

This task involves watching execution times to detect long-term performance trends,
which in turn requires to maintain time-series data in CSV files and to perfrom some
simple statistic calculations, including linear regression. Requiring any external
statistics package as dependency was not deemed adequate for such a simple task,
and thus a set of self-contained helper functions was created as a byproduct.

This task attaches an excerpt of the Yoshimi-test history with those helpers.
2024-03-10 23:20:58 +01:00

108 lines
2.2 KiB
C++

/*
* format - collection of test formatting helpers
*
* Copyright 2021, Hermann Vosseler <Ichthyostega@web.de>
*
* This file is part of the Yoshimi-Testsuite, which is free software:
* you can redistribute 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.
*
* Yoshimi-Testsuite 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 yoshimi. If not, see <http://www.gnu.org/licenses/>.
***************************************************************/
/** @file format.hpp
** Collection of helper functions for text and number output and formatting.
** @todo WIP as of 7/21
**
*/
#ifndef TESTRUNNER_UTIL_FORMAT_HPP_
#define TESTRUNNER_UTIL_FORMAT_HPP_
#include "util/utils.hpp"
#include <string>
#include <sstream>
using std::string;
namespace util
{
/** format number as string */
template<typename NUM>
inline string str(NUM n)
{
std::ostringstream oss;
oss << n;
return oss.str();
}
template<typename X>
inline string formatVal(X x)
{
return str(x);
}
inline string formatVal(string s)
{
return "\""+s+"\"";
}
inline string formatVal(bool yes)
{
return yes? "true":"false";
}
inline string formatVal(float f)
{
std::ostringstream oss;
oss.precision(3);
oss.width(5);
oss << f;
return oss.str();
}
/** parse string representation into typed value */
template<typename TAR>
inline TAR parseAs(string const& encodedVal)
{
std::istringstream converter{encodedVal};
TAR value;
converter >> value;
if (converter.fail())
throw error::Invalid("unable to parse "+formatVal(encodedVal));
return value;
}
template<>
inline bool parseAs(string const& encodedBool)
{
return util::boolVal(encodedBool);
}
template<>
inline string parseAs(string const& string)
{
return string; // pass-through (even if empty)
}
}//namespace util
#endif /*TESTRUNNER_UTIL_FORMAT_HPP_*/