1 /*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
27 /*
28 * dna_reg.c
29 *
30 * Tests basic functioning of L_Dna (number array of doubles)
31 */
32
33 #include <math.h>
34 #include "allheaders.h"
35
main(int argc,char ** argv)36 int main(int argc,
37 char **argv)
38 {
39 l_int32 i, nbins, ival;
40 l_float64 pi, angle, val, sum;
41 L_DNA *da1, *da2, *da3, *da4, *da5;
42 L_DNAA *daa1, *daa2;
43 GPLOT *gplot;
44 NUMA *na, *nahisto, *nax;
45 L_REGPARAMS *rp;
46
47 if (regTestSetup(argc, argv, &rp))
48 return 1;
49
50 pi = 3.1415926535;
51 da1 = l_dnaCreate(50);
52 for (i = 0; i < 5000; i++) {
53 angle = 0.02293 * i * pi;
54 val = 999. * sin(angle);
55 l_dnaAddNumber(da1, val);
56 }
57
58 /* Conversion to Numa; I/O for Dna */
59 na = l_dnaConvertToNuma(da1);
60 da2 = numaConvertToDna(na);
61 l_dnaWrite("/tmp/lept/regout/dna1.da", da1);
62 l_dnaWrite("/tmp/lept/regout/dna2.da", da2);
63 da3 = l_dnaRead("/tmp/lept/regout/dna2.da");
64 l_dnaWrite("/tmp/lept/regout/dna3.da", da3);
65 regTestCheckFile(rp, "/tmp/lept/regout/dna1.da"); /* 0 */
66 regTestCheckFile(rp, "/tmp/lept/regout/dna2.da"); /* 1 */
67 regTestCheckFile(rp, "/tmp/lept/regout/dna3.da"); /* 2 */
68 regTestCompareFiles(rp, 1, 2); /* 3 */
69
70 /* I/O for Dnaa */
71 daa1 = l_dnaaCreate(3);
72 l_dnaaAddDna(daa1, da1, L_INSERT);
73 l_dnaaAddDna(daa1, da2, L_INSERT);
74 l_dnaaAddDna(daa1, da3, L_INSERT);
75 l_dnaaWrite("/tmp/lept/regout/dnaa1.daa", daa1);
76 daa2 = l_dnaaRead("/tmp/lept/regout/dnaa1.daa");
77 l_dnaaWrite("/tmp/lept/regout/dnaa2.daa", daa2);
78 regTestCheckFile(rp, "/tmp/lept/regout/dnaa1.daa"); /* 4 */
79 regTestCheckFile(rp, "/tmp/lept/regout/dnaa2.daa"); /* 5 */
80 regTestCompareFiles(rp, 4, 5); /* 6 */
81 l_dnaaDestroy(&daa1);
82 l_dnaaDestroy(&daa2);
83
84 /* Just for fun -- is the numa ok? */
85 nahisto = numaMakeHistogramClipped(na, 12, 2000);
86 nbins = numaGetCount(nahisto);
87 nax = numaMakeSequence(0, 1, nbins);
88 gplot = gplotCreate("/tmp/lept/regout/historoot", GPLOT_PNG,
89 "Histo example", "i", "histo[i]");
90 gplotAddPlot(gplot, nax, nahisto, GPLOT_LINES, "sine");
91 gplotMakeOutput(gplot);
92 regTestCheckFile(rp, "/tmp/lept/regout/historoot.png"); /* 7 */
93 gplotDestroy(&gplot);
94 numaDestroy(&na);
95 numaDestroy(&nax);
96 numaDestroy(&nahisto);
97
98 /* Handling precision of int32 in double */
99 da4 = l_dnaCreate(25);
100 for (i = 0; i < 1000; i++)
101 l_dnaAddNumber(da4, 1928374 * i);
102 l_dnaWrite("/tmp/lept/regout/dna4.da", da4);
103 da5 = l_dnaRead("/tmp/lept/regout/dna4.da");
104 sum = 0;
105 for (i = 0; i < 1000; i++) {
106 l_dnaGetIValue(da5, i, &ival);
107 sum += L_ABS(ival - i * 1928374); /* we better be adding 0 each time */
108 }
109 regTestCompareValues(rp, sum, 0.0, 0.0); /* 8 */
110 l_dnaDestroy(&da4);
111 l_dnaDestroy(&da5);
112
113 return regTestCleanup(rp);
114 }
115