1// ============================================================================
2// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3// Copyright (C) 2014 - Scilab Enterprises - Simon MARCHETTO
4//
5//  This file is distributed under the same license as the Scilab package.
6// ============================================================================
7//
8// <-- CLI SHELL MODE -->
9
10function generate_error(a, b, msg)
11    disp(typeof(a));
12    disp(a);
13    if isequal(a, b) then
14        disp(" == ");
15    else
16         disp(" <> ");
17    end
18    disp(typeof(b));
19    disp(b);
20    assert_generror(msg);
21end
22
23function check_inter_type(type1_values, type2_values)
24    if isequal(type1_values(2), type2_values(2)) then
25        msg = msprintf("check_inter_type fails: values %s(%d) and %s(%d) must not be equal !", type1_values(1), 2, type2_values(1), 2);
26        generate_error(type1_values(2), type2_values(2), msg);
27    end
28end
29
30function check_intra_type(type_values)
31    for i=2:size(type_values)
32        for j=i:size(type_values)
33            if i <> j then
34                if isequal(type_values(i), type_values(j)) then
35                    msg = msprintf("check_intra_type fails: values %s(%d) and %s(%d) must not be equal !", ..
36                      type_values(1), i, type_values(1), j);
37                    generate_error(type_values(i), type_values(j), msg);
38                end
39            else
40                if ~isequal(type_values(i), type_values(j)) then
41                    msg = msprintf("check_intra_type fails: values %s(%d) and %s(%d) must be equal !", ..
42                      type_values(1), i, type_values(1), j);
43                    generate_error(type_values(i), type_values(j), msg);
44                end
45            end
46        end
47    end
48end
49
50function check_types(types)
51    for i=1:size(types)
52        for j=i+1:size(types)
53            check_inter_type(types(i), types(j));
54        end
55        check_intra_type(types(i));
56    end
57end
58
59// "constant" type
60real_scalars = list(1, -1, 3.2, 3.2000005, %pi, %e, %inf, -%inf, %eps, []);
61real_matrices = list([1.1, 2.2], [1.1 2.2; 3.3 4.4], [1.1 2.2; 3.3 4.4; 5.5 6.6], [1.1 2.2 3.3; 4.4 5.5 6.6]);
62reals = lstcat(real_scalars, real_matrices);
63
64// TODO: check %nan is not equal to anything
65
66complex_scalars = list(%i, 3.2*%i, -%i, 1+0.5*%i);
67complex_matrices = list([1+%i, 2+2*%i], [1+%i 2+2*%i; 3+3*%i 4+4*%i], ..
68    [1+%i 2+2*%i; 3+3*%i 4+4*%i; 5+5*%i 6+6*%i], [1+%i 2+2*%i 3+3*%i; 4+4*%i 5+5*%i 6+6*%i]);
69complexs = lstcat(complex_scalars, complex_matrices);
70
71constants = lstcat(reals, complexs);
72constants(0) = 'constants';
73
74// "polynomial" type
75polynoms = list(%s, -%s, %s+1, 1+%s+%s^2, 1+%i+%s-%s^2, 1+%s+%i*%s^3);
76polynoms(0) = 'polynoms';
77
78// boolean type
79bool_scalars = list(%t, %f);
80bool_matrices = list([%t, %f], [%t %f; %t %f], [%t %f; %t %f; %t %f], [%t %f %t; %t %f %t]);
81bools = lstcat(bool_scalars, bool_matrices);
82bools(0) = 'bools';
83
84// "sparse" type
85sparses = list(sparse([1]), sparse([0]), sparse([0 0]), sparse([0 0; 0 0]), sparse([1 2]), ..
86    sparse([1 2; 3 4]), sparse([-1 2; 3 4]), sparse([1 2; 3 -1]), sparse([1 2; 3 0]), sparse([0 2; 3 4]), sparse([0 1; 2 3]));
87sparses(0) = 'sparses';
88
89// "boolean sparse" type
90boolean_sparses = list(sparse([]), sparse([%t]), sparse([%f]), sparse([%f %f]), sparse([%f %f; %f %f]), sparse([%t %t]), ..
91    sparse([%t %t; %t %t]), sparse([%f %t; %t %t]), sparse([%t %t; %t %f]));
92boolean_sparses(0) = 'boolean_sparses';
93
94// integer types
95int8s_scalars = list(int8(1), int8(-1));
96int8s_matrices = list(int8([1, 2]), int8([1 2; 3 4]), int8([1 2; 3 4; 5 6]), int8([1 2 3; 4 5 6]));
97int8s = lstcat(int8s_scalars, int8s_matrices);
98
99function int_type_values = convert_to_int_type(type_values, int_type)
100    int_type_values = list();
101    for i=1:size(type_values)
102        int_type_values($+1) = iconvert(type_values(i), int_type);
103    end
104end
105
106uint8s = convert_to_int_type(int8s, 11);
107int16s = convert_to_int_type(int8s, 2);
108uint16s = convert_to_int_type(int8s, 12);
109int32s = convert_to_int_type(int8s, 4);
110uint32s = convert_to_int_type(int8s, 14);
111int64s = convert_to_int_type(int8s, 8);
112uint64s = convert_to_int_type(int8s, 18);
113
114ints = lstcat(int8s, uint8s, int16s, uint16s, int32s, uint32s, int64s, uint64s);
115ints(0) = 'ints';
116
117// "handle" type
118
119// "string" type
120string_scalars = list("", " ", "a", "A", "à", ascii(13), "aaaa", "aaab", "aaaa" + ascii(13));
121string_matrices = list(["", ""], [" ", " "], ["a", "a"], ["a", "A"], ["a", "à"], ..
122    ["a" "b"; "c" "d"], ["a" "b"; "c" "d"; "e" "f"], ["a" "b" "c"; "d" "e" "f"]);
123strings = lstcat(string_scalars, string_matrices);
124strings(0) = 'strings';
125
126// "function" type
127function funcNoParamNoBodyNoReturn(); end;
128function funcWithParamNoBodyNoReturn(x); end;
129function funcNoParamWithBodyNoReturn(); disp("Hello"); end;
130function funcWithParamWithBodyNoReturn(x); disp(x); end;
131function y = funcNoParamWithReturn(); y = 1; end;
132function y = funcWithParamWithReturn(x); y = x; end;
133
134functions = list(funcNoParamNoBodyNoReturn, ..
135    funcWithParamNoBodyNoReturn, ..
136    funcNoParamWithBodyNoReturn, ..
137    funcWithParamWithBodyNoReturn, ..
138    funcNoParamWithReturn, ..
139    funcWithParamWithReturn);
140functions(0) = 'functions';
141
142// "fptr" type
143fptrs = list(cos, sin);
144fptrs(0) = 'fptrs';
145
146// "list" type
147lists = list(list(), list(1), list("a"), list(1, 2), list(list(1)), list(list(2)), list(list(1, 2)));
148lists(0) = 'lists';
149
150// "tlist" type
151tlists = list(tlist(["t1"]), tlist(["t2"]), tlist(["t1", "f1"], "a"), tlist(["t1", "f2"], "a"), ..
152    tlist(["t1", "f1", "f2"], "a", "b"), tlist(["t1", "f1"], "b"));
153tlists(0) = 'tlists';
154
155// "mlist" type
156mlists = list(mlist(["m1"]), mlist(["m2"]), mlist(["m1", "f1"], "a"), mlist(["m1", "f2"], "a"), ..
157    mlist(["m1", "f1", "f2"], "a", "b"), mlist(["m1", "f1"], "b"));
158mlists(0) = 'mlists';
159
160// "st" type
161structs = list(struct("a", 1), struct("a", 2), struct("b", 1), struct("a", 1, "b", 2));
162structs(0) = 'structs';
163
164// "pointer" type
165
166// TODO: add functions, fptrs, handles (when operator== will be fixed in that types)
167types = list(constants, polynoms, bools, sparses, boolean_sparses, ints, strings, lists, tlists, mlists, structs);
168
169check_types(types);
170
171
172