File: test_error_stats.cpp

package info (click to toggle)
lammps 20250204%2Bdfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 474,368 kB
  • sloc: cpp: 1,060,070; python: 27,785; ansic: 8,956; f90: 7,254; sh: 6,044; perl: 4,171; fortran: 2,442; xml: 1,714; makefile: 1,352; objc: 238; lisp: 188; yacc: 58; csh: 16; awk: 14; tcl: 6; javascript: 2
file content (49 lines) | stat: -rw-r--r-- 1,246 bytes parent folder | download
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

#include "gtest/gtest.h"
#include <sstream>
#include <string>

// include the implementation since ErrorStats is a standalone class
// this way we don't have to link against the style_tests and lammps libs
#include "error_stats.cpp"
#include "fmtlib_format.cpp"
#include "fmtlib_os.cpp"

// Windows may define this as a macro
#if defined(max)
#undef max
#endif

TEST(ErrorStats, test)
{
    ErrorStats stats;

    stats.add(0.5);
    stats.add(0.1);
    stats.add(2.0);
    stats.add(0.3);
    stats.add(0);

    ASSERT_DOUBLE_EQ(stats.avg(), 0.58);
    ASSERT_DOUBLE_EQ(stats.dev(), 0.73047929470998685);
    ASSERT_DOUBLE_EQ(stats.max(), 2.0);
    ASSERT_EQ(stats.idx(), 3);

    std::stringstream out;
    out << stats;
    ASSERT_EQ(out.str(), "Average:  5.800e-01 StdDev:  7.305e-01 MaxErr:  2.000e+00 @ item: 3");

    stats.reset();
    ASSERT_EQ(stats.has_data(), false);
    ASSERT_DOUBLE_EQ(stats.avg(), 0.0);
    ASSERT_DOUBLE_EQ(stats.dev(), 0.0);
    ASSERT_DOUBLE_EQ(stats.max(), 0.0);
    ASSERT_EQ(stats.idx(), -1);

    stats.add(1.0);
    ASSERT_EQ(stats.has_data(), true);
    ASSERT_DOUBLE_EQ(stats.avg(), 1.0);
    ASSERT_DOUBLE_EQ(stats.dev(), 0.0);
    ASSERT_DOUBLE_EQ(stats.max(), 1.0);
    ASSERT_EQ(stats.idx(), 1);
}