-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_regression.c
154 lines (122 loc) · 3.37 KB
/
test_regression.c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "test_guff.h"
#include "regression.h"
#include <math.h>
static void setup_cb(void *data) {
}
static void teardown_cb(void *data) {
}
DEF_TEST(input_empty) {
double slope = 0;
double intercept = 0;
regression(NULL, 0, TRANSFORM_NONE, &slope, &intercept);
ASSERT(IS_EMPTY(slope));
ASSERT(IS_EMPTY(intercept));
PASS();
}
DEF_TEST(input_simple) {
double slope = 0;
double intercept = 0;
point points[] = {
{.x = 0, .y = 10},
{.x = 1, .y = 15},
{.x = 2, .y = 20},
{.x = 3, .y = 25},
{.x = 4, .y = 30},
};
regression(points, 5, TRANSFORM_NONE, &slope, &intercept);
ASSERT_IN_RANGE(5, slope, 0.001);
ASSERT_IN_RANGE(10, intercept, 0.001);
PASS();
}
DEF_TEST(input_off_axis) {
double slope = 0;
double intercept = 0;
point points[] = {
{.x = 1000, .y = 1000},
{.x = 1010, .y = 1010},
{.x = 1020, .y = 1020},
{.x = 1030, .y = 1035},
{.x = 1040, .y = 1080},
};
regression(points, 5, TRANSFORM_NONE, &slope, &intercept);
ASSERT_IN_RANGE(1.85, slope, 0.001);
ASSERT_IN_RANGE(-858, intercept, 0.001);
PASS();
}
DEF_TEST(input_xy) {
double slope = 0;
double intercept = 0;
point points[] = {
{.x = -3, .y = -2},
{.x = -2, .y = -1},
{.x = 0, .y = 1},
{.x = 3, .y = 3},
{.x = 9, .y = 9},
};
regression(points, 5, TRANSFORM_NONE, &slope, &intercept);
ASSERT_IN_RANGE(0.901, slope, 0.001);
ASSERT_IN_RANGE(0.738197, intercept, 0.001);
PASS();
}
DEF_TEST(input_log_x) {
double slope = 0;
double intercept = 0;
point points[] = {
{.x = exp(0), .y = 0 + 50 },
{.x = exp(1), .y = 1 + 50 },
{.x = exp(2), .y = 2 + 50 },
{.x = exp(3), .y = 3 + 50 },
{.x = exp(4), .y = 4 + 50 },
};
regression(points, 5, TRANSFORM_LOG_X, &slope, &intercept);
ASSERT(!isnan(slope));
ASSERT(!isnan(intercept));
ASSERT_IN_RANGE(1, slope, 0.001);
ASSERT_IN_RANGE(50, intercept, 0.001);
PASS();
}
DEF_TEST(input_log_y_no_intercept) {
double slope = 0;
double intercept = 0;
point points[] = {
{.x = 0, .y = exp(0)},
{.x = 1, .y = exp(1)},
{.x = 2, .y = exp(2)},
{.x = 3, .y = exp(3)},
{.x = 4, .y = exp(4)},
};
regression(points, 5, TRANSFORM_LOG_Y, &slope, &intercept);
ASSERT(!isnan(slope));
ASSERT(!isnan(intercept));
ASSERT_IN_RANGE(1, slope, 0.001);
ASSERT_IN_RANGE(0, intercept, 0.001);
PASS();
}
DEF_TEST(input_log_y_intercept) {
double slope = 0;
double intercept = 0;
point points[] = {
{.x = 0, .y = exp(0) + 10 },
{.x = 1, .y = exp(1) + 10 },
{.x = 2, .y = exp(2) + 10 },
{.x = 3, .y = exp(3) + 10 },
{.x = 4, .y = exp(4) + 10 },
};
regression(points, 5, TRANSFORM_LOG_Y, &slope, &intercept);
ASSERT(!isnan(slope));
ASSERT(!isnan(intercept));
ASSERT_IN_RANGE(0.440159, slope, 0.001);
ASSERT_IN_RANGE(2.19348, intercept, 0.001);
PASS();
}
SUITE(s_regression) {
SET_SETUP(setup_cb, NULL);
SET_TEARDOWN(teardown_cb, NULL);
RUN_TEST(input_empty);
RUN_TEST(input_simple);
RUN_TEST(input_off_axis);
RUN_TEST(input_xy);
RUN_TEST(input_log_x);
RUN_TEST(input_log_y_no_intercept);
RUN_TEST(input_log_y_intercept);
}