@@ -11,14 +11,12 @@ START_TEST(test_meanFilterFloat)
1111}
1212END_TEST
1313
14- START_TEST (test_meanFilterint32 )
15- {
16- int data [] = {1 , 2 , 3 , 4 , 5 };
17- ck_assert_int_eq (meanFilterint32 (data , 5 ), 3.0 );
18- }
19- END_TEST
2014START_TEST (test_medianFilter )
2115{
16+ {
17+ int data [] = {1 , 2 , 3 , 4 , 5 };
18+ ck_assert_int_eq (meanFilterint32 (data , 5 ), 3.0 );
19+ }
2220 float data [] = {21 , 30 , 15 , 25 , 35 };
2321 float median = 0 ;
2422 medianFilter (data , 5 , sizeof (float ), & median , compareFloat );
@@ -99,12 +97,6 @@ START_TEST(test_kalman_filter_multiple_updates)
9997 ck_assert (final_error < 0.2 );
10098}
10199END_TEST
102-
103- // 辅助函数:比较浮点数是否相等(允许一定的误差)
104- int float_equal (float a , float b , float epsilon )
105- {
106- return fabs (a - b ) < epsilon ;
107- }
108100// 测试用例 1:简单的线性数据
109101START_TEST (test_linear_curve_fit_simple )
110102{
@@ -114,8 +106,8 @@ START_TEST(test_linear_curve_fit_simple)
114106 float slope , intercept ;
115107 linear_curve_fit (x , y , size , & slope , & intercept );
116108 // 预期结果:斜率为 2,截距为 0
117- ck_assert ( float_equal ( slope , 2.0f , 1e-6 ) );
118- ck_assert ( float_equal ( intercept , 0.0f , 1e-6 ) );
109+ ck_assert_float_eq_tol ( slope , 2.0f , 1e-6 );
110+ ck_assert_float_eq_tol ( intercept , 0.0f , 1e-6 );
119111}
120112END_TEST
121113// 测试用例 2:带截距的线性数据
@@ -127,8 +119,8 @@ START_TEST(test_linear_curve_fit_intercept)
127119 float slope , intercept ;
128120 linear_curve_fit (x , y , size , & slope , & intercept );
129121 // 预期结果:斜率为 2,截距为 1
130- ck_assert ( float_equal ( slope , 2.0f , 1e-6 ) );
131- ck_assert ( float_equal ( intercept , 1.0f , 1e-6 ) );
122+ ck_assert_float_eq_tol ( slope , 2.0f , 1e-6 );
123+ ck_assert_float_eq_tol ( intercept , 1.0f , 1e-6 );
132124}
133125END_TEST
134126// 测试用例 3:随机数据
@@ -140,8 +132,8 @@ START_TEST(test_linear_curve_fit_random)
140132 float slope , intercept ;
141133 linear_curve_fit (x , y , size , & slope , & intercept );
142134 // 预期结果:斜率接近 2,截距接近 1
143- ck_assert ( float_equal ( slope , 1.65f , 1e-2 ) );
144- ck_assert ( float_equal ( intercept , 1.1f , 1e-2 ) );
135+ ck_assert_float_eq_tol ( slope , 1.65f , 1e-2 );
136+ ck_assert_float_eq_tol ( intercept , 1.1f , 1e-2 );
145137}
146138END_TEST
147139// 测试用例 1:简单的二次拟合
@@ -150,12 +142,15 @@ START_TEST(test_quadratic_fit_simple)
150142 float x [] = {0 , 1 , 2 , 3 , 4 };
151143 float y [] = {1 , 4 , 9 , 16 , 25 };
152144 int size = sizeof (x ) / sizeof (x [0 ]);
153- float a , b , c ;
154- quadratic_fit (x , y , size , & a , & b , & c );
155- // 预期结果:a = 1, b = 2, c = 1 (y = 1*x^2 + 2*x + 1)
156- ck_assert (float_equal (a , 1.0f , 1e-6 ));
157- ck_assert (float_equal (b , 2.0f , 1e-6 ));
158- ck_assert (float_equal (c , 1.0f , 1e-6 ));
145+ float coeff [3 ];
146+ quadratic_fit (x , y , size , coeff );
147+ // 预期结果:y = p1+p2*x+p3*x^2
148+ ck_assert_float_eq_tol (coeff [2 ], 1.0f , 1e-6 );
149+ ck_assert_float_eq_tol (coeff [1 ], 2.0f , 1e-6 );
150+ ck_assert_float_eq_tol (coeff [0 ], 1.0f , 1e-6 );
151+ // 计算R²
152+ float r2_float = r_square_float (x , y , 5 , coeff , 2 );
153+ ck_assert_float_eq_tol (r2_float , 1.0f , 1e-6 );
159154}
160155END_TEST
161156// 测试用例 2:带线性项和常数的二次拟合
@@ -164,12 +159,12 @@ START_TEST(test_quadratic_fit_with_terms)
164159 float x [] = {0 , 1 , 2 , 3 , 4 };
165160 float y [] = {2 , 5 , 12 , 23 , 38 };
166161 int size = sizeof (x ) / sizeof (x [0 ]);
167- float a , b , c ;
168- quadratic_fit (x , y , size , & a , & b , & c );
169- // 预期结果:a = 2, b = 1, c = 2 ( y = 2x^2 + 1x + 2)
170- ck_assert ( float_equal ( a , 2.0f , 1e-6 ) );
171- ck_assert ( float_equal ( b , 1.0f , 1e-6 ) );
172- ck_assert ( float_equal ( c , 2.0f , 1e-6 ) );
162+ float coeff [ 3 ] ;
163+ quadratic_fit (x , y , size , coeff );
164+ // 预期结果:y = p1+p2*x+p3*x^2
165+ ck_assert_float_eq_tol ( coeff [ 2 ] , 2.0f , 1e-6 );
166+ ck_assert_float_eq_tol ( coeff [ 1 ] , 1.0f , 1e-6 );
167+ ck_assert_float_eq_tol ( coeff [ 0 ] , 2.0f , 1e-6 );
173168}
174169END_TEST
175170// 测试用例 3:随机数据的二次拟合
@@ -178,12 +173,12 @@ START_TEST(test_quadratic_fit_random)
178173 float x [] = {-2 , -1 , 0 , 1 , 2 };
179174 float y [] = {8 , -1 , -6 , -1 , 8 };
180175 int size = sizeof (x ) / sizeof (x [0 ]);
181- float a , b , c ;
182- quadratic_fit (x , y , size , & a , & b , & c );
183- // 预期结果:a =3.285714, b = 0.0, c = -4.971429 ( y = ax^2 + bx - c)
184- ck_assert ( float_equal ( a , 3.285714f , 1e-6 ) );
185- ck_assert ( float_equal ( b , 0.0f , 1e-6 ) );
186- ck_assert ( float_equal ( c , -4.971429f , 1e-6 ) );
176+ float coeff [ 3 ] ;
177+ quadratic_fit (x , y , size , coeff );
178+ // 预期结果:y = p1+p2*x+p3*x^2
179+ ck_assert_double_eq_tol ( coeff [ 2 ] , 3.285714f , 1e-6 );
180+ ck_assert_double_eq_tol ( coeff [ 1 ] , 0.0f , 1e-6 );
181+ ck_assert_double_eq_tol ( coeff [ 0 ] , -4.971429f , 1e-6 );
187182}
188183END_TEST
189184
@@ -203,7 +198,9 @@ START_TEST(test_cubic_fit)
203198 double coef [4 ];
204199
205200 polyfit (x , y , n , 3 , coef );
206-
201+ // 计算R²
202+ float r2_float = r_square_double (x , y , n , coef , 3 );
203+ ck_assert_double_eq_tol (r2_float , 0.961957f , 1e-6 );
207204 // printf("拟合的三次多项式为: y = %.12fx^3 + %.12fx^2 + %.12fx + %.12f\n", coef[3], coef[2], coef[1], coef[0]);
208205
209206 double x_max_y , max_y ;
@@ -456,8 +453,6 @@ Suite *algorithms_suite(void)
456453
457454 /* Core test case */
458455 tc_mean = tcase_create ("mean" );
459-
460- tcase_add_test (tc_mean , test_meanFilterint32 );
461456 tcase_add_test (tc_mean , test_meanFilterFloat );
462457 tcase_add_test (tc_mean , test_medianFilter );
463458 tcase_add_test (tc_mean , test_kalman_filter_init );
0 commit comments