d018c732c46478e42c9e2c9210b18476a9273c49
[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 // <-- CLI SHELL MODE -->
15 // <-- NO CHECK REF -->
16
17 // Scalar
18 assert_checkequal(gsort(%t), %t);
19 assert_checkequal(gsort(%f), %f);
20
21 // Vector
22 b = [%f %t %f %t ];
23 assert_checkequal(gsort(b),  [%t %t %f %f]);
24 assert_checkequal(gsort(b'), [%t %t %f %f]');
25 assert_checkequal(gsort(b,"g","i"),  [%f %f %t %t]);
26 assert_checkequal(gsort(b',"g","i"), [%f %f %t %t]');
27
28 assert_checkequal(gsort(b,"r"), b);
29 assert_checkequal(gsort(b,"r","i"), b);
30 assert_checkequal(gsort(b',"c"),    b');
31 assert_checkequal(gsort(b',"c","i"), b');
32
33 assert_checkequal(gsort(b,"lr"), b);
34 assert_checkequal(gsort(b,"lr","i"), b);
35 assert_checkequal(gsort(b',"lc"),    b');
36 assert_checkequal(gsort(b',"lc","i"), b');
37
38 // Matrix
39 b = [%f,%t,%t,%t,%t;%f,%f,%f,%f,%t;%t,%f,%t,%f,%f]; // (3x5)
40 assert_checkequal(gsort(b), [%t,%t,%t,%f,%f;%t,%t,%f,%f,%f;%t,%t,%f,%f,%f]);
41 assert_checkequal(gsort(b,"g","i"), [%f,%f,%f,%t,%t;%f,%f,%f,%t,%t;%f,%f,%t,%t,%t]);
42
43 ref = [%t,%t,%t,%t,%t;%f,%f,%t,%f,%t;%f,%f,%f,%f,%f];
44 assert_checkequal(gsort(b,"r"), ref);
45 assert_checkequal(gsort(b,"r","i"), ref($:-1:1,:));
46 ref = [%t,%t,%t,%t,%f;%t,%f,%f,%f,%f;%t,%t,%f,%f,%f];
47 assert_checkequal(gsort(b,"c"), ref);
48 assert_checkequal(gsort(b,"c","i"), ref(:,$:-1:1));
49
50 ref = [%t,%f,%t,%f,%f;%f,%t,%t,%t,%t;%f,%f,%f,%f,%t];
51 assert_checkequal(gsort(b,"lr"), ref);
52 assert_checkequal(gsort(b,"lr","i"), ref($:-1:1,:));
53 ref = [%t,%t,%t,%t,%f;%t,%f,%f,%f,%f;%f,%t,%f,%f,%t];
54 assert_checkequal(gsort(b,"lc"), ref);
55 assert_checkequal(gsort(b,"lc","i"), ref(:,$:-1:1));
56
57 // Hypermatrix
58 b = cat(3,[%f %t %f %t ; %t %t %t %f ; %f %t %f %t], ..
59           [%t %t %f %f ; %t %t %f %f ; %f %t %f %t]);
60 s = [3 4 2];
61 // "g"
62 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);
63 assert_checkequal(gsort(b), ref);
64 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);
65 assert_checkequal(gsort(b,"g","i"), ref);
66 // "r"
67 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);
68 assert_checkequal(gsort(b,"r"), ref);
69 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);
70 assert_checkequal(gsort(b,"r","i"), ref);
71 // "c"
72 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);
73 assert_checkequal(gsort(b,"c"), ref);
74 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);
75 assert_checkequal(gsort(b,"c","i"), ref);
76 // "lr"
77 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);
78 assert_checkequal(gsort(b,"lr"), ref);
79 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);
80 assert_checkequal(gsort(b,"lr","i"), ref);
81 // "lc"
82 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);
83 assert_checkequal(gsort(b,"lc"), ref);
84 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);
85 assert_checkequal(gsort(b,"lc","i"), ref);
86
87 // ========
88 // K output
89 // ========
90 // Matrix
91 // ------
92 // With some equal rows and cols, to check that initial order is kept
93 b = [%f %t %f %t ; %t %t %t %f ; %f %t %f %t];
94 // "r"
95 [?,k] = gsort(b, "r");
96 assert_checkequal(k, [2,1,2,1;1,2,1,3;3,3,3,2]);
97 [?,k] = gsort(b, "r", "i");
98 assert_checkequal(k, [1,1,1,2;3,2,3,1;2,3,2,3]);
99 // "c"
100 [?,k] = gsort(b, "c");
101 assert_checkequal(k, [2,4,1,3;1,2,3,4;2,4,1,3]);
102 [?,k] = gsort(b, "c", "i");
103 assert_checkequal(k, [1,3,2,4;4,1,2,3;1,3,2,4]);
104 // "lr"
105 [?,k] = gsort(b, "lr");
106 assert_checkequal(k, [2 1 3]');
107 [?,k] = gsort(b, "lr", "i");
108 assert_checkequal(k, [1 3 2]');
109 // "lc"
110 [?,k] = gsort(b, "lc");
111 assert_checkequal(k, [2 4 1 3]);
112 [?,k] = gsort(b, "lc", "i");
113 assert_checkequal(k, [1 3 4 2]);
114
115 // Hypermatrix
116 // -----------
117 b = cat(3,[%f %t %f %t ; %t %t %t %f ; %f %t %f %t], ..
118           [%t %t %f %f ; %t %t %f %f ; %f %t %f %t]);
119 s = [3 4 2];
120 // "r"
121 [?,k] = gsort(b, "r");
122 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));
123 [?,k] = gsort(b, "r", "i");
124 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));
125 // "c"
126 [?,k] = gsort(b, "c");
127 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));
128 [?,k] = gsort(b, "c", "i");
129 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));
130 // "lr"
131 [?,k] = gsort(b, "lr");
132 assert_checkequal(k, cat(3,[2 1 3]', [1 2 3]'));
133 [?,k] = gsort(b, "lr", "i");
134 assert_checkequal(k, cat(3,[1 3 2]', [3 1 2]') );
135 // "lc"
136 [?,k] = gsort(b, "lc");
137 assert_checkequal(k, cat(3,[2 4 1 3], [2 1 4 3]));
138 [?,k] = gsort(b, "lc", "i");
139 assert_checkequal(k, cat(3,[1 3 4 2], [3 4 1 2]));