Newton's method for quadratics involving continued fractions.
[frac.git] / cf_test.c
blobe44229a2139791c4f1950bf9610a47046df05e55
1 // Test demand channel infrastructure.
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <gmp.h>
6 #include "cf.h"
7 #include "test.h"
9 static void *count_fn(cf_t cf) {
10 mpz_t z;
11 mpz_init(z);
12 mpz_set_ui(z, 0);
13 while(cf_wait(cf)) {
14 cf_put(cf, z);
15 mpz_add_ui(z, z, 1);
17 mpz_clear(z);
18 return NULL;
21 static void *count_int_fn(cf_t cf) {
22 int n = 0;
23 while(cf_wait(cf)) {
24 cf_put_int(cf, n);
25 n++;
27 return NULL;
30 int main() {
31 mpz_t z, z1;
32 mpz_init(z);
33 mpz_init(z1);
34 cf_t a, b;
35 a = cf_new_const(count_fn);
36 b = cf_new_const(count_int_fn);
37 for (int i = 0; i < 100; i++) {
38 cf_get(z, a);
39 EXPECT(!mpz_cmp_ui(z, i));
40 cf_get(z, b);
41 EXPECT(!mpz_cmp_ui(z, i));
43 cf_free(a);
44 cf_free(b);
45 mpz_clear(z);
46 mpz_clear(z1);
47 return 0;