cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TestService.cpp
Go to the documentation of this file.
1 #include "cdstd.h"
2 #include <UnitTest++.h>
3 #include "cddefines.h"
4 #include "thirdparty.h"
5 #include "service.h"
6 
7 namespace {
8 
9  TEST(TestFFmtRead)
10  {
11  bool lgEol;
12  char buf[128];
13  init_genrand( 330327317L );
14  for( int i=0; i < 4096; ++i )
15  {
16  double x = pow(10.,genrand_real1()*600. - 300.);
17  if( genrand_int32()%1 )
18  x *= -1.;
19  sprintf( buf, " %.16e", x );
20  long j = 1;
21  double y = FFmtRead( buf, &j, 128, &lgEol );
22  CHECK( fp_equal( x, y, 2 ) );
23  CHECK( !lgEol );
24  }
25  // test edge cases
26  long j = 1;
27  double x = FFmtRead( "HYDROGEN\t1", &j, 10, &lgEol );
28  CHECK( !lgEol && x == 1. );
29  j = 10;
30  x = FFmtRead( "HYDROGEN\t1", &j, 10, &lgEol );
31  CHECK( !lgEol && x == 1. );
32  // these are mainly intended to check agains buffer overruns
33  j = 1;
34  x = FFmtRead( "+", &j, 1, &lgEol );
35  CHECK( lgEol && x == 0. );
36  j = 1;
37  x = FFmtRead( "-.", &j, 2, &lgEol );
38  CHECK( lgEol && x == 0. );
39  j = 1;
40  x = FFmtRead( ".", &j, 1, &lgEol );
41  CHECK( lgEol && x == 0. );
42  }
43 
44  TEST(TestPowi)
45  {
46  init_genrand( 395327317L );
47  for( int i=0; i < 2048; ++i )
48  {
49  double arg1 = genrand_real1()*200. - 100.;
50  long arg2 = (genrand_int32()%200) - 100;
51  CHECK( fp_equal( powi(arg1,arg2), pow(arg1,(double)arg2), max(abs(arg2),3) ) );
52  }
53  }
54 
55  TEST(TestPowpq)
56  {
57  init_genrand( 515337317L );
58  for( int i=0; i < 1024; ++i )
59  {
60  double arg1 = genrand_real1()*1.e50;
61  int arg2 = (genrand_int32()%9) + 1;
62  int arg3 = (genrand_int32()%8) + 2;
63  // powpq() may be more accurate than pow(), e.g. for powpq(x,1,3)...
64  CHECK( fp_equal( powpq(arg1,arg2,arg3), pow(arg1,(double)arg2/(double)arg3), 128 ) );
65  }
66  for( int i=0; i < 1024; ++i )
67  {
68  double arg1 = genrand_real1()*1.e50;
69  int arg2 = -((genrand_int32()%9) + 1);
70  int arg3 = (genrand_int32()%8) + 2;
71  CHECK( fp_equal( powpq(arg1,arg2,arg3), pow(arg1,(double)arg2/(double)arg3), 128 ) );
72  }
73  }
74 }
double genrand_real1()
unsigned long genrand_int32()
bool fp_equal(sys_float x, sys_float y, int n=3)
Definition: cddefines.h:854
long max(int a, long b)
Definition: cddefines.h:817
double powi(double, long int)
Definition: service.cpp:594
double powpq(double x, int p, int q)
Definition: service.cpp:630
void init_genrand(unsigned long s)
double FFmtRead(const char *chCard, long int *ipnt, long int last, bool *lgEOL)
Definition: service.cpp:381