cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TestMonoInterp.cpp
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2022 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 #include "cdstd.h"
4 #include <UnitTest++.h>
5 #include "cddefines.h"
6 #include "monointerp.h"
7 
8 #undef NUMVALS
9 #define NUMVALS(a) sizeof(a)/sizeof((a)[0])
10 
11 namespace {
12  static double xvals1[] = {0.0,1.0},
13  yvals1[] = {0.0,1.0},
14  xvals2[] = {0.0,1.0,2.0,3.0,4.0},
15  yvals2[] = {0.0,1.0,4.0,9.0,16.0},
16  xvals3[] = {0.0,1.0, 2.0,3.0,4.0,5.0,6.0},
17  yvals3[] = {0.0,1.0,-1.0,2.0,1.0,1.0,0.0};
18  struct MonoInterpFixture
19  {
20  Monointerp m1, m2, m3;
21 
22  MonoInterpFixture () :
23  m1(xvals1,yvals1,NUMVALS(xvals1)),
24  m2(xvals2,yvals2,NUMVALS(xvals2)),
25  m3(xvals3,yvals3,NUMVALS(xvals3))
26  {}
27  ~MonoInterpFixture () {}
28  };
29  TEST_FIXTURE(MonoInterpFixture,TestMonoInterpLimits)
30  {
31  CHECK( fp_equal(0.0,m1(0.0)) );
32  CHECK( fp_equal(1.0,m1(1.0)) );
33  }
34  TEST_FIXTURE(MonoInterpFixture,TestMonoInterpInterp)
35  {
36  CHECK( fp_equal(0.5,m1(0.5)) );
37  CHECK( fp_equal(0.333,m1(0.333)) );
38  CHECK( fp_equal(0.99,m1(0.99)) );
39  }
40  TEST_FIXTURE(MonoInterpFixture,TestMonoInterpExtrap)
41  {
42  CHECK( fp_equal(0.,m1(-0.5)) );
43  CHECK( fp_equal(1.,m1(1.5)) );
44  }
45  TEST_FIXTURE(MonoInterpFixture,TestMonoInterpQuad)
46  {
47  for (unsigned int i=0; i<NUMVALS(xvals2); ++i)
48  CHECK( fp_equal(yvals2[i],m2(xvals2[i])) );
49  CHECK_CLOSE(1.5*1.5,m2(1.5),5e-2);
50  CHECK_CLOSE(1.1*1.1,m2(1.1),5e-2);
51  CHECK_CLOSE(1.9*1.9,m2(1.9),5e-2);
52  }
53  TEST_FIXTURE(MonoInterpFixture,TestMonoInterpQuadExtrap)
54  {
55  CHECK( fp_equal(yvals2[0],m2(-0.5)) );
56  CHECK( fp_equal(yvals2[NUMVALS(yvals2)-1],m2(NUMVALS(yvals2)-0.5)) );
57  }
58  TEST_FIXTURE(MonoInterpFixture,TestMonoInterpJump)
59  {
60  for (unsigned int i=0; i<NUMVALS(xvals3); ++i)
61  CHECK( fp_equal(yvals3[i],m3(xvals3[i])) );
62  }
63  TEST_FIXTURE(MonoInterpFixture,TestMonoInterpJumpExtrap)
64  {
65  CHECK( fp_equal(yvals3[0],m3(-0.5)) );
66  CHECK( fp_equal(yvals3[NUMVALS(yvals3)-1],m3(NUMVALS(yvals3)-0.5)) );
67  }
68  TEST_FIXTURE(MonoInterpFixture,TestMonoInterpJumpMonotonic)
69  {
70  const double eps=1e-5;
71  for (unsigned int i=0; i<NUMVALS(xvals3)-1; ++i)
72  {
73  CHECK((yvals3[i]-m3(xvals3[i]+eps))*(yvals3[i+1]-m3(xvals3[i]+eps))<=0);
74  CHECK((yvals3[i]-m3(xvals3[i+1]-eps))
75  *(yvals3[i+1]-m3(xvals3[i+1]-eps))<=0);
76  }
77  }
78 }
bool fp_equal(sys_float x, sys_float y, int n=3)
Definition: cddefines.h:854
#define NUMVALS(a)