]> Creatis software - CreaPhase.git/blob - octave_packages/io-1.0.19/io_ods_testscript.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / io-1.0.19 / io_ods_testscript.m
1 ## Copyright (C) 2012 Philip Nienhuis <pr.nienhuis at users.sf.net>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## (Internal function) Check proper operation of ODS spreadsheet scripts.
17 ## Before running, a character variable 'intf' should be initialized with
18 ## a value of 'otk', 'jod', or 'uno'.
19
20 ## Author: Philip Nienhuis
21 ## Created: 2012-02-25
22 ## Updates:
23
24
25 printf ("\nTesting .ods interface %s ...\n", intf);
26
27 ## 1. Initialize test arrays
28 printf ("\n 1. Initialize arrays.\n");
29 arr1 = [ 1 2; 3 4.5];
30 arr2 = {'r1c1', '=c2+d2'; '', 'r2c2'; true, -83.4};
31 opts = struct ("formulas_as_text", 0);
32
33 ## 2. Insert empty sheet
34 printf ("\n 2. Insert first empty sheet.\n");
35 odswrite ('io-test.ods', {''}, 'EmptySheet', 'b4', intf);
36
37 ## 3. Add data to test sheet
38 printf ("\n 3. Add data to test sheet.\n");
39 odswrite ('io-test.ods', arr1, 'Testsheet', 'c2:d3', intf);
40 odswrite ('io-test.ods', arr2, 'Testsheet', 'd4:z20', intf);
41
42 ## 4. Insert another sheet
43 printf ("\n 4. Add another sheet with just one number in A1.\n");
44 odswrite ('io-test.ods', [1], 'JustOne', 'A1', intf);
45
46 ## 5. Get sheet info & find sheet with data and data range
47 printf ("\n 5. Explore sheet info.\n");
48 [~, shts] = odsfinfo ('io-test.ods', intf);
49 shnr = strmatch ('Testsheet', shts(:, 1));                      # Note case!
50 crange = shts{shnr, 2};
51
52 ## 6. Read data back
53 printf ("\n 6. Read data back.\n");
54 [num, txt, raw, lims] = odsread ('io-test.ods', shnr, crange, intf);
55
56 ## 7. Here come the tests, part 1
57 printf ("\n 7. Tests part 1 (basic I/O):\n");
58 try
59   printf ("    ...Numeric array... ");
60   assert (num(1:2, 1:3), [1, 2, NaN; 3, 4.5, NaN], 1e-10);
61   assert (num(4:5, 1:3), [NaN, NaN, NaN; NaN, 1, -83.4], 1e-10);
62   assert (num(3, 1:2), [NaN, NaN], 1e-10);
63   # Just check if it's numeric, the value depends too much on cached results
64   assert (isnumeric (num(3,3)), true);
65   printf ("matches.\n");
66 catch
67   printf ("Hmmm.... error, see 'num'\n");
68   keyboard
69 end_try_catch
70 try
71   printf ("    ...Cellstr array... ");
72   assert (txt{1, 1}, 'r1c1');
73   assert (txt{2, 2}, 'r2c2');
74   printf ("matches.\n");
75 catch
76   printf ("Hmmm.... error, see 'txt'\n"); 
77   keyboard
78 end_try_catch
79 try
80   printf ("    ...Boolean... "); 
81   assert (islogical (raw{5, 2}), true);                         # Fails on JOD
82   printf ("recovered.\n");
83 catch
84   if (isnumeric (raw{5, 2}))
85     printf ("recovered as numeric '1' rather than logical TRUE\n");
86   else
87     printf ("Hmmm.... error, see 'raw'\n");
88     keyboard
89   endif
90 end_try_catch
91
92 ## Check if formulas_as_text works:
93 printf ("\n 8. Repeat reading, now return formulas as text\n");
94 opts.formulas_as_text = 1;
95 ods = odsopen ('io-test.ods', 0, intf);
96 raw = ods2oct (ods, shnr, crange, opts);
97 ods = odsclose (ods);
98 clear ods;
99
100 ## 9. Here come the tests, part 2. Fails on COM
101 printf ("\n 9. Tests part 2 (read back formula):\n");
102
103 try
104   # Just check if it contains any string
105   assert ( (ischar (raw{3, 3}) && ~isempty (raw(3, 3))), true); 
106   printf ("    ...OK, formula recovered ('%s').\n", raw{3, 3});
107 catch
108   printf ("Hmmm.... error, see 'raw(3, 3)'");
109   if (isnumeric (raw{3, 3}))
110     printf (" (equals %f, should be a string like '=c2+d2')\n", raw{3, 3}); 
111   else
112     printf ("\n"); 
113     keyboard
114   endif
115 end_try_catch
116
117 ## 10. Clean up
118 printf ("\n10. Cleaning up.....");
119 delete 'io-test.ods';
120 clear arr1 arr2 ods num txt raw lims opts shnr shts crange;
121 printf (" OK\n");