/libs/gsl/histogram/test.c

https://github.com/nchaimov/m3l-af · C · 484 lines · 375 code · 91 blank · 18 comment · 109 complexity · e3b2312b2886e4715bf2c05bb9f0b0c8 MD5 · raw file

  1. /* histogram/test.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <config.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <gsl/gsl_histogram.h>
  23. #include <gsl/gsl_test.h>
  24. #include <gsl/gsl_ieee_utils.h>
  25. #define N 397
  26. #define NR 10
  27. int
  28. main (void)
  29. {
  30. double xr[NR + 1] =
  31. {0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
  32. gsl_histogram *h, *h1, *hr, *g;
  33. size_t i, j;
  34. gsl_ieee_env_setup ();
  35. h = gsl_histogram_calloc (N);
  36. h1 = gsl_histogram_calloc (N);
  37. g = gsl_histogram_calloc (N);
  38. gsl_test (h->range == 0, "gsl_histogram_alloc returns valid range pointer");
  39. gsl_test (h->bin == 0, "gsl_histogram_alloc returns valid bin pointer");
  40. gsl_test (h->n != N, "gsl_histogram_alloc returns valid size");
  41. hr = gsl_histogram_calloc_range (NR, xr);
  42. gsl_test (hr->range == 0, "gsl_histogram_calloc_range returns valid range pointer");
  43. gsl_test (hr->bin == 0, "gsl_histogram_calloc_range returns valid bin pointer");
  44. gsl_test (hr->n != NR, "gsl_histogram_calloc_range returns valid size");
  45. {
  46. int status = 0;
  47. for (i = 0; i <= NR; i++)
  48. {
  49. if (hr->range[i] != xr[i])
  50. {
  51. status = 1;
  52. }
  53. };
  54. gsl_test (status, "gsl_histogram_calloc_range creates range correctly");
  55. }
  56. for (i = 0; i <= NR; i++)
  57. {
  58. hr->range[i] = 0.0;
  59. }
  60. {
  61. int status = gsl_histogram_set_ranges (hr, xr, NR+1);
  62. for (i = 0; i <= NR; i++)
  63. {
  64. if (hr->range[i] != xr[i])
  65. {
  66. status = 1;
  67. }
  68. };
  69. gsl_test (status, "gsl_histogram_set_range sets range correctly");
  70. }
  71. for (i = 0; i < N; i++)
  72. {
  73. gsl_histogram_accumulate (h, (double) i, (double) i);
  74. };
  75. {
  76. int status = 0;
  77. for (i = 0; i < N; i++)
  78. {
  79. if (h->bin[i] != (double) i)
  80. {
  81. status = 1;
  82. }
  83. };
  84. gsl_test (status, "gsl_histogram_accumulate writes into array correctly");
  85. }
  86. {
  87. int status = 0;
  88. for (i = 0; i < N; i++)
  89. {
  90. if (gsl_histogram_get (h, i) != i)
  91. status = 1;
  92. };
  93. gsl_test (status, "gsl_histogram_get reads from array correctly");
  94. }
  95. for (i = 0; i <= N; i++)
  96. {
  97. h1->range[i] = 100.0 + i;
  98. }
  99. gsl_histogram_memcpy (h1, h);
  100. {
  101. int status = 0;
  102. for (i = 0; i <= N; i++)
  103. {
  104. if (h1->range[i] != h->range[i])
  105. status = 1;
  106. };
  107. gsl_test (status, "gsl_histogram_memcpy copies bin ranges correctly");
  108. }
  109. {
  110. int status = 0;
  111. for (i = 0; i < N; i++)
  112. {
  113. if (gsl_histogram_get (h1, i) != gsl_histogram_get (h, i))
  114. status = 1;
  115. };
  116. gsl_test (status, "gsl_histogram_memcpy copies bin values correctly");
  117. }
  118. gsl_histogram_free (h1);
  119. h1 = gsl_histogram_clone (h);
  120. {
  121. int status = 0;
  122. for (i = 0; i <= N; i++)
  123. {
  124. if (h1->range[i] != h->range[i])
  125. status = 1;
  126. };
  127. gsl_test (status, "gsl_histogram_clone copies bin ranges correctly");
  128. }
  129. {
  130. int status = 0;
  131. for (i = 0; i < N; i++)
  132. {
  133. if (gsl_histogram_get (h1, i) != gsl_histogram_get (h, i))
  134. status = 1;
  135. };
  136. gsl_test (status, "gsl_histogram_clone copies bin values correctly");
  137. }
  138. gsl_histogram_reset (h);
  139. {
  140. int status = 0;
  141. for (i = 0; i < N; i++)
  142. {
  143. if (h->bin[i] != 0)
  144. status = 1;
  145. }
  146. gsl_test (status, "gsl_histogram_reset zeros array correctly");
  147. }
  148. {
  149. int status = 0;
  150. for (i = 0; i < N; i++)
  151. {
  152. gsl_histogram_increment (h, (double) i);
  153. for (j = 0; j <= i; j++)
  154. {
  155. if (h->bin[j] != 1)
  156. {
  157. status = 1;
  158. }
  159. }
  160. for (j = i + 1; j < N; j++)
  161. {
  162. if (h->bin[j] != 0)
  163. {
  164. status = 1;
  165. }
  166. }
  167. }
  168. gsl_test (status, "gsl_histogram_increment works correctly");
  169. }
  170. {
  171. int status = 0;
  172. for (i = 0; i < N; i++)
  173. {
  174. double x0 = 0, x1 = 0;
  175. gsl_histogram_get_range (h, i, &x0, &x1);
  176. if (x0 != i || x1 != i + 1)
  177. {
  178. status = 1;
  179. }
  180. }
  181. gsl_test (status, "gsl_histogram_getbinrange works correctly");
  182. }
  183. {
  184. int status = 0;
  185. if (gsl_histogram_max (h) != N)
  186. status = 1;
  187. gsl_test (status, "gsl_histogram_max works correctly");
  188. }
  189. {
  190. int status = 0;
  191. if (gsl_histogram_min (h) != 0)
  192. status = 1;
  193. gsl_test (status, "gsl_histogram_min works correctly");
  194. }
  195. {
  196. int status = 0;
  197. if (gsl_histogram_bins (h) != N)
  198. status = 1;
  199. gsl_test (status, "gsl_histogram_bins works correctly");
  200. }
  201. h->bin[2] = 123456.0;
  202. h->bin[4] = -654321;
  203. {
  204. double max = gsl_histogram_max_val (h);
  205. gsl_test (max != 123456.0, "gsl_histogram_max_val finds maximum value");
  206. }
  207. {
  208. double min = gsl_histogram_min_val (h);
  209. gsl_test (min != -654321.0, "gsl_histogram_min_val finds minimum value");
  210. }
  211. {
  212. size_t imax = gsl_histogram_max_bin (h);
  213. gsl_test (imax != 2, "gsl_histogram_max_bin finds maximum value bin");
  214. }
  215. {
  216. size_t imin = gsl_histogram_min_bin (h);
  217. gsl_test (imin != 4, "gsl_histogram_min_bin find minimum value bin");
  218. }
  219. for (i = 0; i < N; i++)
  220. {
  221. h->bin[i] = i + 27;
  222. g->bin[i] = (i + 27) * (i + 1);
  223. }
  224. {
  225. double sum=gsl_histogram_sum (h);
  226. gsl_test(sum != N*27+((N-1)*N)/2, "gsl_histogram_sum sum all bin values");
  227. }
  228. gsl_histogram_memcpy (h1, g);
  229. gsl_histogram_add (h1, h);
  230. {
  231. int status = 0;
  232. for (i = 0; i < N; i++)
  233. {
  234. if (h1->bin[i] != g->bin[i] + h->bin[i])
  235. status = 1;
  236. }
  237. gsl_test (status, "gsl_histogram_add works correctly");
  238. }
  239. gsl_histogram_memcpy (h1, g);
  240. gsl_histogram_sub (h1, h);
  241. {
  242. int status = 0;
  243. for (i = 0; i < N; i++)
  244. {
  245. if (h1->bin[i] != g->bin[i] - h->bin[i])
  246. status = 1;
  247. }
  248. gsl_test (status, "gsl_histogram_sub works correctly");
  249. }
  250. gsl_histogram_memcpy (h1, g);
  251. gsl_histogram_mul (h1, h);
  252. {
  253. int status = 0;
  254. for (i = 0; i < N; i++)
  255. {
  256. if (h1->bin[i] != g->bin[i] * h->bin[i])
  257. status = 1;
  258. }
  259. gsl_test (status, "gsl_histogram_mul works correctly");
  260. }
  261. gsl_histogram_memcpy (h1, g);
  262. gsl_histogram_div (h1, h);
  263. {
  264. int status = 0;
  265. for (i = 0; i < N; i++)
  266. {
  267. if (h1->bin[i] != g->bin[i] / h->bin[i])
  268. status = 1;
  269. }
  270. gsl_test (status, "gsl_histogram_div works correctly");
  271. }
  272. gsl_histogram_memcpy (h1, g);
  273. gsl_histogram_scale (h1, 0.5);
  274. {
  275. int status = 0;
  276. for (i = 0; i < N; i++)
  277. {
  278. if (h1->bin[i] != 0.5 * g->bin[i])
  279. status = 1;
  280. }
  281. gsl_test (status, "gsl_histogram_scale works correctly");
  282. }
  283. gsl_histogram_memcpy (h1, g);
  284. gsl_histogram_shift (h1, 0.25);
  285. {
  286. int status = 0;
  287. for (i = 0; i < N; i++)
  288. {
  289. if (h1->bin[i] != 0.25 + g->bin[i])
  290. status = 1;
  291. }
  292. gsl_test (status, "gsl_histogram_shift works correctly");
  293. }
  294. gsl_histogram_free (h); /* free whatever is in h */
  295. h = gsl_histogram_calloc_uniform (N, 0.0, 1.0);
  296. gsl_test (h->range == 0,
  297. "gsl_histogram_calloc_uniform returns valid range pointer");
  298. gsl_test (h->bin == 0,
  299. "gsl_histogram_calloc_uniform returns valid bin pointer");
  300. gsl_test (h->n != N,
  301. "gsl_histogram_calloc_uniform returns valid size");
  302. gsl_histogram_accumulate (h, 0.0, 1.0);
  303. gsl_histogram_accumulate (h, 0.1, 2.0);
  304. gsl_histogram_accumulate (h, 0.2, 3.0);
  305. gsl_histogram_accumulate (h, 0.3, 4.0);
  306. {
  307. size_t i1, i2, i3, i4;
  308. double expected;
  309. int status = gsl_histogram_find (h, 0.0, &i1);
  310. status = gsl_histogram_find (h, 0.1, &i2);
  311. status = gsl_histogram_find (h, 0.2, &i3);
  312. status = gsl_histogram_find (h, 0.3, &i4);
  313. for (i = 0; i < N; i++)
  314. {
  315. if (i == i1)
  316. {
  317. expected = 1.0;
  318. }
  319. else if (i == i2)
  320. {
  321. expected = 2.0;
  322. }
  323. else if (i == i3)
  324. {
  325. expected = 3.0;
  326. }
  327. else if (i == i4)
  328. {
  329. expected = 4.0;
  330. }
  331. else
  332. {
  333. expected = 0.0;
  334. }
  335. if (h->bin[i] != expected)
  336. {
  337. status = 1;
  338. }
  339. }
  340. gsl_test (status, "gsl_histogram_find works correctly");
  341. }
  342. {
  343. FILE *f = fopen ("test.txt", "w");
  344. gsl_histogram_fprintf (f, h, "%.19g", "%.19g");
  345. fclose (f);
  346. }
  347. {
  348. FILE *f = fopen ("test.txt", "r");
  349. gsl_histogram *hh = gsl_histogram_calloc (N);
  350. int status = 0;
  351. gsl_histogram_fscanf (f, hh);
  352. for (i = 0; i < N; i++)
  353. {
  354. if (h->range[i] != hh->range[i])
  355. status = 1;
  356. if (h->bin[i] != hh->bin[i])
  357. status = 1;
  358. }
  359. if (h->range[N] != hh->range[N])
  360. status = 1;
  361. gsl_test (status, "gsl_histogram_fprintf and fscanf work correctly");
  362. gsl_histogram_free (hh);
  363. fclose (f);
  364. }
  365. {
  366. FILE *f = fopen ("test.dat", "wb");
  367. gsl_histogram_fwrite (f, h);
  368. fclose (f);
  369. }
  370. {
  371. FILE *f = fopen ("test.dat", "rb");
  372. gsl_histogram *hh = gsl_histogram_calloc (N);
  373. int status = 0;
  374. gsl_histogram_fread (f, hh);
  375. for (i = 0; i < N; i++)
  376. {
  377. if (h->range[i] != hh->range[i])
  378. status = 1;
  379. if (h->bin[i] != hh->bin[i])
  380. status = 1;
  381. }
  382. if (h->range[N] != hh->range[N])
  383. status = 1;
  384. gsl_test (status, "gsl_histogram_fwrite and fread work correctly");
  385. gsl_histogram_free (hh);
  386. fclose (f);
  387. }
  388. gsl_histogram_free (h);
  389. gsl_histogram_free (g);
  390. gsl_histogram_free (h1);
  391. gsl_histogram_free (hr);
  392. exit (gsl_test_summary ());
  393. }