File: tlibraryconstant.c

package info (click to toggle)
sollya 8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,592 kB
  • sloc: ansic: 124,655; yacc: 7,543; lex: 2,440; makefile: 888; cpp: 77
file content (142 lines) | stat: -rw-r--r-- 5,234 bytes parent folder | download | duplicates (3)
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
#include <sollya.h>
#include <mpfr.h>
#include <stdlib.h>
#include <string.h>

void euler_gamma(mpfr_t res, mp_prec_t prec) {
  mpfr_set_prec(res, prec);
  mpfr_const_euler(res, GMP_RNDN);
  return;
}

void stupid1(mpfr_t res, mp_prec_t prec) {
  (void)prec; /* Avoiding "unused parameter" warning */
  mpfr_set_ui(res, 0, GMP_RNDN);
  return;
}

void stupid2(mpfr_t res, mp_prec_t prec) {
  (void)prec; /* Avoiding "unused parameter" warning */
  mpfr_set_ui(res, 0, GMP_RNDN);
  return;
}

void stupid3(mpfr_t res, mp_prec_t prec) {
  (void)prec; /* Avoiding "unused parameter" warning */
  mpfr_set_ui(res, 0, GMP_RNDN);
  return;
}

void stupid4(mpfr_t res, mp_prec_t prec) {
  (void)prec; /* Avoiding "unused parameter" warning */
  mpfr_set_ui(res, 0, GMP_RNDN);
  return;
}

void stupid5(mpfr_t res, mp_prec_t prec) {
  (void)prec; /* Avoiding "unused parameter" warning */
  mpfr_set_ui(res, 0, GMP_RNDN);
  return;
}


int main(void) {
  sollya_obj_t f[14];
  int i;
  void *ptr = malloc(1);
  char str[256];
  char str2[256];
  mpfr_t x,y;

  sollya_lib_init();

  /* Normal use */
  f[0] = sollya_lib_libraryconstant("superconst", euler_gamma);
  sollya_lib_printf("%b (expecting superconst)\n", f[0]);
  mpfr_init2(x, 30);
  mpfr_init2(y, 50);
  mpfr_set_ui(x, 0, GMP_RNDN);
  sollya_lib_evaluate_function_at_point(y, f[0], x, NULL);
  sollya_lib_printf("%v (expecting: 0.5772...)\n", y);
  sollya_lib_clear_obj(f[0]);
  f[0] = sollya_lib_parse_string("superconst");
  sollya_lib_evaluate_function_at_point(y, f[0], x, NULL);
  sollya_lib_printf("%v (expecting: 0.5772...)\n", y);

  /* Trying to rebind a constant already bounded */
  f[1] = sollya_lib_libraryconstant("*]%", euler_gamma);
  sollya_lib_printf("%b (expecting superconst)\n", f[1]);
  f[2] = sollya_lib_libraryconstant("superconst", euler_gamma);
  sollya_lib_printf("%b (expecting superconst)\n", f[2]);
  f[3] = sollya_lib_libraryconstant("foo", euler_gamma);
  sollya_lib_printf("%b (expecting superconst)\n", f[3]);
  f[4] = sollya_lib_libraryconstant(NULL, euler_gamma);
  sollya_lib_printf("%b (expecting superconst)\n", f[4]);
  f[5] = sollya_lib_libraryconstant("pi", euler_gamma);
  sollya_lib_printf("%b (expecting superconst)\n", f[5]);

  for(i=1;i<=5;i++) {
    f[6] = sollya_lib_cmp_equal(f[0], f[i]);
    sollya_lib_printf("%b (expecting true)\n", f[6]);
    sollya_lib_clear_obj(f[6]);
  }

   /* Trying to bind a function to an already assigned name */
  f[6] = sollya_lib_libraryconstant("superconst", stupid1);
  sollya_lib_printf("%b (expecting superconst_0)\n", f[6]);

  /* Leaving NULL as first argument */
  f[7] = sollya_lib_libraryconstant(NULL, stupid2);
  sollya_lib_sprintf(str, "%b", f[7]);
  sollya_lib_sprintf(str2, "const_%p", stupid2);
  if (strcmp(str, str2)==0) sollya_lib_printf("The behavior when the first argument is NULL is conform to the semantic.\n");
  else {
    strcpy(str2, "stupid2");
    if (strcmp(str, str2)==0) sollya_lib_printf("The behavior when the first argument is NULL is conform to the semantic.\n");
    else sollya_lib_printf("The behavior when the first argument is *NOT* conform to the semantic.\n");
  }

  /* Unauthorized names */
  f[8] = sollya_lib_libraryconstant("e]xp", stupid3);
  sollya_lib_printf("%b (expecting exp_0)\n", f[8]);

  f[9] = sollya_lib_libraryconstant("]0", stupid4);
  sollya_lib_sprintf(str, "%b", f[9]);
  sollya_lib_sprintf(str2, "const_%p", stupid4);
  if (strcmp(str, str2)==0) sollya_lib_printf("The behavior when the first argument is NULL is conform to the semantic.\n");
  else {
    strcpy(str2, "stupid4");
    if (strcmp(str, str2)==0) sollya_lib_printf("The behavior when the first argument is NULL is conform to the semantic.\n");
    else sollya_lib_printf("The behavior when the first argument is *NOT* conform to the semantic.\n");
  }


  f[10] = sollya_lib_libraryconstant("0]a", stupid5);
  sollya_lib_printf("%b (expecting a)\n", f[10]);

  /* Pointer to non-valid functions, together with NULL/illicit as second argument */
  f[11] = sollya_lib_libraryconstant(NULL, (void (*)(mpfr_t, mp_prec_t))(ptr));
  sollya_lib_sprintf(str, "%b", f[11]);
  sollya_lib_sprintf(str2, "const_%p", ptr);
  if (strcmp(str, str2)==0) sollya_lib_printf("Testing NULL/invalid ptr combination: OK\n");
  else sollya_lib_printf("Testing NULL/invalid ptr combination: *NOT* OK: %b versus %s\n", f[11], str2);

  f[12] = sollya_lib_libraryconstant("", (void (*)(mpfr_t, mp_prec_t))(&x));
  sollya_lib_sprintf(str, "%b", f[12]);
  sollya_lib_sprintf(str2, "const_%p", &x);
  if (strcmp(str, str2)==0) sollya_lib_printf("Testing NULL/invalid ptr combination: OK\n");
  else sollya_lib_printf("Testing NULL/invalid ptr combination: *NOT* OK: %b versus %s\n", f[12], str2);

  f[13] = sollya_lib_libraryconstant("*]%", (void (*)(mpfr_t, mp_prec_t))(ptr));
  sollya_lib_sprintf(str, "%b", f[13]);
  sollya_lib_sprintf(str2, "const_%p", ptr);
  if (strcmp(str, str2)==0) sollya_lib_printf("Testing NULL/invalid ptr combination: OK\n");
  else sollya_lib_printf("Testing NULL/invalid ptr combination: *NOT* OK: %b versus %s\n", f[13], str2);

  for(i=0;i<=13;i++) sollya_lib_clear_obj(f[i]);
  free(ptr);
  mpfr_clear(x);
  mpfr_clear(y);
  sollya_lib_close();
  return 0;
}