feisty meow concerns codebase  2.140
test_ccri_angle_average.cpp
Go to the documentation of this file.
1 
2 /*
3  * Name : test_ccri_angle_average
4  * Author : Chris Koeritz
5  * Purpose:
6  * Tests the angle averaging method (for angles in degrees)
7  *
8  * Copyright (c) 2001-$now By Author. This program is free software; you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation; either version 2 of
11  * the License or (at your option) any later version. This is online at:
12  * http://www.fsf.org/copyleft/gpl.html
13  * Please send any updates to: fred@gruntose.com
14 */
15 
17 #include <basis/astring.h>
18 #include <geometric/angle.h>
22 #include <unit_test/unit_base.h>
23 
24 using namespace application;
25 using namespace basis;
26 using namespace geometric;
27 using namespace loggers;
28 using namespace mathematics;
29 using namespace structures;
30 using namespace unit_test;
31 
33 
34 class test_ccri_angle_average : public virtual unit_base, public virtual application_shell
35 {
36 public:
37  test_ccri_angle_average() : application_shell() {}
38  DEFINE_CLASS_NAME("test_ccri_angle_average");
39  virtual int execute();
40 
41  // returns average of angles a1 and a2, in degrees.
42  double angleAverage(double a1, double a2) {
43  a1 = fmod(a1, 360.0);
44  a2 = fmod(a2, 360.0);
45  if (absolute_value(a1 - a2) > 180.0) {
46  if (a1 < 180.0) a1 += 360.0;
47  else a2 += 360.0;
48  }
49  return fmod( (a1 + a2) / 2.0, 360.0);
50  }
51 
52 };
53 
54 int test_ccri_angle_average::execute()
55 {
56  FUNCDEF("execute");
57 
58  outcome retval;
59  double a1, a2, avg;
60 
61  // there could be two right answers for angles 180 degrees apart, but we only test for one.
62  a1 = 23; a2 = 203;
63  avg = angleAverage(a1, a2);
64  ASSERT_EQUAL(floot(avg), floot(113), a_sprintf("%f and %f 180 degrees apart", a1, a2));
65  a1 = 359; a2 = 179;
66  avg = angleAverage(a1, a2);
67  ASSERT_EQUAL(floot(avg), floot(269), a_sprintf("%f and %f 180 degrees apart", a1, a2));
68  a1 = 90; a2 = 270;
69  avg = angleAverage(a1, a2);
70  ASSERT_EQUAL(floot(avg), floot(180), a_sprintf("%f and %f 180 degrees apart", a1, a2));
71 
72  // more cases.
73  a1 = 89; a2 = 274;
74  avg = angleAverage(a1, a2);
75  ASSERT_EQUAL(floot(avg), floot(1.5), a_sprintf("%f and %f", a1, a2));
76  a1 = 89.9; a2 = 270.1;
77  avg = angleAverage(a1, a2);
78  ASSERT_EQUAL(floot(avg), floot(0), a_sprintf("%f and %f", a1, a2));
79  a1 = 0; a2 = 0;
80  avg = angleAverage(a1, a2);
81  ASSERT_EQUAL(floot(avg), floot(0), a_sprintf("%f and %f", a1, a2));
82  a1 = 0; a2 = 359;
83  avg = angleAverage(a1, a2);
84  ASSERT_EQUAL(floot(avg), floot(359.5), a_sprintf("%f and %f", a1, a2));
85  a1 = 358; a2 = 359;
86  avg = angleAverage(a1, a2);
87  ASSERT_EQUAL(floot(avg), floot(358.5), a_sprintf("%f and %f", a1, a2));
88  a1 = 1; a2 = 357;
89  avg = angleAverage(a1, a2);
90  ASSERT_EQUAL(floot(avg), floot(359), a_sprintf("%f and %f", a1, a2));
91  a1 = 23; a2 = 160;
92  avg = angleAverage(a1, a2);
93  ASSERT_EQUAL(floot(avg), floot(91.5), a_sprintf("%f and %f", a1, a2));
94  a1 = 47; a2 = 221;
95  avg = angleAverage(a1, a2);
96  ASSERT_EQUAL(floot(avg), floot(134), a_sprintf("%f and %f", a1, a2));
97  a1 = 113; a2 = 114;
98  avg = angleAverage(a1, a2);
99  ASSERT_EQUAL(floot(avg), floot(113.5), a_sprintf("%f and %f", a1, a2));
100  a1 = 113; a2 = 270;
101  avg = angleAverage(a1, a2);
102  ASSERT_EQUAL(floot(avg), floot(191.5), a_sprintf("%f and %f", a1, a2));
103  a1 = 190; a2 = 230;
104  avg = angleAverage(a1, a2);
105  ASSERT_EQUAL(floot(avg), floot(210), a_sprintf("%f and %f", a1, a2));
106  a1 = 12; a2 = 273;
107  avg = angleAverage(a1, a2);
108  ASSERT_EQUAL(floot(avg), floot(322.5), a_sprintf("%f and %f", a1, a2));
109  a1 = 181; a2 = 179;
110  avg = angleAverage(a1, a2);
111  ASSERT_EQUAL(floot(avg), floot(180), a_sprintf("%f and %f", a1, a2));
112  a1 = 89; a2 = 271;
113  avg = angleAverage(a1, a2);
114  ASSERT_EQUAL(floot(avg), floot(0), a_sprintf("%f and %f", a1, a2));
115 
116  a1 = 359; a2 = 120;
117  avg = angleAverage(a1, a2);
118  ASSERT_EQUAL(floot(avg), floot(59.5), a_sprintf("%f and %f", a1, a2));
119  a1 = 220; a2 = 359;
120  avg = angleAverage(a1, a2);
121  ASSERT_EQUAL(floot(avg), floot(289.5), a_sprintf("%f and %f", a1, a2));
122  a1 = 3; a2 = 189;
123  avg = angleAverage(a1, a2);
124  ASSERT_EQUAL(floot(avg), floot(276), a_sprintf("%f and %f", a1, a2));
125  a1 = 93; a2 = 275;
126  avg = angleAverage(a1, a2);
127  ASSERT_EQUAL(floot(avg), floot(4), a_sprintf("%f and %f", a1, a2));
128 
129  return final_report();
130 }
131 
133 
134 HOOPLE_MAIN(test_ccri_angle_average, )
135 
The application_shell is a base object for console programs.
a_sprintf is a specialization of astring that provides printf style support.
Definition: astring.h:440
Outcomes describe the state of completion for an operation.
Definition: outcome.h:31
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:45
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:57
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition: hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
type absolute_value(type a)
Returns a if a is non-negative, and returns -a otherwise.
Definition: functions.h:33
Contains all of our objects for geometry and avoids name clashes.
Definition: angle.h:25
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
double_plus floot
#define ASSERT_EQUAL(a, b, test_name)
Definition: unit_base.h:38