1;
2; Alain C., 4 Oct. 2015
3;
4; READS should work with space or comma as separators.
5;
6; First Bug found thanks to the MIRIM simulator
7; Major problem on Complex input see #701
8;
9; Please notice that, yes, maybe some cases we test here may seems to be redundant,
10; but by the way we process now the input, we need to carrefully check
11; we process well all space, comma, braces ...
12;
13; Yes, all the cases here give same results with IDL 8.4
14; -------------------------------------
15;
16; Modifications history :
17;
18; - 2020-April-02 : AC.
19;   * as mentionned in #701, READS is in error with complex
20;   * adding more tests on arrays (basic & mixing types)
21;
22; -------------------------------------
23;
24function ICI_ARRAY_EQUAL, x, y, debug=debug
25if KEYWORD_SET(debug) then begin
26   print, 'expected     :', x
27   print, 'what we have :', y
28endif
29return, ARRAY_EQUAL(x,y)
30end
31; -------------------------------------
32;
33pro TEST_READS_BASIC, cumul_errors, decimal=decimal, complex=complex, basic=basic, $
34                      verbose=verbose, test=test, debug=debug
35;
36errors=0
37;
38if KEYWORD_SET(decimal) then begin
39   inputs=['12.1    13.4',' 12.1, 13.4, 5, 6'' 12.1,13.4   ',' 12.1,13.4,   ']
40endif else begin
41   inputs=['12 13','  12 13','12,13.', '12,13.   ',' 12,13.   ']
42   inputs=[inputs,['12 13  6   7','  12 13 6 7','12,13.,6.,7.', '12,13.   6 7',' 12,13.  ,6,7 '] ]
43endelse
44;
45types=[1,2,3,4,5,6,9]
46;
47if KEYWORD_SET(complex) then begin
48   MESSAGE, /Continue, 'Only Complex types tested'
49   types=[6,9]
50endif
51if KEYWORD_SET(basic) then begin
52   MESSAGE, /Continue, 'Only basic types tested (B, I, L, F, D)'
53   types=[1,2,3,4,5]
54endif
55;
56exp_B=[12b, 13b]
57exp_I=[12, 13]
58exp_L=[12L, 13]
59;
60if KEYWORD_SET(decimal) then exp_F=[12.1, 13.4] else exp_F=[12., 13.]
61exp_D=DOUBLE(exp_F)
62exp_C=Complexarr(2)+exp_F
63exp_DC=DComplexarr(2)+exp_F
64;
65eps=1.e-6
66;
67for ii=0, N_ELEMENTS(inputs)-1 do begin
68   inarray=inputs[ii]
69   for jj=0, N_ELEMENTS(types)-1 do begin
70      res=INDGEN(2, type=types[jj])
71      READS, inarray, res
72      if types[jj] EQ 1 then if ~ICI_ARRAY_EQUAL(exp_B, res, debug=debug) then $
73         ERRORS_ADD, errors, 'basic BYTE bad values !'
74      if types[jj] EQ 2 then if ~ICI_ARRAY_EQUAL(exp_I, res, debug=debug) then $
75         ERRORS_ADD, errors, 'basic INT bad values !'
76      if types[jj] EQ 3 then if ~ICI_ARRAY_EQUAL(exp_L, res, debug=debug) then $
77         ERRORS_ADD, errors, 'basic LONG bad values !'
78      if types[jj] EQ 4 then if ~ICI_ARRAY_EQUAL(exp_F, res, debug=debug) then $
79         if (TOTAL(ABS(exp_F-res)) GT eps) then ERRORS_ADD, errors, 'basic FLOAT bad values !'
80      if types[jj] EQ 5 then if ~ICI_ARRAY_EQUAL(exp_D, res, debug=debug) then $
81         if (TOTAL(ABS(exp_F-res)) GT eps) then ERRORS_ADD, errors, 'basic DOUBLE bad values !'
82      if types[jj] EQ 6 then if ~ICI_ARRAY_EQUAL(exp_C, res, debug=debug) then $
83         if (TOTAL(ABS(exp_F-res)) GT eps) then ERRORS_ADD, errors, 'basic COMPLEX bad values !'
84      if types[jj] EQ 9 then if ~ICI_ARRAY_EQUAL(exp_DC, res, debug=debug) then $
85         if (TOTAL(ABS(exp_F-res)) GT eps) then ERRORS_ADD, errors, 'basic DOUBLE COMPLEX bad values !'
86   endfor
87endfor
88;
89; ----- final ----
90;
91BANNER_FOR_TESTSUITE, 'TEST_READS_BASIC', errors, /status
92ERRORS_CUMUL, cumul_errors, errors
93;
94if KEYWORD_SET(test) then STOP
95;
96end
97;
98; -------------------------------------
99;
100pro TEST_READS_ARRAYS, cumul_errors, verbose=verbose, test=test
101;
102errors=0
103;
104input1= '100,800,600'
105input2= '100 800 600 400'
106;
107ok=EXECUTE('READS, input1, x,y,z')
108;
109if ~ok then ERRORS_ADD, errors, 'EXECUTE failed, first case !'
110;
111if ~ARRAY_EQUAL(x, 100) then ERRORS_ADD, errors, 'c1 bad value X !'
112if ~ARRAY_EQUAL(y, 800) then ERRORS_ADD, errors, 'c1 bad value Y !'
113if ~ARRAY_EQUAL(z, 600) then ERRORS_ADD, errors, 'c1 bad value Z !'
114;
115; we provide only 3 variables for an input with 4 values ...
116;
117ok=EXECUTE('READS, input2, x,y,z')
118;
119if ~ok then ERRORS_ADD, errors, 'EXECUTE failed, second case !'
120;
121if ~ARRAY_EQUAL(x, 100) then ERRORS_ADD, errors, 'c2 bad value X !'
122if ~ARRAY_EQUAL(y, 800) then ERRORS_ADD, errors, 'c2 bad value Y !'
123if ~ARRAY_EQUAL(z, 600) then ERRORS_ADD, errors, 'c2 bad value Z !'
124;
125;
126; ----- final ----
127;
128BANNER_FOR_TESTSUITE, 'TEST_READS_ARRAYS', errors, /status
129ERRORS_CUMUL, cumul_errors, errors
130;
131if KEYWORD_SET(test) then STOP
132;
133end
134;
135; -------------------------------------
136;
137pro TEST_READS_COMPLEX, cumul_errors, verbose=verbose, test=test, debug=debug
138;
139errors=0
140;
141; basic case, one value
142;
143input= '(1,2)'
144var=COMPLEX(0,0)
145ok=EXECUTE('READS, input, var')
146if ~ok then ERRORS_ADD, errors, 'EXECUTE 1 failed !'
147;
148expected=COMPLEX(1,2)
149if ~ICI_ARRAY_EQUAL(expected, var, debug=debug) then $
150   ERRORS_ADD, errors, 'basic complex 1 val, bad values !'
151;
152;----- multiple inputs, same size  Case 1 in #701
153;
154input= '(1,-1) (2,-2) (3,-3)'
155var=COMPLEXARR(3)
156ok=EXECUTE('READS, input, var')
157if ~ok then ERRORS_ADD, errors, 'EXECUTE 2 failed !'
158;
159tmp=INDGEN(3)+1
160expected=COMPLEX(tmp, -tmp)
161if ~ICI_ARRAY_EQUAL(expected, var, debug=debug) then $
162 ERRORS_ADD, errors, 'basic complex 3 val, bad values !'
163;
164;----- multiple inputs, different size
165input= '(1,-1) (2,-2) (3,-3)'
166var=COMPLEXARR(2)
167ok=EXECUTE('READS, input, var')
168if ~ok then ERRORS_ADD, errors, 'EXECUTE 2 failed !'
169;
170tmp=INDGEN(2)+1
171expected=COMPLEX(tmp, -tmp)
172if ~ICI_ARRAY_EQUAL(expected, var, debug=debug) then $
173 ERRORS_ADD, errors, 'basic complex 3 val (bis), bad values !'
174;
175;----- should resist to excess of commas & spaces
176input= '(1,-1) (2,-2),,,,,   (3,-3)'
177var=COMPLEXARR(3)
178ok=EXECUTE('READS, input, var')
179;
180if ~ok then ERRORS_ADD, errors, 'EXECUTE failed !'
181;
182tmp=INDGEN(3)+1
183expected=COMPLEX(tmp, -tmp)
184if ~ICI_ARRAY_EQUAL(expected, var, debug=debug) then $
185 ERRORS_ADD, errors, 'bad format, extra commas, complex bad values !'
186;
187;----- should resist to missing braces too !!
188; case 3 reported in #701 by GD
189;
190input= '12 , 13'
191var=COMPLEXARR(2)
192ok=EXECUTE('READS, input, var')
193if ~ok then ERRORS_ADD, errors, 'EXECUTE failed !'
194;
195tmp=INDGEN(2)+12
196expected=COMPLEX(tmp, 0)
197if ~ICI_ARRAY_EQUAL(expected, var, debug=debug) then $
198 ERRORS_ADD, errors, 'bad format, no brace, complex bad values !'
199;
200;----- should resist to mixed format : case 2 reported in #701 by GD
201;
202input='  ( 12,13) 18 19'
203var=COMPLEXARR(3)
204ok=EXECUTE('READS, input, var')
205if ~ok then ERRORS_ADD, errors, 'EXECUTE failed !'
206;
207expected=COMPLEXARR(3)
208expected[0]=COMPLEX(12,13)
209expected[1]=18
210expected[2]=19
211if ~ICI_ARRAY_EQUAL(expected, var, debug=debug) then $
212 ERRORS_ADD, errors, 'mixed format, complex bad values !'
213;
214; ----- final ----
215;
216BANNER_FOR_TESTSUITE, 'TEST_READS_COMPLEX', errors, /status
217ERRORS_CUMUL, cumul_errors, errors
218;
219if KEYWORD_SET(test) then STOP
220;
221end
222;
223; -------------------------------------
224;
225pro TEST_READS_STRING, cumul_errors, verbose=verbose, test=test
226;
227errors=0
228;
229input= '1000,800,600'
230var=FLTARR(3)
231ok=EXECUTE('READS, input, var')
232;
233if ~ok then ERRORS_ADD, errors, 'EXECUTE failed !'
234;
235expected=[1000,800,600]
236if ~ARRAY_EQUAL(expected, var) then $
237   ERRORS_ADD, errors, 'bad values !'
238;
239; ----- final ----
240;
241BANNER_FOR_TESTSUITE, 'TEST_READS_STRING', errors, /status
242ERRORS_CUMUL, cumul_errors, errors
243;
244if KEYWORD_SET(test) then STOP
245;
246end
247;
248; -------------------------------------
249;
250; following
251; http://www.physics.nyu.edu/grierlab/idl_html_help/files11.html
252; the rules for "free format input" are: [...]
253; 4. Input data must be separated by commas or white space (tabs, spaces, or new lines).
254;
255pro TEST_READS_MIXED, cumul_errors, verbose=verbose, test=test
256;
257errors=0
258;
259; derived from http://www.iac.es/sieinvens/SINFIN/CursoIDL/idlpp3.php
260;
261; separated by spaces
262thisisheader='10 12 2001 This is the date of my file'
263; separeted by comma
264thisisheader2='10,12,2001,This is the date of my file'
265;
266; expected results
267;
268expected1=[10,12,2001]
269expected2a=' This is the date of my file'
270expected2b=',This is the date of my file'
271;
272day=0
273month=0
274year=0
275todaystring1=''
276todaystring2=''
277;
278if KEYWORD_SET(verbose) then $
279   MESSAGE, /continue, 'Case 1 : separator is a space >> <<'
280errors1=0
281;
282ok1=EXECUTE('READS, thisisheader, day, month, year, todaystring1')
283res1=[day, month, year]
284if ~ok1 then ERRORS_ADD, errors1, '(1) EXECUTE failed !'
285if ~ARRAY_EQUAL(expected1, res1) then $
286   ERRORS_ADD, errors1, '(1) bad numerical values D/M/Y !'
287if ~ARRAY_EQUAL(expected2a, todaystring1) then   $
288   ERRORS_ADD, errors1, '(1) bad string value !'
289if KEYWORD_SET(verbose) then $
290   if (errors1 EQ 0) then MESSAGE, /continue, 'Case 1 : succesfully done'
291;
292if KEYWORD_SET(verbose) then $
293   MESSAGE, /continue, 'Case 2 : separator is a comma >>,<<'
294errors2=0
295;
296ok2=EXECUTE('READS, thisisheader2, day, month, year, todaystring2')
297res2=[day, month, year]
298if ~ok2 then ERRORS_ADD, errors2, '(2) EXECUTE failed !'
299if ~ARRAY_EQUAL(expected1, res2) then $
300   ERRORS_ADD, errors2, '(2) bad numerical values D/M/Y !'
301if ~ARRAY_EQUAL(expected2b, todaystring2) then  $
302   ERRORS_ADD, errors2, '(2) bad string value !'
303;
304if KEYWORD_SET(verbose) then $
305   if (errors2 EQ 0) then MESSAGE, /continue, 'Case 2 : succesfully done'
306;
307errors=errors1+errors2
308;
309; ----- final ----
310;
311BANNER_FOR_TESTSUITE, 'TEST_READS_MIXED', errors, /status
312ERRORS_CUMUL, cumul_errors, errors
313;
314if KEYWORD_SET(test) then STOP
315;
316end
317;
318; -------------------------------------
319; as mentionned in https://github.com/gnudatalanguage/gdl/issues/701
320;
321pro TEST_READS_COMPLEX2, cumul_errors, verbose=verbose, test=test
322;
323errors=0
324;
325expected=[complex(12,13),complex(18,19)]
326;
327; Du to the way we process the input, we may fail due to extra space(s)
328;
329f=COMPLEXARR(2) & inarray=' ( 12,13) (18,19) '
330READS, inarray, f
331if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 1 : '+inarray
332;
333f=COMPLEXARR(2) & inarray='( 12,13) (18,19) '
334READS, inarray, f
335if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 1 : '+inarray
336;
337f=COMPLEXARR(2) & inarray='(12,13) (18,19) '
338READS, inarray, f
339if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 1 : '+inarray
340;
341f=COMPLEXARR(2) & inarray=' ( 12 13) (18,19) '
342READS, inarray, f
343if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 1 : '+inarray
344;
345f=COMPLEXARR(2) & inarray=' (12 13) (18,19) '
346READS, inarray, f
347if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 1 : '+inarray
348;
349f=COMPLEXARR(2) & inarray='(12 13) (18,19) '
350READS, inarray, f
351if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 1 : '+inarray
352;
353f=COMPLEXARR(2) & inarray='(12 13 ) (18,19) '
354READS, inarray, f
355if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 1 : '+inarray
356;
357f=COMPLEXARR(2) & inarray=' (12 13 ) ( 18,19) '
358READS, inarray, f
359if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 1 : '+inarray
360;
361; ------ some complex without imaginary parts ...
362;
363expected=[COMPLEX(12,13),COMPLEX(18,0),COMPLEX(19,0)]
364;
365f=COMPLEXARR(3) & inarray=' ( 12,13) 18 19'
366READS,inarray,f
367if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 2 : '+inarray
368;
369f=COMPLEXARR(3) & inarray=' ( 12,13) 18, 19'
370READS,inarray,f
371if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 2 : '+inarray
372;
373f=COMPLEXARR(3) & inarray=' ( 12,13   ) 18 19'
374READS,inarray,f
375if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 2 : '+inarray
376;
377; ------ all complex without imaginary parts ...
378;
379expected=[COMPLEX(12,0),COMPLEX(13,0)]
380;
381f=COMPLEXARR(2) & inarray='12,13'
382READS,inarray,f
383if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 3 : '+inarray
384;
385f=COMPLEXARR(2) & inarray='12,13 ,,,,'
386READS,inarray,f
387if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 3 : '+inarray
388;
389f=COMPLEXARR(2) & inarray=' 12, 13'
390READS,inarray,f
391if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 3 : '+inarray
392;
393f=COMPLEXARR(2) & inarray=' ( 12,) 13'
394READS,inarray,f
395if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 3 : '+inarray
396;
397f=COMPLEXARR(2) & inarray=' ( 12) 13'
398READS,inarray,f
399if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 3 : '+inarray
400;
401f=COMPLEXARR(2) & inarray='  12 13'
402READS,inarray,f
403if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 3 : '+inarray
404;
405f=COMPLEXARR(2) & inarray='  (12) ( 13 )'
406READS,inarray,f
407if ~ARRAY_EQUAL(expected, f) then ERRORS_ADD, errors, 'case 3 : '+inarray
408;
409; ----- final ----
410;
411BANNER_FOR_TESTSUITE, 'TEST_READS_COMPLEX2', errors, /status
412ERRORS_CUMUL, cumul_errors, errors
413;
414if KEYWORD_SET(test) then STOP
415;
416end
417;
418; -------------------------------------
419;
420pro TEST_READS_MIXING_TYPES, cumul_errors, verbose=verbose, test=test, debug=debug
421;
422errors=0
423;
424expected_c=[COMPLEX(12,13),COMPLEX(18,19),COMPLEX(-1)]
425expected_f=[6.,-5.]
426;
427c=COMPLEXARR(3) & f=FLTARR(2) & inarray=' ( 12,13   ) (18 19) (-1 0) 6, -5 '
428READS,inarray, c, f
429if ~ICI_ARRAY_EQUAL(expected_c, c, debug=debug) then ERRORS_ADD, errors, 'case mix C : '+inarray
430if ~ICI_ARRAY_EQUAL(expected_f, f, debug=debug) then ERRORS_ADD, errors, 'case mix F : '+inarray
431;
432c=COMPLEXARR(3) & f=FLTARR(2) & inarray=' ( 12,13   ) (18 19) -1 6, -5 '
433READS,inarray, c, f
434if ~ICI_ARRAY_EQUAL(expected_c, c, debug=debug) then ERRORS_ADD, errors, 'case mix C : '+inarray
435if ~ICI_ARRAY_EQUAL(expected_f, f, debug=debug) then ERRORS_ADD, errors, 'case mix F : '+inarray
436;
437c=COMPLEXARR(3) & f=FLTARR(2) & inarray=' (12 13) (18, 19) -1, 6, -5 '
438READS,inarray, c, f
439if ~ICI_ARRAY_EQUAL(expected_c, c, debug=debug) then ERRORS_ADD, errors, 'case mix C : '+inarray
440if ~ICI_ARRAY_EQUAL(expected_f, f, debug=debug) then ERRORS_ADD, errors, 'case mix F : '+inarray
441;
442c=COMPLEXARR(3) & f=FLTARR(2) & inarray=' (12 13) (18, 19) -1, 6  -5'
443READS,inarray, c, f
444if ~ICI_ARRAY_EQUAL(expected_c, c, debug=debug) then ERRORS_ADD, errors, 'case mix C : '+inarray
445if ~ICI_ARRAY_EQUAL(expected_f, f, debug=debug) then ERRORS_ADD, errors, 'case mix F : '+inarray
446;
447c=COMPLEXARR(3) & f=FLTARR(2) & inarray=' (12 13) (18, 19) -1, 6  -5, 56, 7'
448READS,inarray, c, f
449if ~ICI_ARRAY_EQUAL(expected_c, c, debug=debug) then ERRORS_ADD, errors, 'case mix C : '+inarray
450if ~ICI_ARRAY_EQUAL(expected_f, f, debug=debug) then ERRORS_ADD, errors, 'case mix F : '+inarray
451
452
453;f=complexarr(3) & a=intarr(2) & inarray=' (  12,),,, 14,13,5,7' & reads,inarray,f,a & print, f, a
454
455
456
457; ----- final ----
458;
459BANNER_FOR_TESTSUITE, 'TEST_READS_MIXING_TYPES', errors, /status
460ERRORS_CUMUL, cumul_errors, errors
461;
462if KEYWORD_SET(test) then STOP
463;
464end
465;
466; -------------------------------------
467;
468pro TEST_READS, help=help, verbose=verbose, no_exit=no_exit, test=test
469;
470if KEYWORD_SET(help) then begin
471    print, 'pro '+'TEST_READS_'+', help=help, verbose=verbose, $'
472    print, '                no_exit=no_exit, test=test'
473    return
474endif
475;
476errors=0
477;
478TEST_READS_BASIC, errors, verbose=verbose, test=test
479TEST_READS_BASIC, errors, /decimal, verbose=verbose, test=test
480;
481TEST_READS_ARRAYS, errors, verbose=verbose, test=test
482;
483TEST_READS_COMPLEX, errors, verbose=verbose, test=test
484TEST_READS_COMPLEX2, errors, verbose=verbose, test=test
485;
486TEST_READS_STRING, errors, verbose=verbose, test=test
487;
488TEST_READS_MIXED, errors, verbose=verbose, test=test
489;
490TEST_READS_MIXING_TYPES, errors, verbose=verbose, test=test
491;
492; ----------------- final message ----------
493;
494BANNER_FOR_TESTSUITE, 'TEST_READS', errors, /status
495;
496if (errors GT 0) AND ~KEYWORD_SET(no_exit) then EXIT, status=1
497;
498if KEYWORD_SET(test) then STOP
499;
500end
501