void test_integration(void) { function_t *f = function_create("2**x - log(x)"); ASSERT_EQ(integrate_simpson(f, 0.5, 2.3), 4.60212); function_destroy(f); f = function_create("1/x - sin(x)**3"); ASSERT_EQ(integrate_simpson(f, 2.45, 8.145), 1.54018); function_destroy(f); }
void test_arclength(void) { function_t *f = function_create("2**x - log(x)"); ASSERT_EQ(arc_length(f, 0.5, 2.3), 3.0663188081); function_destroy(f); f = function_create("1/x + sin(x)**0.3"); ASSERT_EQ(arc_length(f, 0.5, 2.3), 2.4417982309); function_destroy(f); f = function_create("3.5*x**4 - 30.3*x**3 + 7.2*x**2 - 3.4*x + 32.0"); ASSERT_EQ(arc_length(f, -5, 7), 8109.52041086); function_destroy(f); f = function_create("3.5*x**4 - 7.2*x**2 - 3.4*x - 32.0"); ASSERT_EQ(arc_length(f, -1.5, 0.73), 14.196939434); function_destroy(f); }
void test_derivates(void) { function_t *f = function_create("x**2"); ASSERT_EQ(derivate_1(f, 0.2), 0.4); ASSERT_EQ(derivate_1(f, 0.0), 0.0); ASSERT_EQ(derivate_2(f, 0.0), 2.0); function_destroy(f); f = function_create("cos(x) - x**3"); ASSERT_EQ(derivate_1(f, 5.0), -75.0-sinl(5.0)); ASSERT_EQ(derivate_1(f, 0.0), 0.0); ASSERT_EQ(derivate_2(f, 0.0), -1.0); ASSERT_EQ(derivate_2(f, 3.75), -21.6794); ASSERT_EQ(derivate(f, 3, -34.2), -6.34995); ASSERT_EQ(derivate(f, 3, -3.2), -5.94163); function_destroy(f); f = function_create("2**x - log(x)"); ASSERT_EQ(derivate_1(f, 1.5), 1.29385); ASSERT_EQ(derivate_1(f, 3.2), 6.05724); ASSERT_EQ(derivate_2(f, 3.2), 4.51282); ASSERT_EQ(derivate_2(f, 5.0), 15.41446); function_destroy(f); }
int test_function_new_and_destroy(void) { f = function_new(DEFAULT, 0, 2); mu_check(f->type == DEFAULT); mu_check(f->function == 0); mu_check(f->arity == 2); function_destroy(f); /* default function */ f = function_new_func(0, 2); mu_check(f->type == DEFAULT); mu_check(f->function == 0); mu_check(f->arity == 2); function_destroy(f); /* classification function */ f = function_new_cfunc(0, 2); mu_check(f->type == CLASSIFICATION); mu_check(f->function == 0); mu_check(f->arity == 2); function_destroy(f); return 0; }
/** Main test */ int main( int argc, char **argv ) { setlocale( LC_ALL, "" ); srand( time( 0 ) ); program_name=L"(ignore)"; say( L"Testing low-level functionality"); say( L"Lines beginning with '(ignore):' are not errors, they are warning messages\ngenerated by the fish parser library when given broken input, and can be\nignored. All actual errors begin with 'Error:'." ); proc_init(); halloc_util_init(); event_init(); parser_init(); function_init(); builtin_init(); reader_init(); env_init(); test_util(); test_escape(); test_convert(); test_tok(); test_parser(); test_expand(); test_path(); say( L"Encountered %d errors in low-level tests", err_count ); /* Skip performance tests for now, since they seem to hang when running from inside make (?) */ // say( L"Testing performance" ); // perf_complete(); env_destroy(); reader_destroy(); parser_destroy(); function_destroy(); builtin_destroy(); wutil_destroy(); event_destroy(); proc_destroy(); halloc_util_destroy(); }
void ui_function_destroy(ui_function *uif) { function_destroy(uif->func); free(uif->color); free(uif); }
void atom_destroy_function(Function* func) { function_destroy(func); }
int main(int argc, char *argv[]) { int ret; char *x0 = NULL, *x1 = NULL; char *epsilon = EPSILON, *func = NULL; char *max_iter = MAXITER, *method = "secant"; long double r; /* parse command line */ while ((ret = getopt(argc, argv, "vm:e:i:a:b:")) != -1) { switch (ret) { case 'e': epsilon = optarg; /* optarg is just a pointer to argv */ break; case 'i': max_iter = optarg; break; case 'a': x0 = optarg; break; case 'b': x1 = optarg; break; case 'm': method = optarg; break; case 'v': root_search_verbose = 1; break; default: return 1; } } if (optind >= argc || !x0) { fprintf(stderr, "usage: %s [-v] [-e <epsilon>] [-i <max-iter>] [-m <method>]" "-a <x0> [-b <x1>] <function eg: cos(x)-x^3>\n", argv[0]); return 1; } else { func = argv[optind]; } function_t *f = function_create(func); interval_t *i = interval_create(atof(x0), x1 ? atof(x1) : (atof(x0) + 1.0)); stop_cond_t *s = stop_cond_create(atof(epsilon), atoi(max_iter)); if (!strcmp(method, "secant")) ret = root_secant(f, i, s, &r); else if (!strcmp(method, "bisection")) ret = root_bisection(f, i, s, &r); else if (!strcmp(method, "regulafalsi")) ret = root_regulafalsi(f, i, s, &r); else if (!strcmp(method, "newton")) ret = root_newton(f, atof(x0), s, &r); else { fprintf(stderr, "Method not recognized: %s\n", method); return 1; } if (ret) { fprintf(stderr, "%s method failed (ret: %d)\n", method, ret); } else { printf("%.15Lg\n", r); } function_destroy(f); interval_destroy(i); stop_cond_destroy(s); return ret; }