* Bug 13303 fixed: mprintf msprintf mfprintf now accept input booleans
[scilab.git] / scilab / modules / output_stream / tests / unit_tests / msprintf.tst
1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2008 - INRIA
4 // Copyright (C) 2013 - Scilab Enterprises - Adeline CARNIS
5 // Copyright (C) 2020 - Samuel GOUGEON - Le Mans Université
6 //
7 //  This file is distributed under the same license as the Scilab package.
8 // =============================================================================
9
10 // <-- CLI SHELL MODE -->
11 // <-- NO CHECK REF -->
12
13 // unit tests for msprintf function
14 // =============================================================================
15
16 // format '%f'
17 // =============================================================================
18
19 assert_checkequal(msprintf("%f",-35), "-35.000000");
20
21 assert_checkequal(msprintf("%f",35.55), "35.550000");
22 assert_checkequal(msprintf("%f",0.00433), "0.004330");
23 assert_checkequal(msprintf("%f",0.0000000345456), "0.000000");
24 assert_checkequal(msprintf("%f",1112423453), "1112423453.000000");
25
26 assert_checkequal(msprintf("%15f",-35), "     -35.000000");
27 assert_checkequal(msprintf("%15f",0.00433), "       0.004330");
28 assert_checkequal(msprintf("%15f",0.0000000345456), "       0.000000");
29 assert_checkequal(msprintf("%15f",1112423453), "1112423453.000000");
30
31 assert_checkequal(msprintf("%.1f" ,-35), "-35.0");
32 assert_checkequal(msprintf("%.0f" ,-35), "-35");
33 assert_checkequal(msprintf("%#.0f",-35), "-35.");
34 assert_checkequal(msprintf("%.1f" ,0.00433), "0.0");
35 assert_checkequal(msprintf("%.15f",0.0000000345456), "0.000000034545600");
36 assert_checkequal(msprintf("%.1f" ,11124234534545), "11124234534545.0");
37
38 // format '%g'
39 // =============================================================================
40
41 assert_checkequal(msprintf("%g" ,-35), "-35");
42 assert_checkequal(msprintf("%g" ,35.55), "35.55");
43 assert_checkequal(msprintf("%g" ,35.551234567890), "35.5512");
44 assert_checkequal(msprintf("%+g",35.551234567890), "+35.5512");
45 assert_checkequal(msprintf("%g" ,0.00433), "0.00433");
46
47 assert_checkequal(msprintf("%g" ,0.0000000345456), "3.45456e-08");
48
49 assert_checkequal(msprintf("%g" ,11124234534545), "1.11242e+13");
50
51 assert_checkequal(msprintf("%15g",-35), "            -35");
52 assert_checkequal(msprintf("%15g",0.00433), "        0.00433");
53
54 assert_checkequal(msprintf("%15g",0.0000000345456), "    3.45456e-08");
55
56 assert_checkequal(msprintf("%15g",11124234534545), "    1.11242e+13");
57
58 assert_checkequal(msprintf("%.1g",-35.1), "-4e+01");
59
60 assert_checkequal(msprintf("%.0g",-35.1), "-4e+01");
61
62 assert_checkequal(msprintf("%#.0g",-35.1), "-4.e+01");
63
64 assert_checkequal(msprintf("%#.0G",-35.1), "-4.E+01");
65
66 assert_checkequal(msprintf("%.1g",0.00433), "0.004");
67
68 assert_checkequal(msprintf("%.15g",0.0000000345456), "3.45456e-08");
69
70 assert_checkequal(msprintf("%.1g",11124234534545), "1e+13");
71
72 // format '%e'
73 // =============================================================================
74
75 assert_checkequal(msprintf("%e",-35), "-3.500000e+01");
76
77 assert_checkequal(msprintf("%e",35.55), "3.555000e+01");
78
79 assert_checkequal(msprintf("%+e",35.55), "+3.555000e+01");
80
81 assert_checkequal(msprintf("%e",35.551234567890), "3.555123e+01");
82
83 assert_checkequal(msprintf("%e",0.00433), "4.330000e-03");
84
85 assert_checkequal(msprintf("%e",0.0000000345456), "3.454560e-08");
86
87 assert_checkequal(msprintf("%e",11124234534545), "1.112423e+13");
88
89 assert_checkequal(msprintf("%E",11124234534545), "1.112423E+13");
90
91 assert_checkequal(msprintf("%15e",-35), "  -3.500000e+01");
92
93 assert_checkequal(msprintf("%15e",0.00433), "   4.330000e-03");
94
95 assert_checkequal(msprintf("%15e",0.0000000345456), "   3.454560e-08");
96
97 assert_checkequal(msprintf("%+15e",0.0000000345456), "  +3.454560e-08");
98
99 assert_checkequal(msprintf("%15e",11124234534545), "   1.112423e+13");
100
101 assert_checkequal(msprintf("%.1e",-35), "-3.5e+01");
102
103 assert_checkequal(msprintf("%.0e",-35.1), "-4e+01");
104
105 assert_checkequal(msprintf("%.1e",0.00433), "4.3e-03");
106
107 assert_checkequal(msprintf("%.15e",0.0000000345456), "3.454560000000000e-08");
108
109 assert_checkequal(msprintf("%.1e",11124234534545), "1.1e+13");
110
111 // format '%c'
112 // =============================================================================
113
114 assert_checkequal(msprintf("%c","t"), "t");
115 assert_checkequal(msprintf("%10c","t"), "         t");
116 assert_checkequal(msprintf("%10.3c","t"), "         t");
117 assert_checkequal(msprintf("%-10c","t"), "t         ");
118
119 // format '%s'
120 // =============================================================================
121
122 assert_checkequal(msprintf("%s","text"), "text");
123 assert_checkequal(msprintf("%10s","text"), "      text");
124 assert_checkequal(msprintf("%10.3s","text"), "       tex");
125 assert_checkequal(msprintf("%-10s","text"), "text      ");
126 assert_checkequal(msprintf("%s","t"), "t");
127 assert_checkequal(msprintf("%s","éàöαβδ"), "éàöαβδ");
128
129 // format '%x'
130 // =============================================================================
131
132 assert_checkequal(msprintf("%x",123), "7b");
133 assert_checkequal(msprintf("%.10x",123), "000000007b");
134 assert_checkequal(msprintf("%x",-123), "ffffff85");
135 assert_checkequal(msprintf("%X",-123), "FFFFFF85");
136 assert_checkequal(msprintf("%#.3X",12), "0X00C");
137
138 // format '%o'
139 // =============================================================================
140 assert_checkequal(msprintf("%015o",-12), "000037777777764");
141
142 // Vectorisation
143 // =============================================================================
144
145 nb_row = 10000;
146
147 // test 1
148
149 A = "row "+string(1:nb_row)';
150 B = 100*rand(nb_row,3);
151 C = msprintf("%10s => %08.4f %08.4f %08.4f\n",A,B);
152
153 assert_checkequal(size(C), [nb_row,1]);
154
155 for i=1:nb_row
156     assert_checkequal(length(C(i)), 40);
157 end
158
159 // test 2
160 B = 100*rand(nb_row,2);
161 C = 100*rand(nb_row,1);
162 D = msprintf("%10s => %08.4f %08.4f %08.4f\n",A,B,C);
163
164 assert_checkequal(size(D), [nb_row,1]);
165
166 for i=1:nb_row
167     assert_checkequal(size(D), [nb_row,1]);
168 end
169
170 // test 3
171 B = 100*rand(nb_row,1);
172 C = 100*rand(nb_row,1);
173 D = 100*rand(nb_row,1);
174 E = msprintf("%10s => %08.4f %08.4f %08.4f\n",A,B,C,D);
175
176 assert_checkequal(size(E), [nb_row,1]);
177
178 for i=1:nb_row
179     assert_checkequal(size(E), [nb_row,1]);
180 end
181
182 // test 4
183 B = 100*rand(nb_row,1);
184
185 assert_checkfalse(execstr("msprintf(""%10s => %08.4f %08.4f %08.4f\n"",A,B,B);","errcatch")     == 0);
186 refMsg = msprintf(_("%s: Wrong number of input arguments: data doesn''t fit with format.\n"), "msprintf");
187 assert_checkerror("msprintf(""%10s => %08.4f %08.4f %08.4f\n"",A,B,B);", refMsg);
188
189 // No arg
190 assert_checkfalse(execstr("msprintf();","errcatch")     == 0);
191 refMsg = msprintf(_("%s: Wrong number of input arguments: at least %d expected.\n"), "msprintf", 1);
192 assert_checkerror("msprintf();", refMsg);
193
194 // overload: Arg not managed
195 s=poly(0,"s");
196 p=1+s+2*s^2;
197
198 assert_checkfalse(execstr("msprintf(""plop"",p);","errcatch") <> 999);
199
200 assert_checkfalse(execstr("msprintf(""%s %s"",""plop"");","errcatch")     <> 999);
201 refMsg = msprintf(_("%s: Wrong number of input arguments: data doesn''t fit with format.\n"), "msprintf");
202 assert_checkerror("msprintf(""%s %s"",""plop"");", refMsg);
203
204 // parameter field
205 // =============================================================================
206
207 assert_checkequal(msprintf("%1$d",1), "1");
208 assert_checkequal(msprintf("%1$d%2$d", 1, 2), "12");
209 assert_checkequal(msprintf("%2$d%1$d", 1, 2), "21");
210
211 refMsg = msprintf(_("%s: Wrong number of input arguments: data doesn''t fit with format.\n"), "msprintf");
212 assert_checkerror("msprintf(""%2$d"", 1);", refMsg);
213 assert_checkerror("msprintf(""%d%1$d"", 1);", refMsg);
214 assert_checkerror("msprintf(""%1$d%d"", 1);", refMsg);
215
216 // Booleans
217 // =============================================================================
218 n = [%pi ; %e];
219 b = [%T ; %F];
220 for f = ["d" "i" "u" "o" "x" "X" "g" "G"]
221     assert_checkequal(msprintf("%"+f+"\n", b), ["1" ; "0"]);
222     assert_checkequal(msprintf("%"+f+" %d\n", b, n), ["1 3" ; "0 2"]);
223     if and(f <> ["u" "o" "x" "X"])  // http://bugzilla.scilab.org/16563
224         assert_checkequal(msprintf("%2$"+f+" %1$d\n", n, b), ["1 3" ; "0 2"]);
225     end
226 end
227 // %f
228 assert_checkequal(msprintf("%f\n", b), ["1.000000" ; "0.000000"]);
229 assert_checkequal(msprintf("%f %d\n", b, n), ["1.000000 3" ; "0.000000 2"]);
230 assert_checkequal(msprintf("%2$f %1$d\n", n, b), ["1.000000 3" ; "0.000000 2"]);
231 // %e
232 assert_checkequal(msprintf("%e\n", b), ["1.000000e+00" ; "0.000000e+00"]);
233 assert_checkequal(msprintf("%e %d\n", b, n), ["1.000000e+00 3" ; "0.000000e+00 2"]);
234 assert_checkequal(msprintf("%2$e %1$d\n", n, b), ["1.000000e+00 3" ; "0.000000e+00 2"]);
235 // %E
236 assert_checkequal(msprintf("%E\n", b), ["1.000000E+00" ; "0.000000E+00"]);
237 assert_checkequal(msprintf("%E %d\n", b, n), ["1.000000E+00 3" ; "0.000000E+00 2"]);
238 assert_checkequal(msprintf("%2$E %1$d\n", n, b), ["1.000000E+00 3" ; "0.000000E+00 2"]);
239 // %s, %c
240 for f = ["s" "c"]
241     assert_checkequal(msprintf("%"+f+"\n", b), ["T" ; "F"]);
242     assert_checkequal(msprintf("%"+f+" %d\n", b, n), ["T 3" ; "F 2"]);
243     assert_checkequal(msprintf("%2$"+f+" %1$d\n", n, b), ["T 3" ; "F 2"]);
244 end