* Bugs 15838 15839 15842 16452 16454 fixed: gsort() for all sparse in all modes
[scilab.git] / scilab / modules / elementary_functions / tests / unit_tests / gsort_boolean.tst
1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 //
4 // Copyright (C) 2019 - Samuel GOUGEON
5 //
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
12 // =============================================================================
13 //
14 // NOTE: gsort() is tested for sparse booleans in gsort_sparse.tst
15 //
16 // <-- CLI SHELL MODE -->
17 // <-- NO CHECK REF -->
18
19 // Scalar
20 assert_checkequal(gsort(%t), %t);
21 assert_checkequal(gsort(%f), %f);
22
23 // Vector
24 b = [%f %t %f %t ];
25 assert_checkequal(gsort(b),  [%t %t %f %f]);
26 assert_checkequal(gsort(b'), [%t %t %f %f]');
27 assert_checkequal(gsort(b,"g","i"),  [%f %f %t %t]);
28 assert_checkequal(gsort(b',"g","i"), [%f %f %t %t]');
29
30 assert_checkequal(gsort(b,"r"), b);
31 assert_checkequal(gsort(b,"r","i"), b);
32 assert_checkequal(gsort(b',"c"),    b');
33 assert_checkequal(gsort(b',"c","i"), b');
34
35 assert_checkequal(gsort(b,"lr"), b);
36 assert_checkequal(gsort(b,"lr","i"), b);
37 assert_checkequal(gsort(b',"lc"),    b');
38 assert_checkequal(gsort(b',"lc","i"), b');
39
40 // Matrix
41 b = [%f,%t,%t,%t,%t;%f,%f,%f,%f,%t;%t,%f,%t,%f,%f]; // (3x5)
42 assert_checkequal(gsort(b), [%t,%t,%t,%f,%f;%t,%t,%f,%f,%f;%t,%t,%f,%f,%f]);
43 assert_checkequal(gsort(b,"g","i"), [%f,%f,%f,%t,%t;%f,%f,%f,%t,%t;%f,%f,%t,%t,%t]);
44
45 ref = [%t,%t,%t,%t,%t;%f,%f,%t,%f,%t;%f,%f,%f,%f,%f];
46 assert_checkequal(gsort(b,"r"), ref);
47 assert_checkequal(gsort(b,"r","i"), ref($:-1:1,:));
48 ref = [%t,%t,%t,%t,%f;%t,%f,%f,%f,%f;%t,%t,%f,%f,%f];
49 assert_checkequal(gsort(b,"c"), ref);
50 assert_checkequal(gsort(b,"c","i"), ref(:,$:-1:1));
51
52 ref = [%t,%f,%t,%f,%f;%f,%t,%t,%t,%t;%f,%f,%f,%f,%t];
53 assert_checkequal(gsort(b,"lr"), ref);
54 assert_checkequal(gsort(b,"lr","i"), ref($:-1:1,:));
55 ref = [%t,%t,%t,%t,%f;%t,%f,%f,%f,%f;%f,%t,%f,%f,%t];
56 assert_checkequal(gsort(b,"lc"), ref);
57 assert_checkequal(gsort(b,"lc","i"), ref(:,$:-1:1));
58
59 // Hypermatrix
60 b = cat(3,[%f %t %f %t ; %t %t %t %f ; %f %t %f %t], ..
61           [%t %t %f %f ; %t %t %f %f ; %f %t %f %t]);
62 s = [3 4 2];
63 // "g"
64 ref = matrix([%t,%t,%t,%t,%t,%t,%t,%t,%t,%t,%t,%t,%t,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f],s);
65 assert_checkequal(gsort(b), ref);
66 ref = matrix([%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%t,%t,%t,%t,%t,%t,%t,%t,%t,%t,%t,%t,%t],s);
67 assert_checkequal(gsort(b,"g","i"), ref);
68 // "r"
69 ref = matrix([%t,%f,%f,%t,%t,%t,%t,%f,%f,%t,%t,%f,%t,%t,%f,%t,%t,%t,%f,%f,%f,%t,%f,%f],s);
70 assert_checkequal(gsort(b,"r"), ref);
71 ref = matrix([%f,%f,%t,%t,%t,%t,%f,%f,%t,%f,%t,%t,%f,%t,%t,%t,%t,%t,%f,%f,%f,%f,%f,%t],s);
72 assert_checkequal(gsort(b,"r","i"), ref);
73 // "c"
74 ref = matrix([%t,%t,%t,%t,%t,%t,%f,%t,%f,%f,%f,%f,%t,%t,%t,%t,%t,%t,%f,%f,%f,%f,%f,%f],s);
75 assert_checkequal(gsort(b,"c"), ref);
76 ref = matrix([%f,%f,%f,%f,%t,%f,%t,%t,%t,%t,%t,%t,%f,%f,%f,%f,%f,%f,%t,%t,%t,%t,%t,%t],s);
77 assert_checkequal(gsort(b,"c","i"), ref);
78 // "lr"
79 ref = matrix([%t,%f,%f,%t,%t,%t,%t,%f,%f,%f,%t,%t,%t,%t,%f,%t,%t,%t,%f,%f,%f,%f,%f,%t],s);
80 assert_checkequal(gsort(b,"lr"), ref);
81 ref = matrix([%f,%f,%t,%t,%t,%t,%f,%f,%t,%t,%t,%f,%f,%t,%t,%t,%t,%t,%f,%f,%f,%t,%f,%f],s);
82 assert_checkequal(gsort(b,"lr","i"), ref);
83 // "lc"
84 ref = matrix([%t,%t,%t,%t,%f,%t,%f,%t,%f,%f,%t,%f,%t,%t,%t,%t,%t,%f,%f,%f,%t,%f,%f,%f],s);
85 assert_checkequal(gsort(b,"lc"), ref);
86 ref = matrix([%f,%t,%f,%f,%t,%f,%t,%f,%t,%t,%t,%t,%f,%f,%f,%f,%f,%t,%t,%t,%f,%t,%t,%t],s);
87 assert_checkequal(gsort(b,"lc","i"), ref);
88
89 // ========
90 // K output
91 // ========
92 // Matrix
93 // ------
94 // With some equal rows and cols, to check that initial order is kept
95 b = [%f %t %f %t ; %t %t %t %f ; %f %t %f %t];
96 // "r"
97 [?,k] = gsort(b, "r");
98 assert_checkequal(k, [2,1,2,1;1,2,1,3;3,3,3,2]);
99 [?,k] = gsort(b, "r", "i");
100 assert_checkequal(k, [1,1,1,2;3,2,3,1;2,3,2,3]);
101 // "c"
102 [?,k] = gsort(b, "c");
103 assert_checkequal(k, [2,4,1,3;1,2,3,4;2,4,1,3]);
104 [?,k] = gsort(b, "c", "i");
105 assert_checkequal(k, [1,3,2,4;4,1,2,3;1,3,2,4]);
106 // "lr"
107 [?,k] = gsort(b, "lr");
108 assert_checkequal(k, [2 1 3]');
109 [?,k] = gsort(b, "lr", "i");
110 assert_checkequal(k, [1 3 2]');
111 // "lc"
112 [?,k] = gsort(b, "lc");
113 assert_checkequal(k, [2 4 1 3]);
114 [?,k] = gsort(b, "lc", "i");
115 assert_checkequal(k, [1 3 4 2]);
116
117 // Hypermatrix
118 // -----------
119 b = cat(3,[%f %t %f %t ; %t %t %t %f ; %f %t %f %t], ..
120           [%t %t %f %f ; %t %t %f %f ; %f %t %f %t]);
121 s = [3 4 2];
122 // "r"
123 [?,k] = gsort(b, "r");
124 assert_checkequal(k, matrix([2,1,3,1,2,3,2,1,3,1,3,2,1,2,3,1,2,3,1,2,3,3,1,2],s));
125 [?,k] = gsort(b, "r", "i");
126 assert_checkequal(k, matrix([1,3,2,1,2,3,1,3,2,2,1,3,3,1,2,1,2,3,1,2,3,1,2,3],s));
127 // "c"
128 [?,k] = gsort(b, "c");
129 assert_checkequal(k, matrix([2,1,2,4,2,4,1,3,1,3,4,3,1,1,2,2,2,4,3,3,1,4,4,3],s));
130 [?,k] = gsort(b, "c", "i");
131 assert_checkequal(k, matrix([1,4,1,3,1,3,2,2,2,4,3,4,3,3,1,4,4,3,1,1,2,2,2,4],s));
132 // "lr"
133 [?,k] = gsort(b, "lr");
134 assert_checkequal(k, cat(3,[2 1 3]', [1 2 3]'));
135 [?,k] = gsort(b, "lr", "i");
136 assert_checkequal(k, cat(3,[1 3 2]', [3 1 2]') );
137 // "lc"
138 [?,k] = gsort(b, "lc");
139 assert_checkequal(k, cat(3,[2 4 1 3], [2 1 4 3]));
140 [?,k] = gsort(b, "lc", "i");
141 assert_checkequal(k, cat(3,[1 3 4 2], [3 4 1 2]));