Repository navigation
Expand file tree
/
Copy pathtest_root_finding_funcs.c
More file actions
152 lines (117 loc) · 2.58 KB
/
Copy pathtest_root_finding_funcs.c
File metadata and controls
152 lines (117 loc) · 2.58 KB
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
#include <math.h>
#include <stdio.h>
#include "root_finding_funcs.h"
int test_quadratic_function()
{
double tolerance = 2e-16;
double test_point = 0.5;
double truth_value = -5.0/12.0;
double test_value = quadratic_function(test_point);
double error = fabs(test_value - truth_value) / fabs(truth_value);
if (error < tolerance)
{
return 0;
}
else
{
return 1;
}
}
int test_quadratic_derivative()
{
double tolerance = 2e-16;
double test_point = 0.5;
double truth_value = -1.0/3.0;
double test_value = quadratic_derivative(test_point);
double error = fabs(test_value - truth_value) / fabs(truth_value);
if (error < tolerance)
{
return 0;
}
else
{
return 1;
}
}
int test_quintic_function()
{
double tolerance = 2e-16;
double test_point = 2;
double truth_value = 29;
double test_value = quintic_function(test_point);
double error = fabs(test_value - truth_value) / fabs(truth_value);
if (error < tolerance)
{
return 0;
}
else
{
return 1;
}
}
int test_quintic_derivative()
{
double tolerance = 2e-16;
double test_point = 2;
double truth_value = 79;
double test_value = quintic_derivative(test_point);
double error = fabs(test_value - truth_value) / fabs(truth_value);
if (error < tolerance)
{
return 0;
}
else
{
return 1;
}
}
int test_find_root()
{
double tolerance = 1e-15;
// The quadratic we're using for this test has roots at 0 and 4/3
double truth_val = 4.0/3.0;
// If we start at 1, we should get the root at 4/3
double guess = 1;
int verbose = 0;
double test_val = find_root(quadratic_function, quadratic_derivative,
guess, verbose);
double error = fabs(test_val - truth_val) / fabs(truth_val);
if (error < tolerance)
{
return 0;
}
else
{
return 1;
}
}
void run_test(int (*test)(), int test_index, int num_tests)
{
printf("Test %i/%i... ", test_index, num_tests);
if (test() == 0)
{
printf("Passed.\n");
}
else
{
printf("Failed.\n");
}
}
int main()
{
int num_tests = 5;
int (*tests[])() = {
test_quadratic_function,
test_quadratic_derivative,
test_quintic_function,
test_quintic_derivative,
test_find_root
};
printf("\n");
for (int i = 0; i < num_tests; i++)
{
run_test(tests[i], i + 1, num_tests);
}
printf("\n");
return 0;
}