bug 13972 : use of wildcard '*' in printf expressions was not managed
[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 //
6 //  This file is distributed under the same license as the Scilab package.
7 // =============================================================================
8
9 // <-- CLI SHELL MODE -->
10
11 // unit tests for msprintf function
12 // =============================================================================
13
14 // format '%f'
15 // =============================================================================
16
17 assert_checkequal(msprintf("%f",-35), "-35.000000");
18
19 assert_checkequal(msprintf("%f",35.55), "35.550000");
20 assert_checkequal(msprintf("%f",0.00433), "0.004330");
21 assert_checkequal(msprintf("%f",0.0000000345456), "0.000000");
22 assert_checkequal(msprintf("%f",1112423453), "1112423453.000000");
23
24 assert_checkequal(msprintf("%15f",-35), "     -35.000000");
25 assert_checkequal(msprintf("%15f",0.00433), "       0.004330");
26 assert_checkequal(msprintf("%15f",0.0000000345456), "       0.000000");
27 assert_checkequal(msprintf("%15f",1112423453), "1112423453.000000");
28
29 assert_checkequal(msprintf("%.1f" ,-35), "-35.0");
30 assert_checkequal(msprintf("%.0f" ,-35), "-35");
31 assert_checkequal(msprintf("%#.0f",-35), "-35.");
32 assert_checkequal(msprintf("%.1f" ,0.00433), "0.0");
33 assert_checkequal(msprintf("%.15f",0.0000000345456), "0.000000034545600");
34 assert_checkequal(msprintf("%.1f" ,11124234534545), "11124234534545.0");
35
36 // format '%g'
37 // =============================================================================
38
39 assert_checkequal(msprintf("%g" ,-35), "-35");
40 assert_checkequal(msprintf("%g" ,35.55), "35.55");
41 assert_checkequal(msprintf("%g" ,35.551234567890), "35.5512");
42 assert_checkequal(msprintf("%+g",35.551234567890), "+35.5512");
43 assert_checkequal(msprintf("%g" ,0.00433), "0.00433");
44
45 assert_checkequal(msprintf("%g" ,0.0000000345456), "3.45456e-08");
46
47 assert_checkequal(msprintf("%g" ,11124234534545), "1.11242e+13");
48
49 assert_checkequal(msprintf("%15g",-35), "            -35");
50 assert_checkequal(msprintf("%15g",0.00433), "        0.00433");
51
52 assert_checkequal(msprintf("%15g",0.0000000345456), "    3.45456e-08");
53
54 assert_checkequal(msprintf("%15g",11124234534545), "    1.11242e+13");
55
56 assert_checkequal(msprintf("%.1g",-35.1), "-4e+01");
57
58 assert_checkequal(msprintf("%.0g",-35.1), "-4e+01");
59
60 assert_checkequal(msprintf("%#.0g",-35.1), "-4.e+01");
61
62 assert_checkequal(msprintf("%#.0G",-35.1), "-4.E+01");
63
64 assert_checkequal(msprintf("%.1g",0.00433), "0.004");
65
66 assert_checkequal(msprintf("%.15g",0.0000000345456), "3.45456e-08");
67
68 assert_checkequal(msprintf("%.1g",11124234534545), "1e+13");
69
70 // format '%e'
71 // =============================================================================
72
73 assert_checkequal(msprintf("%e",-35), "-3.500000e+01");
74
75 assert_checkequal(msprintf("%e",35.55), "3.555000e+01");
76
77 assert_checkequal(msprintf("%+e",35.55), "+3.555000e+01");
78
79 assert_checkequal(msprintf("%e",35.551234567890), "3.555123e+01");
80
81 assert_checkequal(msprintf("%e",0.00433), "4.330000e-03");
82
83 assert_checkequal(msprintf("%e",0.0000000345456), "3.454560e-08");
84
85 assert_checkequal(msprintf("%e",11124234534545), "1.112423e+13");
86
87 assert_checkequal(msprintf("%E",11124234534545), "1.112423E+13");
88
89 assert_checkequal(msprintf("%15e",-35), "  -3.500000e+01");
90
91 assert_checkequal(msprintf("%15e",0.00433), "   4.330000e-03");
92
93 assert_checkequal(msprintf("%15e",0.0000000345456), "   3.454560e-08");
94
95 assert_checkequal(msprintf("%+15e",0.0000000345456), "  +3.454560e-08");
96
97 assert_checkequal(msprintf("%15e",11124234534545), "   1.112423e+13");
98
99 assert_checkequal(msprintf("%.1e",-35), "-3.5e+01");
100
101 assert_checkequal(msprintf("%.0e",-35.1), "-4e+01");
102
103 assert_checkequal(msprintf("%.1e",0.00433), "4.3e-03");
104
105 assert_checkequal(msprintf("%.15e",0.0000000345456), "3.454560000000000e-08");
106
107 assert_checkequal(msprintf("%.1e",11124234534545), "1.1e+13");
108
109 // format '%c'
110 // =============================================================================
111
112 assert_checkequal(msprintf("%c","t"), "t");
113 assert_checkequal(msprintf("%10c","t"), "         t");
114 assert_checkequal(msprintf("%10.3c","t"), "         t");
115 assert_checkequal(msprintf("%-10c","t"), "t         ");
116
117 // format '%s'
118 // =============================================================================
119
120 assert_checkequal(msprintf("%s","text"), "text");
121 assert_checkequal(msprintf("%10s","text"), "      text");
122 assert_checkequal(msprintf("%10.3s","text"), "       tex");
123 assert_checkequal(msprintf("%-10s","text"), "text      ");
124 assert_checkequal(msprintf("%s","t"), "t");
125
126 // format '%x'
127 // =============================================================================
128
129 assert_checkequal(msprintf("%x",123), "7b");
130 assert_checkequal(msprintf("%.10x",123), "000000007b");
131 assert_checkequal(msprintf("%x",-123), "ffffff85");
132 assert_checkequal(msprintf("%X",-123), "FFFFFF85");
133 assert_checkequal(msprintf("%#.3X",12), "0X00C");
134
135 // format '%o'
136 // =============================================================================
137 assert_checkequal(msprintf("%015o",-12), "000037777777764");
138
139 // Vectorisation
140 // =============================================================================
141
142 nb_row = 10000;
143
144 // test 1
145
146 A = "row "+string(1:nb_row)';
147 B = 100*rand(nb_row,3);
148 C = msprintf("%10s => %08.4f %08.4f %08.4f\n",A,B);
149
150 assert_checkequal(size(C), [nb_row,1]);
151
152 for i=1:nb_row
153     assert_checkequal(length(C(i)), 40);
154 end
155
156 // test 2
157 B = 100*rand(nb_row,2);
158 C = 100*rand(nb_row,1);
159 D = msprintf("%10s => %08.4f %08.4f %08.4f\n",A,B,C);
160
161 assert_checkequal(size(D), [nb_row,1]);
162
163 for i=1:nb_row
164     assert_checkequal(size(D), [nb_row,1]);
165 end
166
167 // test 3
168 B = 100*rand(nb_row,1);
169 C = 100*rand(nb_row,1);
170 D = 100*rand(nb_row,1);
171 E = msprintf("%10s => %08.4f %08.4f %08.4f\n",A,B,C,D);
172
173 assert_checkequal(size(E), [nb_row,1]);
174
175 for i=1:nb_row
176     assert_checkequal(size(E), [nb_row,1]);
177 end
178
179 // test 4
180 B = 100*rand(nb_row,1);
181
182 assert_checkfalse(execstr("msprintf(""%10s => %08.4f %08.4f %08.4f\n"",A,B,B);","errcatch")     == 0);
183 refMsg = msprintf(_("%s: Wrong number of input arguments: data doesn''t fit with format.\n"), "msprintf");
184 assert_checkerror("msprintf(""%10s => %08.4f %08.4f %08.4f\n"",A,B,B);", refMsg);
185
186 // No arg
187 assert_checkfalse(execstr("msprintf();","errcatch")     == 0);
188 refMsg = msprintf(_("%s: Wrong number of input arguments: at least %d expected.\n"), "msprintf", 1);
189 assert_checkerror("msprintf();", refMsg);
190
191 // overload: Arg not managed
192 s=poly(0,"s");
193 p=1+s+2*s^2;
194
195 assert_checkfalse(execstr("msprintf(""plop"",p);","errcatch") <> 999);
196
197 assert_checkfalse(execstr("msprintf(""%s %s"",""plop"");","errcatch")     <> 999);
198 refMsg = msprintf(_("%s: Wrong number of input arguments: data doesn''t fit with format.\n"), "msprintf");
199 assert_checkerror("msprintf(""%s %s"",""plop"");", refMsg);
200